NSArray+Lookin.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // NSArray+Lookin.m
  3. // Lookin
  4. //
  5. // Created by Li Kai on 2018/9/3.
  6. // https://lookin.work
  7. //
  8. #import "NSArray+Lookin.h"
  9. @implementation NSArray (Lookin)
  10. - (NSArray *)lookin_resizeWithCount:(NSUInteger)count add:(id (^)(NSUInteger idx))addBlock remove:(void (^)(NSUInteger idx, id obj))removeBlock doNext:(void (^)(NSUInteger idx, id obj))doBlock {
  11. NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:count];
  12. for (NSUInteger i = 0; i < count; i++) {
  13. if (self.count > i) {
  14. id obj = [self objectAtIndex:i];
  15. [resultArray addObject:obj];
  16. if (doBlock) {
  17. doBlock(i, obj);
  18. }
  19. } else {
  20. if (addBlock) {
  21. id newObj = addBlock(i);
  22. if (newObj) {
  23. [resultArray addObject:newObj];
  24. if (doBlock) {
  25. doBlock(i, newObj);
  26. }
  27. } else {
  28. NSAssert(NO, @"");
  29. }
  30. } else {
  31. NSAssert(NO, @"");
  32. }
  33. }
  34. }
  35. if (removeBlock) {
  36. if (self.count > count) {
  37. for (NSUInteger i = count; i < self.count; i++) {
  38. id obj = [self objectAtIndex:i];
  39. removeBlock(i, obj);
  40. }
  41. }
  42. }
  43. return [resultArray copy];
  44. }
  45. + (NSArray *)lookin_arrayWithCount:(NSUInteger)count block:(id (^)(NSUInteger idx))block {
  46. NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
  47. for (NSUInteger i = 0; i < count; i++) {
  48. id obj = block(i);
  49. if (obj) {
  50. [array addObject:obj];
  51. }
  52. }
  53. return [array copy];
  54. }
  55. - (BOOL)lookin_hasIndex:(NSInteger)index {
  56. if (index == NSNotFound || index < 0) {
  57. return NO;
  58. }
  59. return self.count > index;
  60. }
  61. - (NSArray *)lookin_map:(id (^)(NSUInteger , id))block {
  62. if (!block) {
  63. NSAssert(NO, @"");
  64. return nil;
  65. }
  66. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:self.count];
  67. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  68. id newObj = block(idx, obj);
  69. if (newObj) {
  70. [array addObject:newObj];
  71. }
  72. }];
  73. return [array copy];
  74. }
  75. - (NSArray *)lookin_filter:(BOOL (^)(id obj))block {
  76. if (!block) {
  77. NSAssert(NO, @"");
  78. return nil;
  79. }
  80. NSMutableArray *mArray = [NSMutableArray array];
  81. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  82. if (block(obj)) {
  83. [mArray addObject:obj];
  84. }
  85. }];
  86. return [mArray copy];
  87. }
  88. - (id)lookin_firstFiltered:(BOOL (^)(id obj))block {
  89. if (!block) {
  90. NSAssert(NO, @"");
  91. return nil;
  92. }
  93. __block id targetObj = nil;
  94. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  95. if (block(obj)) {
  96. targetObj = obj;
  97. *stop = YES;
  98. }
  99. }];
  100. return targetObj;
  101. }
  102. - (id)lookin_lastFiltered:(BOOL (^)(id obj))block {
  103. if (!block) {
  104. NSAssert(NO, @"");
  105. return nil;
  106. }
  107. __block id targetObj = nil;
  108. [self enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  109. if (block(obj)) {
  110. targetObj = obj;
  111. *stop = YES;
  112. }
  113. }];
  114. return targetObj;
  115. }
  116. - (id)lookin_reduce:(id (^)(id accumulator, NSUInteger idx, id obj))block {
  117. if (!block) {
  118. NSAssert(NO, @"");
  119. return nil;
  120. }
  121. __block id accumulator = nil;
  122. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  123. accumulator = block(accumulator, idx, obj);
  124. }];
  125. return accumulator;
  126. }
  127. - (CGFloat)lookin_reduceCGFloat:(CGFloat (^)(CGFloat accumulator, NSUInteger idx, id obj))block initialAccumlator:(CGFloat)initialAccumlator {
  128. if (!block) {
  129. NSAssert(NO, @"");
  130. return initialAccumlator;
  131. }
  132. __block CGFloat accumulator = initialAccumlator;
  133. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  134. accumulator = block(accumulator, idx, obj);
  135. }];
  136. return accumulator;
  137. }
  138. - (NSInteger)lookin_reduceInteger:(NSInteger (^)(NSInteger, NSUInteger, id))block initialAccumlator:(NSInteger)initialAccumlator {
  139. if (!block) {
  140. NSAssert(NO, @"");
  141. return initialAccumlator;
  142. }
  143. __block NSInteger accumulator = initialAccumlator;
  144. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  145. accumulator = block(accumulator, idx, obj);
  146. }];
  147. return accumulator;
  148. }
  149. - (BOOL)lookin_all:(BOOL (^)(id obj))block {
  150. if (!block) {
  151. NSAssert(NO, @"");
  152. return NO;
  153. }
  154. __block BOOL allPass = YES;
  155. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  156. BOOL boolValue = block(obj);
  157. if (!boolValue) {
  158. allPass = NO;
  159. *stop = YES;
  160. }
  161. }];
  162. return allPass;
  163. }
  164. - (BOOL)lookin_any:(BOOL (^)(id obj))block {
  165. if (!block) {
  166. NSAssert(NO, @"");
  167. return NO;
  168. }
  169. __block BOOL anyPass = NO;
  170. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  171. BOOL boolValue = block(obj);
  172. if (boolValue) {
  173. anyPass = YES;
  174. *stop = YES;
  175. }
  176. }];
  177. return anyPass;
  178. }
  179. - (NSArray *)lookin_arrayByRemovingObject:(id)obj {
  180. if (!obj || ![self containsObject:obj]) {
  181. return self;
  182. }
  183. NSMutableArray *mutableArray = self.mutableCopy;
  184. [mutableArray removeObject:obj];
  185. return mutableArray.copy;
  186. }
  187. - (NSArray *)lookin_nonredundantArray {
  188. NSSet *set = [NSSet setWithArray:self];
  189. NSArray *newArray = [set allObjects];
  190. return newArray;
  191. }
  192. - (id)lookin_safeObjectAtIndex:(NSInteger)idx {
  193. if (idx == NSNotFound || idx < 0) {
  194. return nil;
  195. }
  196. if (self.count <= idx) {
  197. return nil;
  198. }
  199. return [self objectAtIndex:idx];
  200. }
  201. - (NSArray *)lookin_sortedArrayByStringLength {
  202. NSArray<NSString *> *sortedArray = [self sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
  203. if (obj1.length > obj2.length) {
  204. return NSOrderedDescending;
  205. } else if (obj1.length == obj2.length) {
  206. return NSOrderedSame;
  207. } else {
  208. return NSOrderedAscending;
  209. }
  210. }];
  211. return sortedArray;
  212. }
  213. @end
  214. @implementation NSMutableArray (Lookin)
  215. - (void)lookin_dequeueWithCount:(NSUInteger)count add:(id (^)(NSUInteger idx))addBlock notDequeued:(void (^)(NSUInteger idx, id obj))notDequeuedBlock doNext:(void (^)(NSUInteger idx, id obj))doBlock {
  216. for (NSUInteger i = 0; i < count; i++) {
  217. if ([self lookin_hasIndex:i]) {
  218. id obj = [self objectAtIndex:i];
  219. if (doBlock) {
  220. doBlock(i, obj);
  221. }
  222. } else {
  223. if (addBlock) {
  224. id newObj = addBlock(i);
  225. if (newObj) {
  226. [self addObject:newObj];
  227. if (doBlock) {
  228. doBlock(i, newObj);
  229. }
  230. } else {
  231. NSAssert(NO, @"");
  232. }
  233. } else {
  234. NSAssert(NO, @"");
  235. }
  236. }
  237. }
  238. if (notDequeuedBlock) {
  239. if (self.count > count) {
  240. for (NSUInteger i = count; i < self.count; i++) {
  241. id obj = [self objectAtIndex:i];
  242. notDequeuedBlock(i, obj);
  243. }
  244. }
  245. }
  246. }
  247. - (void)lookin_removeObjectsPassingTest:(BOOL (^)(NSUInteger idx, id obj))block {
  248. if (!block) {
  249. return;
  250. }
  251. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  252. [self enumerateObjectsUsingBlock:^(id _Nonnull currentObj, NSUInteger idx, BOOL * _Nonnull stop) {
  253. BOOL boolValue = block(idx, currentObj);
  254. if (boolValue) {
  255. [indexSet addIndex:idx];
  256. }
  257. }];
  258. [self removeObjectsAtIndexes:indexSet];
  259. }
  260. @end