GPBArray.m 75 KB

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