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