GPBArray.m 76 KB

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