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