GPBArray.m 74 KB

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