transmission/macosx/NSMutableArrayAdditions.mm

42 lines
1.1 KiB
Plaintext
Raw Normal View History

// This file Copyright © 2011-2022 Transmission authors and contributors.
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
2011-05-23 00:57:29 +00:00
#import "NSMutableArrayAdditions.h"
@implementation NSMutableArray (NSMutableArrayAdditions)
/*
Note: This assumes Apple implemented this as an array under the hood.
If the underlying data structure is a linked-list, for example, then this might be less
efficient than simply removing the object and re-adding it.
*/
- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
2011-05-23 00:57:29 +00:00
{
if (fromIndex == toIndex)
{
2011-05-23 00:57:29 +00:00
return;
}
2017-07-29 16:14:22 +00:00
id object = self[fromIndex];
2011-05-23 00:57:29 +00:00
//shift objects - more efficient than simply removing the object and re-inserting the object
if (fromIndex < toIndex)
{
for (NSUInteger i = fromIndex; i < toIndex; ++i)
{
self[i] = self[i + 1];
}
2011-05-23 00:57:29 +00:00
}
else
{
for (NSUInteger i = fromIndex; i > toIndex; --i)
{
self[i] = self[i - 1];
}
2011-05-23 00:57:29 +00:00
}
2017-07-08 09:16:01 +00:00
self[toIndex] = object;
2011-05-23 00:57:29 +00:00
}
@end