1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2015 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import "GPBArray_PackagePrivate.h"
33 #import "GPBMessage_PackagePrivate.h"
35 // Direct access is use for speed, to avoid even internally declaring things
36 // read/write, etc. The warning is enabled in the project to ensure code calling
37 // protos can turn on -Wdirect-ivar-access without issues.
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
41 // Mutable arrays use an internal buffer that can always hold a multiple of this elements.
43 #define CapacityFromCount(x) (((x / kChunkSize) + 1) * kChunkSize)
45 static BOOL ArrayDefault_IsValidValue(int32_t value) {
46 // Anything but the bad value marker is allowed.
47 return (value != kGPBUnrecognizedEnumeratorValue);
50 //%PDDM-DEFINE VALIDATE_RANGE(INDEX, COUNT)
51 //% if (INDEX >= COUNT) {
52 //% [NSException raise:NSRangeException
53 //% format:@"Index (%lu) beyond bounds (%lu)",
54 //% (unsigned long)INDEX, (unsigned long)COUNT];
56 //%PDDM-DEFINE MAYBE_GROW_TO_SET_COUNT(NEW_COUNT)
57 //% if (NEW_COUNT > _capacity) {
58 //% [self internalResizeToCapacity:CapacityFromCount(NEW_COUNT)];
60 //% _count = NEW_COUNT;
61 //%PDDM-DEFINE SET_COUNT_AND_MAYBE_SHRINK(NEW_COUNT)
62 //% _count = NEW_COUNT;
63 //% if ((NEW_COUNT + (2 * kChunkSize)) < _capacity) {
64 //% [self internalResizeToCapacity:CapacityFromCount(NEW_COUNT)];
68 // Macros for the common basic cases.
71 //%PDDM-DEFINE ARRAY_INTERFACE_SIMPLE(NAME, TYPE, FORMAT)
72 //%#pragma mark - NAME
74 //%@implementation GPB##NAME##Array {
77 //% NSUInteger _count;
78 //% NSUInteger _capacity;
81 //%@synthesize count = _count;
83 //%+ (instancetype)array {
84 //% return [[[self alloc] init] autorelease];
87 //%+ (instancetype)arrayWithValue:(TYPE)value {
88 //% // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
89 //% // the type correct.
90 //% return [[(GPB##NAME##Array*)[self alloc] initWithValues:&value count:1] autorelease];
93 //%+ (instancetype)arrayWithValueArray:(GPB##NAME##Array *)array {
94 //% return [[(GPB##NAME##Array*)[self alloc] initWithValueArray:array] autorelease];
97 //%+ (instancetype)arrayWithCapacity:(NSUInteger)count {
98 //% return [[[self alloc] initWithCapacity:count] autorelease];
101 //%- (instancetype)init {
102 //% self = [super init];
103 //% // No work needed;
107 //%- (instancetype)initWithValueArray:(GPB##NAME##Array *)array {
108 //% return [self initWithValues:array->_values count:array->_count];
111 //%- (instancetype)initWithValues:(const TYPE [])values count:(NSUInteger)count {
112 //% self = [self init];
114 //% if (count && values) {
115 //% _values = reallocf(_values, count * sizeof(TYPE));
116 //% if (_values != NULL) {
117 //% _capacity = count;
118 //% memcpy(_values, values, count * sizeof(TYPE));
122 //% [NSException raise:NSMallocException
123 //% format:@"Failed to allocate %lu bytes",
124 //% (unsigned long)(count * sizeof(TYPE))];
131 //%- (instancetype)initWithCapacity:(NSUInteger)count {
132 //% self = [self initWithValues:NULL count:0];
133 //% if (self && count) {
134 //% [self internalResizeToCapacity:count];
139 //%- (instancetype)copyWithZone:(NSZone *)zone {
140 //% return [[GPB##NAME##Array allocWithZone:zone] initWithValues:_values count:_count];
143 //%ARRAY_IMMUTABLE_CORE(NAME, TYPE, , FORMAT)
145 //%- (TYPE)valueAtIndex:(NSUInteger)index {
146 //%VALIDATE_RANGE(index, _count)
147 //% return _values[index];
150 //%ARRAY_MUTABLE_CORE(NAME, TYPE, , FORMAT)
155 // Some core macros used for both the simple types and Enums.
158 //%PDDM-DEFINE ARRAY_IMMUTABLE_CORE(NAME, TYPE, ACCESSOR_NAME, FORMAT)
160 //% NSAssert(!_autocreator,
161 //% @"%@: Autocreator must be cleared before release, autocreator: %@",
162 //% [self class], _autocreator);
167 //%- (BOOL)isEqual:(id)other {
168 //% if (self == other) {
171 //% if (![other isKindOfClass:[GPB##NAME##Array class]]) {
174 //% GPB##NAME##Array *otherArray = other;
175 //% return (_count == otherArray->_count
176 //% && memcmp(_values, otherArray->_values, (_count * sizeof(TYPE))) == 0);
179 //%- (NSUInteger)hash {
180 //% // Follow NSArray's lead, and use the count as the hash.
184 //%- (NSString *)description {
185 //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
186 //% for (NSUInteger i = 0, count = _count; i < count; ++i) {
188 //% [result appendFormat:@"##FORMAT##", _values[i]];
190 //% [result appendFormat:@", ##FORMAT##", _values[i]];
193 //% [result appendFormat:@" }"];
197 //%- (void)enumerate##ACCESSOR_NAME##ValuesWithBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block {
198 //% [self enumerate##ACCESSOR_NAME##ValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
201 //%- (void)enumerate##ACCESSOR_NAME##ValuesWithOptions:(NSEnumerationOptions)opts
202 //% ACCESSOR_NAME$S usingBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block {
203 //% // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
205 //% if ((opts & NSEnumerationReverse) == 0) {
206 //% for (NSUInteger i = 0, count = _count; i < count; ++i) {
207 //% block(_values[i], i, &stop);
210 //% } else if (_count > 0) {
211 //% for (NSUInteger i = _count; i > 0; --i) {
212 //% block(_values[i - 1], (i - 1), &stop);
218 //%PDDM-DEFINE MUTATION_HOOK_None()
219 //%PDDM-DEFINE MUTATION_METHODS(NAME, TYPE, ACCESSOR_NAME, HOOK_1, HOOK_2)
220 //%- (void)add##ACCESSOR_NAME##Value:(TYPE)value {
221 //% [self add##ACCESSOR_NAME##Values:&value count:1];
224 //%- (void)add##ACCESSOR_NAME##Values:(const TYPE [])values count:(NSUInteger)count {
225 //% if (values == NULL || count == 0) return;
226 //%MUTATION_HOOK_##HOOK_1() NSUInteger initialCount = _count;
227 //% NSUInteger newCount = initialCount + count;
228 //%MAYBE_GROW_TO_SET_COUNT(newCount)
229 //% memcpy(&_values[initialCount], values, count * sizeof(TYPE));
230 //% if (_autocreator) {
231 //% GPBAutocreatedArrayModified(_autocreator, self);
235 //%- (void)insert##ACCESSOR_NAME##Value:(TYPE)value atIndex:(NSUInteger)index {
236 //%VALIDATE_RANGE(index, _count + 1)
237 //%MUTATION_HOOK_##HOOK_2() NSUInteger initialCount = _count;
238 //% NSUInteger newCount = initialCount + 1;
239 //%MAYBE_GROW_TO_SET_COUNT(newCount)
240 //% if (index != initialCount) {
241 //% memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(TYPE));
243 //% _values[index] = value;
244 //% if (_autocreator) {
245 //% GPBAutocreatedArrayModified(_autocreator, self);
249 //%- (void)replaceValueAtIndex:(NSUInteger)index with##ACCESSOR_NAME##Value:(TYPE)value {
250 //%VALIDATE_RANGE(index, _count)
251 //%MUTATION_HOOK_##HOOK_2() _values[index] = value;
254 //%PDDM-DEFINE ARRAY_MUTABLE_CORE(NAME, TYPE, ACCESSOR_NAME, FORMAT)
255 //%- (void)internalResizeToCapacity:(NSUInteger)newCapacity {
256 //% _values = reallocf(_values, newCapacity * sizeof(TYPE));
257 //% if (_values == NULL) {
260 //% [NSException raise:NSMallocException
261 //% format:@"Failed to allocate %lu bytes",
262 //% (unsigned long)(newCapacity * sizeof(TYPE))];
264 //% _capacity = newCapacity;
267 //%MUTATION_METHODS(NAME, TYPE, ACCESSOR_NAME, None, None)
269 //%- (void)add##ACCESSOR_NAME##ValuesFromArray:(GPB##NAME##Array *)array {
270 //% [self add##ACCESSOR_NAME##Values:array->_values count:array->_count];
273 //%- (void)removeValueAtIndex:(NSUInteger)index {
274 //%VALIDATE_RANGE(index, _count)
275 //% NSUInteger newCount = _count - 1;
276 //% if (index != newCount) {
277 //% memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(TYPE));
279 //%SET_COUNT_AND_MAYBE_SHRINK(newCount)
282 //%- (void)removeAll {
283 //%SET_COUNT_AND_MAYBE_SHRINK(0)
286 //%- (void)exchangeValueAtIndex:(NSUInteger)idx1
287 //% withValueAtIndex:(NSUInteger)idx2 {
288 //%VALIDATE_RANGE(idx1, _count)
289 //%VALIDATE_RANGE(idx2, _count)
290 //% TYPE temp = _values[idx1];
291 //% _values[idx1] = _values[idx2];
292 //% _values[idx2] = temp;
296 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Int32, int32_t, %d)
297 // This block of code is generated, do not edit it directly.
301 @implementation GPBInt32Array {
305 NSUInteger _capacity;
308 @synthesize count = _count;
310 + (instancetype)array {
311 return [[[self alloc] init] autorelease];
314 + (instancetype)arrayWithValue:(int32_t)value {
315 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
317 return [[(GPBInt32Array*)[self alloc] initWithValues:&value count:1] autorelease];
320 + (instancetype)arrayWithValueArray:(GPBInt32Array *)array {
321 return [[(GPBInt32Array*)[self alloc] initWithValueArray:array] autorelease];
324 + (instancetype)arrayWithCapacity:(NSUInteger)count {
325 return [[[self alloc] initWithCapacity:count] autorelease];
328 - (instancetype)init {
334 - (instancetype)initWithValueArray:(GPBInt32Array *)array {
335 return [self initWithValues:array->_values count:array->_count];
338 - (instancetype)initWithValues:(const int32_t [])values count:(NSUInteger)count {
341 if (count && values) {
342 _values = reallocf(_values, count * sizeof(int32_t));
343 if (_values != NULL) {
345 memcpy(_values, values, count * sizeof(int32_t));
349 [NSException raise:NSMallocException
350 format:@"Failed to allocate %lu bytes",
351 (unsigned long)(count * sizeof(int32_t))];
358 - (instancetype)initWithCapacity:(NSUInteger)count {
359 self = [self initWithValues:NULL count:0];
361 [self internalResizeToCapacity:count];
366 - (instancetype)copyWithZone:(NSZone *)zone {
367 return [[GPBInt32Array allocWithZone:zone] initWithValues:_values count:_count];
371 NSAssert(!_autocreator,
372 @"%@: Autocreator must be cleared before release, autocreator: %@",
373 [self class], _autocreator);
378 - (BOOL)isEqual:(id)other {
382 if (![other isKindOfClass:[GPBInt32Array class]]) {
385 GPBInt32Array *otherArray = other;
386 return (_count == otherArray->_count
387 && memcmp(_values, otherArray->_values, (_count * sizeof(int32_t))) == 0);
391 // Follow NSArray's lead, and use the count as the hash.
395 - (NSString *)description {
396 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
397 for (NSUInteger i = 0, count = _count; i < count; ++i) {
399 [result appendFormat:@"%d", _values[i]];
401 [result appendFormat:@", %d", _values[i]];
404 [result appendFormat:@" }"];
408 - (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
409 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
412 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
413 usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
414 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
416 if ((opts & NSEnumerationReverse) == 0) {
417 for (NSUInteger i = 0, count = _count; i < count; ++i) {
418 block(_values[i], i, &stop);
421 } else if (_count > 0) {
422 for (NSUInteger i = _count; i > 0; --i) {
423 block(_values[i - 1], (i - 1), &stop);
429 - (int32_t)valueAtIndex:(NSUInteger)index {
430 if (index >= _count) {
431 [NSException raise:NSRangeException
432 format:@"Index (%lu) beyond bounds (%lu)",
433 (unsigned long)index, (unsigned long)_count];
435 return _values[index];
438 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
439 _values = reallocf(_values, newCapacity * sizeof(int32_t));
440 if (_values == NULL) {
443 [NSException raise:NSMallocException
444 format:@"Failed to allocate %lu bytes",
445 (unsigned long)(newCapacity * sizeof(int32_t))];
447 _capacity = newCapacity;
450 - (void)addValue:(int32_t)value {
451 [self addValues:&value count:1];
454 - (void)addValues:(const int32_t [])values count:(NSUInteger)count {
455 if (values == NULL || count == 0) return;
456 NSUInteger initialCount = _count;
457 NSUInteger newCount = initialCount + count;
458 if (newCount > _capacity) {
459 [self internalResizeToCapacity:CapacityFromCount(newCount)];
462 memcpy(&_values[initialCount], values, count * sizeof(int32_t));
464 GPBAutocreatedArrayModified(_autocreator, self);
468 - (void)insertValue:(int32_t)value atIndex:(NSUInteger)index {
469 if (index >= _count + 1) {
470 [NSException raise:NSRangeException
471 format:@"Index (%lu) beyond bounds (%lu)",
472 (unsigned long)index, (unsigned long)_count + 1];
474 NSUInteger initialCount = _count;
475 NSUInteger newCount = initialCount + 1;
476 if (newCount > _capacity) {
477 [self internalResizeToCapacity:CapacityFromCount(newCount)];
480 if (index != initialCount) {
481 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int32_t));
483 _values[index] = value;
485 GPBAutocreatedArrayModified(_autocreator, self);
489 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(int32_t)value {
490 if (index >= _count) {
491 [NSException raise:NSRangeException
492 format:@"Index (%lu) beyond bounds (%lu)",
493 (unsigned long)index, (unsigned long)_count];
495 _values[index] = value;
498 - (void)addValuesFromArray:(GPBInt32Array *)array {
499 [self addValues:array->_values count:array->_count];
502 - (void)removeValueAtIndex:(NSUInteger)index {
503 if (index >= _count) {
504 [NSException raise:NSRangeException
505 format:@"Index (%lu) beyond bounds (%lu)",
506 (unsigned long)index, (unsigned long)_count];
508 NSUInteger newCount = _count - 1;
509 if (index != newCount) {
510 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(int32_t));
513 if ((newCount + (2 * kChunkSize)) < _capacity) {
514 [self internalResizeToCapacity:CapacityFromCount(newCount)];
520 if ((0 + (2 * kChunkSize)) < _capacity) {
521 [self internalResizeToCapacity:CapacityFromCount(0)];
525 - (void)exchangeValueAtIndex:(NSUInteger)idx1
526 withValueAtIndex:(NSUInteger)idx2 {
527 if (idx1 >= _count) {
528 [NSException raise:NSRangeException
529 format:@"Index (%lu) beyond bounds (%lu)",
530 (unsigned long)idx1, (unsigned long)_count];
532 if (idx2 >= _count) {
533 [NSException raise:NSRangeException
534 format:@"Index (%lu) beyond bounds (%lu)",
535 (unsigned long)idx2, (unsigned long)_count];
537 int32_t temp = _values[idx1];
538 _values[idx1] = _values[idx2];
539 _values[idx2] = temp;
544 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(UInt32, uint32_t, %u)
545 // This block of code is generated, do not edit it directly.
547 #pragma mark - UInt32
549 @implementation GPBUInt32Array {
553 NSUInteger _capacity;
556 @synthesize count = _count;
558 + (instancetype)array {
559 return [[[self alloc] init] autorelease];
562 + (instancetype)arrayWithValue:(uint32_t)value {
563 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
565 return [[(GPBUInt32Array*)[self alloc] initWithValues:&value count:1] autorelease];
568 + (instancetype)arrayWithValueArray:(GPBUInt32Array *)array {
569 return [[(GPBUInt32Array*)[self alloc] initWithValueArray:array] autorelease];
572 + (instancetype)arrayWithCapacity:(NSUInteger)count {
573 return [[[self alloc] initWithCapacity:count] autorelease];
576 - (instancetype)init {
582 - (instancetype)initWithValueArray:(GPBUInt32Array *)array {
583 return [self initWithValues:array->_values count:array->_count];
586 - (instancetype)initWithValues:(const uint32_t [])values count:(NSUInteger)count {
589 if (count && values) {
590 _values = reallocf(_values, count * sizeof(uint32_t));
591 if (_values != NULL) {
593 memcpy(_values, values, count * sizeof(uint32_t));
597 [NSException raise:NSMallocException
598 format:@"Failed to allocate %lu bytes",
599 (unsigned long)(count * sizeof(uint32_t))];
606 - (instancetype)initWithCapacity:(NSUInteger)count {
607 self = [self initWithValues:NULL count:0];
609 [self internalResizeToCapacity:count];
614 - (instancetype)copyWithZone:(NSZone *)zone {
615 return [[GPBUInt32Array allocWithZone:zone] initWithValues:_values count:_count];
619 NSAssert(!_autocreator,
620 @"%@: Autocreator must be cleared before release, autocreator: %@",
621 [self class], _autocreator);
626 - (BOOL)isEqual:(id)other {
630 if (![other isKindOfClass:[GPBUInt32Array class]]) {
633 GPBUInt32Array *otherArray = other;
634 return (_count == otherArray->_count
635 && memcmp(_values, otherArray->_values, (_count * sizeof(uint32_t))) == 0);
639 // Follow NSArray's lead, and use the count as the hash.
643 - (NSString *)description {
644 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
645 for (NSUInteger i = 0, count = _count; i < count; ++i) {
647 [result appendFormat:@"%u", _values[i]];
649 [result appendFormat:@", %u", _values[i]];
652 [result appendFormat:@" }"];
656 - (void)enumerateValuesWithBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block {
657 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
660 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
661 usingBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block {
662 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
664 if ((opts & NSEnumerationReverse) == 0) {
665 for (NSUInteger i = 0, count = _count; i < count; ++i) {
666 block(_values[i], i, &stop);
669 } else if (_count > 0) {
670 for (NSUInteger i = _count; i > 0; --i) {
671 block(_values[i - 1], (i - 1), &stop);
677 - (uint32_t)valueAtIndex:(NSUInteger)index {
678 if (index >= _count) {
679 [NSException raise:NSRangeException
680 format:@"Index (%lu) beyond bounds (%lu)",
681 (unsigned long)index, (unsigned long)_count];
683 return _values[index];
686 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
687 _values = reallocf(_values, newCapacity * sizeof(uint32_t));
688 if (_values == NULL) {
691 [NSException raise:NSMallocException
692 format:@"Failed to allocate %lu bytes",
693 (unsigned long)(newCapacity * sizeof(uint32_t))];
695 _capacity = newCapacity;
698 - (void)addValue:(uint32_t)value {
699 [self addValues:&value count:1];
702 - (void)addValues:(const uint32_t [])values count:(NSUInteger)count {
703 if (values == NULL || count == 0) return;
704 NSUInteger initialCount = _count;
705 NSUInteger newCount = initialCount + count;
706 if (newCount > _capacity) {
707 [self internalResizeToCapacity:CapacityFromCount(newCount)];
710 memcpy(&_values[initialCount], values, count * sizeof(uint32_t));
712 GPBAutocreatedArrayModified(_autocreator, self);
716 - (void)insertValue:(uint32_t)value atIndex:(NSUInteger)index {
717 if (index >= _count + 1) {
718 [NSException raise:NSRangeException
719 format:@"Index (%lu) beyond bounds (%lu)",
720 (unsigned long)index, (unsigned long)_count + 1];
722 NSUInteger initialCount = _count;
723 NSUInteger newCount = initialCount + 1;
724 if (newCount > _capacity) {
725 [self internalResizeToCapacity:CapacityFromCount(newCount)];
728 if (index != initialCount) {
729 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(uint32_t));
731 _values[index] = value;
733 GPBAutocreatedArrayModified(_autocreator, self);
737 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(uint32_t)value {
738 if (index >= _count) {
739 [NSException raise:NSRangeException
740 format:@"Index (%lu) beyond bounds (%lu)",
741 (unsigned long)index, (unsigned long)_count];
743 _values[index] = value;
746 - (void)addValuesFromArray:(GPBUInt32Array *)array {
747 [self addValues:array->_values count:array->_count];
750 - (void)removeValueAtIndex:(NSUInteger)index {
751 if (index >= _count) {
752 [NSException raise:NSRangeException
753 format:@"Index (%lu) beyond bounds (%lu)",
754 (unsigned long)index, (unsigned long)_count];
756 NSUInteger newCount = _count - 1;
757 if (index != newCount) {
758 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(uint32_t));
761 if ((newCount + (2 * kChunkSize)) < _capacity) {
762 [self internalResizeToCapacity:CapacityFromCount(newCount)];
768 if ((0 + (2 * kChunkSize)) < _capacity) {
769 [self internalResizeToCapacity:CapacityFromCount(0)];
773 - (void)exchangeValueAtIndex:(NSUInteger)idx1
774 withValueAtIndex:(NSUInteger)idx2 {
775 if (idx1 >= _count) {
776 [NSException raise:NSRangeException
777 format:@"Index (%lu) beyond bounds (%lu)",
778 (unsigned long)idx1, (unsigned long)_count];
780 if (idx2 >= _count) {
781 [NSException raise:NSRangeException
782 format:@"Index (%lu) beyond bounds (%lu)",
783 (unsigned long)idx2, (unsigned long)_count];
785 uint32_t temp = _values[idx1];
786 _values[idx1] = _values[idx2];
787 _values[idx2] = temp;
792 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Int64, int64_t, %lld)
793 // This block of code is generated, do not edit it directly.
797 @implementation GPBInt64Array {
801 NSUInteger _capacity;
804 @synthesize count = _count;
806 + (instancetype)array {
807 return [[[self alloc] init] autorelease];
810 + (instancetype)arrayWithValue:(int64_t)value {
811 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
813 return [[(GPBInt64Array*)[self alloc] initWithValues:&value count:1] autorelease];
816 + (instancetype)arrayWithValueArray:(GPBInt64Array *)array {
817 return [[(GPBInt64Array*)[self alloc] initWithValueArray:array] autorelease];
820 + (instancetype)arrayWithCapacity:(NSUInteger)count {
821 return [[[self alloc] initWithCapacity:count] autorelease];
824 - (instancetype)init {
830 - (instancetype)initWithValueArray:(GPBInt64Array *)array {
831 return [self initWithValues:array->_values count:array->_count];
834 - (instancetype)initWithValues:(const int64_t [])values count:(NSUInteger)count {
837 if (count && values) {
838 _values = reallocf(_values, count * sizeof(int64_t));
839 if (_values != NULL) {
841 memcpy(_values, values, count * sizeof(int64_t));
845 [NSException raise:NSMallocException
846 format:@"Failed to allocate %lu bytes",
847 (unsigned long)(count * sizeof(int64_t))];
854 - (instancetype)initWithCapacity:(NSUInteger)count {
855 self = [self initWithValues:NULL count:0];
857 [self internalResizeToCapacity:count];
862 - (instancetype)copyWithZone:(NSZone *)zone {
863 return [[GPBInt64Array allocWithZone:zone] initWithValues:_values count:_count];
867 NSAssert(!_autocreator,
868 @"%@: Autocreator must be cleared before release, autocreator: %@",
869 [self class], _autocreator);
874 - (BOOL)isEqual:(id)other {
878 if (![other isKindOfClass:[GPBInt64Array class]]) {
881 GPBInt64Array *otherArray = other;
882 return (_count == otherArray->_count
883 && memcmp(_values, otherArray->_values, (_count * sizeof(int64_t))) == 0);
887 // Follow NSArray's lead, and use the count as the hash.
891 - (NSString *)description {
892 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
893 for (NSUInteger i = 0, count = _count; i < count; ++i) {
895 [result appendFormat:@"%lld", _values[i]];
897 [result appendFormat:@", %lld", _values[i]];
900 [result appendFormat:@" }"];
904 - (void)enumerateValuesWithBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block {
905 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
908 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
909 usingBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block {
910 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
912 if ((opts & NSEnumerationReverse) == 0) {
913 for (NSUInteger i = 0, count = _count; i < count; ++i) {
914 block(_values[i], i, &stop);
917 } else if (_count > 0) {
918 for (NSUInteger i = _count; i > 0; --i) {
919 block(_values[i - 1], (i - 1), &stop);
925 - (int64_t)valueAtIndex:(NSUInteger)index {
926 if (index >= _count) {
927 [NSException raise:NSRangeException
928 format:@"Index (%lu) beyond bounds (%lu)",
929 (unsigned long)index, (unsigned long)_count];
931 return _values[index];
934 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
935 _values = reallocf(_values, newCapacity * sizeof(int64_t));
936 if (_values == NULL) {
939 [NSException raise:NSMallocException
940 format:@"Failed to allocate %lu bytes",
941 (unsigned long)(newCapacity * sizeof(int64_t))];
943 _capacity = newCapacity;
946 - (void)addValue:(int64_t)value {
947 [self addValues:&value count:1];
950 - (void)addValues:(const int64_t [])values count:(NSUInteger)count {
951 if (values == NULL || count == 0) return;
952 NSUInteger initialCount = _count;
953 NSUInteger newCount = initialCount + count;
954 if (newCount > _capacity) {
955 [self internalResizeToCapacity:CapacityFromCount(newCount)];
958 memcpy(&_values[initialCount], values, count * sizeof(int64_t));
960 GPBAutocreatedArrayModified(_autocreator, self);
964 - (void)insertValue:(int64_t)value atIndex:(NSUInteger)index {
965 if (index >= _count + 1) {
966 [NSException raise:NSRangeException
967 format:@"Index (%lu) beyond bounds (%lu)",
968 (unsigned long)index, (unsigned long)_count + 1];
970 NSUInteger initialCount = _count;
971 NSUInteger newCount = initialCount + 1;
972 if (newCount > _capacity) {
973 [self internalResizeToCapacity:CapacityFromCount(newCount)];
976 if (index != initialCount) {
977 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int64_t));
979 _values[index] = value;
981 GPBAutocreatedArrayModified(_autocreator, self);
985 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(int64_t)value {
986 if (index >= _count) {
987 [NSException raise:NSRangeException
988 format:@"Index (%lu) beyond bounds (%lu)",
989 (unsigned long)index, (unsigned long)_count];
991 _values[index] = value;
994 - (void)addValuesFromArray:(GPBInt64Array *)array {
995 [self addValues:array->_values count:array->_count];
998 - (void)removeValueAtIndex:(NSUInteger)index {
999 if (index >= _count) {
1000 [NSException raise:NSRangeException
1001 format:@"Index (%lu) beyond bounds (%lu)",
1002 (unsigned long)index, (unsigned long)_count];
1004 NSUInteger newCount = _count - 1;
1005 if (index != newCount) {
1006 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(int64_t));
1009 if ((newCount + (2 * kChunkSize)) < _capacity) {
1010 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1016 if ((0 + (2 * kChunkSize)) < _capacity) {
1017 [self internalResizeToCapacity:CapacityFromCount(0)];
1021 - (void)exchangeValueAtIndex:(NSUInteger)idx1
1022 withValueAtIndex:(NSUInteger)idx2 {
1023 if (idx1 >= _count) {
1024 [NSException raise:NSRangeException
1025 format:@"Index (%lu) beyond bounds (%lu)",
1026 (unsigned long)idx1, (unsigned long)_count];
1028 if (idx2 >= _count) {
1029 [NSException raise:NSRangeException
1030 format:@"Index (%lu) beyond bounds (%lu)",
1031 (unsigned long)idx2, (unsigned long)_count];
1033 int64_t temp = _values[idx1];
1034 _values[idx1] = _values[idx2];
1035 _values[idx2] = temp;
1040 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(UInt64, uint64_t, %llu)
1041 // This block of code is generated, do not edit it directly.
1043 #pragma mark - UInt64
1045 @implementation GPBUInt64Array {
1049 NSUInteger _capacity;
1052 @synthesize count = _count;
1054 + (instancetype)array {
1055 return [[[self alloc] init] autorelease];
1058 + (instancetype)arrayWithValue:(uint64_t)value {
1059 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
1060 // the type correct.
1061 return [[(GPBUInt64Array*)[self alloc] initWithValues:&value count:1] autorelease];
1064 + (instancetype)arrayWithValueArray:(GPBUInt64Array *)array {
1065 return [[(GPBUInt64Array*)[self alloc] initWithValueArray:array] autorelease];
1068 + (instancetype)arrayWithCapacity:(NSUInteger)count {
1069 return [[[self alloc] initWithCapacity:count] autorelease];
1072 - (instancetype)init {
1073 self = [super init];
1078 - (instancetype)initWithValueArray:(GPBUInt64Array *)array {
1079 return [self initWithValues:array->_values count:array->_count];
1082 - (instancetype)initWithValues:(const uint64_t [])values count:(NSUInteger)count {
1085 if (count && values) {
1086 _values = reallocf(_values, count * sizeof(uint64_t));
1087 if (_values != NULL) {
1089 memcpy(_values, values, count * sizeof(uint64_t));
1093 [NSException raise:NSMallocException
1094 format:@"Failed to allocate %lu bytes",
1095 (unsigned long)(count * sizeof(uint64_t))];
1102 - (instancetype)initWithCapacity:(NSUInteger)count {
1103 self = [self initWithValues:NULL count:0];
1104 if (self && count) {
1105 [self internalResizeToCapacity:count];
1110 - (instancetype)copyWithZone:(NSZone *)zone {
1111 return [[GPBUInt64Array allocWithZone:zone] initWithValues:_values count:_count];
1115 NSAssert(!_autocreator,
1116 @"%@: Autocreator must be cleared before release, autocreator: %@",
1117 [self class], _autocreator);
1122 - (BOOL)isEqual:(id)other {
1123 if (self == other) {
1126 if (![other isKindOfClass:[GPBUInt64Array class]]) {
1129 GPBUInt64Array *otherArray = other;
1130 return (_count == otherArray->_count
1131 && memcmp(_values, otherArray->_values, (_count * sizeof(uint64_t))) == 0);
1134 - (NSUInteger)hash {
1135 // Follow NSArray's lead, and use the count as the hash.
1139 - (NSString *)description {
1140 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
1141 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1143 [result appendFormat:@"%llu", _values[i]];
1145 [result appendFormat:@", %llu", _values[i]];
1148 [result appendFormat:@" }"];
1152 - (void)enumerateValuesWithBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block {
1153 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
1156 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
1157 usingBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block {
1158 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
1160 if ((opts & NSEnumerationReverse) == 0) {
1161 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1162 block(_values[i], i, &stop);
1165 } else if (_count > 0) {
1166 for (NSUInteger i = _count; i > 0; --i) {
1167 block(_values[i - 1], (i - 1), &stop);
1173 - (uint64_t)valueAtIndex:(NSUInteger)index {
1174 if (index >= _count) {
1175 [NSException raise:NSRangeException
1176 format:@"Index (%lu) beyond bounds (%lu)",
1177 (unsigned long)index, (unsigned long)_count];
1179 return _values[index];
1182 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
1183 _values = reallocf(_values, newCapacity * sizeof(uint64_t));
1184 if (_values == NULL) {
1187 [NSException raise:NSMallocException
1188 format:@"Failed to allocate %lu bytes",
1189 (unsigned long)(newCapacity * sizeof(uint64_t))];
1191 _capacity = newCapacity;
1194 - (void)addValue:(uint64_t)value {
1195 [self addValues:&value count:1];
1198 - (void)addValues:(const uint64_t [])values count:(NSUInteger)count {
1199 if (values == NULL || count == 0) return;
1200 NSUInteger initialCount = _count;
1201 NSUInteger newCount = initialCount + count;
1202 if (newCount > _capacity) {
1203 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1206 memcpy(&_values[initialCount], values, count * sizeof(uint64_t));
1208 GPBAutocreatedArrayModified(_autocreator, self);
1212 - (void)insertValue:(uint64_t)value atIndex:(NSUInteger)index {
1213 if (index >= _count + 1) {
1214 [NSException raise:NSRangeException
1215 format:@"Index (%lu) beyond bounds (%lu)",
1216 (unsigned long)index, (unsigned long)_count + 1];
1218 NSUInteger initialCount = _count;
1219 NSUInteger newCount = initialCount + 1;
1220 if (newCount > _capacity) {
1221 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1224 if (index != initialCount) {
1225 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(uint64_t));
1227 _values[index] = value;
1229 GPBAutocreatedArrayModified(_autocreator, self);
1233 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(uint64_t)value {
1234 if (index >= _count) {
1235 [NSException raise:NSRangeException
1236 format:@"Index (%lu) beyond bounds (%lu)",
1237 (unsigned long)index, (unsigned long)_count];
1239 _values[index] = value;
1242 - (void)addValuesFromArray:(GPBUInt64Array *)array {
1243 [self addValues:array->_values count:array->_count];
1246 - (void)removeValueAtIndex:(NSUInteger)index {
1247 if (index >= _count) {
1248 [NSException raise:NSRangeException
1249 format:@"Index (%lu) beyond bounds (%lu)",
1250 (unsigned long)index, (unsigned long)_count];
1252 NSUInteger newCount = _count - 1;
1253 if (index != newCount) {
1254 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(uint64_t));
1257 if ((newCount + (2 * kChunkSize)) < _capacity) {
1258 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1264 if ((0 + (2 * kChunkSize)) < _capacity) {
1265 [self internalResizeToCapacity:CapacityFromCount(0)];
1269 - (void)exchangeValueAtIndex:(NSUInteger)idx1
1270 withValueAtIndex:(NSUInteger)idx2 {
1271 if (idx1 >= _count) {
1272 [NSException raise:NSRangeException
1273 format:@"Index (%lu) beyond bounds (%lu)",
1274 (unsigned long)idx1, (unsigned long)_count];
1276 if (idx2 >= _count) {
1277 [NSException raise:NSRangeException
1278 format:@"Index (%lu) beyond bounds (%lu)",
1279 (unsigned long)idx2, (unsigned long)_count];
1281 uint64_t temp = _values[idx1];
1282 _values[idx1] = _values[idx2];
1283 _values[idx2] = temp;
1288 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Float, float, %f)
1289 // This block of code is generated, do not edit it directly.
1291 #pragma mark - Float
1293 @implementation GPBFloatArray {
1297 NSUInteger _capacity;
1300 @synthesize count = _count;
1302 + (instancetype)array {
1303 return [[[self alloc] init] autorelease];
1306 + (instancetype)arrayWithValue:(float)value {
1307 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
1308 // the type correct.
1309 return [[(GPBFloatArray*)[self alloc] initWithValues:&value count:1] autorelease];
1312 + (instancetype)arrayWithValueArray:(GPBFloatArray *)array {
1313 return [[(GPBFloatArray*)[self alloc] initWithValueArray:array] autorelease];
1316 + (instancetype)arrayWithCapacity:(NSUInteger)count {
1317 return [[[self alloc] initWithCapacity:count] autorelease];
1320 - (instancetype)init {
1321 self = [super init];
1326 - (instancetype)initWithValueArray:(GPBFloatArray *)array {
1327 return [self initWithValues:array->_values count:array->_count];
1330 - (instancetype)initWithValues:(const float [])values count:(NSUInteger)count {
1333 if (count && values) {
1334 _values = reallocf(_values, count * sizeof(float));
1335 if (_values != NULL) {
1337 memcpy(_values, values, count * sizeof(float));
1341 [NSException raise:NSMallocException
1342 format:@"Failed to allocate %lu bytes",
1343 (unsigned long)(count * sizeof(float))];
1350 - (instancetype)initWithCapacity:(NSUInteger)count {
1351 self = [self initWithValues:NULL count:0];
1352 if (self && count) {
1353 [self internalResizeToCapacity:count];
1358 - (instancetype)copyWithZone:(NSZone *)zone {
1359 return [[GPBFloatArray allocWithZone:zone] initWithValues:_values count:_count];
1363 NSAssert(!_autocreator,
1364 @"%@: Autocreator must be cleared before release, autocreator: %@",
1365 [self class], _autocreator);
1370 - (BOOL)isEqual:(id)other {
1371 if (self == other) {
1374 if (![other isKindOfClass:[GPBFloatArray class]]) {
1377 GPBFloatArray *otherArray = other;
1378 return (_count == otherArray->_count
1379 && memcmp(_values, otherArray->_values, (_count * sizeof(float))) == 0);
1382 - (NSUInteger)hash {
1383 // Follow NSArray's lead, and use the count as the hash.
1387 - (NSString *)description {
1388 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
1389 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1391 [result appendFormat:@"%f", _values[i]];
1393 [result appendFormat:@", %f", _values[i]];
1396 [result appendFormat:@" }"];
1400 - (void)enumerateValuesWithBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block {
1401 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
1404 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
1405 usingBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block {
1406 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
1408 if ((opts & NSEnumerationReverse) == 0) {
1409 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1410 block(_values[i], i, &stop);
1413 } else if (_count > 0) {
1414 for (NSUInteger i = _count; i > 0; --i) {
1415 block(_values[i - 1], (i - 1), &stop);
1421 - (float)valueAtIndex:(NSUInteger)index {
1422 if (index >= _count) {
1423 [NSException raise:NSRangeException
1424 format:@"Index (%lu) beyond bounds (%lu)",
1425 (unsigned long)index, (unsigned long)_count];
1427 return _values[index];
1430 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
1431 _values = reallocf(_values, newCapacity * sizeof(float));
1432 if (_values == NULL) {
1435 [NSException raise:NSMallocException
1436 format:@"Failed to allocate %lu bytes",
1437 (unsigned long)(newCapacity * sizeof(float))];
1439 _capacity = newCapacity;
1442 - (void)addValue:(float)value {
1443 [self addValues:&value count:1];
1446 - (void)addValues:(const float [])values count:(NSUInteger)count {
1447 if (values == NULL || count == 0) return;
1448 NSUInteger initialCount = _count;
1449 NSUInteger newCount = initialCount + count;
1450 if (newCount > _capacity) {
1451 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1454 memcpy(&_values[initialCount], values, count * sizeof(float));
1456 GPBAutocreatedArrayModified(_autocreator, self);
1460 - (void)insertValue:(float)value atIndex:(NSUInteger)index {
1461 if (index >= _count + 1) {
1462 [NSException raise:NSRangeException
1463 format:@"Index (%lu) beyond bounds (%lu)",
1464 (unsigned long)index, (unsigned long)_count + 1];
1466 NSUInteger initialCount = _count;
1467 NSUInteger newCount = initialCount + 1;
1468 if (newCount > _capacity) {
1469 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1472 if (index != initialCount) {
1473 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(float));
1475 _values[index] = value;
1477 GPBAutocreatedArrayModified(_autocreator, self);
1481 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(float)value {
1482 if (index >= _count) {
1483 [NSException raise:NSRangeException
1484 format:@"Index (%lu) beyond bounds (%lu)",
1485 (unsigned long)index, (unsigned long)_count];
1487 _values[index] = value;
1490 - (void)addValuesFromArray:(GPBFloatArray *)array {
1491 [self addValues:array->_values count:array->_count];
1494 - (void)removeValueAtIndex:(NSUInteger)index {
1495 if (index >= _count) {
1496 [NSException raise:NSRangeException
1497 format:@"Index (%lu) beyond bounds (%lu)",
1498 (unsigned long)index, (unsigned long)_count];
1500 NSUInteger newCount = _count - 1;
1501 if (index != newCount) {
1502 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(float));
1505 if ((newCount + (2 * kChunkSize)) < _capacity) {
1506 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1512 if ((0 + (2 * kChunkSize)) < _capacity) {
1513 [self internalResizeToCapacity:CapacityFromCount(0)];
1517 - (void)exchangeValueAtIndex:(NSUInteger)idx1
1518 withValueAtIndex:(NSUInteger)idx2 {
1519 if (idx1 >= _count) {
1520 [NSException raise:NSRangeException
1521 format:@"Index (%lu) beyond bounds (%lu)",
1522 (unsigned long)idx1, (unsigned long)_count];
1524 if (idx2 >= _count) {
1525 [NSException raise:NSRangeException
1526 format:@"Index (%lu) beyond bounds (%lu)",
1527 (unsigned long)idx2, (unsigned long)_count];
1529 float temp = _values[idx1];
1530 _values[idx1] = _values[idx2];
1531 _values[idx2] = temp;
1536 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Double, double, %lf)
1537 // This block of code is generated, do not edit it directly.
1539 #pragma mark - Double
1541 @implementation GPBDoubleArray {
1545 NSUInteger _capacity;
1548 @synthesize count = _count;
1550 + (instancetype)array {
1551 return [[[self alloc] init] autorelease];
1554 + (instancetype)arrayWithValue:(double)value {
1555 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
1556 // the type correct.
1557 return [[(GPBDoubleArray*)[self alloc] initWithValues:&value count:1] autorelease];
1560 + (instancetype)arrayWithValueArray:(GPBDoubleArray *)array {
1561 return [[(GPBDoubleArray*)[self alloc] initWithValueArray:array] autorelease];
1564 + (instancetype)arrayWithCapacity:(NSUInteger)count {
1565 return [[[self alloc] initWithCapacity:count] autorelease];
1568 - (instancetype)init {
1569 self = [super init];
1574 - (instancetype)initWithValueArray:(GPBDoubleArray *)array {
1575 return [self initWithValues:array->_values count:array->_count];
1578 - (instancetype)initWithValues:(const double [])values count:(NSUInteger)count {
1581 if (count && values) {
1582 _values = reallocf(_values, count * sizeof(double));
1583 if (_values != NULL) {
1585 memcpy(_values, values, count * sizeof(double));
1589 [NSException raise:NSMallocException
1590 format:@"Failed to allocate %lu bytes",
1591 (unsigned long)(count * sizeof(double))];
1598 - (instancetype)initWithCapacity:(NSUInteger)count {
1599 self = [self initWithValues:NULL count:0];
1600 if (self && count) {
1601 [self internalResizeToCapacity:count];
1606 - (instancetype)copyWithZone:(NSZone *)zone {
1607 return [[GPBDoubleArray allocWithZone:zone] initWithValues:_values count:_count];
1611 NSAssert(!_autocreator,
1612 @"%@: Autocreator must be cleared before release, autocreator: %@",
1613 [self class], _autocreator);
1618 - (BOOL)isEqual:(id)other {
1619 if (self == other) {
1622 if (![other isKindOfClass:[GPBDoubleArray class]]) {
1625 GPBDoubleArray *otherArray = other;
1626 return (_count == otherArray->_count
1627 && memcmp(_values, otherArray->_values, (_count * sizeof(double))) == 0);
1630 - (NSUInteger)hash {
1631 // Follow NSArray's lead, and use the count as the hash.
1635 - (NSString *)description {
1636 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
1637 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1639 [result appendFormat:@"%lf", _values[i]];
1641 [result appendFormat:@", %lf", _values[i]];
1644 [result appendFormat:@" }"];
1648 - (void)enumerateValuesWithBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block {
1649 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
1652 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
1653 usingBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block {
1654 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
1656 if ((opts & NSEnumerationReverse) == 0) {
1657 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1658 block(_values[i], i, &stop);
1661 } else if (_count > 0) {
1662 for (NSUInteger i = _count; i > 0; --i) {
1663 block(_values[i - 1], (i - 1), &stop);
1669 - (double)valueAtIndex:(NSUInteger)index {
1670 if (index >= _count) {
1671 [NSException raise:NSRangeException
1672 format:@"Index (%lu) beyond bounds (%lu)",
1673 (unsigned long)index, (unsigned long)_count];
1675 return _values[index];
1678 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
1679 _values = reallocf(_values, newCapacity * sizeof(double));
1680 if (_values == NULL) {
1683 [NSException raise:NSMallocException
1684 format:@"Failed to allocate %lu bytes",
1685 (unsigned long)(newCapacity * sizeof(double))];
1687 _capacity = newCapacity;
1690 - (void)addValue:(double)value {
1691 [self addValues:&value count:1];
1694 - (void)addValues:(const double [])values count:(NSUInteger)count {
1695 if (values == NULL || count == 0) return;
1696 NSUInteger initialCount = _count;
1697 NSUInteger newCount = initialCount + count;
1698 if (newCount > _capacity) {
1699 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1702 memcpy(&_values[initialCount], values, count * sizeof(double));
1704 GPBAutocreatedArrayModified(_autocreator, self);
1708 - (void)insertValue:(double)value atIndex:(NSUInteger)index {
1709 if (index >= _count + 1) {
1710 [NSException raise:NSRangeException
1711 format:@"Index (%lu) beyond bounds (%lu)",
1712 (unsigned long)index, (unsigned long)_count + 1];
1714 NSUInteger initialCount = _count;
1715 NSUInteger newCount = initialCount + 1;
1716 if (newCount > _capacity) {
1717 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1720 if (index != initialCount) {
1721 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(double));
1723 _values[index] = value;
1725 GPBAutocreatedArrayModified(_autocreator, self);
1729 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(double)value {
1730 if (index >= _count) {
1731 [NSException raise:NSRangeException
1732 format:@"Index (%lu) beyond bounds (%lu)",
1733 (unsigned long)index, (unsigned long)_count];
1735 _values[index] = value;
1738 - (void)addValuesFromArray:(GPBDoubleArray *)array {
1739 [self addValues:array->_values count:array->_count];
1742 - (void)removeValueAtIndex:(NSUInteger)index {
1743 if (index >= _count) {
1744 [NSException raise:NSRangeException
1745 format:@"Index (%lu) beyond bounds (%lu)",
1746 (unsigned long)index, (unsigned long)_count];
1748 NSUInteger newCount = _count - 1;
1749 if (index != newCount) {
1750 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(double));
1753 if ((newCount + (2 * kChunkSize)) < _capacity) {
1754 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1760 if ((0 + (2 * kChunkSize)) < _capacity) {
1761 [self internalResizeToCapacity:CapacityFromCount(0)];
1765 - (void)exchangeValueAtIndex:(NSUInteger)idx1
1766 withValueAtIndex:(NSUInteger)idx2 {
1767 if (idx1 >= _count) {
1768 [NSException raise:NSRangeException
1769 format:@"Index (%lu) beyond bounds (%lu)",
1770 (unsigned long)idx1, (unsigned long)_count];
1772 if (idx2 >= _count) {
1773 [NSException raise:NSRangeException
1774 format:@"Index (%lu) beyond bounds (%lu)",
1775 (unsigned long)idx2, (unsigned long)_count];
1777 double temp = _values[idx1];
1778 _values[idx1] = _values[idx2];
1779 _values[idx2] = temp;
1784 //%PDDM-EXPAND ARRAY_INTERFACE_SIMPLE(Bool, BOOL, %d)
1785 // This block of code is generated, do not edit it directly.
1789 @implementation GPBBoolArray {
1793 NSUInteger _capacity;
1796 @synthesize count = _count;
1798 + (instancetype)array {
1799 return [[[self alloc] init] autorelease];
1802 + (instancetype)arrayWithValue:(BOOL)value {
1803 // Cast is needed so the compiler knows what class we are invoking initWithValues: on to get
1804 // the type correct.
1805 return [[(GPBBoolArray*)[self alloc] initWithValues:&value count:1] autorelease];
1808 + (instancetype)arrayWithValueArray:(GPBBoolArray *)array {
1809 return [[(GPBBoolArray*)[self alloc] initWithValueArray:array] autorelease];
1812 + (instancetype)arrayWithCapacity:(NSUInteger)count {
1813 return [[[self alloc] initWithCapacity:count] autorelease];
1816 - (instancetype)init {
1817 self = [super init];
1822 - (instancetype)initWithValueArray:(GPBBoolArray *)array {
1823 return [self initWithValues:array->_values count:array->_count];
1826 - (instancetype)initWithValues:(const BOOL [])values count:(NSUInteger)count {
1829 if (count && values) {
1830 _values = reallocf(_values, count * sizeof(BOOL));
1831 if (_values != NULL) {
1833 memcpy(_values, values, count * sizeof(BOOL));
1837 [NSException raise:NSMallocException
1838 format:@"Failed to allocate %lu bytes",
1839 (unsigned long)(count * sizeof(BOOL))];
1846 - (instancetype)initWithCapacity:(NSUInteger)count {
1847 self = [self initWithValues:NULL count:0];
1848 if (self && count) {
1849 [self internalResizeToCapacity:count];
1854 - (instancetype)copyWithZone:(NSZone *)zone {
1855 return [[GPBBoolArray allocWithZone:zone] initWithValues:_values count:_count];
1859 NSAssert(!_autocreator,
1860 @"%@: Autocreator must be cleared before release, autocreator: %@",
1861 [self class], _autocreator);
1866 - (BOOL)isEqual:(id)other {
1867 if (self == other) {
1870 if (![other isKindOfClass:[GPBBoolArray class]]) {
1873 GPBBoolArray *otherArray = other;
1874 return (_count == otherArray->_count
1875 && memcmp(_values, otherArray->_values, (_count * sizeof(BOOL))) == 0);
1878 - (NSUInteger)hash {
1879 // Follow NSArray's lead, and use the count as the hash.
1883 - (NSString *)description {
1884 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
1885 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1887 [result appendFormat:@"%d", _values[i]];
1889 [result appendFormat:@", %d", _values[i]];
1892 [result appendFormat:@" }"];
1896 - (void)enumerateValuesWithBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block {
1897 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
1900 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
1901 usingBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block {
1902 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
1904 if ((opts & NSEnumerationReverse) == 0) {
1905 for (NSUInteger i = 0, count = _count; i < count; ++i) {
1906 block(_values[i], i, &stop);
1909 } else if (_count > 0) {
1910 for (NSUInteger i = _count; i > 0; --i) {
1911 block(_values[i - 1], (i - 1), &stop);
1917 - (BOOL)valueAtIndex:(NSUInteger)index {
1918 if (index >= _count) {
1919 [NSException raise:NSRangeException
1920 format:@"Index (%lu) beyond bounds (%lu)",
1921 (unsigned long)index, (unsigned long)_count];
1923 return _values[index];
1926 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
1927 _values = reallocf(_values, newCapacity * sizeof(BOOL));
1928 if (_values == NULL) {
1931 [NSException raise:NSMallocException
1932 format:@"Failed to allocate %lu bytes",
1933 (unsigned long)(newCapacity * sizeof(BOOL))];
1935 _capacity = newCapacity;
1938 - (void)addValue:(BOOL)value {
1939 [self addValues:&value count:1];
1942 - (void)addValues:(const BOOL [])values count:(NSUInteger)count {
1943 if (values == NULL || count == 0) return;
1944 NSUInteger initialCount = _count;
1945 NSUInteger newCount = initialCount + count;
1946 if (newCount > _capacity) {
1947 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1950 memcpy(&_values[initialCount], values, count * sizeof(BOOL));
1952 GPBAutocreatedArrayModified(_autocreator, self);
1956 - (void)insertValue:(BOOL)value atIndex:(NSUInteger)index {
1957 if (index >= _count + 1) {
1958 [NSException raise:NSRangeException
1959 format:@"Index (%lu) beyond bounds (%lu)",
1960 (unsigned long)index, (unsigned long)_count + 1];
1962 NSUInteger initialCount = _count;
1963 NSUInteger newCount = initialCount + 1;
1964 if (newCount > _capacity) {
1965 [self internalResizeToCapacity:CapacityFromCount(newCount)];
1968 if (index != initialCount) {
1969 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(BOOL));
1971 _values[index] = value;
1973 GPBAutocreatedArrayModified(_autocreator, self);
1977 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(BOOL)value {
1978 if (index >= _count) {
1979 [NSException raise:NSRangeException
1980 format:@"Index (%lu) beyond bounds (%lu)",
1981 (unsigned long)index, (unsigned long)_count];
1983 _values[index] = value;
1986 - (void)addValuesFromArray:(GPBBoolArray *)array {
1987 [self addValues:array->_values count:array->_count];
1990 - (void)removeValueAtIndex:(NSUInteger)index {
1991 if (index >= _count) {
1992 [NSException raise:NSRangeException
1993 format:@"Index (%lu) beyond bounds (%lu)",
1994 (unsigned long)index, (unsigned long)_count];
1996 NSUInteger newCount = _count - 1;
1997 if (index != newCount) {
1998 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(BOOL));
2001 if ((newCount + (2 * kChunkSize)) < _capacity) {
2002 [self internalResizeToCapacity:CapacityFromCount(newCount)];
2008 if ((0 + (2 * kChunkSize)) < _capacity) {
2009 [self internalResizeToCapacity:CapacityFromCount(0)];
2013 - (void)exchangeValueAtIndex:(NSUInteger)idx1
2014 withValueAtIndex:(NSUInteger)idx2 {
2015 if (idx1 >= _count) {
2016 [NSException raise:NSRangeException
2017 format:@"Index (%lu) beyond bounds (%lu)",
2018 (unsigned long)idx1, (unsigned long)_count];
2020 if (idx2 >= _count) {
2021 [NSException raise:NSRangeException
2022 format:@"Index (%lu) beyond bounds (%lu)",
2023 (unsigned long)idx2, (unsigned long)_count];
2025 BOOL temp = _values[idx1];
2026 _values[idx1] = _values[idx2];
2027 _values[idx2] = temp;
2032 //%PDDM-EXPAND-END (7 expansions)
2036 @implementation GPBEnumArray {
2038 GPBEnumValidationFunc _validationFunc;
2041 NSUInteger _capacity;
2044 @synthesize count = _count;
2045 @synthesize validationFunc = _validationFunc;
2047 + (instancetype)array {
2048 return [[[self alloc] initWithValidationFunction:NULL] autorelease];
2051 + (instancetype)arrayWithValidationFunction:(GPBEnumValidationFunc)func {
2052 return [[[self alloc] initWithValidationFunction:func] autorelease];
2055 + (instancetype)arrayWithValidationFunction:(GPBEnumValidationFunc)func
2056 rawValue:(int32_t)value {
2057 return [[[self alloc] initWithValidationFunction:func
2059 count:1] autorelease];
2062 + (instancetype)arrayWithValueArray:(GPBEnumArray *)array {
2063 return [[(GPBEnumArray*)[self alloc] initWithValueArray:array] autorelease];
2066 + (instancetype)arrayWithValidationFunction:(GPBEnumValidationFunc)func
2067 capacity:(NSUInteger)count {
2068 return [[[self alloc] initWithValidationFunction:func capacity:count] autorelease];
2071 - (instancetype)init {
2072 return [self initWithValidationFunction:NULL];
2075 - (instancetype)initWithValueArray:(GPBEnumArray *)array {
2076 return [self initWithValidationFunction:array->_validationFunc
2077 rawValues:array->_values
2078 count:array->_count];
2081 - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
2082 self = [super init];
2084 _validationFunc = (func != NULL ? func : ArrayDefault_IsValidValue);
2089 - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
2090 rawValues:(const int32_t [])values
2091 count:(NSUInteger)count {
2092 self = [self initWithValidationFunction:func];
2094 if (count && values) {
2095 _values = reallocf(_values, count * sizeof(int32_t));
2096 if (_values != NULL) {
2098 memcpy(_values, values, count * sizeof(int32_t));
2102 [NSException raise:NSMallocException
2103 format:@"Failed to allocate %lu bytes",
2104 (unsigned long)(count * sizeof(int32_t))];
2111 - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
2112 capacity:(NSUInteger)count {
2113 self = [self initWithValidationFunction:func];
2114 if (self && count) {
2115 [self internalResizeToCapacity:count];
2120 - (instancetype)copyWithZone:(NSZone *)zone {
2121 return [[GPBEnumArray allocWithZone:zone]
2122 initWithValidationFunction:_validationFunc
2127 //%PDDM-EXPAND ARRAY_IMMUTABLE_CORE(Enum, int32_t, Raw, %d)
2128 // This block of code is generated, do not edit it directly.
2131 NSAssert(!_autocreator,
2132 @"%@: Autocreator must be cleared before release, autocreator: %@",
2133 [self class], _autocreator);
2138 - (BOOL)isEqual:(id)other {
2139 if (self == other) {
2142 if (![other isKindOfClass:[GPBEnumArray class]]) {
2145 GPBEnumArray *otherArray = other;
2146 return (_count == otherArray->_count
2147 && memcmp(_values, otherArray->_values, (_count * sizeof(int32_t))) == 0);
2150 - (NSUInteger)hash {
2151 // Follow NSArray's lead, and use the count as the hash.
2155 - (NSString *)description {
2156 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> { ", [self class], self];
2157 for (NSUInteger i = 0, count = _count; i < count; ++i) {
2159 [result appendFormat:@"%d", _values[i]];
2161 [result appendFormat:@", %d", _values[i]];
2164 [result appendFormat:@" }"];
2168 - (void)enumerateRawValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
2169 [self enumerateRawValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
2172 - (void)enumerateRawValuesWithOptions:(NSEnumerationOptions)opts
2173 usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
2174 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
2176 if ((opts & NSEnumerationReverse) == 0) {
2177 for (NSUInteger i = 0, count = _count; i < count; ++i) {
2178 block(_values[i], i, &stop);
2181 } else if (_count > 0) {
2182 for (NSUInteger i = _count; i > 0; --i) {
2183 block(_values[i - 1], (i - 1), &stop);
2188 //%PDDM-EXPAND-END ARRAY_IMMUTABLE_CORE(Enum, int32_t, Raw, %d)
2190 - (int32_t)valueAtIndex:(NSUInteger)index {
2191 //%PDDM-EXPAND VALIDATE_RANGE(index, _count)
2192 // This block of code is generated, do not edit it directly.
2194 if (index >= _count) {
2195 [NSException raise:NSRangeException
2196 format:@"Index (%lu) beyond bounds (%lu)",
2197 (unsigned long)index, (unsigned long)_count];
2199 //%PDDM-EXPAND-END VALIDATE_RANGE(index, _count)
2200 int32_t result = _values[index];
2201 if (!_validationFunc(result)) {
2202 result = kGPBUnrecognizedEnumeratorValue;
2207 - (int32_t)rawValueAtIndex:(NSUInteger)index {
2208 //%PDDM-EXPAND VALIDATE_RANGE(index, _count)
2209 // This block of code is generated, do not edit it directly.
2211 if (index >= _count) {
2212 [NSException raise:NSRangeException
2213 format:@"Index (%lu) beyond bounds (%lu)",
2214 (unsigned long)index, (unsigned long)_count];
2216 //%PDDM-EXPAND-END VALIDATE_RANGE(index, _count)
2217 return _values[index];
2220 - (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
2221 [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block];
2224 - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts
2225 usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block {
2226 // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok).
2228 GPBEnumValidationFunc func = _validationFunc;
2229 if ((opts & NSEnumerationReverse) == 0) {
2230 int32_t *scan = _values;
2231 int32_t *end = scan + _count;
2232 for (NSUInteger i = 0; scan < end; ++i, ++scan) {
2233 int32_t value = *scan;
2235 value = kGPBUnrecognizedEnumeratorValue;
2237 block(value, i, &stop);
2240 } else if (_count > 0) {
2241 int32_t *end = _values;
2242 int32_t *scan = end + (_count - 1);
2243 for (NSUInteger i = (_count - 1); scan >= end; --i, --scan) {
2244 int32_t value = *scan;
2246 value = kGPBUnrecognizedEnumeratorValue;
2248 block(value, i, &stop);
2254 //%PDDM-EXPAND ARRAY_MUTABLE_CORE(Enum, int32_t, Raw, %d)
2255 // This block of code is generated, do not edit it directly.
2257 - (void)internalResizeToCapacity:(NSUInteger)newCapacity {
2258 _values = reallocf(_values, newCapacity * sizeof(int32_t));
2259 if (_values == NULL) {
2262 [NSException raise:NSMallocException
2263 format:@"Failed to allocate %lu bytes",
2264 (unsigned long)(newCapacity * sizeof(int32_t))];
2266 _capacity = newCapacity;
2269 - (void)addRawValue:(int32_t)value {
2270 [self addRawValues:&value count:1];
2273 - (void)addRawValues:(const int32_t [])values count:(NSUInteger)count {
2274 if (values == NULL || count == 0) return;
2275 NSUInteger initialCount = _count;
2276 NSUInteger newCount = initialCount + count;
2277 if (newCount > _capacity) {
2278 [self internalResizeToCapacity:CapacityFromCount(newCount)];
2281 memcpy(&_values[initialCount], values, count * sizeof(int32_t));
2283 GPBAutocreatedArrayModified(_autocreator, self);
2287 - (void)insertRawValue:(int32_t)value atIndex:(NSUInteger)index {
2288 if (index >= _count + 1) {
2289 [NSException raise:NSRangeException
2290 format:@"Index (%lu) beyond bounds (%lu)",
2291 (unsigned long)index, (unsigned long)_count + 1];
2293 NSUInteger initialCount = _count;
2294 NSUInteger newCount = initialCount + 1;
2295 if (newCount > _capacity) {
2296 [self internalResizeToCapacity:CapacityFromCount(newCount)];
2299 if (index != initialCount) {
2300 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int32_t));
2302 _values[index] = value;
2304 GPBAutocreatedArrayModified(_autocreator, self);
2308 - (void)replaceValueAtIndex:(NSUInteger)index withRawValue:(int32_t)value {
2309 if (index >= _count) {
2310 [NSException raise:NSRangeException
2311 format:@"Index (%lu) beyond bounds (%lu)",
2312 (unsigned long)index, (unsigned long)_count];
2314 _values[index] = value;
2317 - (void)addRawValuesFromArray:(GPBEnumArray *)array {
2318 [self addRawValues:array->_values count:array->_count];
2321 - (void)removeValueAtIndex:(NSUInteger)index {
2322 if (index >= _count) {
2323 [NSException raise:NSRangeException
2324 format:@"Index (%lu) beyond bounds (%lu)",
2325 (unsigned long)index, (unsigned long)_count];
2327 NSUInteger newCount = _count - 1;
2328 if (index != newCount) {
2329 memmove(&_values[index], &_values[index + 1], (newCount - index) * sizeof(int32_t));
2332 if ((newCount + (2 * kChunkSize)) < _capacity) {
2333 [self internalResizeToCapacity:CapacityFromCount(newCount)];
2339 if ((0 + (2 * kChunkSize)) < _capacity) {
2340 [self internalResizeToCapacity:CapacityFromCount(0)];
2344 - (void)exchangeValueAtIndex:(NSUInteger)idx1
2345 withValueAtIndex:(NSUInteger)idx2 {
2346 if (idx1 >= _count) {
2347 [NSException raise:NSRangeException
2348 format:@"Index (%lu) beyond bounds (%lu)",
2349 (unsigned long)idx1, (unsigned long)_count];
2351 if (idx2 >= _count) {
2352 [NSException raise:NSRangeException
2353 format:@"Index (%lu) beyond bounds (%lu)",
2354 (unsigned long)idx2, (unsigned long)_count];
2356 int32_t temp = _values[idx1];
2357 _values[idx1] = _values[idx2];
2358 _values[idx2] = temp;
2361 //%PDDM-EXPAND MUTATION_METHODS(Enum, int32_t, , EnumValidationList, EnumValidationOne)
2362 // This block of code is generated, do not edit it directly.
2364 - (void)addValue:(int32_t)value {
2365 [self addValues:&value count:1];
2368 - (void)addValues:(const int32_t [])values count:(NSUInteger)count {
2369 if (values == NULL || count == 0) return;
2370 GPBEnumValidationFunc func = _validationFunc;
2371 for (NSUInteger i = 0; i < count; ++i) {
2372 if (!func(values[i])) {
2373 [NSException raise:NSInvalidArgumentException
2374 format:@"%@: Attempt to set an unknown enum value (%d)",
2375 [self class], values[i]];
2378 NSUInteger initialCount = _count;
2379 NSUInteger newCount = initialCount + count;
2380 if (newCount > _capacity) {
2381 [self internalResizeToCapacity:CapacityFromCount(newCount)];
2384 memcpy(&_values[initialCount], values, count * sizeof(int32_t));
2386 GPBAutocreatedArrayModified(_autocreator, self);
2390 - (void)insertValue:(int32_t)value atIndex:(NSUInteger)index {
2391 if (index >= _count + 1) {
2392 [NSException raise:NSRangeException
2393 format:@"Index (%lu) beyond bounds (%lu)",
2394 (unsigned long)index, (unsigned long)_count + 1];
2396 if (!_validationFunc(value)) {
2397 [NSException raise:NSInvalidArgumentException
2398 format:@"%@: Attempt to set an unknown enum value (%d)",
2399 [self class], value];
2401 NSUInteger initialCount = _count;
2402 NSUInteger newCount = initialCount + 1;
2403 if (newCount > _capacity) {
2404 [self internalResizeToCapacity:CapacityFromCount(newCount)];
2407 if (index != initialCount) {
2408 memmove(&_values[index + 1], &_values[index], (initialCount - index) * sizeof(int32_t));
2410 _values[index] = value;
2412 GPBAutocreatedArrayModified(_autocreator, self);
2416 - (void)replaceValueAtIndex:(NSUInteger)index withValue:(int32_t)value {
2417 if (index >= _count) {
2418 [NSException raise:NSRangeException
2419 format:@"Index (%lu) beyond bounds (%lu)",
2420 (unsigned long)index, (unsigned long)_count];
2422 if (!_validationFunc(value)) {
2423 [NSException raise:NSInvalidArgumentException
2424 format:@"%@: Attempt to set an unknown enum value (%d)",
2425 [self class], value];
2427 _values[index] = value;
2429 //%PDDM-EXPAND-END (2 expansions)
2431 //%PDDM-DEFINE MUTATION_HOOK_EnumValidationList()
2432 //% GPBEnumValidationFunc func = _validationFunc;
2433 //% for (NSUInteger i = 0; i < count; ++i) {
2434 //% if (!func(values[i])) {
2435 //% [NSException raise:NSInvalidArgumentException
2436 //% format:@"%@: Attempt to set an unknown enum value (%d)",
2437 //% [self class], values[i]];
2441 //%PDDM-DEFINE MUTATION_HOOK_EnumValidationOne()
2442 //% if (!_validationFunc(value)) {
2443 //% [NSException raise:NSInvalidArgumentException
2444 //% format:@"%@: Attempt to set an unknown enum value (%d)",
2445 //% [self class], value];
2451 #pragma mark - NSArray Subclass
2453 @implementation GPBAutocreatedArray {
2454 NSMutableArray *_array;
2458 NSAssert(!_autocreator,
2459 @"%@: Autocreator must be cleared before release, autocreator: %@",
2460 [self class], _autocreator);
2465 #pragma mark Required NSArray overrides
2467 - (NSUInteger)count {
2468 return [_array count];
2471 - (id)objectAtIndex:(NSUInteger)idx {
2472 return [_array objectAtIndex:idx];
2475 #pragma mark Required NSMutableArray overrides
2477 // Only need to call GPBAutocreatedArrayModified() when adding things since
2478 // we only autocreate empty arrays.
2480 - (void)insertObject:(id)anObject atIndex:(NSUInteger)idx {
2481 if (_array == nil) {
2482 _array = [[NSMutableArray alloc] init];
2484 [_array insertObject:anObject atIndex:idx];
2487 GPBAutocreatedArrayModified(_autocreator, self);
2491 - (void)removeObject:(id)anObject {
2492 [_array removeObject:anObject];
2495 - (void)removeObjectAtIndex:(NSUInteger)idx {
2496 [_array removeObjectAtIndex:idx];
2499 - (void)addObject:(id)anObject {
2500 if (_array == nil) {
2501 _array = [[NSMutableArray alloc] init];
2503 [_array addObject:anObject];
2506 GPBAutocreatedArrayModified(_autocreator, self);
2510 - (void)removeLastObject {
2511 [_array removeLastObject];
2514 - (void)replaceObjectAtIndex:(NSUInteger)idx withObject:(id)anObject {
2515 [_array replaceObjectAtIndex:idx withObject:anObject];
2518 #pragma mark Extra things hooked
2520 - (id)copyWithZone:(NSZone *)zone {
2521 if (_array == nil) {
2522 return [[NSMutableArray allocWithZone:zone] init];
2524 return [_array copyWithZone:zone];
2527 - (id)mutableCopyWithZone:(NSZone *)zone {
2528 if (_array == nil) {
2529 return [[NSMutableArray allocWithZone:zone] init];
2531 return [_array mutableCopyWithZone:zone];
2534 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
2535 objects:(id __unsafe_unretained [])buffer
2536 count:(NSUInteger)len {
2537 return [_array countByEnumeratingWithState:state objects:buffer count:len];
2540 - (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))block {
2541 [_array enumerateObjectsUsingBlock:block];
2544 - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts
2545 usingBlock:(void (NS_NOESCAPE ^)(id obj, NSUInteger idx, BOOL *stop))block {
2546 [_array enumerateObjectsWithOptions:opts usingBlock:block];
2551 #pragma clang diagnostic pop