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