GPBArray.m 75 KB

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