CLSafeMutableArray.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // CLSafeMutableArray.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 2017/3/13.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "CLSafeMutableArray.h"
  9. @implementation CLSafeMutableArray
  10. - (void)dealloc
  11. {
  12. // pthread_mutex_destroy(&_mutex);
  13. }
  14. - (instancetype)init
  15. {
  16. if (self = [super init])
  17. {
  18. // pthread_mutex_init(&_mutex, NULL);
  19. _safeArray = [NSMutableArray array];
  20. _lock = OS_SPINLOCK_INIT;
  21. }
  22. return self;
  23. }
  24. - (void)lock
  25. {
  26. // pthread_mutex_lock(&_mutex);
  27. OSSpinLockLock(&_lock);
  28. }
  29. - (void)unlock
  30. {
  31. // pthread_mutex_unlock(&_mutex);
  32. OSSpinLockUnlock(&_lock);
  33. }
  34. - (void)addObject:(id)anObject
  35. {
  36. [self lock];
  37. [_safeArray addObject:anObject];
  38. [self unlock];
  39. }
  40. - (void)insertObject:(id)anObject atIndex:(NSUInteger)index
  41. {
  42. [self lock];
  43. [_safeArray insertObject:anObject atIndex:index];
  44. [self unlock];
  45. }
  46. - (void)removeLastObject;
  47. {
  48. [self lock];
  49. [_safeArray removeLastObject];
  50. [self unlock];
  51. }
  52. - (void)removeObjectAtIndex:(NSUInteger)index;
  53. {
  54. [self lock];
  55. [_safeArray removeObjectAtIndex:index];
  56. [self unlock];
  57. }
  58. - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
  59. {
  60. [self lock];
  61. [_safeArray replaceObjectAtIndex:index withObject:anObject];
  62. [self unlock];
  63. }
  64. - (void)insertObjectsFromArray:(NSArray *)otherArray atIndex:(NSInteger)index
  65. {
  66. NSInteger count = _safeArray.count;
  67. if (count == 0)
  68. {
  69. [self lock];
  70. [_safeArray addObjectsFromArray:otherArray];
  71. [self unlock];
  72. }
  73. else
  74. {
  75. if (index >= 0 && index < count && otherArray.count > 0)
  76. {
  77. [self lock];
  78. for (NSInteger i = otherArray.count - 1; i >= 0; i--)
  79. {
  80. [_safeArray insertObject:otherArray[i] atIndex:index];
  81. }
  82. [self unlock];
  83. }
  84. }
  85. }
  86. - (void)addObjectsFromArray:(NSArray *)otherArray
  87. {
  88. [self lock];
  89. [_safeArray addObjectsFromArray:otherArray];
  90. [self unlock];
  91. }
  92. - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
  93. {
  94. [self lock];
  95. [_safeArray exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
  96. [self unlock];
  97. }
  98. - (void)removeAllObjects
  99. {
  100. [self lock];
  101. [_safeArray removeAllObjects];
  102. [self unlock];
  103. }
  104. - (void)removeObject:(id)anObject inRange:(NSRange)range
  105. {
  106. [self lock];
  107. [_safeArray removeObject:anObject inRange:range];
  108. [self unlock];
  109. }
  110. - (void)removeObject:(id)anObject
  111. {
  112. [self lock];
  113. [_safeArray removeObject:anObject];
  114. [self unlock];
  115. }
  116. - (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range
  117. {
  118. [self lock];
  119. [_safeArray removeObjectIdenticalTo:anObject inRange:range];
  120. [self unlock];
  121. }
  122. - (void)removeObjectIdenticalTo:(id)anObject
  123. {
  124. [self lock];
  125. [_safeArray removeObjectIdenticalTo:anObject];
  126. [self unlock];
  127. }
  128. - (void)removeObjectsInArray:(NSArray *)otherArray
  129. {
  130. [self lock];
  131. [_safeArray removeObjectsInArray:otherArray];
  132. [self unlock];
  133. }
  134. - (void)removeObjectsInRange:(NSRange)range
  135. {
  136. [self lock];
  137. [_safeArray removeObjectsInRange:range];
  138. [self unlock];
  139. }
  140. - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange
  141. {
  142. [self lock];
  143. [_safeArray replaceObjectsInRange:range withObjectsFromArray:otherArray range:range];
  144. [self unlock];
  145. }
  146. - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray
  147. {
  148. [self lock];
  149. [_safeArray replaceObjectsInRange:range withObjectsFromArray:otherArray];
  150. [self unlock];
  151. }
  152. - (void)setArray:(NSArray *)otherArray
  153. {
  154. [self lock];
  155. [_safeArray setArray:otherArray];
  156. [self unlock];
  157. }
  158. - (void)subArrayWithRange:(NSRange)range
  159. {
  160. [self lock];
  161. NSMutableArray *temp = [NSMutableArray arrayWithArray:[_safeArray subarrayWithRange:range]];
  162. [_safeArray removeAllObjects];
  163. [_safeArray addObjectsFromArray:temp];
  164. }
  165. - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
  166. {
  167. [self lock];
  168. [_safeArray insertObjects:objects atIndexes:indexes];
  169. [self unlock];
  170. }
  171. - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes
  172. {
  173. [self lock];
  174. [_safeArray removeObjectsAtIndexes:indexes];
  175. [self unlock];
  176. }
  177. - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects
  178. {
  179. [self lock];
  180. [_safeArray replaceObjectsAtIndexes:indexes withObjects:objects];
  181. [self unlock];
  182. }
  183. - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0)
  184. {
  185. [self lock];
  186. [_safeArray setObject:obj atIndexedSubscript:idx];
  187. [self unlock];
  188. }
  189. - (NSInteger)count
  190. {
  191. return _safeArray.count;
  192. }
  193. - (id)objectAtIndex:(NSUInteger)index
  194. {
  195. return [_safeArray objectAtIndex:index];
  196. }
  197. - (NSUInteger)indexOfObject:(id)obj
  198. {
  199. return [_safeArray indexOfObject:obj];
  200. }
  201. - (BOOL)containsObject:(id)anObject
  202. {
  203. return [_safeArray containsObject:anObject];
  204. }
  205. @end
  206. @implementation CLSafeSetArray
  207. - (void)addObject:(id)anObject
  208. {
  209. [self lock];
  210. NSUInteger idx = [_safeArray indexOfObject:anObject];
  211. if (idx < _safeArray.count)
  212. {
  213. [_safeArray replaceObjectAtIndex:idx withObject:anObject];
  214. }
  215. else
  216. {
  217. [_safeArray addObject:anObject];
  218. }
  219. [self unlock];
  220. }
  221. - (void)insertObject:(id)anObject atIndex:(NSUInteger)index
  222. {
  223. [self lock];
  224. NSUInteger idx = [_safeArray indexOfObject:anObject];
  225. if (idx < _safeArray.count && idx != index)
  226. {
  227. [_safeArray replaceObjectAtIndex:idx withObject:anObject];
  228. }
  229. else
  230. {
  231. [_safeArray insertObject:anObject atIndex:index];
  232. }
  233. [self unlock];
  234. }
  235. @end