| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- //
- // CLSafeMutableArray.m
- // BuguLive
- //
- // Created by xfg on 2017/3/13.
- // Copyright © 2017年 xfg. All rights reserved.
- //
- #import "CLSafeMutableArray.h"
- @implementation CLSafeMutableArray
- - (void)dealloc
- {
- // pthread_mutex_destroy(&_mutex);
- }
- - (instancetype)init
- {
- if (self = [super init])
- {
- // pthread_mutex_init(&_mutex, NULL);
- _safeArray = [NSMutableArray array];
- _lock = OS_SPINLOCK_INIT;
- }
- return self;
- }
- - (void)lock
- {
- // pthread_mutex_lock(&_mutex);
- OSSpinLockLock(&_lock);
- }
- - (void)unlock
- {
- // pthread_mutex_unlock(&_mutex);
- OSSpinLockUnlock(&_lock);
- }
- - (void)addObject:(id)anObject
- {
- [self lock];
- [_safeArray addObject:anObject];
- [self unlock];
- }
- - (void)insertObject:(id)anObject atIndex:(NSUInteger)index
- {
- [self lock];
- [_safeArray insertObject:anObject atIndex:index];
- [self unlock];
- }
- - (void)removeLastObject;
- {
- [self lock];
- [_safeArray removeLastObject];
- [self unlock];
- }
- - (void)removeObjectAtIndex:(NSUInteger)index;
- {
- [self lock];
- [_safeArray removeObjectAtIndex:index];
- [self unlock];
- }
- - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
- {
- [self lock];
- [_safeArray replaceObjectAtIndex:index withObject:anObject];
- [self unlock];
- }
- - (void)insertObjectsFromArray:(NSArray *)otherArray atIndex:(NSInteger)index
- {
- NSInteger count = _safeArray.count;
- if (count == 0)
- {
- [self lock];
- [_safeArray addObjectsFromArray:otherArray];
- [self unlock];
- }
- else
- {
- if (index >= 0 && index < count && otherArray.count > 0)
- {
- [self lock];
-
- for (NSInteger i = otherArray.count - 1; i >= 0; i--)
- {
- [_safeArray insertObject:otherArray[i] atIndex:index];
- }
- [self unlock];
- }
- }
- }
- - (void)addObjectsFromArray:(NSArray *)otherArray
- {
- [self lock];
- [_safeArray addObjectsFromArray:otherArray];
- [self unlock];
- }
- - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
- {
- [self lock];
- [_safeArray exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
- [self unlock];
- }
- - (void)removeAllObjects
- {
- [self lock];
- [_safeArray removeAllObjects];
- [self unlock];
- }
- - (void)removeObject:(id)anObject inRange:(NSRange)range
- {
- [self lock];
- [_safeArray removeObject:anObject inRange:range];
- [self unlock];
- }
- - (void)removeObject:(id)anObject
- {
- [self lock];
- [_safeArray removeObject:anObject];
- [self unlock];
- }
- - (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range
- {
- [self lock];
- [_safeArray removeObjectIdenticalTo:anObject inRange:range];
- [self unlock];
- }
- - (void)removeObjectIdenticalTo:(id)anObject
- {
- [self lock];
- [_safeArray removeObjectIdenticalTo:anObject];
- [self unlock];
- }
- - (void)removeObjectsInArray:(NSArray *)otherArray
- {
- [self lock];
- [_safeArray removeObjectsInArray:otherArray];
- [self unlock];
- }
- - (void)removeObjectsInRange:(NSRange)range
- {
- [self lock];
- [_safeArray removeObjectsInRange:range];
- [self unlock];
- }
- - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange
- {
- [self lock];
- [_safeArray replaceObjectsInRange:range withObjectsFromArray:otherArray range:range];
- [self unlock];
- }
- - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray
- {
- [self lock];
- [_safeArray replaceObjectsInRange:range withObjectsFromArray:otherArray];
- [self unlock];
- }
- - (void)setArray:(NSArray *)otherArray
- {
- [self lock];
- [_safeArray setArray:otherArray];
- [self unlock];
- }
- - (void)subArrayWithRange:(NSRange)range
- {
- [self lock];
- NSMutableArray *temp = [NSMutableArray arrayWithArray:[_safeArray subarrayWithRange:range]];
- [_safeArray removeAllObjects];
- [_safeArray addObjectsFromArray:temp];
- }
- - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
- {
- [self lock];
- [_safeArray insertObjects:objects atIndexes:indexes];
- [self unlock];
- }
- - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes
- {
- [self lock];
- [_safeArray removeObjectsAtIndexes:indexes];
- [self unlock];
- }
- - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects
- {
- [self lock];
- [_safeArray replaceObjectsAtIndexes:indexes withObjects:objects];
- [self unlock];
- }
- - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0)
- {
- [self lock];
- [_safeArray setObject:obj atIndexedSubscript:idx];
- [self unlock];
- }
- - (NSInteger)count
- {
- return _safeArray.count;
- }
- - (id)objectAtIndex:(NSUInteger)index
- {
- return [_safeArray objectAtIndex:index];
- }
- - (NSUInteger)indexOfObject:(id)obj
- {
- return [_safeArray indexOfObject:obj];
- }
- - (BOOL)containsObject:(id)anObject
- {
- return [_safeArray containsObject:anObject];
- }
- @end
- @implementation CLSafeSetArray
- - (void)addObject:(id)anObject
- {
- [self lock];
- NSUInteger idx = [_safeArray indexOfObject:anObject];
- if (idx < _safeArray.count)
- {
- [_safeArray replaceObjectAtIndex:idx withObject:anObject];
- }
- else
- {
- [_safeArray addObject:anObject];
- }
- [self unlock];
- }
- - (void)insertObject:(id)anObject atIndex:(NSUInteger)index
- {
- [self lock];
-
- NSUInteger idx = [_safeArray indexOfObject:anObject];
- if (idx < _safeArray.count && idx != index)
- {
- [_safeArray replaceObjectAtIndex:idx withObject:anObject];
- }
- else
- {
- [_safeArray insertObject:anObject atIndex:index];
- }
- [self unlock];
- }
- @end
|