2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2011-2023 Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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)
|
|
|
|
|
2012-04-29 00:53:34 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
|
2011-05-23 00:57:29 +00:00
|
|
|
{
|
|
|
|
if (fromIndex == toIndex)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2011-05-23 00:57:29 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-29 16:14:22 +00:00
|
|
|
id object = self[fromIndex];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
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)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
self[i] = self[i + 1];
|
|
|
|
}
|
2011-05-23 00:57:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-07 23:15:12 +00:00
|
|
|
for (NSUInteger i = fromIndex; i > toIndex; --i)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
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
|