GPBArray.m 75 KB

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