GPBArray.m 75 KB

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