GPBArray.m 75 KB

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