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