You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2551 lines
76 KiB

6 years ago
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBArray_PackagePrivate.h"
  31. #import "GPBMessage_PackagePrivate.h"
  32. // Direct access is use for speed, to avoid even internally declaring things
  33. // read/write, etc. The warning is enabled in the project to ensure code calling
  34. // protos can turn on -Wdirect-ivar-access without issues.
  35. #pragma clang diagnostic push
  36. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  37. // Mutable arrays use an internal buffer that can always hold a multiple of this elements.
  38. #define kChunkSize 16
  39. #define CapacityFromCount(x) (((x / kChunkSize) + 1) * kChunkSize)
  40. static BOOL ArrayDefault_IsValidValue(int32_t value) {
  41. // Anything but the bad value marker is allowed.
  42. return (value != kGPBUnrecognizedEnumeratorValue);
  43. }
  44. //%PDDM-DEFINE VALIDATE_RANGE(INDEX, COUNT)
  45. //% if (INDEX >= COUNT) {
  46. //% [NSException raise:NSRangeException
  47. //% format:@"Index (%lu) beyond bounds (%lu)",
  48. //% (unsigned long)INDEX, (unsigned long)COUNT];
  49. //% }
  50. //%PDDM-DEFINE MAYBE_GROW_TO_SET_COUNT(NEW_COUNT)
  51. //% if (NEW_COUNT > _capacity) {
  52. //% [self internalResizeToCapacity:CapacityFromCount(NEW_COUNT)];
  53. //% }
  54. //% _count = NEW_COUNT;
  55. //%PDDM-DEFINE SET_COUNT_AND_MAYBE_SHRINK(NEW_COUNT)
  56. //% _count = NEW_COUNT;
  57. //% if ((NEW_COUNT + (2 * kChunkSize)) < _capacity) {
  58. //% [self internalResizeToCapacity:CapacityFromCount(NEW_COUNT)];
  59. //% }
  60. //
  61. // Macros for the common basic cases.
  62. //
  63. //%PDDM-DEFINE ARRAY_INTERFACE_SIMPLE(NAME, TYPE, FORMAT)
  64. //%#pragma mark - NAME
  65. //%
  66. //%@implementation GPB##NAME##Array {
  67. //% @package
  68. //% TYPE *_values;
  69. //% NSUInteger _count;
  70. //% NSUInteger _capacity;
  71. //%}
  72. //%
  73. //%@synthesize count = _count;
  74. //%
  75. //%+ (instancetype)array {
  76. //% return [[[self alloc] init] autorelease];
  77. //%}
  78. //%
  79. //%+ (instancetype)arrayWithValue:(TYPE)value {
  80. //% // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  81. //% // the type correct.
  82. //% return [[(GPB##NAME##Array*)[self alloc] initWithValues:&value count:1] autorelease];
  83. //%}
  84. //%
  85. //%+ (instancetype)arrayWithValueArray:(GPB##NAME##Array *)array {
  86. //% return [[(GPB##NAME##Array*)[self alloc] initWithValueArray:array] autorelease];
  87. //%}
  88. //%
  89. //%+ (instancetype)arrayWithCapacity:(NSUInteger)count {
  90. //% return [[[self alloc] initWithCapacity:count] autorelease];
  91. //%}
  92. //%
  93. //%- (instancetype)init {
  94. //% self = [super init];
  95. //% // No work needed;
  96. //% return self;
  97. //%}
  98. //%
  99. //%- (instancetype)initWithValueArray:(GPB##NAME##Array *)array {
  100. //% return [self initWithValues:array->_values count:array->_count];
  101. //%}
  102. //%
  103. //%- (instancetype)initWithValues:(const TYPE [])values count:(NSUInteger)count {
  104. //% self = [self init];
  105. //% if (self) {
  106. //% if (count && values) {
  107. //% _values = reallocf(_values, count * sizeof(TYPE));
  108. //% if (_values != NULL) {
  109. //% _capacity = count;
  110. //% memcpy(_values, values, count * sizeof(TYPE));
  111. //% _count = count;
  112. //% } else {
  113. //% [self release];
  114. //% [NSException raise:NSMallocException
  115. //% format:@"Failed to allocate %lu bytes",
  116. //% (unsigned long)(count * sizeof(TYPE))];
  117. //% }
  118. //% }
  119. //% }
  120. //% return self;
  121. //%}
  122. //%
  123. //%- (instancetype)initWithCapacity:(NSUInteger)count {
  124. //% self = [self initWithValues:NULL count:0];
  125. //% if (self && count) {
  126. //% [self internalResizeToCapacity:count];
  127. //% }
  128. //% return self;
  129. //%}
  130. //%
  131. //%- (instancetype)copyWithZone:(NSZone *)zone {
  132. //% return [[GPB##NAME##Array allocWithZone:zone] initWithValues:_values count:_count];
  133. //%}
  134. //%
  135. //%ARRAY_IMMUTABLE_CORE(NAME, TYPE, , FORMAT)
  136. //%
  137. //%- (TYPE)valueAtIndex:(NSUInteger)index {
  138. //%VALIDATE_RANGE(index, _count)
  139. //% return _values[index];
  140. //%}
  141. //%
  142. //%ARRAY_MUTABLE_CORE(NAME, TYPE, , FORMAT)
  143. //%@end
  144. //%
  145. //
  146. // Some core macros used for both the simple types and Enums.
  147. //
  148. //%PDDM-DEFINE ARRAY_IMMUTABLE_CORE(NAME, TYPE, ACCESSOR_NAME, FORMAT)
  149. //%- (void)dealloc {
  150. //% NSAssert(!_autocreator,
  151. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  152. //% [self class], _autocreator);
  153. //% free(_values);
  154. //% [super dealloc];
  155. //%}
  156. //%
  157. //%- (BOOL)isEqual:(id)other {
  158. //% if (self == other) {
  159. //% return YES;
  160. //% }
  161. //% if (![other isKindOfClass:[GPB##NAME##Array class]]) {
  162. //% return NO;
  163. //% }
  164. //% GPB##NAME##Array *otherArray = other;
  165. //% return (_count == otherArray->_count
  166. //% && memcmp(_values, otherArray->_values, (_count * sizeof(TYPE))) == 0);
  167. //%}
  168. //%
  169. //%- (NSUInteger)hash {
  170. //% // Follow NSArray's lead, and use the count as the hash.
  171. //% return _count;
  172. //%}
  173. //%
  174. //%- (NSString *)description {
  175. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  176. //% for (NSUInteger i = 0, count = _count; i < count; ++i) {
  177. //% if (i == 0) {
  178. //% [result appendFormat:@"##FORMAT##", _values[i]];
  179. //% } else {
  180. //% [result appendFormat:@", ##FORMAT##", _values[i]];
  181. //% }
  182. //% }
  183. //% [result appendFormat:@" }"];
  184. //% return result;
  185. //%}
  186. //%
  187. //%- (void)enumerate##ACCESSOR_NAME##ValuesWithBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block {
  188. //% [self enumerate##ACCESSOR_NAME##ValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  189. //%}
  190. //%
  191. //%- (void)enumerate##ACCESSOR_NAME##ValuesWithOptions:(NSEnumerationOptions)opts
  192. //% ACCESSOR_NAME$S usingBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block {
  193. //% // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  194. //% BOOL stop = NO;
  195. //% if ((opts & NSEnumerationReverse) == 0) {
  196. //% for (NSUInteger i = 0, count = _count; i < count; ++i) {
  197. //% block(_values[i], i, &stop);
  198. //% if (stop) break;
  199. //% }
  200. //% } else if (_count > 0) {
  201. //% for (NSUInteger i = _count; i > 0; --i) {
  202. //% block(_values[i - 1], (i - 1), &stop);
  203. //% if (stop) break;
  204. //% }
  205. //% }
  206. //%}
  207. //%PDDM-DEFINE MUTATION_HOOK_None()
  208. //%PDDM-DEFINE MUTATION_METHODS(NAME, TYPE, ACCESSOR_NAME, HOOK_1, HOOK_2)
  209. //%- (void)add##ACCESSOR_NAME##Value:(TYPE)value {
  210. //% [self add##ACCESSOR_NAME##Values:&value count:1];
  211. //%}
  212. //%
  213. //%- (void)add##ACCESSOR_NAME##Values:(const TYPE [])values count:(NSUInteger)count {
  214. //% if (values == NULL || count == 0) return;
  215. //%MUTATION_HOOK_##HOOK_1() NSUInteger initialCount = _count;
  216. //% NSUInteger newCount = initialCount + count;
  217. //%MAYBE_GROW_TO_SET_COUNT(newCount)
  218. //% memcpy(&_values[initialCount], values, count * sizeof(TYPE));
  219. //% if (_autocreator) {
  220. //% GPBAutocreatedArrayModified(_autocreator, self);
  221. //% }
  222. //%}
  223. //%
  224. //%- (void)insert##ACCESSOR_NAME##Value:(TYPE)value atIndex:(NSUInteger)index {
  225. //%VALIDATE_RANGE(index, _count + 1)
  226. //%MUTATION_HOOK_##HOOK_2() NSUInteger initialCount = _count;
  227. //% NSUInteger newCount = initialCount + 1;
  228. //%MAYBE_GROW_TO_SET_COUNT(newCount)
  229. //% if (index != initialCount) {
  230. //% memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(TYPE));
  231. //% }
  232. //% _values[index] = value;
  233. //% if (_autocreator) {
  234. //% GPBAutocreatedArrayModified(_autocreator, self);
  235. //% }
  236. //%}
  237. //%
  238. //%- (void)replaceValueAtIndex:(NSUInteger)index with##ACCESSOR_NAME##Value:(TYPE)value {
  239. //%VALIDATE_RANGE(index, _count)
  240. //%MUTATION_HOOK_##HOOK_2() _values[index] = value;
  241. //%}
  242. //%PDDM-DEFINE ARRAY_MUTABLE_CORE(NAME, TYPE, ACCESSOR_NAME, FORMAT)
  243. //%- (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  244. //% _values = reallocf(_values, newCapacity * sizeof(TYPE));
  245. //% if (_values == NULL) {
  246. //% _capacity = 0;
  247. //% _count = 0;
  248. //% [NSException raise:NSMallocException
  249. //% format:@"Failed to allocate %lu bytes",
  250. //% (unsigned long)(newCapacity * sizeof(TYPE))];
  251. //% }
  252. //% _capacity = newCapacity;
  253. //%}
  254. //%
  255. //%MUTATION_METHODS(NAME, TYPE, ACCESSOR_NAME, None, None)
  256. //%
  257. //%- (void)add##ACCESSOR_NAME##ValuesFromArray:(GPB##NAME##Array *)array {
  258. //% [self add##ACCESSOR_NAME##Values:array->_values count:array->_count];
  259. //%}
  260. //%
  261. //%- (void)removeValueAtIndex:(NSUInteger)index {
  262. //%VALIDATE_RANGE(index, _count)
  263. //% NSUInteger newCount = _count - 1;
  264. //% if (index != newCount) {
  265. //% memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(TYPE));
  266. //% }
  267. //%SET_COUNT_AND_MAYBE_SHRINK(newCount)
  268. //%}
  269. //%
  270. //%- (void)removeAll {
  271. //%SET_COUNT_AND_MAYBE_SHRINK(0)
  272. //%}
  273. //%
  274. //%- (void)exchangeValueAtIndex:(NSUInteger)idx1
  275. //% withValueAtIndex:(NSUInteger)idx2 {
  276. //%VALIDATE_RANGE(idx1, _count)
  277. //%VALIDATE_RANGE(idx2, _count)
  278. //% TYPE temp = _values[idx1];
  279. //% _values[idx1] = _values[idx2];
  280. //% _values[idx2] = temp;
  281. //%}
  282. //%
  283. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Int32, int32_t, %d)
  284. // This block of code is generated, do not edit it directly.
  285. #pragma mark - Int32
  286. @implementation GPBInt32Array {
  287. @package
  288. int32_t *_values;
  289. NSUInteger _count;
  290. NSUInteger _capacity;
  291. }
  292. @synthesize count = _count;
  293. + (instancetype)array {
  294. return [[[self alloc] init] autorelease];
  295. }
  296. + (instancetype)arrayWithValue:(int32_t)value {
  297. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  298. // the type correct.
  299. return [[(GPBInt32Array*)[self alloc] initWithValues:&value count:1] autorelease];
  300. }
  301. + (instancetype)arrayWithValueArray:(GPBInt32Array *)array {
  302. return [[(GPBInt32Array*)[self alloc] initWithValueArray:array] autorelease];
  303. }
  304. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  305. return [[[self alloc] initWithCapacity:count] autorelease];
  306. }
  307. - (instancetype)init {
  308. self = [super init];
  309. // No work needed;
  310. return self;
  311. }
  312. - (instancetype)initWithValueArray:(GPBInt32Array *)array {
  313. return [self initWithValues:array->_values count:array->_count];
  314. }
  315. - (instancetype)initWithValues:(const int32_t [])values count:(NSUInteger)count {
  316. self = [self init];
  317. if (self) {
  318. if (count && values) {
  319. _values = reallocf(_values, count * sizeof(int32_t));
  320. if (_values != NULL) {
  321. _capacity = count;
  322. memcpy(_values, values, count * sizeof(int32_t));
  323. _count = count;
  324. } else {
  325. [self release];
  326. [NSException raise:NSMallocException
  327. format:@"Failed to allocate %lu bytes",
  328. (unsigned long)(count * sizeof(int32_t))];
  329. }
  330. }
  331. }
  332. return self;
  333. }
  334. - (instancetype)initWithCapacity:(NSUInteger)count {
  335. self = [self initWithValues:NULL count:0];
  336. if (self && count) {
  337. [self internalResizeToCapacity:count];
  338. }
  339. return self;
  340. }
  341. - (instancetype)copyWithZone:(NSZone *)zone {
  342. return [[GPBInt32Array allocWithZone:zone] initWithValues:_values count:_count];
  343. }
  344. - (void)dealloc {
  345. NSAssert(!_autocreator,
  346. @"%@: Autocreator must be cleared before release, autocreator: %@",
  347. [self class], _autocreator);
  348. free(_values);
  349. [super dealloc];
  350. }
  351. - (BOOL)isEqual:(id)other {
  352. if (self == other) {
  353. return YES;
  354. }
  355. if (![other isKindOfClass:[GPBInt32Array class]]) {
  356. return NO;
  357. }
  358. GPBInt32Array *otherArray = other;
  359. return (_count == otherArray->_count
  360. && memcmp(_values, otherArray->_values, (_count * sizeof(int32_t))) == 0);
  361. }
  362. - (NSUInteger)hash {
  363. // Follow NSArray's lead, and use the count as the hash.
  364. return _count;
  365. }
  366. - (NSString *)description {
  367. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  368. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  369. if (i == 0) {
  370. [result appendFormat:@"%d", _values[i]];
  371. } else {
  372. [result appendFormat:@", %d", _values[i]];
  373. }
  374. }
  375. [result appendFormat:@" }"];
  376. return result;
  377. }
  378. - (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
  379. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  380. }
  381. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  382. usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
  383. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  384. BOOL stop = NO;
  385. if ((opts & NSEnumerationReverse) == 0) {
  386. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  387. block(_values[i], i, &stop);
  388. if (stop) break;
  389. }
  390. } else if (_count > 0) {
  391. for (NSUInteger i = _count; i > 0; --i) {
  392. block(_values[i - 1], (i - 1), &stop);
  393. if (stop) break;
  394. }
  395. }
  396. }
  397. - (int32_t)valueAtIndex:(NSUInteger)index {
  398. if (index >= _count) {
  399. [NSException raise:NSRangeException
  400. format:@"Index (%lu) beyond bounds (%lu)",
  401. (unsigned long)index, (unsigned long)_count];
  402. }
  403. return _values[index];
  404. }
  405. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  406. _values = reallocf(_values, newCapacity * sizeof(int32_t));
  407. if (_values == NULL) {
  408. _capacity = 0;
  409. _count = 0;
  410. [NSException raise:NSMallocException
  411. format:@"Failed to allocate %lu bytes",
  412. (unsigned long)(newCapacity * sizeof(int32_t))];
  413. }
  414. _capacity = newCapacity;
  415. }
  416. - (void)addValue:(int32_t)value {
  417. [self addValues:&value count:1];
  418. }
  419. - (void)addValues:(const int32_t [])values count:(NSUInteger)count {
  420. if (values == NULL || count == 0) return;
  421. NSUInteger initialCount = _count;
  422. NSUInteger newCount = initialCount + count;
  423. if (newCount > _capacity) {
  424. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  425. }
  426. _count = newCount;
  427. memcpy(&_values[initialCount], values, count * sizeof(int32_t));
  428. if (_autocreator) {
  429. GPBAutocreatedArrayModified(_autocreator, self);
  430. }
  431. }
  432. - (void)insertValue:(int32_t)value atIndex:(NSUInteger)index {
  433. if (index >= _count + 1) {
  434. [NSException raise:NSRangeException
  435. format:@"Index (%lu) beyond bounds (%lu)",
  436. (unsigned long)index, (unsigned long)_count + 1];
  437. }
  438. NSUInteger initialCount = _count;
  439. NSUInteger newCount = initialCount + 1;
  440. if (newCount > _capacity) {
  441. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  442. }
  443. _count = newCount;
  444. if (index != initialCount) {
  445. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int32_t));
  446. }
  447. _values[index] = value;
  448. if (_autocreator) {
  449. GPBAutocreatedArrayModified(_autocreator, self);
  450. }
  451. }
  452. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(int32_t)value {
  453. if (index >= _count) {
  454. [NSException raise:NSRangeException
  455. format:@"Index (%lu) beyond bounds (%lu)",
  456. (unsigned long)index, (unsigned long)_count];
  457. }
  458. _values[index] = value;
  459. }
  460. - (void)addValuesFromArray:(GPBInt32Array *)array {
  461. [self addValues:array->_values count:array->_count];
  462. }
  463. - (void)removeValueAtIndex:(NSUInteger)index {
  464. if (index >= _count) {
  465. [NSException raise:NSRangeException
  466. format:@"Index (%lu) beyond bounds (%lu)",
  467. (unsigned long)index, (unsigned long)_count];
  468. }
  469. NSUInteger newCount = _count - 1;
  470. if (index != newCount) {
  471. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(int32_t));
  472. }
  473. _count = newCount;
  474. if ((newCount + (2 * kChunkSize)) < _capacity) {
  475. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  476. }
  477. }
  478. - (void)removeAll {
  479. _count = 0;
  480. if ((0 + (2 * kChunkSize)) < _capacity) {
  481. [self internalResizeToCapacity:CapacityFromCount(0)];
  482. }
  483. }
  484. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  485. withValueAtIndex:(NSUInteger)idx2 {
  486. if (idx1 >= _count) {
  487. [NSException raise:NSRangeException
  488. format:@"Index (%lu) beyond bounds (%lu)",
  489. (unsigned long)idx1, (unsigned long)_count];
  490. }
  491. if (idx2 >= _count) {
  492. [NSException raise:NSRangeException
  493. format:@"Index (%lu) beyond bounds (%lu)",
  494. (unsigned long)idx2, (unsigned long)_count];
  495. }
  496. int32_t temp = _values[idx1];
  497. _values[idx1] = _values[idx2];
  498. _values[idx2] = temp;
  499. }
  500. @end
  501. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(UInt32, uint32_t, %u)
  502. // This block of code is generated, do not edit it directly.
  503. #pragma mark - UInt32
  504. @implementation GPBUInt32Array {
  505. @package
  506. uint32_t *_values;
  507. NSUInteger _count;
  508. NSUInteger _capacity;
  509. }
  510. @synthesize count = _count;
  511. + (instancetype)array {
  512. return [[[self alloc] init] autorelease];
  513. }
  514. + (instancetype)arrayWithValue:(uint32_t)value {
  515. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  516. // the type correct.
  517. return [[(GPBUInt32Array*)[self alloc] initWithValues:&value count:1] autorelease];
  518. }
  519. + (instancetype)arrayWithValueArray:(GPBUInt32Array *)array {
  520. return [[(GPBUInt32Array*)[self alloc] initWithValueArray:array] autorelease];
  521. }
  522. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  523. return [[[self alloc] initWithCapacity:count] autorelease];
  524. }
  525. - (instancetype)init {
  526. self = [super init];
  527. // No work needed;
  528. return self;
  529. }
  530. - (instancetype)initWithValueArray:(GPBUInt32Array *)array {
  531. return [self initWithValues:array->_values count:array->_count];
  532. }
  533. - (instancetype)initWithValues:(const uint32_t [])values count:(NSUInteger)count {
  534. self = [self init];
  535. if (self) {
  536. if (count && values) {
  537. _values = reallocf(_values, count * sizeof(uint32_t));
  538. if (_values != NULL) {
  539. _capacity = count;
  540. memcpy(_values, values, count * sizeof(uint32_t));
  541. _count = count;
  542. } else {
  543. [self release];
  544. [NSException raise:NSMallocException
  545. format:@"Failed to allocate %lu bytes",
  546. (unsigned long)(count * sizeof(uint32_t))];
  547. }
  548. }
  549. }
  550. return self;
  551. }
  552. - (instancetype)initWithCapacity:(NSUInteger)count {
  553. self = [self initWithValues:NULL count:0];
  554. if (self && count) {
  555. [self internalResizeToCapacity:count];
  556. }
  557. return self;
  558. }
  559. - (instancetype)copyWithZone:(NSZone *)zone {
  560. return [[GPBUInt32Array allocWithZone:zone] initWithValues:_values count:_count];
  561. }
  562. - (void)dealloc {
  563. NSAssert(!_autocreator,
  564. @"%@: Autocreator must be cleared before release, autocreator: %@",
  565. [self class], _autocreator);
  566. free(_values);
  567. [super dealloc];
  568. }
  569. - (BOOL)isEqual:(id)other {
  570. if (self == other) {
  571. return YES;
  572. }
  573. if (![other isKindOfClass:[GPBUInt32Array class]]) {
  574. return NO;
  575. }
  576. GPBUInt32Array *otherArray = other;
  577. return (_count == otherArray->_count
  578. && memcmp(_values, otherArray->_values, (_count * sizeof(uint32_t))) == 0);
  579. }
  580. - (NSUInteger)hash {
  581. // Follow NSArray's lead, and use the count as the hash.
  582. return _count;
  583. }
  584. - (NSString *)description {
  585. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  586. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  587. if (i == 0) {
  588. [result appendFormat:@"%u", _values[i]];
  589. } else {
  590. [result appendFormat:@", %u", _values[i]];
  591. }
  592. }
  593. [result appendFormat:@" }"];
  594. return result;
  595. }
  596. - (void)enumerateValuesWithBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block {
  597. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  598. }
  599. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  600. usingBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block {
  601. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  602. BOOL stop = NO;
  603. if ((opts & NSEnumerationReverse) == 0) {
  604. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  605. block(_values[i], i, &stop);
  606. if (stop) break;
  607. }
  608. } else if (_count > 0) {
  609. for (NSUInteger i = _count; i > 0; --i) {
  610. block(_values[i - 1], (i - 1), &stop);
  611. if (stop) break;
  612. }
  613. }
  614. }
  615. - (uint32_t)valueAtIndex:(NSUInteger)index {
  616. if (index >= _count) {
  617. [NSException raise:NSRangeException
  618. format:@"Index (%lu) beyond bounds (%lu)",
  619. (unsigned long)index, (unsigned long)_count];
  620. }
  621. return _values[index];
  622. }
  623. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  624. _values = reallocf(_values, newCapacity * sizeof(uint32_t));
  625. if (_values == NULL) {
  626. _capacity = 0;
  627. _count = 0;
  628. [NSException raise:NSMallocException
  629. format:@"Failed to allocate %lu bytes",
  630. (unsigned long)(newCapacity * sizeof(uint32_t))];
  631. }
  632. _capacity = newCapacity;
  633. }
  634. - (void)addValue:(uint32_t)value {
  635. [self addValues:&value count:1];
  636. }
  637. - (void)addValues:(const uint32_t [])values count:(NSUInteger)count {
  638. if (values == NULL || count == 0) return;
  639. NSUInteger initialCount = _count;
  640. NSUInteger newCount = initialCount + count;
  641. if (newCount > _capacity) {
  642. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  643. }
  644. _count = newCount;
  645. memcpy(&_values[initialCount], values, count * sizeof(uint32_t));
  646. if (_autocreator) {
  647. GPBAutocreatedArrayModified(_autocreator, self);
  648. }
  649. }
  650. - (void)insertValue:(uint32_t)value atIndex:(NSUInteger)index {
  651. if (index >= _count + 1) {
  652. [NSException raise:NSRangeException
  653. format:@"Index (%lu) beyond bounds (%lu)",
  654. (unsigned long)index, (unsigned long)_count + 1];
  655. }
  656. NSUInteger initialCount = _count;
  657. NSUInteger newCount = initialCount + 1;
  658. if (newCount > _capacity) {
  659. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  660. }
  661. _count = newCount;
  662. if (index != initialCount) {
  663. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(uint32_t));
  664. }
  665. _values[index] = value;
  666. if (_autocreator) {
  667. GPBAutocreatedArrayModified(_autocreator, self);
  668. }
  669. }
  670. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(uint32_t)value {
  671. if (index >= _count) {
  672. [NSException raise:NSRangeException
  673. format:@"Index (%lu) beyond bounds (%lu)",
  674. (unsigned long)index, (unsigned long)_count];
  675. }
  676. _values[index] = value;
  677. }
  678. - (void)addValuesFromArray:(GPBUInt32Array *)array {
  679. [self addValues:array->_values count:array->_count];
  680. }
  681. - (void)removeValueAtIndex:(NSUInteger)index {
  682. if (index >= _count) {
  683. [NSException raise:NSRangeException
  684. format:@"Index (%lu) beyond bounds (%lu)",
  685. (unsigned long)index, (unsigned long)_count];
  686. }
  687. NSUInteger newCount = _count - 1;
  688. if (index != newCount) {
  689. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(uint32_t));
  690. }
  691. _count = newCount;
  692. if ((newCount + (2 * kChunkSize)) < _capacity) {
  693. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  694. }
  695. }
  696. - (void)removeAll {
  697. _count = 0;
  698. if ((0 + (2 * kChunkSize)) < _capacity) {
  699. [self internalResizeToCapacity:CapacityFromCount(0)];
  700. }
  701. }
  702. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  703. withValueAtIndex:(NSUInteger)idx2 {
  704. if (idx1 >= _count) {
  705. [NSException raise:NSRangeException
  706. format:@"Index (%lu) beyond bounds (%lu)",
  707. (unsigned long)idx1, (unsigned long)_count];
  708. }
  709. if (idx2 >= _count) {
  710. [NSException raise:NSRangeException
  711. format:@"Index (%lu) beyond bounds (%lu)",
  712. (unsigned long)idx2, (unsigned long)_count];
  713. }
  714. uint32_t temp = _values[idx1];
  715. _values[idx1] = _values[idx2];
  716. _values[idx2] = temp;
  717. }
  718. @end
  719. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Int64, int64_t, %lld)
  720. // This block of code is generated, do not edit it directly.
  721. #pragma mark - Int64
  722. @implementation GPBInt64Array {
  723. @package
  724. int64_t *_values;
  725. NSUInteger _count;
  726. NSUInteger _capacity;
  727. }
  728. @synthesize count = _count;
  729. + (instancetype)array {
  730. return [[[self alloc] init] autorelease];
  731. }
  732. + (instancetype)arrayWithValue:(int64_t)value {
  733. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  734. // the type correct.
  735. return [[(GPBInt64Array*)[self alloc] initWithValues:&value count:1] autorelease];
  736. }
  737. + (instancetype)arrayWithValueArray:(GPBInt64Array *)array {
  738. return [[(GPBInt64Array*)[self alloc] initWithValueArray:array] autorelease];
  739. }
  740. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  741. return [[[self alloc] initWithCapacity:count] autorelease];
  742. }
  743. - (instancetype)init {
  744. self = [super init];
  745. // No work needed;
  746. return self;
  747. }
  748. - (instancetype)initWithValueArray:(GPBInt64Array *)array {
  749. return [self initWithValues:array->_values count:array->_count];
  750. }
  751. - (instancetype)initWithValues:(const int64_t [])values count:(NSUInteger)count {
  752. self = [self init];
  753. if (self) {
  754. if (count && values) {
  755. _values = reallocf(_values, count * sizeof(int64_t));
  756. if (_values != NULL) {
  757. _capacity = count;
  758. memcpy(_values, values, count * sizeof(int64_t));
  759. _count = count;
  760. } else {
  761. [self release];
  762. [NSException raise:NSMallocException
  763. format:@"Failed to allocate %lu bytes",
  764. (unsigned long)(count * sizeof(int64_t))];
  765. }
  766. }
  767. }
  768. return self;
  769. }
  770. - (instancetype)initWithCapacity:(NSUInteger)count {
  771. self = [self initWithValues:NULL count:0];
  772. if (self && count) {
  773. [self internalResizeToCapacity:count];
  774. }
  775. return self;
  776. }
  777. - (instancetype)copyWithZone:(NSZone *)zone {
  778. return [[GPBInt64Array allocWithZone:zone] initWithValues:_values count:_count];
  779. }
  780. - (void)dealloc {
  781. NSAssert(!_autocreator,
  782. @"%@: Autocreator must be cleared before release, autocreator: %@",
  783. [self class], _autocreator);
  784. free(_values);
  785. [super dealloc];
  786. }
  787. - (BOOL)isEqual:(id)other {
  788. if (self == other) {
  789. return YES;
  790. }
  791. if (![other isKindOfClass:[GPBInt64Array class]]) {
  792. return NO;
  793. }
  794. GPBInt64Array *otherArray = other;
  795. return (_count == otherArray->_count
  796. && memcmp(_values, otherArray->_values, (_count * sizeof(int64_t))) == 0);
  797. }
  798. - (NSUInteger)hash {
  799. // Follow NSArray's lead, and use the count as the hash.
  800. return _count;
  801. }
  802. - (NSString *)description {
  803. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  804. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  805. if (i == 0) {
  806. [result appendFormat:@"%lld", _values[i]];
  807. } else {
  808. [result appendFormat:@", %lld", _values[i]];
  809. }
  810. }
  811. [result appendFormat:@" }"];
  812. return result;
  813. }
  814. - (void)enumerateValuesWithBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block {
  815. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  816. }
  817. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  818. usingBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block {
  819. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  820. BOOL stop = NO;
  821. if ((opts & NSEnumerationReverse) == 0) {
  822. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  823. block(_values[i], i, &stop);
  824. if (stop) break;
  825. }
  826. } else if (_count > 0) {
  827. for (NSUInteger i = _count; i > 0; --i) {
  828. block(_values[i - 1], (i - 1), &stop);
  829. if (stop) break;
  830. }
  831. }
  832. }
  833. - (int64_t)valueAtIndex:(NSUInteger)index {
  834. if (index >= _count) {
  835. [NSException raise:NSRangeException
  836. format:@"Index (%lu) beyond bounds (%lu)",
  837. (unsigned long)index, (unsigned long)_count];
  838. }
  839. return _values[index];
  840. }
  841. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  842. _values = reallocf(_values, newCapacity * sizeof(int64_t));
  843. if (_values == NULL) {
  844. _capacity = 0;
  845. _count = 0;
  846. [NSException raise:NSMallocException
  847. format:@"Failed to allocate %lu bytes",
  848. (unsigned long)(newCapacity * sizeof(int64_t))];
  849. }
  850. _capacity = newCapacity;
  851. }
  852. - (void)addValue:(int64_t)value {
  853. [self addValues:&value count:1];
  854. }
  855. - (void)addValues:(const int64_t [])values count:(NSUInteger)count {
  856. if (values == NULL || count == 0) return;
  857. NSUInteger initialCount = _count;
  858. NSUInteger newCount = initialCount + count;
  859. if (newCount > _capacity) {
  860. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  861. }
  862. _count = newCount;
  863. memcpy(&_values[initialCount], values, count * sizeof(int64_t));
  864. if (_autocreator) {
  865. GPBAutocreatedArrayModified(_autocreator, self);
  866. }
  867. }
  868. - (void)insertValue:(int64_t)value atIndex:(NSUInteger)index {
  869. if (index >= _count + 1) {
  870. [NSException raise:NSRangeException
  871. format:@"Index (%lu) beyond bounds (%lu)",
  872. (unsigned long)index, (unsigned long)_count + 1];
  873. }
  874. NSUInteger initialCount = _count;
  875. NSUInteger newCount = initialCount + 1;
  876. if (newCount > _capacity) {
  877. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  878. }
  879. _count = newCount;
  880. if (index != initialCount) {
  881. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int64_t));
  882. }
  883. _values[index] = value;
  884. if (_autocreator) {
  885. GPBAutocreatedArrayModified(_autocreator, self);
  886. }
  887. }
  888. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(int64_t)value {
  889. if (index >= _count) {
  890. [NSException raise:NSRangeException
  891. format:@"Index (%lu) beyond bounds (%lu)",
  892. (unsigned long)index, (unsigned long)_count];
  893. }
  894. _values[index] = value;
  895. }
  896. - (void)addValuesFromArray:(GPBInt64Array *)array {
  897. [self addValues:array->_values count:array->_count];
  898. }
  899. - (void)removeValueAtIndex:(NSUInteger)index {
  900. if (index >= _count) {
  901. [NSException raise:NSRangeException
  902. format:@"Index (%lu) beyond bounds (%lu)",
  903. (unsigned long)index, (unsigned long)_count];
  904. }
  905. NSUInteger newCount = _count - 1;
  906. if (index != newCount) {
  907. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(int64_t));
  908. }
  909. _count = newCount;
  910. if ((newCount + (2 * kChunkSize)) < _capacity) {
  911. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  912. }
  913. }
  914. - (void)removeAll {
  915. _count = 0;
  916. if ((0 + (2 * kChunkSize)) < _capacity) {
  917. [self internalResizeToCapacity:CapacityFromCount(0)];
  918. }
  919. }
  920. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  921. withValueAtIndex:(NSUInteger)idx2 {
  922. if (idx1 >= _count) {
  923. [NSException raise:NSRangeException
  924. format:@"Index (%lu) beyond bounds (%lu)",
  925. (unsigned long)idx1, (unsigned long)_count];
  926. }
  927. if (idx2 >= _count) {
  928. [NSException raise:NSRangeException
  929. format:@"Index (%lu) beyond bounds (%lu)",
  930. (unsigned long)idx2, (unsigned long)_count];
  931. }
  932. int64_t temp = _values[idx1];
  933. _values[idx1] = _values[idx2];
  934. _values[idx2] = temp;
  935. }
  936. @end
  937. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(UInt64, uint64_t, %llu)
  938. // This block of code is generated, do not edit it directly.
  939. #pragma mark - UInt64
  940. @implementation GPBUInt64Array {
  941. @package
  942. uint64_t *_values;
  943. NSUInteger _count;
  944. NSUInteger _capacity;
  945. }
  946. @synthesize count = _count;
  947. + (instancetype)array {
  948. return [[[self alloc] init] autorelease];
  949. }
  950. + (instancetype)arrayWithValue:(uint64_t)value {
  951. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  952. // the type correct.
  953. return [[(GPBUInt64Array*)[self alloc] initWithValues:&value count:1] autorelease];
  954. }
  955. + (instancetype)arrayWithValueArray:(GPBUInt64Array *)array {
  956. return [[(GPBUInt64Array*)[self alloc] initWithValueArray:array] autorelease];
  957. }
  958. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  959. return [[[self alloc] initWithCapacity:count] autorelease];
  960. }
  961. - (instancetype)init {
  962. self = [super init];
  963. // No work needed;
  964. return self;
  965. }
  966. - (instancetype)initWithValueArray:(GPBUInt64Array *)array {
  967. return [self initWithValues:array->_values count:array->_count];
  968. }
  969. - (instancetype)initWithValues:(const uint64_t [])values count:(NSUInteger)count {
  970. self = [self init];
  971. if (self) {
  972. if (count && values) {
  973. _values = reallocf(_values, count * sizeof(uint64_t));
  974. if (_values != NULL) {
  975. _capacity = count;
  976. memcpy(_values, values, count * sizeof(uint64_t));
  977. _count = count;
  978. } else {
  979. [self release];
  980. [NSException raise:NSMallocException
  981. format:@"Failed to allocate %lu bytes",
  982. (unsigned long)(count * sizeof(uint64_t))];
  983. }
  984. }
  985. }
  986. return self;
  987. }
  988. - (instancetype)initWithCapacity:(NSUInteger)count {
  989. self = [self initWithValues:NULL count:0];
  990. if (self && count) {
  991. [self internalResizeToCapacity:count];
  992. }
  993. return self;
  994. }
  995. - (instancetype)copyWithZone:(NSZone *)zone {
  996. return [[GPBUInt64Array allocWithZone:zone] initWithValues:_values count:_count];
  997. }
  998. - (void)dealloc {
  999. NSAssert(!_autocreator,
  1000. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1001. [self class], _autocreator);
  1002. free(_values);
  1003. [super dealloc];
  1004. }
  1005. - (BOOL)isEqual:(id)other {
  1006. if (self == other) {
  1007. return YES;
  1008. }
  1009. if (![other isKindOfClass:[GPBUInt64Array class]]) {
  1010. return NO;
  1011. }
  1012. GPBUInt64Array *otherArray = other;
  1013. return (_count == otherArray->_count
  1014. && memcmp(_values, otherArray->_values, (_count * sizeof(uint64_t))) == 0);
  1015. }
  1016. - (NSUInteger)hash {
  1017. // Follow NSArray's lead, and use the count as the hash.
  1018. return _count;
  1019. }
  1020. - (NSString *)description {
  1021. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  1022. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1023. if (i == 0) {
  1024. [result appendFormat:@"%llu", _values[i]];
  1025. } else {
  1026. [result appendFormat:@", %llu", _values[i]];
  1027. }
  1028. }
  1029. [result appendFormat:@" }"];
  1030. return result;
  1031. }
  1032. - (void)enumerateValuesWithBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block {
  1033. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  1034. }
  1035. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  1036. usingBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block {
  1037. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  1038. BOOL stop = NO;
  1039. if ((opts & NSEnumerationReverse) == 0) {
  1040. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1041. block(_values[i], i, &stop);
  1042. if (stop) break;
  1043. }
  1044. } else if (_count > 0) {
  1045. for (NSUInteger i = _count; i > 0; --i) {
  1046. block(_values[i - 1], (i - 1), &stop);
  1047. if (stop) break;
  1048. }
  1049. }
  1050. }
  1051. - (uint64_t)valueAtIndex:(NSUInteger)index {
  1052. if (index >= _count) {
  1053. [NSException raise:NSRangeException
  1054. format:@"Index (%lu) beyond bounds (%lu)",
  1055. (unsigned long)index, (unsigned long)_count];
  1056. }
  1057. return _values[index];
  1058. }
  1059. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  1060. _values = reallocf(_values, newCapacity * sizeof(uint64_t));
  1061. if (_values == NULL) {
  1062. _capacity = 0;
  1063. _count = 0;
  1064. [NSException raise:NSMallocException
  1065. format:@"Failed to allocate %lu bytes",
  1066. (unsigned long)(newCapacity * sizeof(uint64_t))];
  1067. }
  1068. _capacity = newCapacity;
  1069. }
  1070. - (void)addValue:(uint64_t)value {
  1071. [self addValues:&value count:1];
  1072. }
  1073. - (void)addValues:(const uint64_t [])values count:(NSUInteger)count {
  1074. if (values == NULL || count == 0) return;
  1075. NSUInteger initialCount = _count;
  1076. NSUInteger newCount = initialCount + count;
  1077. if (newCount > _capacity) {
  1078. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1079. }
  1080. _count = newCount;
  1081. memcpy(&_values[initialCount], values, count * sizeof(uint64_t));
  1082. if (_autocreator) {
  1083. GPBAutocreatedArrayModified(_autocreator, self);
  1084. }
  1085. }
  1086. - (void)insertValue:(uint64_t)value atIndex:(NSUInteger)index {
  1087. if (index >= _count + 1) {
  1088. [NSException raise:NSRangeException
  1089. format:@"Index (%lu) beyond bounds (%lu)",
  1090. (unsigned long)index, (unsigned long)_count + 1];
  1091. }
  1092. NSUInteger initialCount = _count;
  1093. NSUInteger newCount = initialCount + 1;
  1094. if (newCount > _capacity) {
  1095. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1096. }
  1097. _count = newCount;
  1098. if (index != initialCount) {
  1099. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(uint64_t));
  1100. }
  1101. _values[index] = value;
  1102. if (_autocreator) {
  1103. GPBAutocreatedArrayModified(_autocreator, self);
  1104. }
  1105. }
  1106. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(uint64_t)value {
  1107. if (index >= _count) {
  1108. [NSException raise:NSRangeException
  1109. format:@"Index (%lu) beyond bounds (%lu)",
  1110. (unsigned long)index, (unsigned long)_count];
  1111. }
  1112. _values[index] = value;
  1113. }
  1114. - (void)addValuesFromArray:(GPBUInt64Array *)array {
  1115. [self addValues:array->_values count:array->_count];
  1116. }
  1117. - (void)removeValueAtIndex:(NSUInteger)index {
  1118. if (index >= _count) {
  1119. [NSException raise:NSRangeException
  1120. format:@"Index (%lu) beyond bounds (%lu)",
  1121. (unsigned long)index, (unsigned long)_count];
  1122. }
  1123. NSUInteger newCount = _count - 1;
  1124. if (index != newCount) {
  1125. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(uint64_t));
  1126. }
  1127. _count = newCount;
  1128. if ((newCount + (2 * kChunkSize)) < _capacity) {
  1129. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1130. }
  1131. }
  1132. - (void)removeAll {
  1133. _count = 0;
  1134. if ((0 + (2 * kChunkSize)) < _capacity) {
  1135. [self internalResizeToCapacity:CapacityFromCount(0)];
  1136. }
  1137. }
  1138. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  1139. withValueAtIndex:(NSUInteger)idx2 {
  1140. if (idx1 >= _count) {
  1141. [NSException raise:NSRangeException
  1142. format:@"Index (%lu) beyond bounds (%lu)",
  1143. (unsigned long)idx1, (unsigned long)_count];
  1144. }
  1145. if (idx2 >= _count) {
  1146. [NSException raise:NSRangeException
  1147. format:@"Index (%lu) beyond bounds (%lu)",
  1148. (unsigned long)idx2, (unsigned long)_count];
  1149. }
  1150. uint64_t temp = _values[idx1];
  1151. _values[idx1] = _values[idx2];
  1152. _values[idx2] = temp;
  1153. }
  1154. @end
  1155. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Float, float, %f)
  1156. // This block of code is generated, do not edit it directly.
  1157. #pragma mark - Float
  1158. @implementation GPBFloatArray {
  1159. @package
  1160. float *_values;
  1161. NSUInteger _count;
  1162. NSUInteger _capacity;
  1163. }
  1164. @synthesize count = _count;
  1165. + (instancetype)array {
  1166. return [[[self alloc] init] autorelease];
  1167. }
  1168. + (instancetype)arrayWithValue:(float)value {
  1169. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  1170. // the type correct.
  1171. return [[(GPBFloatArray*)[self alloc] initWithValues:&value count:1] autorelease];
  1172. }
  1173. + (instancetype)arrayWithValueArray:(GPBFloatArray *)array {
  1174. return [[(GPBFloatArray*)[self alloc] initWithValueArray:array] autorelease];
  1175. }
  1176. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  1177. return [[[self alloc] initWithCapacity:count] autorelease];
  1178. }
  1179. - (instancetype)init {
  1180. self = [super init];
  1181. // No work needed;
  1182. return self;
  1183. }
  1184. - (instancetype)initWithValueArray:(GPBFloatArray *)array {
  1185. return [self initWithValues:array->_values count:array->_count];
  1186. }
  1187. - (instancetype)initWithValues:(const float [])values count:(NSUInteger)count {
  1188. self = [self init];
  1189. if (self) {
  1190. if (count && values) {
  1191. _values = reallocf(_values, count * sizeof(float));
  1192. if (_values != NULL) {
  1193. _capacity = count;
  1194. memcpy(_values, values, count * sizeof(float));
  1195. _count = count;
  1196. } else {
  1197. [self release];
  1198. [NSException raise:NSMallocException
  1199. format:@"Failed to allocate %lu bytes",
  1200. (unsigned long)(count * sizeof(float))];
  1201. }
  1202. }
  1203. }
  1204. return self;
  1205. }
  1206. - (instancetype)initWithCapacity:(NSUInteger)count {
  1207. self = [self initWithValues:NULL count:0];
  1208. if (self && count) {
  1209. [self internalResizeToCapacity:count];
  1210. }
  1211. return self;
  1212. }
  1213. - (instancetype)copyWithZone:(NSZone *)zone {
  1214. return [[GPBFloatArray allocWithZone:zone] initWithValues:_values count:_count];
  1215. }
  1216. - (void)dealloc {
  1217. NSAssert(!_autocreator,
  1218. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1219. [self class], _autocreator);
  1220. free(_values);
  1221. [super dealloc];
  1222. }
  1223. - (BOOL)isEqual:(id)other {
  1224. if (self == other) {
  1225. return YES;
  1226. }
  1227. if (![other isKindOfClass:[GPBFloatArray class]]) {
  1228. return NO;
  1229. }
  1230. GPBFloatArray *otherArray = other;
  1231. return (_count == otherArray->_count
  1232. && memcmp(_values, otherArray->_values, (_count * sizeof(float))) == 0);
  1233. }
  1234. - (NSUInteger)hash {
  1235. // Follow NSArray's lead, and use the count as the hash.
  1236. return _count;
  1237. }
  1238. - (NSString *)description {
  1239. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  1240. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1241. if (i == 0) {
  1242. [result appendFormat:@"%f", _values[i]];
  1243. } else {
  1244. [result appendFormat:@", %f", _values[i]];
  1245. }
  1246. }
  1247. [result appendFormat:@" }"];
  1248. return result;
  1249. }
  1250. - (void)enumerateValuesWithBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block {
  1251. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  1252. }
  1253. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  1254. usingBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block {
  1255. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  1256. BOOL stop = NO;
  1257. if ((opts & NSEnumerationReverse) == 0) {
  1258. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1259. block(_values[i], i, &stop);
  1260. if (stop) break;
  1261. }
  1262. } else if (_count > 0) {
  1263. for (NSUInteger i = _count; i > 0; --i) {
  1264. block(_values[i - 1], (i - 1), &stop);
  1265. if (stop) break;
  1266. }
  1267. }
  1268. }
  1269. - (float)valueAtIndex:(NSUInteger)index {
  1270. if (index >= _count) {
  1271. [NSException raise:NSRangeException
  1272. format:@"Index (%lu) beyond bounds (%lu)",
  1273. (unsigned long)index, (unsigned long)_count];
  1274. }
  1275. return _values[index];
  1276. }
  1277. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  1278. _values = reallocf(_values, newCapacity * sizeof(float));
  1279. if (_values == NULL) {
  1280. _capacity = 0;
  1281. _count = 0;
  1282. [NSException raise:NSMallocException
  1283. format:@"Failed to allocate %lu bytes",
  1284. (unsigned long)(newCapacity * sizeof(float))];
  1285. }
  1286. _capacity = newCapacity;
  1287. }
  1288. - (void)addValue:(float)value {
  1289. [self addValues:&value count:1];
  1290. }
  1291. - (void)addValues:(const float [])values count:(NSUInteger)count {
  1292. if (values == NULL || count == 0) return;
  1293. NSUInteger initialCount = _count;
  1294. NSUInteger newCount = initialCount + count;
  1295. if (newCount > _capacity) {
  1296. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1297. }
  1298. _count = newCount;
  1299. memcpy(&_values[initialCount], values, count * sizeof(float));
  1300. if (_autocreator) {
  1301. GPBAutocreatedArrayModified(_autocreator, self);
  1302. }
  1303. }
  1304. - (void)insertValue:(float)value atIndex:(NSUInteger)index {
  1305. if (index >= _count + 1) {
  1306. [NSException raise:NSRangeException
  1307. format:@"Index (%lu) beyond bounds (%lu)",
  1308. (unsigned long)index, (unsigned long)_count + 1];
  1309. }
  1310. NSUInteger initialCount = _count;
  1311. NSUInteger newCount = initialCount + 1;
  1312. if (newCount > _capacity) {
  1313. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1314. }
  1315. _count = newCount;
  1316. if (index != initialCount) {
  1317. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(float));
  1318. }
  1319. _values[index] = value;
  1320. if (_autocreator) {
  1321. GPBAutocreatedArrayModified(_autocreator, self);
  1322. }
  1323. }
  1324. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(float)value {
  1325. if (index >= _count) {
  1326. [NSException raise:NSRangeException
  1327. format:@"Index (%lu) beyond bounds (%lu)",
  1328. (unsigned long)index, (unsigned long)_count];
  1329. }
  1330. _values[index] = value;
  1331. }
  1332. - (void)addValuesFromArray:(GPBFloatArray *)array {
  1333. [self addValues:array->_values count:array->_count];
  1334. }
  1335. - (void)removeValueAtIndex:(NSUInteger)index {
  1336. if (index >= _count) {
  1337. [NSException raise:NSRangeException
  1338. format:@"Index (%lu) beyond bounds (%lu)",
  1339. (unsigned long)index, (unsigned long)_count];
  1340. }
  1341. NSUInteger newCount = _count - 1;
  1342. if (index != newCount) {
  1343. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(float));
  1344. }
  1345. _count = newCount;
  1346. if ((newCount + (2 * kChunkSize)) < _capacity) {
  1347. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1348. }
  1349. }
  1350. - (void)removeAll {
  1351. _count = 0;
  1352. if ((0 + (2 * kChunkSize)) < _capacity) {
  1353. [self internalResizeToCapacity:CapacityFromCount(0)];
  1354. }
  1355. }
  1356. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  1357. withValueAtIndex:(NSUInteger)idx2 {
  1358. if (idx1 >= _count) {
  1359. [NSException raise:NSRangeException
  1360. format:@"Index (%lu) beyond bounds (%lu)",
  1361. (unsigned long)idx1, (unsigned long)_count];
  1362. }
  1363. if (idx2 >= _count) {
  1364. [NSException raise:NSRangeException
  1365. format:@"Index (%lu) beyond bounds (%lu)",
  1366. (unsigned long)idx2, (unsigned long)_count];
  1367. }
  1368. float temp = _values[idx1];
  1369. _values[idx1] = _values[idx2];
  1370. _values[idx2] = temp;
  1371. }
  1372. @end
  1373. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Double, double, %lf)
  1374. // This block of code is generated, do not edit it directly.
  1375. #pragma mark - Double
  1376. @implementation GPBDoubleArray {
  1377. @package
  1378. double *_values;
  1379. NSUInteger _count;
  1380. NSUInteger _capacity;
  1381. }
  1382. @synthesize count = _count;
  1383. + (instancetype)array {
  1384. return [[[self alloc] init] autorelease];
  1385. }
  1386. + (instancetype)arrayWithValue:(double)value {
  1387. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  1388. // the type correct.
  1389. return [[(GPBDoubleArray*)[self alloc] initWithValues:&value count:1] autorelease];
  1390. }
  1391. + (instancetype)arrayWithValueArray:(GPBDoubleArray *)array {
  1392. return [[(GPBDoubleArray*)[self alloc] initWithValueArray:array] autorelease];
  1393. }
  1394. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  1395. return [[[self alloc] initWithCapacity:count] autorelease];
  1396. }
  1397. - (instancetype)init {
  1398. self = [super init];
  1399. // No work needed;
  1400. return self;
  1401. }
  1402. - (instancetype)initWithValueArray:(GPBDoubleArray *)array {
  1403. return [self initWithValues:array->_values count:array->_count];
  1404. }
  1405. - (instancetype)initWithValues:(const double [])values count:(NSUInteger)count {
  1406. self = [self init];
  1407. if (self) {
  1408. if (count && values) {
  1409. _values = reallocf(_values, count * sizeof(double));
  1410. if (_values != NULL) {
  1411. _capacity = count;
  1412. memcpy(_values, values, count * sizeof(double));
  1413. _count = count;
  1414. } else {
  1415. [self release];
  1416. [NSException raise:NSMallocException
  1417. format:@"Failed to allocate %lu bytes",
  1418. (unsigned long)(count * sizeof(double))];
  1419. }
  1420. }
  1421. }
  1422. return self;
  1423. }
  1424. - (instancetype)initWithCapacity:(NSUInteger)count {
  1425. self = [self initWithValues:NULL count:0];
  1426. if (self && count) {
  1427. [self internalResizeToCapacity:count];
  1428. }
  1429. return self;
  1430. }
  1431. - (instancetype)copyWithZone:(NSZone *)zone {
  1432. return [[GPBDoubleArray allocWithZone:zone] initWithValues:_values count:_count];
  1433. }
  1434. - (void)dealloc {
  1435. NSAssert(!_autocreator,
  1436. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1437. [self class], _autocreator);
  1438. free(_values);
  1439. [super dealloc];
  1440. }
  1441. - (BOOL)isEqual:(id)other {
  1442. if (self == other) {
  1443. return YES;
  1444. }
  1445. if (![other isKindOfClass:[GPBDoubleArray class]]) {
  1446. return NO;
  1447. }
  1448. GPBDoubleArray *otherArray = other;
  1449. return (_count == otherArray->_count
  1450. && memcmp(_values, otherArray->_values, (_count * sizeof(double))) == 0);
  1451. }
  1452. - (NSUInteger)hash {
  1453. // Follow NSArray's lead, and use the count as the hash.
  1454. return _count;
  1455. }
  1456. - (NSString *)description {
  1457. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  1458. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1459. if (i == 0) {
  1460. [result appendFormat:@"%lf", _values[i]];
  1461. } else {
  1462. [result appendFormat:@", %lf", _values[i]];
  1463. }
  1464. }
  1465. [result appendFormat:@" }"];
  1466. return result;
  1467. }
  1468. - (void)enumerateValuesWithBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block {
  1469. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  1470. }
  1471. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  1472. usingBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block {
  1473. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  1474. BOOL stop = NO;
  1475. if ((opts & NSEnumerationReverse) == 0) {
  1476. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1477. block(_values[i], i, &stop);
  1478. if (stop) break;
  1479. }
  1480. } else if (_count > 0) {
  1481. for (NSUInteger i = _count; i > 0; --i) {
  1482. block(_values[i - 1], (i - 1), &stop);
  1483. if (stop) break;
  1484. }
  1485. }
  1486. }
  1487. - (double)valueAtIndex:(NSUInteger)index {
  1488. if (index >= _count) {
  1489. [NSException raise:NSRangeException
  1490. format:@"Index (%lu) beyond bounds (%lu)",
  1491. (unsigned long)index, (unsigned long)_count];
  1492. }
  1493. return _values[index];
  1494. }
  1495. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  1496. _values = reallocf(_values, newCapacity * sizeof(double));
  1497. if (_values == NULL) {
  1498. _capacity = 0;
  1499. _count = 0;
  1500. [NSException raise:NSMallocException
  1501. format:@"Failed to allocate %lu bytes",
  1502. (unsigned long)(newCapacity * sizeof(double))];
  1503. }
  1504. _capacity = newCapacity;
  1505. }
  1506. - (void)addValue:(double)value {
  1507. [self addValues:&value count:1];
  1508. }
  1509. - (void)addValues:(const double [])values count:(NSUInteger)count {
  1510. if (values == NULL || count == 0) return;
  1511. NSUInteger initialCount = _count;
  1512. NSUInteger newCount = initialCount + count;
  1513. if (newCount > _capacity) {
  1514. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1515. }
  1516. _count = newCount;
  1517. memcpy(&_values[initialCount], values, count * sizeof(double));
  1518. if (_autocreator) {
  1519. GPBAutocreatedArrayModified(_autocreator, self);
  1520. }
  1521. }
  1522. - (void)insertValue:(double)value atIndex:(NSUInteger)index {
  1523. if (index >= _count + 1) {
  1524. [NSException raise:NSRangeException
  1525. format:@"Index (%lu) beyond bounds (%lu)",
  1526. (unsigned long)index, (unsigned long)_count + 1];
  1527. }
  1528. NSUInteger initialCount = _count;
  1529. NSUInteger newCount = initialCount + 1;
  1530. if (newCount > _capacity) {
  1531. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1532. }
  1533. _count = newCount;
  1534. if (index != initialCount) {
  1535. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(double));
  1536. }
  1537. _values[index] = value;
  1538. if (_autocreator) {
  1539. GPBAutocreatedArrayModified(_autocreator, self);
  1540. }
  1541. }
  1542. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(double)value {
  1543. if (index >= _count) {
  1544. [NSException raise:NSRangeException
  1545. format:@"Index (%lu) beyond bounds (%lu)",
  1546. (unsigned long)index, (unsigned long)_count];
  1547. }
  1548. _values[index] = value;
  1549. }
  1550. - (void)addValuesFromArray:(GPBDoubleArray *)array {
  1551. [self addValues:array->_values count:array->_count];
  1552. }
  1553. - (void)removeValueAtIndex:(NSUInteger)index {
  1554. if (index >= _count) {
  1555. [NSException raise:NSRangeException
  1556. format:@"Index (%lu) beyond bounds (%lu)",
  1557. (unsigned long)index, (unsigned long)_count];
  1558. }
  1559. NSUInteger newCount = _count - 1;
  1560. if (index != newCount) {
  1561. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(double));
  1562. }
  1563. _count = newCount;
  1564. if ((newCount + (2 * kChunkSize)) < _capacity) {
  1565. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1566. }
  1567. }
  1568. - (void)removeAll {
  1569. _count = 0;
  1570. if ((0 + (2 * kChunkSize)) < _capacity) {
  1571. [self internalResizeToCapacity:CapacityFromCount(0)];
  1572. }
  1573. }
  1574. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  1575. withValueAtIndex:(NSUInteger)idx2 {
  1576. if (idx1 >= _count) {
  1577. [NSException raise:NSRangeException
  1578. format:@"Index (%lu) beyond bounds (%lu)",
  1579. (unsigned long)idx1, (unsigned long)_count];
  1580. }
  1581. if (idx2 >= _count) {
  1582. [NSException raise:NSRangeException
  1583. format:@"Index (%lu) beyond bounds (%lu)",
  1584. (unsigned long)idx2, (unsigned long)_count];
  1585. }
  1586. double temp = _values[idx1];
  1587. _values[idx1] = _values[idx2];
  1588. _values[idx2] = temp;
  1589. }
  1590. @end
  1591. //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Bool, BOOL, %d)
  1592. // This block of code is generated, do not edit it directly.
  1593. #pragma mark - Bool
  1594. @implementation GPBBoolArray {
  1595. @package
  1596. BOOL *_values;
  1597. NSUInteger _count;
  1598. NSUInteger _capacity;
  1599. }
  1600. @synthesize count = _count;
  1601. + (instancetype)array {
  1602. return [[[self alloc] init] autorelease];
  1603. }
  1604. + (instancetype)arrayWithValue:(BOOL)value {
  1605. // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
  1606. // the type correct.
  1607. return [[(GPBBoolArray*)[self alloc] initWithValues:&value count:1] autorelease];
  1608. }
  1609. + (instancetype)arrayWithValueArray:(GPBBoolArray *)array {
  1610. return [[(GPBBoolArray*)[self alloc] initWithValueArray:array] autorelease];
  1611. }
  1612. + (instancetype)arrayWithCapacity:(NSUInteger)count {
  1613. return [[[self alloc] initWithCapacity:count] autorelease];
  1614. }
  1615. - (instancetype)init {
  1616. self = [super init];
  1617. // No work needed;
  1618. return self;
  1619. }
  1620. - (instancetype)initWithValueArray:(GPBBoolArray *)array {
  1621. return [self initWithValues:array->_values count:array->_count];
  1622. }
  1623. - (instancetype)initWithValues:(const BOOL [])values count:(NSUInteger)count {
  1624. self = [self init];
  1625. if (self) {
  1626. if (count && values) {
  1627. _values = reallocf(_values, count * sizeof(BOOL));
  1628. if (_values != NULL) {
  1629. _capacity = count;
  1630. memcpy(_values, values, count * sizeof(BOOL));
  1631. _count = count;
  1632. } else {
  1633. [self release];
  1634. [NSException raise:NSMallocException
  1635. format:@"Failed to allocate %lu bytes",
  1636. (unsigned long)(count * sizeof(BOOL))];
  1637. }
  1638. }
  1639. }
  1640. return self;
  1641. }
  1642. - (instancetype)initWithCapacity:(NSUInteger)count {
  1643. self = [self initWithValues:NULL count:0];
  1644. if (self && count) {
  1645. [self internalResizeToCapacity:count];
  1646. }
  1647. return self;
  1648. }
  1649. - (instancetype)copyWithZone:(NSZone *)zone {
  1650. return [[GPBBoolArray allocWithZone:zone] initWithValues:_values count:_count];
  1651. }
  1652. - (void)dealloc {
  1653. NSAssert(!_autocreator,
  1654. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1655. [self class], _autocreator);
  1656. free(_values);
  1657. [super dealloc];
  1658. }
  1659. - (BOOL)isEqual:(id)other {
  1660. if (self == other) {
  1661. return YES;
  1662. }
  1663. if (![other isKindOfClass:[GPBBoolArray class]]) {
  1664. return NO;
  1665. }
  1666. GPBBoolArray *otherArray = other;
  1667. return (_count == otherArray->_count
  1668. && memcmp(_values, otherArray->_values, (_count * sizeof(BOOL))) == 0);
  1669. }
  1670. - (NSUInteger)hash {
  1671. // Follow NSArray's lead, and use the count as the hash.
  1672. return _count;
  1673. }
  1674. - (NSString *)description {
  1675. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  1676. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1677. if (i == 0) {
  1678. [result appendFormat:@"%d", _values[i]];
  1679. } else {
  1680. [result appendFormat:@", %d", _values[i]];
  1681. }
  1682. }
  1683. [result appendFormat:@" }"];
  1684. return result;
  1685. }
  1686. - (void)enumerateValuesWithBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block {
  1687. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  1688. }
  1689. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  1690. usingBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block {
  1691. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  1692. BOOL stop = NO;
  1693. if ((opts & NSEnumerationReverse) == 0) {
  1694. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1695. block(_values[i], i, &stop);
  1696. if (stop) break;
  1697. }
  1698. } else if (_count > 0) {
  1699. for (NSUInteger i = _count; i > 0; --i) {
  1700. block(_values[i - 1], (i - 1), &stop);
  1701. if (stop) break;
  1702. }
  1703. }
  1704. }
  1705. - (BOOL)valueAtIndex:(NSUInteger)index {
  1706. if (index >= _count) {
  1707. [NSException raise:NSRangeException
  1708. format:@"Index (%lu) beyond bounds (%lu)",
  1709. (unsigned long)index, (unsigned long)_count];
  1710. }
  1711. return _values[index];
  1712. }
  1713. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  1714. _values = reallocf(_values, newCapacity * sizeof(BOOL));
  1715. if (_values == NULL) {
  1716. _capacity = 0;
  1717. _count = 0;
  1718. [NSException raise:NSMallocException
  1719. format:@"Failed to allocate %lu bytes",
  1720. (unsigned long)(newCapacity * sizeof(BOOL))];
  1721. }
  1722. _capacity = newCapacity;
  1723. }
  1724. - (void)addValue:(BOOL)value {
  1725. [self addValues:&value count:1];
  1726. }
  1727. - (void)addValues:(const BOOL [])values count:(NSUInteger)count {
  1728. if (values == NULL || count == 0) return;
  1729. NSUInteger initialCount = _count;
  1730. NSUInteger newCount = initialCount + count;
  1731. if (newCount > _capacity) {
  1732. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1733. }
  1734. _count = newCount;
  1735. memcpy(&_values[initialCount], values, count * sizeof(BOOL));
  1736. if (_autocreator) {
  1737. GPBAutocreatedArrayModified(_autocreator, self);
  1738. }
  1739. }
  1740. - (void)insertValue:(BOOL)value atIndex:(NSUInteger)index {
  1741. if (index >= _count + 1) {
  1742. [NSException raise:NSRangeException
  1743. format:@"Index (%lu) beyond bounds (%lu)",
  1744. (unsigned long)index, (unsigned long)_count + 1];
  1745. }
  1746. NSUInteger initialCount = _count;
  1747. NSUInteger newCount = initialCount + 1;
  1748. if (newCount > _capacity) {
  1749. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1750. }
  1751. _count = newCount;
  1752. if (index != initialCount) {
  1753. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(BOOL));
  1754. }
  1755. _values[index] = value;
  1756. if (_autocreator) {
  1757. GPBAutocreatedArrayModified(_autocreator, self);
  1758. }
  1759. }
  1760. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(BOOL)value {
  1761. if (index >= _count) {
  1762. [NSException raise:NSRangeException
  1763. format:@"Index (%lu) beyond bounds (%lu)",
  1764. (unsigned long)index, (unsigned long)_count];
  1765. }
  1766. _values[index] = value;
  1767. }
  1768. - (void)addValuesFromArray:(GPBBoolArray *)array {
  1769. [self addValues:array->_values count:array->_count];
  1770. }
  1771. - (void)removeValueAtIndex:(NSUInteger)index {
  1772. if (index >= _count) {
  1773. [NSException raise:NSRangeException
  1774. format:@"Index (%lu) beyond bounds (%lu)",
  1775. (unsigned long)index, (unsigned long)_count];
  1776. }
  1777. NSUInteger newCount = _count - 1;
  1778. if (index != newCount) {
  1779. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(BOOL));
  1780. }
  1781. _count = newCount;
  1782. if ((newCount + (2 * kChunkSize)) < _capacity) {
  1783. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  1784. }
  1785. }
  1786. - (void)removeAll {
  1787. _count = 0;
  1788. if ((0 + (2 * kChunkSize)) < _capacity) {
  1789. [self internalResizeToCapacity:CapacityFromCount(0)];
  1790. }
  1791. }
  1792. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  1793. withValueAtIndex:(NSUInteger)idx2 {
  1794. if (idx1 >= _count) {
  1795. [NSException raise:NSRangeException
  1796. format:@"Index (%lu) beyond bounds (%lu)",
  1797. (unsigned long)idx1, (unsigned long)_count];
  1798. }
  1799. if (idx2 >= _count) {
  1800. [NSException raise:NSRangeException
  1801. format:@"Index (%lu) beyond bounds (%lu)",
  1802. (unsigned long)idx2, (unsigned long)_count];
  1803. }
  1804. BOOL temp = _values[idx1];
  1805. _values[idx1] = _values[idx2];
  1806. _values[idx2] = temp;
  1807. }
  1808. @end
  1809. //%PDDM-EXPAND-END (7 expansions)
  1810. #pragma mark - Enum
  1811. @implementation GPBEnumArray {
  1812. @package
  1813. GPBEnumValidationFunc _validationFunc;
  1814. int32_t *_values;
  1815. NSUInteger _count;
  1816. NSUInteger _capacity;
  1817. }
  1818. @synthesize count = _count;
  1819. @synthesize validationFunc = _validationFunc;
  1820. + (instancetype)array {
  1821. return [[[self alloc] initWithValidationFunction:NULL] autorelease];
  1822. }
  1823. + (instancetype)arrayWithValidationFunction:(GPBEnumValidationFunc)func {
  1824. return [[[self alloc] initWithValidationFunction:func] autorelease];
  1825. }
  1826. + (instancetype)arrayWithValidationFunction:(GPBEnumValidationFunc)func
  1827. rawValue:(int32_t)value {
  1828. return [[[self alloc] initWithValidationFunction:func
  1829. rawValues:&value
  1830. count:1] autorelease];
  1831. }
  1832. + (instancetype)arrayWithValueArray:(GPBEnumArray *)array {
  1833. return [[(GPBEnumArray*)[self alloc] initWithValueArray:array] autorelease];
  1834. }
  1835. + (instancetype)arrayWithValidationFunction:(GPBEnumValidationFunc)func
  1836. capacity:(NSUInteger)count {
  1837. return [[[self alloc] initWithValidationFunction:func capacity:count] autorelease];
  1838. }
  1839. - (instancetype)init {
  1840. return [self initWithValidationFunction:NULL];
  1841. }
  1842. - (instancetype)initWithValueArray:(GPBEnumArray *)array {
  1843. return [self initWithValidationFunction:array->_validationFunc
  1844. rawValues:array->_values
  1845. count:array->_count];
  1846. }
  1847. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  1848. self = [super init];
  1849. if (self) {
  1850. _validationFunc = (func != NULL ? func : ArrayDefault_IsValidValue);
  1851. }
  1852. return self;
  1853. }
  1854. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  1855. rawValues:(const int32_t [])values
  1856. count:(NSUInteger)count {
  1857. self = [self initWithValidationFunction:func];
  1858. if (self) {
  1859. if (count && values) {
  1860. _values = reallocf(_values, count * sizeof(int32_t));
  1861. if (_values != NULL) {
  1862. _capacity = count;
  1863. memcpy(_values, values, count * sizeof(int32_t));
  1864. _count = count;
  1865. } else {
  1866. [self release];
  1867. [NSException raise:NSMallocException
  1868. format:@"Failed to allocate %lu bytes",
  1869. (unsigned long)(count * sizeof(int32_t))];
  1870. }
  1871. }
  1872. }
  1873. return self;
  1874. }
  1875. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  1876. capacity:(NSUInteger)count {
  1877. self = [self initWithValidationFunction:func];
  1878. if (self && count) {
  1879. [self internalResizeToCapacity:count];
  1880. }
  1881. return self;
  1882. }
  1883. - (instancetype)copyWithZone:(NSZone *)zone {
  1884. return [[GPBEnumArray allocWithZone:zone]
  1885. initWithValidationFunction:_validationFunc
  1886. rawValues:_values
  1887. count:_count];
  1888. }
  1889. //%PDDM-EXPAND ARRAY_IMMUTABLE_CORE(Enum, int32_t, Raw, %d)
  1890. // This block of code is generated, do not edit it directly.
  1891. - (void)dealloc {
  1892. NSAssert(!_autocreator,
  1893. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1894. [self class], _autocreator);
  1895. free(_values);
  1896. [super dealloc];
  1897. }
  1898. - (BOOL)isEqual:(id)other {
  1899. if (self == other) {
  1900. return YES;
  1901. }
  1902. if (![other isKindOfClass:[GPBEnumArray class]]) {
  1903. return NO;
  1904. }
  1905. GPBEnumArray *otherArray = other;
  1906. return (_count == otherArray->_count
  1907. && memcmp(_values, otherArray->_values, (_count * sizeof(int32_t))) == 0);
  1908. }
  1909. - (NSUInteger)hash {
  1910. // Follow NSArray's lead, and use the count as the hash.
  1911. return _count;
  1912. }
  1913. - (NSString *)description {
  1914. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
  1915. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1916. if (i == 0) {
  1917. [result appendFormat:@"%d", _values[i]];
  1918. } else {
  1919. [result appendFormat:@", %d", _values[i]];
  1920. }
  1921. }
  1922. [result appendFormat:@" }"];
  1923. return result;
  1924. }
  1925. - (void)enumerateRawValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
  1926. [self enumerateRawValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  1927. }
  1928. - (void)enumerateRawValuesWithOptions:(NSEnumerationOptions)opts
  1929. usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
  1930. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  1931. BOOL stop = NO;
  1932. if ((opts & NSEnumerationReverse) == 0) {
  1933. for (NSUInteger i = 0, count = _count; i < count; ++i) {
  1934. block(_values[i], i, &stop);
  1935. if (stop) break;
  1936. }
  1937. } else if (_count > 0) {
  1938. for (NSUInteger i = _count; i > 0; --i) {
  1939. block(_values[i - 1], (i - 1), &stop);
  1940. if (stop) break;
  1941. }
  1942. }
  1943. }
  1944. //%PDDM-EXPAND-END ARRAY_IMMUTABLE_CORE(Enum, int32_t, Raw, %d)
  1945. - (int32_t)valueAtIndex:(NSUInteger)index {
  1946. //%PDDM-EXPAND VALIDATE_RANGE(index, _count)
  1947. // This block of code is generated, do not edit it directly.
  1948. if (index >= _count) {
  1949. [NSException raise:NSRangeException
  1950. format:@"Index (%lu) beyond bounds (%lu)",
  1951. (unsigned long)index, (unsigned long)_count];
  1952. }
  1953. //%PDDM-EXPAND-END VALIDATE_RANGE(index, _count)
  1954. int32_t result = _values[index];
  1955. if (!_validationFunc(result)) {
  1956. result = kGPBUnrecognizedEnumeratorValue;
  1957. }
  1958. return result;
  1959. }
  1960. - (int32_t)rawValueAtIndex:(NSUInteger)index {
  1961. //%PDDM-EXPAND VALIDATE_RANGE(index, _count)
  1962. // This block of code is generated, do not edit it directly.
  1963. if (index >= _count) {
  1964. [NSException raise:NSRangeException
  1965. format:@"Index (%lu) beyond bounds (%lu)",
  1966. (unsigned long)index, (unsigned long)_count];
  1967. }
  1968. //%PDDM-EXPAND-END VALIDATE_RANGE(index, _count)
  1969. return _values[index];
  1970. }
  1971. - (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
  1972. [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
  1973. }
  1974. - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
  1975. usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
  1976. // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
  1977. BOOL stop = NO;
  1978. GPBEnumValidationFunc func = _validationFunc;
  1979. if ((opts & NSEnumerationReverse) == 0) {
  1980. int32_t *scan = _values;
  1981. int32_t *end = scan + _count;
  1982. for (NSUInteger i = 0; scan < end; ++i, ++scan) {
  1983. int32_t value = *scan;
  1984. if (!func(value)) {
  1985. value = kGPBUnrecognizedEnumeratorValue;
  1986. }
  1987. block(value, i, &stop);
  1988. if (stop) break;
  1989. }
  1990. } else if (_count > 0) {
  1991. int32_t *end = _values;
  1992. int32_t *scan = end + (_count - 1);
  1993. for (NSUInteger i = (_count - 1); scan >= end; --i, --scan) {
  1994. int32_t value = *scan;
  1995. if (!func(value)) {
  1996. value = kGPBUnrecognizedEnumeratorValue;
  1997. }
  1998. block(value, i, &stop);
  1999. if (stop) break;
  2000. }
  2001. }
  2002. }
  2003. //%PDDM-EXPAND ARRAY_MUTABLE_CORE(Enum, int32_t, Raw, %d)
  2004. // This block of code is generated, do not edit it directly.
  2005. - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
  2006. _values = reallocf(_values, newCapacity * sizeof(int32_t));
  2007. if (_values == NULL) {
  2008. _capacity = 0;
  2009. _count = 0;
  2010. [NSException raise:NSMallocException
  2011. format:@"Failed to allocate %lu bytes",
  2012. (unsigned long)(newCapacity * sizeof(int32_t))];
  2013. }
  2014. _capacity = newCapacity;
  2015. }
  2016. - (void)addRawValue:(int32_t)value {
  2017. [self addRawValues:&value count:1];
  2018. }
  2019. - (void)addRawValues:(const int32_t [])values count:(NSUInteger)count {
  2020. if (values == NULL || count == 0) return;
  2021. NSUInteger initialCount = _count;
  2022. NSUInteger newCount = initialCount + count;
  2023. if (newCount > _capacity) {
  2024. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  2025. }
  2026. _count = newCount;
  2027. memcpy(&_values[initialCount], values, count * sizeof(int32_t));
  2028. if (_autocreator) {
  2029. GPBAutocreatedArrayModified(_autocreator, self);
  2030. }
  2031. }
  2032. - (void)insertRawValue:(int32_t)value atIndex:(NSUInteger)index {
  2033. if (index >= _count + 1) {
  2034. [NSException raise:NSRangeException
  2035. format:@"Index (%lu) beyond bounds (%lu)",
  2036. (unsigned long)index, (unsigned long)_count + 1];
  2037. }
  2038. NSUInteger initialCount = _count;
  2039. NSUInteger newCount = initialCount + 1;
  2040. if (newCount > _capacity) {
  2041. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  2042. }
  2043. _count = newCount;
  2044. if (index != initialCount) {
  2045. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int32_t));
  2046. }
  2047. _values[index] = value;
  2048. if (_autocreator) {
  2049. GPBAutocreatedArrayModified(_autocreator, self);
  2050. }
  2051. }
  2052. - (void)replaceValueAtIndex:(NSUInteger)index withRawValue:(int32_t)value {
  2053. if (index >= _count) {
  2054. [NSException raise:NSRangeException
  2055. format:@"Index (%lu) beyond bounds (%lu)",
  2056. (unsigned long)index, (unsigned long)_count];
  2057. }
  2058. _values[index] = value;
  2059. }
  2060. - (void)addRawValuesFromArray:(GPBEnumArray *)array {
  2061. [self addRawValues:array->_values count:array->_count];
  2062. }
  2063. - (void)removeValueAtIndex:(NSUInteger)index {
  2064. if (index >= _count) {
  2065. [NSException raise:NSRangeException
  2066. format:@"Index (%lu) beyond bounds (%lu)",
  2067. (unsigned long)index, (unsigned long)_count];
  2068. }
  2069. NSUInteger newCount = _count - 1;
  2070. if (index != newCount) {
  2071. memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(int32_t));
  2072. }
  2073. _count = newCount;
  2074. if ((newCount + (2 * kChunkSize)) < _capacity) {
  2075. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  2076. }
  2077. }
  2078. - (void)removeAll {
  2079. _count = 0;
  2080. if ((0 + (2 * kChunkSize)) < _capacity) {
  2081. [self internalResizeToCapacity:CapacityFromCount(0)];
  2082. }
  2083. }
  2084. - (void)exchangeValueAtIndex:(NSUInteger)idx1
  2085. withValueAtIndex:(NSUInteger)idx2 {
  2086. if (idx1 >= _count) {
  2087. [NSException raise:NSRangeException
  2088. format:@"Index (%lu) beyond bounds (%lu)",
  2089. (unsigned long)idx1, (unsigned long)_count];
  2090. }
  2091. if (idx2 >= _count) {
  2092. [NSException raise:NSRangeException
  2093. format:@"Index (%lu) beyond bounds (%lu)",
  2094. (unsigned long)idx2, (unsigned long)_count];
  2095. }
  2096. int32_t temp = _values[idx1];
  2097. _values[idx1] = _values[idx2];
  2098. _values[idx2] = temp;
  2099. }
  2100. //%PDDM-EXPAND MUTATION_METHODS(Enum, int32_t, , EnumValidationList, EnumValidationOne)
  2101. // This block of code is generated, do not edit it directly.
  2102. - (void)addValue:(int32_t)value {
  2103. [self addValues:&value count:1];
  2104. }
  2105. - (void)addValues:(const int32_t [])values count:(NSUInteger)count {
  2106. if (values == NULL || count == 0) return;
  2107. GPBEnumValidationFunc func = _validationFunc;
  2108. for (NSUInteger i = 0; i < count; ++i) {
  2109. if (!func(values[i])) {
  2110. [NSException raise:NSInvalidArgumentException
  2111. format:@"%@: Attempt to set an unknown enum value (%d)",
  2112. [self class], values[i]];
  2113. }
  2114. }
  2115. NSUInteger initialCount = _count;
  2116. NSUInteger newCount = initialCount + count;
  2117. if (newCount > _capacity) {
  2118. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  2119. }
  2120. _count = newCount;
  2121. memcpy(&_values[initialCount], values, count * sizeof(int32_t));
  2122. if (_autocreator) {
  2123. GPBAutocreatedArrayModified(_autocreator, self);
  2124. }
  2125. }
  2126. - (void)insertValue:(int32_t)value atIndex:(NSUInteger)index {
  2127. if (index >= _count + 1) {
  2128. [NSException raise:NSRangeException
  2129. format:@"Index (%lu) beyond bounds (%lu)",
  2130. (unsigned long)index, (unsigned long)_count + 1];
  2131. }
  2132. if (!_validationFunc(value)) {
  2133. [NSException raise:NSInvalidArgumentException
  2134. format:@"%@: Attempt to set an unknown enum value (%d)",
  2135. [self class], value];
  2136. }
  2137. NSUInteger initialCount = _count;
  2138. NSUInteger newCount = initialCount + 1;
  2139. if (newCount > _capacity) {
  2140. [self internalResizeToCapacity:CapacityFromCount(newCount)];
  2141. }
  2142. _count = newCount;
  2143. if (index != initialCount) {
  2144. memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int32_t));
  2145. }
  2146. _values[index] = value;
  2147. if (_autocreator) {
  2148. GPBAutocreatedArrayModified(_autocreator, self);
  2149. }
  2150. }
  2151. - (void)replaceValueAtIndex:(NSUInteger)index withValue:(int32_t)value {
  2152. if (index >= _count) {
  2153. [NSException raise:NSRangeException
  2154. format:@"Index (%lu) beyond bounds (%lu)",
  2155. (unsigned long)index, (unsigned long)_count];
  2156. }
  2157. if (!_validationFunc(value)) {
  2158. [NSException raise:NSInvalidArgumentException
  2159. format:@"%@: Attempt to set an unknown enum value (%d)",
  2160. [self class], value];
  2161. }
  2162. _values[index] = value;
  2163. }
  2164. //%PDDM-EXPAND-END (2 expansions)
  2165. //%PDDM-DEFINE MUTATION_HOOK_EnumValidationList()
  2166. //% GPBEnumValidationFunc func = _validationFunc;
  2167. //% for (NSUInteger i = 0; i < count; ++i) {
  2168. //% if (!func(values[i])) {
  2169. //% [NSException raise:NSInvalidArgumentException
  2170. //% format:@"%@: Attempt to set an unknown enum value (%d)",
  2171. //% [self class], values[i]];
  2172. //% }
  2173. //% }
  2174. //%
  2175. //%PDDM-DEFINE MUTATION_HOOK_EnumValidationOne()
  2176. //% if (!_validationFunc(value)) {
  2177. //% [NSException raise:NSInvalidArgumentException
  2178. //% format:@"%@: Attempt to set an unknown enum value (%d)",
  2179. //% [self class], value];
  2180. //% }
  2181. //%
  2182. @end
  2183. #pragma mark - NSArray Subclass
  2184. @implementation GPBAutocreatedArray {
  2185. NSMutableArray *_array;
  2186. }
  2187. - (void)dealloc {
  2188. NSAssert(!_autocreator,
  2189. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2190. [self class], _autocreator);
  2191. [_array release];
  2192. [super dealloc];
  2193. }
  2194. #pragma mark Required NSArray overrides
  2195. - (NSUInteger)count {
  2196. return [_array count];
  2197. }
  2198. - (id)objectAtIndex:(NSUInteger)idx {
  2199. return [_array objectAtIndex:idx];
  2200. }
  2201. #pragma mark Required NSMutableArray overrides
  2202. // Only need to call GPBAutocreatedArrayModified() when adding things since
  2203. // we only autocreate empty arrays.
  2204. - (void)insertObject:(id)anObject atIndex:(NSUInteger)idx {
  2205. if (_array == nil) {
  2206. _array = [[NSMutableArray alloc] init];
  2207. }
  2208. [_array insertObject:anObject atIndex:idx];
  2209. if (_autocreator) {
  2210. GPBAutocreatedArrayModified(_autocreator, self);
  2211. }
  2212. }
  2213. - (void)removeObject:(id)anObject {
  2214. [_array removeObject:anObject];
  2215. }
  2216. - (void)removeObjectAtIndex:(NSUInteger)idx {
  2217. [_array removeObjectAtIndex:idx];
  2218. }
  2219. - (void)addObject:(id)anObject {
  2220. if (_array == nil) {
  2221. _array = [[NSMutableArray alloc] init];
  2222. }
  2223. [_array addObject:anObject];
  2224. if (_autocreator) {
  2225. GPBAutocreatedArrayModified(_autocreator, self);
  2226. }
  2227. }
  2228. - (void)removeLastObject {
  2229. [_array removeLastObject];
  2230. }
  2231. - (void)replaceObjectAtIndex:(NSUInteger)idx withObject:(id)anObject {
  2232. [_array replaceObjectAtIndex:idx withObject:anObject];
  2233. }
  2234. #pragma mark Extra things hooked
  2235. - (id)copyWithZone:(NSZone *)zone {
  2236. if (_array == nil) {
  2237. return [[NSMutableArray allocWithZone:zone] init];
  2238. }
  2239. return [_array copyWithZone:zone];
  2240. }
  2241. - (id)mutableCopyWithZone:(NSZone *)zone {
  2242. if (_array == nil) {
  2243. return [[NSMutableArray allocWithZone:zone] init];
  2244. }
  2245. return [_array mutableCopyWithZone:zone];
  2246. }
  2247. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  2248. objects:(id __unsafe_unretained [])buffer
  2249. count:(NSUInteger)len {
  2250. return [_array countByEnumeratingWithState:state objects:buffer count:len];
  2251. }
  2252. - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block {
  2253. [_array enumerateObjectsUsingBlock:block];
  2254. }
  2255. - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts
  2256. usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block {
  2257. [_array enumerateObjectsWithOptions:opts usingBlock:block];
  2258. }
  2259. @end
  2260. #pragma clang diagnostic pop