1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 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
31#import "GPBDictionary_PackagePrivate.h"
32
33#import "GPBCodedInputStream_PackagePrivate.h"
34#import "GPBCodedOutputStream_PackagePrivate.h"
35#import "GPBDescriptor_PackagePrivate.h"
36#import "GPBMessage_PackagePrivate.h"
37#import "GPBUtilities_PackagePrivate.h"
38
39// ------------------------------ NOTE ------------------------------
40// At the moment, this is all using NSNumbers in NSDictionaries under
41// the hood, but it is all hidden so we can come back and optimize
42// with direct CFDictionary usage later.  The reason that wasn't
43// done yet is needing to support 32bit iOS builds.  Otherwise
44// it would be pretty simple to store all this data in CFDictionaries
45// directly.
46// ------------------------------------------------------------------
47
48// Direct access is use for speed, to avoid even internally declaring things
49// read/write, etc. The warning is enabled in the project to ensure code calling
50// protos can turn on -Wdirect-ivar-access without issues.
51#pragma clang diagnostic push
52#pragma clang diagnostic ignored "-Wdirect-ivar-access"
53
54// Used to include code only visible to specific versions of the static
55// analyzer. Useful for wrapping code that only exists to silence the analyzer.
56// Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
57// END_APPLE_BUILD_VERSION using:
58//   xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
59// Example usage:
60//  #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
61#define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
62    (defined(__clang_analyzer__) && \
63     (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
64      __apple_build_version__ <= END_APPLE_BUILD_VERSION))
65
66enum {
67  kMapKeyFieldNumber = 1,
68  kMapValueFieldNumber = 2,
69};
70
71static BOOL DictDefault_IsValidValue(int32_t value) {
72  // Anything but the bad value marker is allowed.
73  return (value != kGPBUnrecognizedEnumeratorValue);
74}
75
76//%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
77//%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
78//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
79//%    return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
80//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
81//%    return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
82//%  } else {
83//%    NSCAssert(NO, @"Unexpected type %d", dataType);
84//%    return 0;
85//%  }
86//%}
87//%
88//%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
89//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
90//%    [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
91//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
92//%    [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
93//%  } else {
94//%    NSCAssert(NO, @"Unexpected type %d", dataType);
95//%  }
96//%}
97//%
98//%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
99//%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
100//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
101//%    return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
102//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
103//%    return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
104//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
105//%    return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
106//%  } else {
107//%    NSCAssert(NO, @"Unexpected type %d", dataType);
108//%    return 0;
109//%  }
110//%}
111//%
112//%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
113//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
114//%    [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
115//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
116//%    [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
117//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
118//%    [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
119//%  } else {
120//%    NSCAssert(NO, @"Unexpected type %d", dataType);
121//%  }
122//%}
123//%
124//%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
125//%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
126//%  NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
127//%  #pragma unused(dataType)  // For when asserts are off in release.
128//%  return GPBCompute##VALUE_NAME##Size(fieldNum, value);
129//%}
130//%
131//%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
132//%  NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
133//%  #pragma unused(dataType)  // For when asserts are off in release.
134//%  [stream write##VALUE_NAME##:fieldNum value:value];
135//%}
136//%
137//%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
138//%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
139//%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
140//%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
141//%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
142//%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
143//%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
144//%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
145//%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
146//%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
147//%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
148//%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
149// This block of code is generated, do not edit it directly.
150
151static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
152  if (dataType == GPBDataTypeInt32) {
153    return GPBComputeInt32Size(fieldNum, value);
154  } else if (dataType == GPBDataTypeSInt32) {
155    return GPBComputeSInt32Size(fieldNum, value);
156  } else if (dataType == GPBDataTypeSFixed32) {
157    return GPBComputeSFixed32Size(fieldNum, value);
158  } else {
159    NSCAssert(NO, @"Unexpected type %d", dataType);
160    return 0;
161  }
162}
163
164static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
165  if (dataType == GPBDataTypeInt32) {
166    [stream writeInt32:fieldNum value:value];
167  } else if (dataType == GPBDataTypeSInt32) {
168    [stream writeSInt32:fieldNum value:value];
169  } else if (dataType == GPBDataTypeSFixed32) {
170    [stream writeSFixed32:fieldNum value:value];
171  } else {
172    NSCAssert(NO, @"Unexpected type %d", dataType);
173  }
174}
175
176static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
177  if (dataType == GPBDataTypeUInt32) {
178    return GPBComputeUInt32Size(fieldNum, value);
179  } else if (dataType == GPBDataTypeFixed32) {
180    return GPBComputeFixed32Size(fieldNum, value);
181  } else {
182    NSCAssert(NO, @"Unexpected type %d", dataType);
183    return 0;
184  }
185}
186
187static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
188  if (dataType == GPBDataTypeUInt32) {
189    [stream writeUInt32:fieldNum value:value];
190  } else if (dataType == GPBDataTypeFixed32) {
191    [stream writeFixed32:fieldNum value:value];
192  } else {
193    NSCAssert(NO, @"Unexpected type %d", dataType);
194  }
195}
196
197static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
198  if (dataType == GPBDataTypeInt64) {
199    return GPBComputeInt64Size(fieldNum, value);
200  } else if (dataType == GPBDataTypeSInt64) {
201    return GPBComputeSInt64Size(fieldNum, value);
202  } else if (dataType == GPBDataTypeSFixed64) {
203    return GPBComputeSFixed64Size(fieldNum, value);
204  } else {
205    NSCAssert(NO, @"Unexpected type %d", dataType);
206    return 0;
207  }
208}
209
210static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
211  if (dataType == GPBDataTypeInt64) {
212    [stream writeInt64:fieldNum value:value];
213  } else if (dataType == GPBDataTypeSInt64) {
214    [stream writeSInt64:fieldNum value:value];
215  } else if (dataType == GPBDataTypeSFixed64) {
216    [stream writeSFixed64:fieldNum value:value];
217  } else {
218    NSCAssert(NO, @"Unexpected type %d", dataType);
219  }
220}
221
222static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
223  if (dataType == GPBDataTypeUInt64) {
224    return GPBComputeUInt64Size(fieldNum, value);
225  } else if (dataType == GPBDataTypeFixed64) {
226    return GPBComputeFixed64Size(fieldNum, value);
227  } else {
228    NSCAssert(NO, @"Unexpected type %d", dataType);
229    return 0;
230  }
231}
232
233static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
234  if (dataType == GPBDataTypeUInt64) {
235    [stream writeUInt64:fieldNum value:value];
236  } else if (dataType == GPBDataTypeFixed64) {
237    [stream writeFixed64:fieldNum value:value];
238  } else {
239    NSCAssert(NO, @"Unexpected type %d", dataType);
240  }
241}
242
243static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
244  NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
245  #pragma unused(dataType)  // For when asserts are off in release.
246  return GPBComputeBoolSize(fieldNum, value);
247}
248
249static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
250  NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
251  #pragma unused(dataType)  // For when asserts are off in release.
252  [stream writeBool:fieldNum value:value];
253}
254
255static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
256  NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
257  #pragma unused(dataType)  // For when asserts are off in release.
258  return GPBComputeEnumSize(fieldNum, value);
259}
260
261static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
262  NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
263  #pragma unused(dataType)  // For when asserts are off in release.
264  [stream writeEnum:fieldNum value:value];
265}
266
267static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
268  NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
269  #pragma unused(dataType)  // For when asserts are off in release.
270  return GPBComputeFloatSize(fieldNum, value);
271}
272
273static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
274  NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
275  #pragma unused(dataType)  // For when asserts are off in release.
276  [stream writeFloat:fieldNum value:value];
277}
278
279static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
280  NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
281  #pragma unused(dataType)  // For when asserts are off in release.
282  return GPBComputeDoubleSize(fieldNum, value);
283}
284
285static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
286  NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
287  #pragma unused(dataType)  // For when asserts are off in release.
288  [stream writeDouble:fieldNum value:value];
289}
290
291static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
292  NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
293  #pragma unused(dataType)  // For when asserts are off in release.
294  return GPBComputeStringSize(fieldNum, value);
295}
296
297static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
298  NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
299  #pragma unused(dataType)  // For when asserts are off in release.
300  [stream writeString:fieldNum value:value];
301}
302
303static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
304  if (dataType == GPBDataTypeMessage) {
305    return GPBComputeMessageSize(fieldNum, value);
306  } else if (dataType == GPBDataTypeString) {
307    return GPBComputeStringSize(fieldNum, value);
308  } else if (dataType == GPBDataTypeBytes) {
309    return GPBComputeBytesSize(fieldNum, value);
310  } else {
311    NSCAssert(NO, @"Unexpected type %d", dataType);
312    return 0;
313  }
314}
315
316static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
317  if (dataType == GPBDataTypeMessage) {
318    [stream writeMessage:fieldNum value:value];
319  } else if (dataType == GPBDataTypeString) {
320    [stream writeString:fieldNum value:value];
321  } else if (dataType == GPBDataTypeBytes) {
322    [stream writeBytes:fieldNum value:value];
323  } else {
324    NSCAssert(NO, @"Unexpected type %d", dataType);
325  }
326}
327
328//%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
329
330size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
331  GPBDataType mapValueType = GPBGetFieldDataType(field);
332  __block size_t result = 0;
333  [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
334    #pragma unused(stop)
335    size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
336    msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
337    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
338  }];
339  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
340  result += tagSize * dict.count;
341  return result;
342}
343
344void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
345                                              NSDictionary *dict,
346                                              GPBFieldDescriptor *field) {
347  NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
348  GPBDataType mapValueType = GPBGetFieldDataType(field);
349  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
350  [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
351    #pragma unused(stop)
352    // Write the tag.
353    [outputStream writeInt32NoTag:tag];
354    // Write the size of the message.
355    size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
356    msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
357
358    // Write the size and fields.
359    [outputStream writeInt32NoTag:(int32_t)msgSize];
360    [outputStream writeString:kMapKeyFieldNumber value:key];
361    WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
362  }];
363}
364
365BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
366  NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
367  NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
368  #pragma unused(field)  // For when asserts are off in release.
369  for (GPBMessage *msg in [dict objectEnumerator]) {
370    if (!msg.initialized) {
371      return NO;
372    }
373  }
374  return YES;
375}
376
377// Note: if the type is an object, it the retain pass back to the caller.
378static void ReadValue(GPBCodedInputStream *stream,
379                      GPBGenericValue *valueToFill,
380                      GPBDataType type,
381                      GPBExtensionRegistry *registry,
382                      GPBFieldDescriptor *field) {
383  switch (type) {
384    case GPBDataTypeBool:
385      valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
386      break;
387    case GPBDataTypeFixed32:
388      valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
389      break;
390    case GPBDataTypeSFixed32:
391      valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
392      break;
393    case GPBDataTypeFloat:
394      valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
395      break;
396    case GPBDataTypeFixed64:
397      valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
398      break;
399    case GPBDataTypeSFixed64:
400      valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
401      break;
402    case GPBDataTypeDouble:
403      valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
404      break;
405    case GPBDataTypeInt32:
406      valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
407      break;
408    case GPBDataTypeInt64:
409      valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
410      break;
411    case GPBDataTypeSInt32:
412      valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
413      break;
414    case GPBDataTypeSInt64:
415      valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
416      break;
417    case GPBDataTypeUInt32:
418      valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
419      break;
420    case GPBDataTypeUInt64:
421      valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
422      break;
423    case GPBDataTypeBytes:
424      [valueToFill->valueData release];
425      valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
426      break;
427    case GPBDataTypeString:
428      [valueToFill->valueString release];
429      valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
430      break;
431    case GPBDataTypeMessage: {
432      GPBMessage *message = [[field.msgClass alloc] init];
433      [stream readMessage:message extensionRegistry:registry];
434      [valueToFill->valueMessage release];
435      valueToFill->valueMessage = message;
436      break;
437    }
438    case GPBDataTypeGroup:
439      NSCAssert(NO, @"Can't happen");
440      break;
441    case GPBDataTypeEnum:
442      valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
443      break;
444  }
445}
446
447void GPBDictionaryReadEntry(id mapDictionary,
448                            GPBCodedInputStream *stream,
449                            GPBExtensionRegistry *registry,
450                            GPBFieldDescriptor *field,
451                            GPBMessage *parentMessage) {
452  GPBDataType keyDataType = field.mapKeyDataType;
453  GPBDataType valueDataType = GPBGetFieldDataType(field);
454
455  GPBGenericValue key;
456  GPBGenericValue value;
457  // Zero them (but pick up any enum default for proto2).
458  key.valueString = value.valueString = nil;
459  if (valueDataType == GPBDataTypeEnum) {
460    value = field.defaultValue;
461  }
462
463  GPBCodedInputStreamState *state = &stream->state_;
464  uint32_t keyTag =
465      GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
466  uint32_t valueTag =
467      GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
468
469  BOOL hitError = NO;
470  while (YES) {
471    uint32_t tag = GPBCodedInputStreamReadTag(state);
472    if (tag == keyTag) {
473      ReadValue(stream, &key, keyDataType, registry, field);
474    } else if (tag == valueTag) {
475      ReadValue(stream, &value, valueDataType, registry, field);
476    } else if (tag == 0) {
477      // zero signals EOF / limit reached
478      break;
479    } else {  // Unknown
480      if (![stream skipField:tag]){
481        hitError = YES;
482        break;
483      }
484    }
485  }
486
487  if (!hitError) {
488    // Handle the special defaults and/or missing key/value.
489    if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
490      key.valueString = [@"" retain];
491    }
492    if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
493#pragma clang diagnostic push
494#pragma clang diagnostic ignored "-Wswitch-enum"
495      switch (valueDataType) {
496        case GPBDataTypeString:
497          value.valueString = [@"" retain];
498          break;
499        case GPBDataTypeBytes:
500          value.valueData = [GPBEmptyNSData() retain];
501          break;
502#if defined(__clang_analyzer__)
503        case GPBDataTypeGroup:
504          // Maps can't really have Groups as the value type, but this case is needed
505          // so the analyzer won't report the posibility of send nil in for the value
506          // in the NSMutableDictionary case below.
507#endif
508        case GPBDataTypeMessage: {
509          value.valueMessage = [[field.msgClass alloc] init];
510          break;
511        }
512        default:
513          // Nothing
514          break;
515      }
516#pragma clang diagnostic pop
517    }
518
519    if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
520#if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
521     // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
522     // be raised as needed for new Xcodes.
523     //
524     // This is only needed on a "shallow" analyze; on a "deep" analyze, the
525     // existing code path gets this correct. In shallow, the analyzer decides
526     // GPBDataTypeIsObject(valueDataType) is both false and true on a single
527     // path through this function, allowing nil to be used for the
528     // setObject:forKey:.
529     if (value.valueString == nil) {
530       value.valueString = [@"" retain];
531     }
532#endif
533      // mapDictionary is an NSMutableDictionary
534      [(NSMutableDictionary *)mapDictionary setObject:value.valueString
535                                               forKey:key.valueString];
536    } else {
537      if (valueDataType == GPBDataTypeEnum) {
538        if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
539            [field isValidEnumValue:value.valueEnum]) {
540          [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
541        } else {
542          NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
543                                                               forKey:&key
544                                                          keyDataType:keyDataType];
545          [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
546        }
547      } else {
548        [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
549      }
550    }
551  }
552
553  if (GPBDataTypeIsObject(keyDataType)) {
554    [key.valueString release];
555  }
556  if (GPBDataTypeIsObject(valueDataType)) {
557    [value.valueString release];
558  }
559}
560
561//
562// Macros for the common basic cases.
563//
564
565//%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
566//%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
567//%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
568
569//%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
570//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
571//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
572//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
573//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
574//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
575//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
576//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
577//%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
578
579//%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
580//%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
581
582//%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
583//%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
584
585//%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
586//%#pragma mark - KEY_NAME -> VALUE_NAME
587//%
588//%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
589//% @package
590//%  NSMutableDictionary *_dictionary;
591//%}
592//%
593//%+ (instancetype)dictionary {
594//%  return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
595//%}
596//%
597//%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)##VNAME_VAR
598//%                      ##VNAME$S##  forKey:(KEY_TYPE##KisP$S##KisP)key {
599//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
600//%  // on to get the type correct.
601//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
602//%               KEY_NAME$S VALUE_NAME$S                        ##VNAME$S##  forKeys:&key
603//%               KEY_NAME$S VALUE_NAME$S                        ##VNAME$S##    count:1] autorelease];
604//%}
605//%
606//%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
607//%                      ##VNAME$S##  forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
608//%                      ##VNAME$S##    count:(NSUInteger)count {
609//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
610//%  // on to get the type correct.
611//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
612//%               KEY_NAME$S VALUE_NAME$S                               forKeys:keys
613//%               KEY_NAME$S VALUE_NAME$S                                 count:count] autorelease];
614//%}
615//%
616//%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
617//%  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
618//%  // on to get the type correct.
619//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
620//%}
621//%
622//%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
623//%  return [[[self alloc] initWithCapacity:numItems] autorelease];
624//%}
625//%
626//%- (instancetype)init {
627//%  return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
628//%}
629//%
630//%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
631//%                ##VNAME$S##  forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
632//%                ##VNAME$S##    count:(NSUInteger)count {
633//%  self = [super init];
634//%  if (self) {
635//%    _dictionary = [[NSMutableDictionary alloc] init];
636//%    if (count && VNAME_VAR##s && keys) {
637//%      for (NSUInteger i = 0; i < count; ++i) {
638//%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______)        [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
639//%      }
640//%    }
641//%  }
642//%  return self;
643//%}
644//%
645//%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
646//%  self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
647//%  if (self) {
648//%    if (dictionary) {
649//%      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
650//%    }
651//%  }
652//%  return self;
653//%}
654//%
655//%- (instancetype)initWithCapacity:(NSUInteger)numItems {
656//%  #pragma unused(numItems)
657//%  return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
658//%}
659//%
660//%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
661//%
662//%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
663//%
664//%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
665//%
666//%@end
667//%
668
669//%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
670//%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
671//%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
672//%#pragma mark - KEY_NAME -> VALUE_NAME
673//%
674//%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
675//% @package
676//%  NSMutableDictionary *_dictionary;
677//%  GPBEnumValidationFunc _validationFunc;
678//%}
679//%
680//%@synthesize validationFunc = _validationFunc;
681//%
682//%+ (instancetype)dictionary {
683//%  return [[[self alloc] initWithValidationFunction:NULL
684//%                                         rawValues:NULL
685//%                                           forKeys:NULL
686//%                                             count:0] autorelease];
687//%}
688//%
689//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
690//%  return [[[self alloc] initWithValidationFunction:func
691//%                                         rawValues:NULL
692//%                                           forKeys:NULL
693//%                                             count:0] autorelease];
694//%}
695//%
696//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
697//%                                        rawValue:(VALUE_TYPE)rawValue
698//%                                          forKey:(KEY_TYPE##KisP$S##KisP)key {
699//%  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
700//%  // on to get the type correct.
701//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
702//%               KEY_NAME$S VALUE_NAME$S                                         rawValues:&rawValue
703//%               KEY_NAME$S VALUE_NAME$S                                           forKeys:&key
704//%               KEY_NAME$S VALUE_NAME$S                                             count:1] autorelease];
705//%}
706//%
707//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
708//%                                       rawValues:(const VALUE_TYPE [])rawValues
709//%                                         forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
710//%                                           count:(NSUInteger)count {
711//%  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
712//%  // on to get the type correct.
713//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
714//%               KEY_NAME$S VALUE_NAME$S                                         rawValues:rawValues
715//%               KEY_NAME$S VALUE_NAME$S                                           forKeys:keys
716//%               KEY_NAME$S VALUE_NAME$S                                             count:count] autorelease];
717//%}
718//%
719//%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
720//%  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
721//%  // on to get the type correct.
722//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
723//%}
724//%
725//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
726//%                                        capacity:(NSUInteger)numItems {
727//%  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
728//%}
729//%
730//%- (instancetype)init {
731//%  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
732//%}
733//%
734//%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
735//%  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
736//%}
737//%
738//%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
739//%                                 rawValues:(const VALUE_TYPE [])rawValues
740//%                                   forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
741//%                                     count:(NSUInteger)count {
742//%  self = [super init];
743//%  if (self) {
744//%    _dictionary = [[NSMutableDictionary alloc] init];
745//%    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
746//%    if (count && rawValues && keys) {
747//%      for (NSUInteger i = 0; i < count; ++i) {
748//%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______)        [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
749//%      }
750//%    }
751//%  }
752//%  return self;
753//%}
754//%
755//%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
756//%  self = [self initWithValidationFunction:dictionary.validationFunc
757//%                                rawValues:NULL
758//%                                  forKeys:NULL
759//%                                    count:0];
760//%  if (self) {
761//%    if (dictionary) {
762//%      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
763//%    }
764//%  }
765//%  return self;
766//%}
767//%
768//%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
769//%                                  capacity:(NSUInteger)numItems {
770//%  #pragma unused(numItems)
771//%  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
772//%}
773//%
774//%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
775//%
776//%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
777//%  NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
778//%  if (wrapped && value) {
779//%    VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
780//%    if (!_validationFunc(result)) {
781//%      result = kGPBUnrecognizedEnumeratorValue;
782//%    }
783//%    *value = result;
784//%  }
785//%  return (wrapped != NULL);
786//%}
787//%
788//%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
789//%  NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
790//%  if (wrapped && rawValue) {
791//%    *rawValue = UNWRAP##VALUE_NAME(wrapped);
792//%  }
793//%  return (wrapped != NULL);
794//%}
795//%
796//%- (void)enumerateKeysAndEnumsUsingBlock:
797//%    (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
798//%  GPBEnumValidationFunc func = _validationFunc;
799//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
800//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
801//%                                                   BOOL *stop) {
802//%      VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
803//%      if (!func(unwrapped)) {
804//%        unwrapped = kGPBUnrecognizedEnumeratorValue;
805//%      }
806//%      block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
807//%  }];
808//%}
809//%
810//%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
811//%
812//%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
813//%DICTIONARY_VALIDATE_KEY_##KHELPER(key, )  if (!_validationFunc(value)) {
814//%    [NSException raise:NSInvalidArgumentException
815//%                format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
816//%                       value];
817//%  }
818//%
819//%  [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
820//%  if (_autocreator) {
821//%    GPBAutocreatedDictionaryModified(_autocreator, self);
822//%  }
823//%}
824//%
825//%@end
826//%
827
828//%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
829//%- (void)dealloc {
830//%  NSAssert(!_autocreator,
831//%           @"%@: Autocreator must be cleared before release, autocreator: %@",
832//%           [self class], _autocreator);
833//%  [_dictionary release];
834//%  [super dealloc];
835//%}
836//%
837//%- (instancetype)copyWithZone:(NSZone *)zone {
838//%  return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
839//%}
840//%
841//%- (BOOL)isEqual:(id)other {
842//%  if (self == other) {
843//%    return YES;
844//%  }
845//%  if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
846//%    return NO;
847//%  }
848//%  GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
849//%  return [_dictionary isEqual:otherDictionary->_dictionary];
850//%}
851//%
852//%- (NSUInteger)hash {
853//%  return _dictionary.count;
854//%}
855//%
856//%- (NSString *)description {
857//%  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
858//%}
859//%
860//%- (NSUInteger)count {
861//%  return _dictionary.count;
862//%}
863//%
864//%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
865//%    (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
866//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
867//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u,
868//%                                                   BOOL *stop) {
869//%      block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), stop);
870//%  }];
871//%}
872//%
873//%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
874//%  NSUInteger count = _dictionary.count;
875//%  if (count == 0) {
876//%    return 0;
877//%  }
878//%
879//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
880//%  GPBDataType keyDataType = field.mapKeyDataType;
881//%  __block size_t result = 0;
882//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
883//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u##,
884//%                                                   BOOL *stop) {
885//%    #pragma unused(stop)
886//%    size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
887//%    msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
888//%    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
889//%  }];
890//%  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
891//%  result += tagSize * count;
892//%  return result;
893//%}
894//%
895//%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
896//%                         asField:(GPBFieldDescriptor *)field {
897//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
898//%  GPBDataType keyDataType = field.mapKeyDataType;
899//%  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
900//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
901//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u,
902//%                                                   BOOL *stop) {
903//%    #pragma unused(stop)
904//%    // Write the tag.
905//%    [outputStream writeInt32NoTag:tag];
906//%    // Write the size of the message.
907//%    size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
908//%    msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
909//%    [outputStream writeInt32NoTag:(int32_t)msgSize];
910//%    // Write the fields.
911//%    WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
912//%    WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
913//%  }];
914//%}
915//%
916//%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
917//%     forGPBGenericValueKey:(GPBGenericValue *)key {
918//%  [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
919//%}
920//%
921//%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
922//%  [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) {
923//%      #pragma unused(stop)
924//%      block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
925//%  }];
926//%}
927//%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
928//%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
929//%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
930//%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
931//%  if (otherDictionary) {
932//%    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
933//%    if (_autocreator) {
934//%      GPBAutocreatedDictionaryModified(_autocreator, self);
935//%    }
936//%  }
937//%}
938//%
939//%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
940//%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, )  [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
941//%  if (_autocreator) {
942//%    GPBAutocreatedDictionaryModified(_autocreator, self);
943//%  }
944//%}
945//%
946//%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
947//%  [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
948//%}
949//%
950//%- (void)removeAll {
951//%  [_dictionary removeAllObjects];
952//%}
953
954//
955// Custom Generation for Bool keys
956//
957
958//%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
959//%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
960//%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
961//%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
962
963//%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
964//%#pragma mark - Bool -> VALUE_NAME
965//%
966//%@implementation GPBBool##VALUE_NAME##Dictionary {
967//% @package
968//%  VALUE_TYPE _values[2];
969//%BOOL_DICT_HAS_STORAGE_##HELPER()}
970//%
971//%+ (instancetype)dictionary {
972//%  return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
973//%}
974//%
975//%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)VNAME_VAR
976//%                      ##VNAME$S##  forKey:(BOOL)key {
977//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
978//%  // on to get the type correct.
979//%  return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
980//%                    VALUE_NAME$S                        ##VNAME$S##  forKeys:&key
981//%                    VALUE_NAME$S                        ##VNAME$S##    count:1] autorelease];
982//%}
983//%
984//%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
985//%                      ##VNAME$S##  forKeys:(const BOOL [])keys
986//%                      ##VNAME$S##    count:(NSUInteger)count {
987//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
988//%  // on to get the type correct.
989//%  return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
990//%                    VALUE_NAME$S                        ##VNAME$S##  forKeys:keys
991//%                    VALUE_NAME$S                        ##VNAME$S##    count:count] autorelease];
992//%}
993//%
994//%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
995//%  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
996//%  // on to get the type correct.
997//%  return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
998//%}
999//%
1000//%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1001//%  return [[[self alloc] initWithCapacity:numItems] autorelease];
1002//%}
1003//%
1004//%- (instancetype)init {
1005//%  return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
1006//%}
1007//%
1008//%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
1009//%
1010//%- (instancetype)initWithCapacity:(NSUInteger)numItems {
1011//%  #pragma unused(numItems)
1012//%  return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
1013//%}
1014//%
1015//%BOOL_DICT_DEALLOC##HELPER()
1016//%
1017//%- (instancetype)copyWithZone:(NSZone *)zone {
1018//%  return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
1019//%}
1020//%
1021//%- (BOOL)isEqual:(id)other {
1022//%  if (self == other) {
1023//%    return YES;
1024//%  }
1025//%  if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
1026//%    return NO;
1027//%  }
1028//%  GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
1029//%  if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
1030//%      (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
1031//%    return NO;
1032//%  }
1033//%  if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
1034//%      (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
1035//%    return NO;
1036//%  }
1037//%  return YES;
1038//%}
1039//%
1040//%- (NSUInteger)hash {
1041//%  return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
1042//%}
1043//%
1044//%- (NSString *)description {
1045//%  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
1046//%  if (BOOL_DICT_W_HAS##HELPER(0, )) {
1047//%    [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
1048//%  }
1049//%  if (BOOL_DICT_W_HAS##HELPER(1, )) {
1050//%    [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
1051//%  }
1052//%  [result appendString:@" }"];
1053//%  return result;
1054//%}
1055//%
1056//%- (NSUInteger)count {
1057//%  return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
1058//%}
1059//%
1060//%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
1061//%
1062//%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
1063//%
1064//%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
1065//%  if (BOOL_DICT_HAS##HELPER(0, )) {
1066//%    block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
1067//%  }
1068//%  if (BOOL_DICT_W_HAS##HELPER(1, )) {
1069//%    block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
1070//%  }
1071//%}
1072//%
1073//%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
1074//%    (void (^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
1075//%  BOOL stop = NO;
1076//%  if (BOOL_DICT_HAS##HELPER(0, )) {
1077//%    block(NO, _values[0], &stop);
1078//%  }
1079//%  if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
1080//%    block(YES, _values[1], &stop);
1081//%  }
1082//%}
1083//%
1084//%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
1085//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
1086//%  NSUInteger count = 0;
1087//%  size_t result = 0;
1088//%  for (int i = 0; i < 2; ++i) {
1089//%    if (BOOL_DICT_HAS##HELPER(i, )) {
1090//%      ++count;
1091//%      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
1092//%      msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
1093//%      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
1094//%    }
1095//%  }
1096//%  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
1097//%  result += tagSize * count;
1098//%  return result;
1099//%}
1100//%
1101//%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
1102//%                         asField:(GPBFieldDescriptor *)field {
1103//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
1104//%  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
1105//%  for (int i = 0; i < 2; ++i) {
1106//%    if (BOOL_DICT_HAS##HELPER(i, )) {
1107//%      // Write the tag.
1108//%      [outputStream writeInt32NoTag:tag];
1109//%      // Write the size of the message.
1110//%      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
1111//%      msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
1112//%      [outputStream writeInt32NoTag:(int32_t)msgSize];
1113//%      // Write the fields.
1114//%      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
1115//%      WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
1116//%    }
1117//%  }
1118//%}
1119//%
1120//%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
1121//%
1122//%@end
1123//%
1124
1125
1126//
1127// Helpers for PODs
1128//
1129
1130//%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
1131//%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
1132//%  NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
1133//%  if (wrapped && value) {
1134//%    *value = UNWRAP##VALUE_NAME(wrapped);
1135//%  }
1136//%  return (wrapped != NULL);
1137//%}
1138//%PDDM-DEFINE WRAPPEDPOD(VALUE)
1139//%@(VALUE)
1140//%PDDM-DEFINE UNWRAPUInt32(VALUE)
1141//%[VALUE unsignedIntValue]
1142//%PDDM-DEFINE UNWRAPInt32(VALUE)
1143//%[VALUE intValue]
1144//%PDDM-DEFINE UNWRAPUInt64(VALUE)
1145//%[VALUE unsignedLongLongValue]
1146//%PDDM-DEFINE UNWRAPInt64(VALUE)
1147//%[VALUE longLongValue]
1148//%PDDM-DEFINE UNWRAPBool(VALUE)
1149//%[VALUE boolValue]
1150//%PDDM-DEFINE UNWRAPFloat(VALUE)
1151//%[VALUE floatValue]
1152//%PDDM-DEFINE UNWRAPDouble(VALUE)
1153//%[VALUE doubleValue]
1154//%PDDM-DEFINE UNWRAPEnum(VALUE)
1155//%[VALUE intValue]
1156//%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
1157//%[NSString stringWithFormat:@"%u", VALUE]
1158//%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
1159//%[NSString stringWithFormat:@"%d", VALUE]
1160//%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
1161//%[NSString stringWithFormat:@"%llu", VALUE]
1162//%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
1163//%[NSString stringWithFormat:@"%lld", VALUE]
1164//%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
1165//%(VALUE ? @"true" : @"false")
1166//%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
1167//%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
1168//%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
1169//%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
1170//%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
1171//%@(VALUE)
1172//%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
1173//%NSNumber *
1174//%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
1175//%VAL1 != VAL2
1176//%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
1177// Empty
1178//%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
1179// Empty
1180//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
1181//%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
1182//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
1183// Empty
1184//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
1185// Empty
1186//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
1187// Empty
1188//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
1189// Empty
1190//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
1191// Empty
1192//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
1193// Empty
1194//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
1195// Empty
1196//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
1197//%- (NSData *)serializedDataForUnknownValue:(int32_t)value
1198//%                                   forKey:(GPBGenericValue *)key
1199//%                              keyDataType:(GPBDataType)keyDataType {
1200//%  size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
1201//%  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
1202//%  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
1203//%  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
1204//%  WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
1205//%  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
1206//%  [outputStream release];
1207//%  return data;
1208//%}
1209//%
1210//%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
1211//%value##VALUE_NAME
1212//%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
1213// Empty
1214//%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
1215// Empty
1216
1217//%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
1218//%  BOOL _valueSet[2];
1219//%
1220//%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
1221//%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
1222//%                 ##VALUE_NAME$S## forKeys:(const BOOL [])keys
1223//%                 ##VALUE_NAME$S##   count:(NSUInteger)count {
1224//%  self = [super init];
1225//%  if (self) {
1226//%    for (NSUInteger i = 0; i < count; ++i) {
1227//%      int idx = keys[i] ? 1 : 0;
1228//%      _values[idx] = values[i];
1229//%      _valueSet[idx] = YES;
1230//%    }
1231//%  }
1232//%  return self;
1233//%}
1234//%
1235//%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
1236//%  self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
1237//%  if (self) {
1238//%    if (dictionary) {
1239//%      for (int i = 0; i < 2; ++i) {
1240//%        if (dictionary->_valueSet[i]) {
1241//%          _values[i] = dictionary->_values[i];
1242//%          _valueSet[i] = YES;
1243//%        }
1244//%      }
1245//%    }
1246//%  }
1247//%  return self;
1248//%}
1249//%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
1250//%#if !defined(NS_BLOCK_ASSERTIONS)
1251//%- (void)dealloc {
1252//%  NSAssert(!_autocreator,
1253//%           @"%@: Autocreator must be cleared before release, autocreator: %@",
1254//%           [self class], _autocreator);
1255//%  [super dealloc];
1256//%}
1257//%#endif  // !defined(NS_BLOCK_ASSERTIONS)
1258//%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
1259//%BOOL_DICT_HASPOD(IDX, REF)
1260//%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
1261//%REF##_valueSet[IDX]
1262//%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
1263//%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
1264//%  int idx = (key ? 1 : 0);
1265//%  if (_valueSet[idx]) {
1266//%    if (value) {
1267//%      *value = _values[idx];
1268//%    }
1269//%    return YES;
1270//%  }
1271//%  return NO;
1272//%}
1273//%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
1274//%- (void)setGPBGenericValue:(GPBGenericValue *)value
1275//%     forGPBGenericValueKey:(GPBGenericValue *)key {
1276//%  int idx = (key->valueBool ? 1 : 0);
1277//%  _values[idx] = value->value##VALUE_NAME;
1278//%  _valueSet[idx] = YES;
1279//%}
1280//%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
1281//%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
1282//%  if (otherDictionary) {
1283//%    for (int i = 0; i < 2; ++i) {
1284//%      if (otherDictionary->_valueSet[i]) {
1285//%        _valueSet[i] = YES;
1286//%        _values[i] = otherDictionary->_values[i];
1287//%      }
1288//%    }
1289//%    if (_autocreator) {
1290//%      GPBAutocreatedDictionaryModified(_autocreator, self);
1291//%    }
1292//%  }
1293//%}
1294//%
1295//%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
1296//%  int idx = (key ? 1 : 0);
1297//%  _values[idx] = value;
1298//%  _valueSet[idx] = YES;
1299//%  if (_autocreator) {
1300//%    GPBAutocreatedDictionaryModified(_autocreator, self);
1301//%  }
1302//%}
1303//%
1304//%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
1305//%  _valueSet[aKey ? 1 : 0] = NO;
1306//%}
1307//%
1308//%- (void)removeAll {
1309//%  _valueSet[0] = NO;
1310//%  _valueSet[1] = NO;
1311//%}
1312//%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
1313//%STR_FORMAT_##VALUE_NAME()
1314//%PDDM-DEFINE STR_FORMAT_UInt32()
1315//%%u
1316//%PDDM-DEFINE STR_FORMAT_Int32()
1317//%%d
1318//%PDDM-DEFINE STR_FORMAT_UInt64()
1319//%%llu
1320//%PDDM-DEFINE STR_FORMAT_Int64()
1321//%%lld
1322//%PDDM-DEFINE STR_FORMAT_Bool()
1323//%%d
1324//%PDDM-DEFINE STR_FORMAT_Float()
1325//%%f
1326//%PDDM-DEFINE STR_FORMAT_Double()
1327//%%lf
1328
1329//
1330// Helpers for Objects
1331//
1332
1333//%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
1334//%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
1335//%  VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
1336//%  return result;
1337//%}
1338//%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
1339//%VALUE
1340//%PDDM-DEFINE UNWRAPString(VALUE)
1341//%VALUE
1342//%PDDM-DEFINE UNWRAPObject(VALUE)
1343//%VALUE
1344//%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
1345//%VALUE
1346//%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
1347//%VALUE
1348//%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
1349//%ENUM_TYPEOBJECT_##TYPE()
1350//%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
1351//%NSString *
1352//%PDDM-DEFINE ENUM_TYPEOBJECT_id()
1353//%id ##
1354//%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
1355//%![VAL1 isEqual:VAL2]
1356//%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
1357//%- (BOOL)isInitialized {
1358//%  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
1359//%    if (!msg.initialized) {
1360//%      return NO;
1361//%    }
1362//%  }
1363//%  return YES;
1364//%}
1365//%
1366//%- (instancetype)deepCopyWithZone:(NSZone *)zone {
1367//%  GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
1368//%      [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
1369//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
1370//%                                                   GPBMessage *msg,
1371//%                                                   BOOL *stop) {
1372//%    #pragma unused(stop)
1373//%    GPBMessage *copiedMsg = [msg copyWithZone:zone];
1374//%    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
1375//%    [copiedMsg release];
1376//%  }];
1377//%  return newDict;
1378//%}
1379//%
1380//%
1381//%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
1382//%- (BOOL)isInitialized {
1383//%  if (_values[0] && ![_values[0] isInitialized]) {
1384//%    return NO;
1385//%  }
1386//%  if (_values[1] && ![_values[1] isInitialized]) {
1387//%    return NO;
1388//%  }
1389//%  return YES;
1390//%}
1391//%
1392//%- (instancetype)deepCopyWithZone:(NSZone *)zone {
1393//%  GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
1394//%      [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
1395//%  for (int i = 0; i < 2; ++i) {
1396//%    if (_values[i] != nil) {
1397//%      newDict->_values[i] = [_values[i] copyWithZone:zone];
1398//%    }
1399//%  }
1400//%  return newDict;
1401//%}
1402//%
1403//%
1404//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
1405// Empty
1406//%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
1407//%valueString
1408//%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
1409//%##EXTRA_INDENT$S##  if (!##VALUE_NAME) {
1410//%##EXTRA_INDENT$S##    [NSException raise:NSInvalidArgumentException
1411//%##EXTRA_INDENT$S##                format:@"Attempting to add nil object to a Dictionary"];
1412//%##EXTRA_INDENT$S##  }
1413//%
1414//%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
1415//%##EXTRA_INDENT$S##  if (!##KEY_NAME) {
1416//%##EXTRA_INDENT$S##    [NSException raise:NSInvalidArgumentException
1417//%##EXTRA_INDENT$S##                format:@"Attempting to add nil key to a Dictionary"];
1418//%##EXTRA_INDENT$S##  }
1419//%
1420
1421//%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
1422// Empty
1423//%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
1424//%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
1425//%                        forKeys:(const BOOL [])keys
1426//%                          count:(NSUInteger)count {
1427//%  self = [super init];
1428//%  if (self) {
1429//%    for (NSUInteger i = 0; i < count; ++i) {
1430//%      if (!objects[i]) {
1431//%        [NSException raise:NSInvalidArgumentException
1432//%                    format:@"Attempting to add nil object to a Dictionary"];
1433//%      }
1434//%      int idx = keys[i] ? 1 : 0;
1435//%      [_values[idx] release];
1436//%      _values[idx] = (VALUE_TYPE)[objects[i] retain];
1437//%    }
1438//%  }
1439//%  return self;
1440//%}
1441//%
1442//%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
1443//%  self = [self initWithObjects:NULL forKeys:NULL count:0];
1444//%  if (self) {
1445//%    if (dictionary) {
1446//%      _values[0] = [dictionary->_values[0] retain];
1447//%      _values[1] = [dictionary->_values[1] retain];
1448//%    }
1449//%  }
1450//%  return self;
1451//%}
1452//%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
1453//%- (void)dealloc {
1454//%  NSAssert(!_autocreator,
1455//%           @"%@: Autocreator must be cleared before release, autocreator: %@",
1456//%           [self class], _autocreator);
1457//%  [_values[0] release];
1458//%  [_values[1] release];
1459//%  [super dealloc];
1460//%}
1461//%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
1462//%(BOOL_DICT_HASOBJECT(IDX, REF))
1463//%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
1464//%REF##_values[IDX] != nil
1465//%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
1466//%- (VALUE_TYPE)objectForKey:(BOOL)key {
1467//%  return _values[key ? 1 : 0];
1468//%}
1469//%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
1470//%- (void)setGPBGenericValue:(GPBGenericValue *)value
1471//%     forGPBGenericValueKey:(GPBGenericValue *)key {
1472//%  int idx = (key->valueBool ? 1 : 0);
1473//%  [_values[idx] release];
1474//%  _values[idx] = [value->valueString retain];
1475//%}
1476
1477//%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
1478//%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
1479//%  if (otherDictionary) {
1480//%    for (int i = 0; i < 2; ++i) {
1481//%      if (otherDictionary->_values[i] != nil) {
1482//%        [_values[i] release];
1483//%        _values[i] = [otherDictionary->_values[i] retain];
1484//%      }
1485//%    }
1486//%    if (_autocreator) {
1487//%      GPBAutocreatedDictionaryModified(_autocreator, self);
1488//%    }
1489//%  }
1490//%}
1491//%
1492//%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
1493//%  if (!object) {
1494//%    [NSException raise:NSInvalidArgumentException
1495//%                format:@"Attempting to add nil object to a Dictionary"];
1496//%  }
1497//%  int idx = (key ? 1 : 0);
1498//%  [_values[idx] release];
1499//%  _values[idx] = [object retain];
1500//%  if (_autocreator) {
1501//%    GPBAutocreatedDictionaryModified(_autocreator, self);
1502//%  }
1503//%}
1504//%
1505//%- (void)removeObjectForKey:(BOOL)aKey {
1506//%  int idx = (aKey ? 1 : 0);
1507//%  [_values[idx] release];
1508//%  _values[idx] = nil;
1509//%}
1510//%
1511//%- (void)removeAll {
1512//%  for (int i = 0; i < 2; ++i) {
1513//%    [_values[i] release];
1514//%    _values[i] = nil;
1515//%  }
1516//%}
1517//%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
1518//%%@
1519
1520
1521//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
1522// This block of code is generated, do not edit it directly.
1523
1524#pragma mark - UInt32 -> UInt32
1525
1526@implementation GPBUInt32UInt32Dictionary {
1527 @package
1528  NSMutableDictionary *_dictionary;
1529}
1530
1531+ (instancetype)dictionary {
1532  return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
1533}
1534
1535+ (instancetype)dictionaryWithUInt32:(uint32_t)value
1536                              forKey:(uint32_t)key {
1537  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
1538  // on to get the type correct.
1539  return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
1540                                                            forKeys:&key
1541                                                              count:1] autorelease];
1542}
1543
1544+ (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
1545                              forKeys:(const uint32_t [])keys
1546                                count:(NSUInteger)count {
1547  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
1548  // on to get the type correct.
1549  return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
1550                                                           forKeys:keys
1551                                                             count:count] autorelease];
1552}
1553
1554+ (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
1555  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
1556  // on to get the type correct.
1557  return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
1558}
1559
1560+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1561  return [[[self alloc] initWithCapacity:numItems] autorelease];
1562}
1563
1564- (instancetype)init {
1565  return [self initWithUInt32s:NULL forKeys:NULL count:0];
1566}
1567
1568- (instancetype)initWithUInt32s:(const uint32_t [])values
1569                        forKeys:(const uint32_t [])keys
1570                          count:(NSUInteger)count {
1571  self = [super init];
1572  if (self) {
1573    _dictionary = [[NSMutableDictionary alloc] init];
1574    if (count && values && keys) {
1575      for (NSUInteger i = 0; i < count; ++i) {
1576        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
1577      }
1578    }
1579  }
1580  return self;
1581}
1582
1583- (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
1584  self = [self initWithUInt32s:NULL forKeys:NULL count:0];
1585  if (self) {
1586    if (dictionary) {
1587      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
1588    }
1589  }
1590  return self;
1591}
1592
1593- (instancetype)initWithCapacity:(NSUInteger)numItems {
1594  #pragma unused(numItems)
1595  return [self initWithUInt32s:NULL forKeys:NULL count:0];
1596}
1597
1598- (void)dealloc {
1599  NSAssert(!_autocreator,
1600           @"%@: Autocreator must be cleared before release, autocreator: %@",
1601           [self class], _autocreator);
1602  [_dictionary release];
1603  [super dealloc];
1604}
1605
1606- (instancetype)copyWithZone:(NSZone *)zone {
1607  return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
1608}
1609
1610- (BOOL)isEqual:(id)other {
1611  if (self == other) {
1612    return YES;
1613  }
1614  if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
1615    return NO;
1616  }
1617  GPBUInt32UInt32Dictionary *otherDictionary = other;
1618  return [_dictionary isEqual:otherDictionary->_dictionary];
1619}
1620
1621- (NSUInteger)hash {
1622  return _dictionary.count;
1623}
1624
1625- (NSString *)description {
1626  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
1627}
1628
1629- (NSUInteger)count {
1630  return _dictionary.count;
1631}
1632
1633- (void)enumerateKeysAndUInt32sUsingBlock:
1634    (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
1635  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1636                                                   NSNumber *aValue,
1637                                                   BOOL *stop) {
1638      block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
1639  }];
1640}
1641
1642- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
1643  NSUInteger count = _dictionary.count;
1644  if (count == 0) {
1645    return 0;
1646  }
1647
1648  GPBDataType valueDataType = GPBGetFieldDataType(field);
1649  GPBDataType keyDataType = field.mapKeyDataType;
1650  __block size_t result = 0;
1651  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1652                                                   NSNumber *aValue,
1653                                                   BOOL *stop) {
1654    #pragma unused(stop)
1655    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1656    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
1657    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
1658  }];
1659  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
1660  result += tagSize * count;
1661  return result;
1662}
1663
1664- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
1665                         asField:(GPBFieldDescriptor *)field {
1666  GPBDataType valueDataType = GPBGetFieldDataType(field);
1667  GPBDataType keyDataType = field.mapKeyDataType;
1668  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
1669  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1670                                                   NSNumber *aValue,
1671                                                   BOOL *stop) {
1672    #pragma unused(stop)
1673    // Write the tag.
1674    [outputStream writeInt32NoTag:tag];
1675    // Write the size of the message.
1676    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1677    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
1678    [outputStream writeInt32NoTag:(int32_t)msgSize];
1679    // Write the fields.
1680    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1681    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
1682  }];
1683}
1684
1685- (void)setGPBGenericValue:(GPBGenericValue *)value
1686     forGPBGenericValueKey:(GPBGenericValue *)key {
1687  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
1688}
1689
1690- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
1691  [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
1692      #pragma unused(stop)
1693      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
1694  }];
1695}
1696
1697- (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
1698  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
1699  if (wrapped && value) {
1700    *value = [wrapped unsignedIntValue];
1701  }
1702  return (wrapped != NULL);
1703}
1704
1705- (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
1706  if (otherDictionary) {
1707    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
1708    if (_autocreator) {
1709      GPBAutocreatedDictionaryModified(_autocreator, self);
1710    }
1711  }
1712}
1713
1714- (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
1715  [_dictionary setObject:@(value) forKey:@(key)];
1716  if (_autocreator) {
1717    GPBAutocreatedDictionaryModified(_autocreator, self);
1718  }
1719}
1720
1721- (void)removeUInt32ForKey:(uint32_t)aKey {
1722  [_dictionary removeObjectForKey:@(aKey)];
1723}
1724
1725- (void)removeAll {
1726  [_dictionary removeAllObjects];
1727}
1728
1729@end
1730
1731#pragma mark - UInt32 -> Int32
1732
1733@implementation GPBUInt32Int32Dictionary {
1734 @package
1735  NSMutableDictionary *_dictionary;
1736}
1737
1738+ (instancetype)dictionary {
1739  return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
1740}
1741
1742+ (instancetype)dictionaryWithInt32:(int32_t)value
1743                             forKey:(uint32_t)key {
1744  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
1745  // on to get the type correct.
1746  return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
1747                                                          forKeys:&key
1748                                                            count:1] autorelease];
1749}
1750
1751+ (instancetype)dictionaryWithInt32s:(const int32_t [])values
1752                             forKeys:(const uint32_t [])keys
1753                               count:(NSUInteger)count {
1754  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
1755  // on to get the type correct.
1756  return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:values
1757                                                          forKeys:keys
1758                                                            count:count] autorelease];
1759}
1760
1761+ (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
1762  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
1763  // on to get the type correct.
1764  return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
1765}
1766
1767+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1768  return [[[self alloc] initWithCapacity:numItems] autorelease];
1769}
1770
1771- (instancetype)init {
1772  return [self initWithInt32s:NULL forKeys:NULL count:0];
1773}
1774
1775- (instancetype)initWithInt32s:(const int32_t [])values
1776                       forKeys:(const uint32_t [])keys
1777                         count:(NSUInteger)count {
1778  self = [super init];
1779  if (self) {
1780    _dictionary = [[NSMutableDictionary alloc] init];
1781    if (count && values && keys) {
1782      for (NSUInteger i = 0; i < count; ++i) {
1783        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
1784      }
1785    }
1786  }
1787  return self;
1788}
1789
1790- (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
1791  self = [self initWithInt32s:NULL forKeys:NULL count:0];
1792  if (self) {
1793    if (dictionary) {
1794      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
1795    }
1796  }
1797  return self;
1798}
1799
1800- (instancetype)initWithCapacity:(NSUInteger)numItems {
1801  #pragma unused(numItems)
1802  return [self initWithInt32s:NULL forKeys:NULL count:0];
1803}
1804
1805- (void)dealloc {
1806  NSAssert(!_autocreator,
1807           @"%@: Autocreator must be cleared before release, autocreator: %@",
1808           [self class], _autocreator);
1809  [_dictionary release];
1810  [super dealloc];
1811}
1812
1813- (instancetype)copyWithZone:(NSZone *)zone {
1814  return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
1815}
1816
1817- (BOOL)isEqual:(id)other {
1818  if (self == other) {
1819    return YES;
1820  }
1821  if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
1822    return NO;
1823  }
1824  GPBUInt32Int32Dictionary *otherDictionary = other;
1825  return [_dictionary isEqual:otherDictionary->_dictionary];
1826}
1827
1828- (NSUInteger)hash {
1829  return _dictionary.count;
1830}
1831
1832- (NSString *)description {
1833  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
1834}
1835
1836- (NSUInteger)count {
1837  return _dictionary.count;
1838}
1839
1840- (void)enumerateKeysAndInt32sUsingBlock:
1841    (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
1842  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1843                                                   NSNumber *aValue,
1844                                                   BOOL *stop) {
1845      block([aKey unsignedIntValue], [aValue intValue], stop);
1846  }];
1847}
1848
1849- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
1850  NSUInteger count = _dictionary.count;
1851  if (count == 0) {
1852    return 0;
1853  }
1854
1855  GPBDataType valueDataType = GPBGetFieldDataType(field);
1856  GPBDataType keyDataType = field.mapKeyDataType;
1857  __block size_t result = 0;
1858  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1859                                                   NSNumber *aValue,
1860                                                   BOOL *stop) {
1861    #pragma unused(stop)
1862    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1863    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
1864    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
1865  }];
1866  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
1867  result += tagSize * count;
1868  return result;
1869}
1870
1871- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
1872                         asField:(GPBFieldDescriptor *)field {
1873  GPBDataType valueDataType = GPBGetFieldDataType(field);
1874  GPBDataType keyDataType = field.mapKeyDataType;
1875  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
1876  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1877                                                   NSNumber *aValue,
1878                                                   BOOL *stop) {
1879    #pragma unused(stop)
1880    // Write the tag.
1881    [outputStream writeInt32NoTag:tag];
1882    // Write the size of the message.
1883    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1884    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
1885    [outputStream writeInt32NoTag:(int32_t)msgSize];
1886    // Write the fields.
1887    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1888    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
1889  }];
1890}
1891
1892- (void)setGPBGenericValue:(GPBGenericValue *)value
1893     forGPBGenericValueKey:(GPBGenericValue *)key {
1894  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
1895}
1896
1897- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
1898  [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
1899      #pragma unused(stop)
1900      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
1901  }];
1902}
1903
1904- (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
1905  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
1906  if (wrapped && value) {
1907    *value = [wrapped intValue];
1908  }
1909  return (wrapped != NULL);
1910}
1911
1912- (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
1913  if (otherDictionary) {
1914    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
1915    if (_autocreator) {
1916      GPBAutocreatedDictionaryModified(_autocreator, self);
1917    }
1918  }
1919}
1920
1921- (void)setInt32:(int32_t)value forKey:(uint32_t)key {
1922  [_dictionary setObject:@(value) forKey:@(key)];
1923  if (_autocreator) {
1924    GPBAutocreatedDictionaryModified(_autocreator, self);
1925  }
1926}
1927
1928- (void)removeInt32ForKey:(uint32_t)aKey {
1929  [_dictionary removeObjectForKey:@(aKey)];
1930}
1931
1932- (void)removeAll {
1933  [_dictionary removeAllObjects];
1934}
1935
1936@end
1937
1938#pragma mark - UInt32 -> UInt64
1939
1940@implementation GPBUInt32UInt64Dictionary {
1941 @package
1942  NSMutableDictionary *_dictionary;
1943}
1944
1945+ (instancetype)dictionary {
1946  return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
1947}
1948
1949+ (instancetype)dictionaryWithUInt64:(uint64_t)value
1950                              forKey:(uint32_t)key {
1951  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
1952  // on to get the type correct.
1953  return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
1954                                                            forKeys:&key
1955                                                              count:1] autorelease];
1956}
1957
1958+ (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
1959                              forKeys:(const uint32_t [])keys
1960                                count:(NSUInteger)count {
1961  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
1962  // on to get the type correct.
1963  return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
1964                                                           forKeys:keys
1965                                                             count:count] autorelease];
1966}
1967
1968+ (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
1969  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
1970  // on to get the type correct.
1971  return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
1972}
1973
1974+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1975  return [[[self alloc] initWithCapacity:numItems] autorelease];
1976}
1977
1978- (instancetype)init {
1979  return [self initWithUInt64s:NULL forKeys:NULL count:0];
1980}
1981
1982- (instancetype)initWithUInt64s:(const uint64_t [])values
1983                        forKeys:(const uint32_t [])keys
1984                          count:(NSUInteger)count {
1985  self = [super init];
1986  if (self) {
1987    _dictionary = [[NSMutableDictionary alloc] init];
1988    if (count && values && keys) {
1989      for (NSUInteger i = 0; i < count; ++i) {
1990        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
1991      }
1992    }
1993  }
1994  return self;
1995}
1996
1997- (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
1998  self = [self initWithUInt64s:NULL forKeys:NULL count:0];
1999  if (self) {
2000    if (dictionary) {
2001      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2002    }
2003  }
2004  return self;
2005}
2006
2007- (instancetype)initWithCapacity:(NSUInteger)numItems {
2008  #pragma unused(numItems)
2009  return [self initWithUInt64s:NULL forKeys:NULL count:0];
2010}
2011
2012- (void)dealloc {
2013  NSAssert(!_autocreator,
2014           @"%@: Autocreator must be cleared before release, autocreator: %@",
2015           [self class], _autocreator);
2016  [_dictionary release];
2017  [super dealloc];
2018}
2019
2020- (instancetype)copyWithZone:(NSZone *)zone {
2021  return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
2022}
2023
2024- (BOOL)isEqual:(id)other {
2025  if (self == other) {
2026    return YES;
2027  }
2028  if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
2029    return NO;
2030  }
2031  GPBUInt32UInt64Dictionary *otherDictionary = other;
2032  return [_dictionary isEqual:otherDictionary->_dictionary];
2033}
2034
2035- (NSUInteger)hash {
2036  return _dictionary.count;
2037}
2038
2039- (NSString *)description {
2040  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2041}
2042
2043- (NSUInteger)count {
2044  return _dictionary.count;
2045}
2046
2047- (void)enumerateKeysAndUInt64sUsingBlock:
2048    (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
2049  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2050                                                   NSNumber *aValue,
2051                                                   BOOL *stop) {
2052      block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
2053  }];
2054}
2055
2056- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2057  NSUInteger count = _dictionary.count;
2058  if (count == 0) {
2059    return 0;
2060  }
2061
2062  GPBDataType valueDataType = GPBGetFieldDataType(field);
2063  GPBDataType keyDataType = field.mapKeyDataType;
2064  __block size_t result = 0;
2065  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2066                                                   NSNumber *aValue,
2067                                                   BOOL *stop) {
2068    #pragma unused(stop)
2069    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2070    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
2071    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2072  }];
2073  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2074  result += tagSize * count;
2075  return result;
2076}
2077
2078- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2079                         asField:(GPBFieldDescriptor *)field {
2080  GPBDataType valueDataType = GPBGetFieldDataType(field);
2081  GPBDataType keyDataType = field.mapKeyDataType;
2082  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2083  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2084                                                   NSNumber *aValue,
2085                                                   BOOL *stop) {
2086    #pragma unused(stop)
2087    // Write the tag.
2088    [outputStream writeInt32NoTag:tag];
2089    // Write the size of the message.
2090    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2091    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
2092    [outputStream writeInt32NoTag:(int32_t)msgSize];
2093    // Write the fields.
2094    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2095    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
2096  }];
2097}
2098
2099- (void)setGPBGenericValue:(GPBGenericValue *)value
2100     forGPBGenericValueKey:(GPBGenericValue *)key {
2101  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
2102}
2103
2104- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2105  [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
2106      #pragma unused(stop)
2107      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
2108  }];
2109}
2110
2111- (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
2112  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2113  if (wrapped && value) {
2114    *value = [wrapped unsignedLongLongValue];
2115  }
2116  return (wrapped != NULL);
2117}
2118
2119- (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
2120  if (otherDictionary) {
2121    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2122    if (_autocreator) {
2123      GPBAutocreatedDictionaryModified(_autocreator, self);
2124    }
2125  }
2126}
2127
2128- (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
2129  [_dictionary setObject:@(value) forKey:@(key)];
2130  if (_autocreator) {
2131    GPBAutocreatedDictionaryModified(_autocreator, self);
2132  }
2133}
2134
2135- (void)removeUInt64ForKey:(uint32_t)aKey {
2136  [_dictionary removeObjectForKey:@(aKey)];
2137}
2138
2139- (void)removeAll {
2140  [_dictionary removeAllObjects];
2141}
2142
2143@end
2144
2145#pragma mark - UInt32 -> Int64
2146
2147@implementation GPBUInt32Int64Dictionary {
2148 @package
2149  NSMutableDictionary *_dictionary;
2150}
2151
2152+ (instancetype)dictionary {
2153  return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
2154}
2155
2156+ (instancetype)dictionaryWithInt64:(int64_t)value
2157                             forKey:(uint32_t)key {
2158  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
2159  // on to get the type correct.
2160  return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
2161                                                          forKeys:&key
2162                                                            count:1] autorelease];
2163}
2164
2165+ (instancetype)dictionaryWithInt64s:(const int64_t [])values
2166                             forKeys:(const uint32_t [])keys
2167                               count:(NSUInteger)count {
2168  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
2169  // on to get the type correct.
2170  return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:values
2171                                                          forKeys:keys
2172                                                            count:count] autorelease];
2173}
2174
2175+ (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
2176  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2177  // on to get the type correct.
2178  return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2179}
2180
2181+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2182  return [[[self alloc] initWithCapacity:numItems] autorelease];
2183}
2184
2185- (instancetype)init {
2186  return [self initWithInt64s:NULL forKeys:NULL count:0];
2187}
2188
2189- (instancetype)initWithInt64s:(const int64_t [])values
2190                       forKeys:(const uint32_t [])keys
2191                         count:(NSUInteger)count {
2192  self = [super init];
2193  if (self) {
2194    _dictionary = [[NSMutableDictionary alloc] init];
2195    if (count && values && keys) {
2196      for (NSUInteger i = 0; i < count; ++i) {
2197        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2198      }
2199    }
2200  }
2201  return self;
2202}
2203
2204- (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
2205  self = [self initWithInt64s:NULL forKeys:NULL count:0];
2206  if (self) {
2207    if (dictionary) {
2208      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2209    }
2210  }
2211  return self;
2212}
2213
2214- (instancetype)initWithCapacity:(NSUInteger)numItems {
2215  #pragma unused(numItems)
2216  return [self initWithInt64s:NULL forKeys:NULL count:0];
2217}
2218
2219- (void)dealloc {
2220  NSAssert(!_autocreator,
2221           @"%@: Autocreator must be cleared before release, autocreator: %@",
2222           [self class], _autocreator);
2223  [_dictionary release];
2224  [super dealloc];
2225}
2226
2227- (instancetype)copyWithZone:(NSZone *)zone {
2228  return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
2229}
2230
2231- (BOOL)isEqual:(id)other {
2232  if (self == other) {
2233    return YES;
2234  }
2235  if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
2236    return NO;
2237  }
2238  GPBUInt32Int64Dictionary *otherDictionary = other;
2239  return [_dictionary isEqual:otherDictionary->_dictionary];
2240}
2241
2242- (NSUInteger)hash {
2243  return _dictionary.count;
2244}
2245
2246- (NSString *)description {
2247  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2248}
2249
2250- (NSUInteger)count {
2251  return _dictionary.count;
2252}
2253
2254- (void)enumerateKeysAndInt64sUsingBlock:
2255    (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
2256  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2257                                                   NSNumber *aValue,
2258                                                   BOOL *stop) {
2259      block([aKey unsignedIntValue], [aValue longLongValue], stop);
2260  }];
2261}
2262
2263- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2264  NSUInteger count = _dictionary.count;
2265  if (count == 0) {
2266    return 0;
2267  }
2268
2269  GPBDataType valueDataType = GPBGetFieldDataType(field);
2270  GPBDataType keyDataType = field.mapKeyDataType;
2271  __block size_t result = 0;
2272  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2273                                                   NSNumber *aValue,
2274                                                   BOOL *stop) {
2275    #pragma unused(stop)
2276    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2277    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
2278    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2279  }];
2280  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2281  result += tagSize * count;
2282  return result;
2283}
2284
2285- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2286                         asField:(GPBFieldDescriptor *)field {
2287  GPBDataType valueDataType = GPBGetFieldDataType(field);
2288  GPBDataType keyDataType = field.mapKeyDataType;
2289  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2290  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2291                                                   NSNumber *aValue,
2292                                                   BOOL *stop) {
2293    #pragma unused(stop)
2294    // Write the tag.
2295    [outputStream writeInt32NoTag:tag];
2296    // Write the size of the message.
2297    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2298    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
2299    [outputStream writeInt32NoTag:(int32_t)msgSize];
2300    // Write the fields.
2301    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2302    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
2303  }];
2304}
2305
2306- (void)setGPBGenericValue:(GPBGenericValue *)value
2307     forGPBGenericValueKey:(GPBGenericValue *)key {
2308  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
2309}
2310
2311- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2312  [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
2313      #pragma unused(stop)
2314      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
2315  }];
2316}
2317
2318- (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
2319  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2320  if (wrapped && value) {
2321    *value = [wrapped longLongValue];
2322  }
2323  return (wrapped != NULL);
2324}
2325
2326- (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
2327  if (otherDictionary) {
2328    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2329    if (_autocreator) {
2330      GPBAutocreatedDictionaryModified(_autocreator, self);
2331    }
2332  }
2333}
2334
2335- (void)setInt64:(int64_t)value forKey:(uint32_t)key {
2336  [_dictionary setObject:@(value) forKey:@(key)];
2337  if (_autocreator) {
2338    GPBAutocreatedDictionaryModified(_autocreator, self);
2339  }
2340}
2341
2342- (void)removeInt64ForKey:(uint32_t)aKey {
2343  [_dictionary removeObjectForKey:@(aKey)];
2344}
2345
2346- (void)removeAll {
2347  [_dictionary removeAllObjects];
2348}
2349
2350@end
2351
2352#pragma mark - UInt32 -> Bool
2353
2354@implementation GPBUInt32BoolDictionary {
2355 @package
2356  NSMutableDictionary *_dictionary;
2357}
2358
2359+ (instancetype)dictionary {
2360  return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
2361}
2362
2363+ (instancetype)dictionaryWithBool:(BOOL)value
2364                            forKey:(uint32_t)key {
2365  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
2366  // on to get the type correct.
2367  return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:&value
2368                                                        forKeys:&key
2369                                                          count:1] autorelease];
2370}
2371
2372+ (instancetype)dictionaryWithBools:(const BOOL [])values
2373                            forKeys:(const uint32_t [])keys
2374                              count:(NSUInteger)count {
2375  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
2376  // on to get the type correct.
2377  return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:values
2378                                                         forKeys:keys
2379                                                           count:count] autorelease];
2380}
2381
2382+ (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
2383  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2384  // on to get the type correct.
2385  return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2386}
2387
2388+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2389  return [[[self alloc] initWithCapacity:numItems] autorelease];
2390}
2391
2392- (instancetype)init {
2393  return [self initWithBools:NULL forKeys:NULL count:0];
2394}
2395
2396- (instancetype)initWithBools:(const BOOL [])values
2397                      forKeys:(const uint32_t [])keys
2398                        count:(NSUInteger)count {
2399  self = [super init];
2400  if (self) {
2401    _dictionary = [[NSMutableDictionary alloc] init];
2402    if (count && values && keys) {
2403      for (NSUInteger i = 0; i < count; ++i) {
2404        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2405      }
2406    }
2407  }
2408  return self;
2409}
2410
2411- (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
2412  self = [self initWithBools:NULL forKeys:NULL count:0];
2413  if (self) {
2414    if (dictionary) {
2415      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2416    }
2417  }
2418  return self;
2419}
2420
2421- (instancetype)initWithCapacity:(NSUInteger)numItems {
2422  #pragma unused(numItems)
2423  return [self initWithBools:NULL forKeys:NULL count:0];
2424}
2425
2426- (void)dealloc {
2427  NSAssert(!_autocreator,
2428           @"%@: Autocreator must be cleared before release, autocreator: %@",
2429           [self class], _autocreator);
2430  [_dictionary release];
2431  [super dealloc];
2432}
2433
2434- (instancetype)copyWithZone:(NSZone *)zone {
2435  return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
2436}
2437
2438- (BOOL)isEqual:(id)other {
2439  if (self == other) {
2440    return YES;
2441  }
2442  if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
2443    return NO;
2444  }
2445  GPBUInt32BoolDictionary *otherDictionary = other;
2446  return [_dictionary isEqual:otherDictionary->_dictionary];
2447}
2448
2449- (NSUInteger)hash {
2450  return _dictionary.count;
2451}
2452
2453- (NSString *)description {
2454  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2455}
2456
2457- (NSUInteger)count {
2458  return _dictionary.count;
2459}
2460
2461- (void)enumerateKeysAndBoolsUsingBlock:
2462    (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
2463  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2464                                                   NSNumber *aValue,
2465                                                   BOOL *stop) {
2466      block([aKey unsignedIntValue], [aValue boolValue], stop);
2467  }];
2468}
2469
2470- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2471  NSUInteger count = _dictionary.count;
2472  if (count == 0) {
2473    return 0;
2474  }
2475
2476  GPBDataType valueDataType = GPBGetFieldDataType(field);
2477  GPBDataType keyDataType = field.mapKeyDataType;
2478  __block size_t result = 0;
2479  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2480                                                   NSNumber *aValue,
2481                                                   BOOL *stop) {
2482    #pragma unused(stop)
2483    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2484    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
2485    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2486  }];
2487  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2488  result += tagSize * count;
2489  return result;
2490}
2491
2492- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2493                         asField:(GPBFieldDescriptor *)field {
2494  GPBDataType valueDataType = GPBGetFieldDataType(field);
2495  GPBDataType keyDataType = field.mapKeyDataType;
2496  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2497  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2498                                                   NSNumber *aValue,
2499                                                   BOOL *stop) {
2500    #pragma unused(stop)
2501    // Write the tag.
2502    [outputStream writeInt32NoTag:tag];
2503    // Write the size of the message.
2504    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2505    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
2506    [outputStream writeInt32NoTag:(int32_t)msgSize];
2507    // Write the fields.
2508    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2509    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
2510  }];
2511}
2512
2513- (void)setGPBGenericValue:(GPBGenericValue *)value
2514     forGPBGenericValueKey:(GPBGenericValue *)key {
2515  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
2516}
2517
2518- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2519  [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
2520      #pragma unused(stop)
2521      block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
2522  }];
2523}
2524
2525- (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
2526  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2527  if (wrapped && value) {
2528    *value = [wrapped boolValue];
2529  }
2530  return (wrapped != NULL);
2531}
2532
2533- (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
2534  if (otherDictionary) {
2535    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2536    if (_autocreator) {
2537      GPBAutocreatedDictionaryModified(_autocreator, self);
2538    }
2539  }
2540}
2541
2542- (void)setBool:(BOOL)value forKey:(uint32_t)key {
2543  [_dictionary setObject:@(value) forKey:@(key)];
2544  if (_autocreator) {
2545    GPBAutocreatedDictionaryModified(_autocreator, self);
2546  }
2547}
2548
2549- (void)removeBoolForKey:(uint32_t)aKey {
2550  [_dictionary removeObjectForKey:@(aKey)];
2551}
2552
2553- (void)removeAll {
2554  [_dictionary removeAllObjects];
2555}
2556
2557@end
2558
2559#pragma mark - UInt32 -> Float
2560
2561@implementation GPBUInt32FloatDictionary {
2562 @package
2563  NSMutableDictionary *_dictionary;
2564}
2565
2566+ (instancetype)dictionary {
2567  return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
2568}
2569
2570+ (instancetype)dictionaryWithFloat:(float)value
2571                             forKey:(uint32_t)key {
2572  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
2573  // on to get the type correct.
2574  return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:&value
2575                                                          forKeys:&key
2576                                                            count:1] autorelease];
2577}
2578
2579+ (instancetype)dictionaryWithFloats:(const float [])values
2580                             forKeys:(const uint32_t [])keys
2581                               count:(NSUInteger)count {
2582  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
2583  // on to get the type correct.
2584  return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:values
2585                                                          forKeys:keys
2586                                                            count:count] autorelease];
2587}
2588
2589+ (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
2590  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2591  // on to get the type correct.
2592  return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2593}
2594
2595+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2596  return [[[self alloc] initWithCapacity:numItems] autorelease];
2597}
2598
2599- (instancetype)init {
2600  return [self initWithFloats:NULL forKeys:NULL count:0];
2601}
2602
2603- (instancetype)initWithFloats:(const float [])values
2604                       forKeys:(const uint32_t [])keys
2605                         count:(NSUInteger)count {
2606  self = [super init];
2607  if (self) {
2608    _dictionary = [[NSMutableDictionary alloc] init];
2609    if (count && values && keys) {
2610      for (NSUInteger i = 0; i < count; ++i) {
2611        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2612      }
2613    }
2614  }
2615  return self;
2616}
2617
2618- (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
2619  self = [self initWithFloats:NULL forKeys:NULL count:0];
2620  if (self) {
2621    if (dictionary) {
2622      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2623    }
2624  }
2625  return self;
2626}
2627
2628- (instancetype)initWithCapacity:(NSUInteger)numItems {
2629  #pragma unused(numItems)
2630  return [self initWithFloats:NULL forKeys:NULL count:0];
2631}
2632
2633- (void)dealloc {
2634  NSAssert(!_autocreator,
2635           @"%@: Autocreator must be cleared before release, autocreator: %@",
2636           [self class], _autocreator);
2637  [_dictionary release];
2638  [super dealloc];
2639}
2640
2641- (instancetype)copyWithZone:(NSZone *)zone {
2642  return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
2643}
2644
2645- (BOOL)isEqual:(id)other {
2646  if (self == other) {
2647    return YES;
2648  }
2649  if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
2650    return NO;
2651  }
2652  GPBUInt32FloatDictionary *otherDictionary = other;
2653  return [_dictionary isEqual:otherDictionary->_dictionary];
2654}
2655
2656- (NSUInteger)hash {
2657  return _dictionary.count;
2658}
2659
2660- (NSString *)description {
2661  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2662}
2663
2664- (NSUInteger)count {
2665  return _dictionary.count;
2666}
2667
2668- (void)enumerateKeysAndFloatsUsingBlock:
2669    (void (^)(uint32_t key, float value, BOOL *stop))block {
2670  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2671                                                   NSNumber *aValue,
2672                                                   BOOL *stop) {
2673      block([aKey unsignedIntValue], [aValue floatValue], stop);
2674  }];
2675}
2676
2677- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2678  NSUInteger count = _dictionary.count;
2679  if (count == 0) {
2680    return 0;
2681  }
2682
2683  GPBDataType valueDataType = GPBGetFieldDataType(field);
2684  GPBDataType keyDataType = field.mapKeyDataType;
2685  __block size_t result = 0;
2686  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2687                                                   NSNumber *aValue,
2688                                                   BOOL *stop) {
2689    #pragma unused(stop)
2690    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2691    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
2692    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2693  }];
2694  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2695  result += tagSize * count;
2696  return result;
2697}
2698
2699- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2700                         asField:(GPBFieldDescriptor *)field {
2701  GPBDataType valueDataType = GPBGetFieldDataType(field);
2702  GPBDataType keyDataType = field.mapKeyDataType;
2703  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2704  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2705                                                   NSNumber *aValue,
2706                                                   BOOL *stop) {
2707    #pragma unused(stop)
2708    // Write the tag.
2709    [outputStream writeInt32NoTag:tag];
2710    // Write the size of the message.
2711    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2712    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
2713    [outputStream writeInt32NoTag:(int32_t)msgSize];
2714    // Write the fields.
2715    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2716    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
2717  }];
2718}
2719
2720- (void)setGPBGenericValue:(GPBGenericValue *)value
2721     forGPBGenericValueKey:(GPBGenericValue *)key {
2722  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
2723}
2724
2725- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2726  [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) {
2727      #pragma unused(stop)
2728      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
2729  }];
2730}
2731
2732- (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
2733  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2734  if (wrapped && value) {
2735    *value = [wrapped floatValue];
2736  }
2737  return (wrapped != NULL);
2738}
2739
2740- (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
2741  if (otherDictionary) {
2742    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2743    if (_autocreator) {
2744      GPBAutocreatedDictionaryModified(_autocreator, self);
2745    }
2746  }
2747}
2748
2749- (void)setFloat:(float)value forKey:(uint32_t)key {
2750  [_dictionary setObject:@(value) forKey:@(key)];
2751  if (_autocreator) {
2752    GPBAutocreatedDictionaryModified(_autocreator, self);
2753  }
2754}
2755
2756- (void)removeFloatForKey:(uint32_t)aKey {
2757  [_dictionary removeObjectForKey:@(aKey)];
2758}
2759
2760- (void)removeAll {
2761  [_dictionary removeAllObjects];
2762}
2763
2764@end
2765
2766#pragma mark - UInt32 -> Double
2767
2768@implementation GPBUInt32DoubleDictionary {
2769 @package
2770  NSMutableDictionary *_dictionary;
2771}
2772
2773+ (instancetype)dictionary {
2774  return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
2775}
2776
2777+ (instancetype)dictionaryWithDouble:(double)value
2778                              forKey:(uint32_t)key {
2779  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
2780  // on to get the type correct.
2781  return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
2782                                                            forKeys:&key
2783                                                              count:1] autorelease];
2784}
2785
2786+ (instancetype)dictionaryWithDoubles:(const double [])values
2787                              forKeys:(const uint32_t [])keys
2788                                count:(NSUInteger)count {
2789  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
2790  // on to get the type correct.
2791  return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:values
2792                                                           forKeys:keys
2793                                                             count:count] autorelease];
2794}
2795
2796+ (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
2797  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2798  // on to get the type correct.
2799  return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2800}
2801
2802+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2803  return [[[self alloc] initWithCapacity:numItems] autorelease];
2804}
2805
2806- (instancetype)init {
2807  return [self initWithDoubles:NULL forKeys:NULL count:0];
2808}
2809
2810- (instancetype)initWithDoubles:(const double [])values
2811                        forKeys:(const uint32_t [])keys
2812                          count:(NSUInteger)count {
2813  self = [super init];
2814  if (self) {
2815    _dictionary = [[NSMutableDictionary alloc] init];
2816    if (count && values && keys) {
2817      for (NSUInteger i = 0; i < count; ++i) {
2818        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2819      }
2820    }
2821  }
2822  return self;
2823}
2824
2825- (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
2826  self = [self initWithDoubles:NULL forKeys:NULL count:0];
2827  if (self) {
2828    if (dictionary) {
2829      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2830    }
2831  }
2832  return self;
2833}
2834
2835- (instancetype)initWithCapacity:(NSUInteger)numItems {
2836  #pragma unused(numItems)
2837  return [self initWithDoubles:NULL forKeys:NULL count:0];
2838}
2839
2840- (void)dealloc {
2841  NSAssert(!_autocreator,
2842           @"%@: Autocreator must be cleared before release, autocreator: %@",
2843           [self class], _autocreator);
2844  [_dictionary release];
2845  [super dealloc];
2846}
2847
2848- (instancetype)copyWithZone:(NSZone *)zone {
2849  return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
2850}
2851
2852- (BOOL)isEqual:(id)other {
2853  if (self == other) {
2854    return YES;
2855  }
2856  if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
2857    return NO;
2858  }
2859  GPBUInt32DoubleDictionary *otherDictionary = other;
2860  return [_dictionary isEqual:otherDictionary->_dictionary];
2861}
2862
2863- (NSUInteger)hash {
2864  return _dictionary.count;
2865}
2866
2867- (NSString *)description {
2868  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2869}
2870
2871- (NSUInteger)count {
2872  return _dictionary.count;
2873}
2874
2875- (void)enumerateKeysAndDoublesUsingBlock:
2876    (void (^)(uint32_t key, double value, BOOL *stop))block {
2877  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2878                                                   NSNumber *aValue,
2879                                                   BOOL *stop) {
2880      block([aKey unsignedIntValue], [aValue doubleValue], stop);
2881  }];
2882}
2883
2884- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2885  NSUInteger count = _dictionary.count;
2886  if (count == 0) {
2887    return 0;
2888  }
2889
2890  GPBDataType valueDataType = GPBGetFieldDataType(field);
2891  GPBDataType keyDataType = field.mapKeyDataType;
2892  __block size_t result = 0;
2893  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2894                                                   NSNumber *aValue,
2895                                                   BOOL *stop) {
2896    #pragma unused(stop)
2897    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2898    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
2899    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2900  }];
2901  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2902  result += tagSize * count;
2903  return result;
2904}
2905
2906- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2907                         asField:(GPBFieldDescriptor *)field {
2908  GPBDataType valueDataType = GPBGetFieldDataType(field);
2909  GPBDataType keyDataType = field.mapKeyDataType;
2910  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2911  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2912                                                   NSNumber *aValue,
2913                                                   BOOL *stop) {
2914    #pragma unused(stop)
2915    // Write the tag.
2916    [outputStream writeInt32NoTag:tag];
2917    // Write the size of the message.
2918    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2919    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
2920    [outputStream writeInt32NoTag:(int32_t)msgSize];
2921    // Write the fields.
2922    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2923    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
2924  }];
2925}
2926
2927- (void)setGPBGenericValue:(GPBGenericValue *)value
2928     forGPBGenericValueKey:(GPBGenericValue *)key {
2929  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
2930}
2931
2932- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2933  [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
2934      #pragma unused(stop)
2935      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
2936  }];
2937}
2938
2939- (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
2940  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2941  if (wrapped && value) {
2942    *value = [wrapped doubleValue];
2943  }
2944  return (wrapped != NULL);
2945}
2946
2947- (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
2948  if (otherDictionary) {
2949    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2950    if (_autocreator) {
2951      GPBAutocreatedDictionaryModified(_autocreator, self);
2952    }
2953  }
2954}
2955
2956- (void)setDouble:(double)value forKey:(uint32_t)key {
2957  [_dictionary setObject:@(value) forKey:@(key)];
2958  if (_autocreator) {
2959    GPBAutocreatedDictionaryModified(_autocreator, self);
2960  }
2961}
2962
2963- (void)removeDoubleForKey:(uint32_t)aKey {
2964  [_dictionary removeObjectForKey:@(aKey)];
2965}
2966
2967- (void)removeAll {
2968  [_dictionary removeAllObjects];
2969}
2970
2971@end
2972
2973#pragma mark - UInt32 -> Enum
2974
2975@implementation GPBUInt32EnumDictionary {
2976 @package
2977  NSMutableDictionary *_dictionary;
2978  GPBEnumValidationFunc _validationFunc;
2979}
2980
2981@synthesize validationFunc = _validationFunc;
2982
2983+ (instancetype)dictionary {
2984  return [[[self alloc] initWithValidationFunction:NULL
2985                                         rawValues:NULL
2986                                           forKeys:NULL
2987                                             count:0] autorelease];
2988}
2989
2990+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
2991  return [[[self alloc] initWithValidationFunction:func
2992                                         rawValues:NULL
2993                                           forKeys:NULL
2994                                             count:0] autorelease];
2995}
2996
2997+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
2998                                        rawValue:(int32_t)rawValue
2999                                          forKey:(uint32_t)key {
3000  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3001  // on to get the type correct.
3002  return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
3003                                                                   rawValues:&rawValue
3004                                                                     forKeys:&key
3005                                                                       count:1] autorelease];
3006}
3007
3008+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
3009                                       rawValues:(const int32_t [])rawValues
3010                                         forKeys:(const uint32_t [])keys
3011                                           count:(NSUInteger)count {
3012  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3013  // on to get the type correct.
3014  return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
3015                                                                   rawValues:rawValues
3016                                                                     forKeys:keys
3017                                                                       count:count] autorelease];
3018}
3019
3020+ (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
3021  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3022  // on to get the type correct.
3023  return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3024}
3025
3026+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
3027                                        capacity:(NSUInteger)numItems {
3028  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
3029}
3030
3031- (instancetype)init {
3032  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
3033}
3034
3035- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
3036  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
3037}
3038
3039- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
3040                                 rawValues:(const int32_t [])rawValues
3041                                   forKeys:(const uint32_t [])keys
3042                                     count:(NSUInteger)count {
3043  self = [super init];
3044  if (self) {
3045    _dictionary = [[NSMutableDictionary alloc] init];
3046    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
3047    if (count && rawValues && keys) {
3048      for (NSUInteger i = 0; i < count; ++i) {
3049        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
3050      }
3051    }
3052  }
3053  return self;
3054}
3055
3056- (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
3057  self = [self initWithValidationFunction:dictionary.validationFunc
3058                                rawValues:NULL
3059                                  forKeys:NULL
3060                                    count:0];
3061  if (self) {
3062    if (dictionary) {
3063      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3064    }
3065  }
3066  return self;
3067}
3068
3069- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
3070                                  capacity:(NSUInteger)numItems {
3071  #pragma unused(numItems)
3072  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
3073}
3074
3075- (void)dealloc {
3076  NSAssert(!_autocreator,
3077           @"%@: Autocreator must be cleared before release, autocreator: %@",
3078           [self class], _autocreator);
3079  [_dictionary release];
3080  [super dealloc];
3081}
3082
3083- (instancetype)copyWithZone:(NSZone *)zone {
3084  return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
3085}
3086
3087- (BOOL)isEqual:(id)other {
3088  if (self == other) {
3089    return YES;
3090  }
3091  if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
3092    return NO;
3093  }
3094  GPBUInt32EnumDictionary *otherDictionary = other;
3095  return [_dictionary isEqual:otherDictionary->_dictionary];
3096}
3097
3098- (NSUInteger)hash {
3099  return _dictionary.count;
3100}
3101
3102- (NSString *)description {
3103  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3104}
3105
3106- (NSUInteger)count {
3107  return _dictionary.count;
3108}
3109
3110- (void)enumerateKeysAndRawValuesUsingBlock:
3111    (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
3112  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3113                                                   NSNumber *aValue,
3114                                                   BOOL *stop) {
3115      block([aKey unsignedIntValue], [aValue intValue], stop);
3116  }];
3117}
3118
3119- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3120  NSUInteger count = _dictionary.count;
3121  if (count == 0) {
3122    return 0;
3123  }
3124
3125  GPBDataType valueDataType = GPBGetFieldDataType(field);
3126  GPBDataType keyDataType = field.mapKeyDataType;
3127  __block size_t result = 0;
3128  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3129                                                   NSNumber *aValue,
3130                                                   BOOL *stop) {
3131    #pragma unused(stop)
3132    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3133    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3134    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3135  }];
3136  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3137  result += tagSize * count;
3138  return result;
3139}
3140
3141- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3142                         asField:(GPBFieldDescriptor *)field {
3143  GPBDataType valueDataType = GPBGetFieldDataType(field);
3144  GPBDataType keyDataType = field.mapKeyDataType;
3145  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3146  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3147                                                   NSNumber *aValue,
3148                                                   BOOL *stop) {
3149    #pragma unused(stop)
3150    // Write the tag.
3151    [outputStream writeInt32NoTag:tag];
3152    // Write the size of the message.
3153    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3154    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3155    [outputStream writeInt32NoTag:(int32_t)msgSize];
3156    // Write the fields.
3157    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3158    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
3159  }];
3160}
3161
3162- (NSData *)serializedDataForUnknownValue:(int32_t)value
3163                                   forKey:(GPBGenericValue *)key
3164                              keyDataType:(GPBDataType)keyDataType {
3165  size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
3166  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
3167  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
3168  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
3169  WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
3170  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
3171  [outputStream release];
3172  return data;
3173}
3174- (void)setGPBGenericValue:(GPBGenericValue *)value
3175     forGPBGenericValueKey:(GPBGenericValue *)key {
3176  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
3177}
3178
3179- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3180  [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
3181      #pragma unused(stop)
3182      block([NSString stringWithFormat:@"%u", key], @(value));
3183  }];
3184}
3185
3186- (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
3187  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3188  if (wrapped && value) {
3189    int32_t result = [wrapped intValue];
3190    if (!_validationFunc(result)) {
3191      result = kGPBUnrecognizedEnumeratorValue;
3192    }
3193    *value = result;
3194  }
3195  return (wrapped != NULL);
3196}
3197
3198- (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
3199  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3200  if (wrapped && rawValue) {
3201    *rawValue = [wrapped intValue];
3202  }
3203  return (wrapped != NULL);
3204}
3205
3206- (void)enumerateKeysAndEnumsUsingBlock:
3207    (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
3208  GPBEnumValidationFunc func = _validationFunc;
3209  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3210                                                   NSNumber *aValue,
3211                                                   BOOL *stop) {
3212      int32_t unwrapped = [aValue intValue];
3213      if (!func(unwrapped)) {
3214        unwrapped = kGPBUnrecognizedEnumeratorValue;
3215      }
3216      block([aKey unsignedIntValue], unwrapped, stop);
3217  }];
3218}
3219
3220- (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
3221  if (otherDictionary) {
3222    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3223    if (_autocreator) {
3224      GPBAutocreatedDictionaryModified(_autocreator, self);
3225    }
3226  }
3227}
3228
3229- (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
3230  [_dictionary setObject:@(value) forKey:@(key)];
3231  if (_autocreator) {
3232    GPBAutocreatedDictionaryModified(_autocreator, self);
3233  }
3234}
3235
3236- (void)removeEnumForKey:(uint32_t)aKey {
3237  [_dictionary removeObjectForKey:@(aKey)];
3238}
3239
3240- (void)removeAll {
3241  [_dictionary removeAllObjects];
3242}
3243
3244- (void)setEnum:(int32_t)value forKey:(uint32_t)key {
3245  if (!_validationFunc(value)) {
3246    [NSException raise:NSInvalidArgumentException
3247                format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
3248                       value];
3249  }
3250
3251  [_dictionary setObject:@(value) forKey:@(key)];
3252  if (_autocreator) {
3253    GPBAutocreatedDictionaryModified(_autocreator, self);
3254  }
3255}
3256
3257@end
3258
3259#pragma mark - UInt32 -> Object
3260
3261@implementation GPBUInt32ObjectDictionary {
3262 @package
3263  NSMutableDictionary *_dictionary;
3264}
3265
3266+ (instancetype)dictionary {
3267  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
3268}
3269
3270+ (instancetype)dictionaryWithObject:(id)object
3271                              forKey:(uint32_t)key {
3272  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
3273  // on to get the type correct.
3274  return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
3275                                                            forKeys:&key
3276                                                              count:1] autorelease];
3277}
3278
3279+ (instancetype)dictionaryWithObjects:(const id [])objects
3280                              forKeys:(const uint32_t [])keys
3281                                count:(NSUInteger)count {
3282  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
3283  // on to get the type correct.
3284  return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
3285                                                           forKeys:keys
3286                                                             count:count] autorelease];
3287}
3288
3289+ (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
3290  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3291  // on to get the type correct.
3292  return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3293}
3294
3295+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3296  return [[[self alloc] initWithCapacity:numItems] autorelease];
3297}
3298
3299- (instancetype)init {
3300  return [self initWithObjects:NULL forKeys:NULL count:0];
3301}
3302
3303- (instancetype)initWithObjects:(const id [])objects
3304                        forKeys:(const uint32_t [])keys
3305                          count:(NSUInteger)count {
3306  self = [super init];
3307  if (self) {
3308    _dictionary = [[NSMutableDictionary alloc] init];
3309    if (count && objects && keys) {
3310      for (NSUInteger i = 0; i < count; ++i) {
3311        if (!objects[i]) {
3312          [NSException raise:NSInvalidArgumentException
3313                      format:@"Attempting to add nil object to a Dictionary"];
3314        }
3315        [_dictionary setObject:objects[i] forKey:@(keys[i])];
3316      }
3317    }
3318  }
3319  return self;
3320}
3321
3322- (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
3323  self = [self initWithObjects:NULL forKeys:NULL count:0];
3324  if (self) {
3325    if (dictionary) {
3326      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3327    }
3328  }
3329  return self;
3330}
3331
3332- (instancetype)initWithCapacity:(NSUInteger)numItems {
3333  #pragma unused(numItems)
3334  return [self initWithObjects:NULL forKeys:NULL count:0];
3335}
3336
3337- (void)dealloc {
3338  NSAssert(!_autocreator,
3339           @"%@: Autocreator must be cleared before release, autocreator: %@",
3340           [self class], _autocreator);
3341  [_dictionary release];
3342  [super dealloc];
3343}
3344
3345- (instancetype)copyWithZone:(NSZone *)zone {
3346  return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
3347}
3348
3349- (BOOL)isEqual:(id)other {
3350  if (self == other) {
3351    return YES;
3352  }
3353  if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
3354    return NO;
3355  }
3356  GPBUInt32ObjectDictionary *otherDictionary = other;
3357  return [_dictionary isEqual:otherDictionary->_dictionary];
3358}
3359
3360- (NSUInteger)hash {
3361  return _dictionary.count;
3362}
3363
3364- (NSString *)description {
3365  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3366}
3367
3368- (NSUInteger)count {
3369  return _dictionary.count;
3370}
3371
3372- (void)enumerateKeysAndObjectsUsingBlock:
3373    (void (^)(uint32_t key, id object, BOOL *stop))block {
3374  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3375                                                   id aObject,
3376                                                   BOOL *stop) {
3377      block([aKey unsignedIntValue], aObject, stop);
3378  }];
3379}
3380
3381- (BOOL)isInitialized {
3382  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
3383    if (!msg.initialized) {
3384      return NO;
3385    }
3386  }
3387  return YES;
3388}
3389
3390- (instancetype)deepCopyWithZone:(NSZone *)zone {
3391  GPBUInt32ObjectDictionary *newDict =
3392      [[GPBUInt32ObjectDictionary alloc] init];
3393  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
3394                                                   GPBMessage *msg,
3395                                                   BOOL *stop) {
3396    #pragma unused(stop)
3397    GPBMessage *copiedMsg = [msg copyWithZone:zone];
3398    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
3399    [copiedMsg release];
3400  }];
3401  return newDict;
3402}
3403
3404- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3405  NSUInteger count = _dictionary.count;
3406  if (count == 0) {
3407    return 0;
3408  }
3409
3410  GPBDataType valueDataType = GPBGetFieldDataType(field);
3411  GPBDataType keyDataType = field.mapKeyDataType;
3412  __block size_t result = 0;
3413  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3414                                                   id aObject,
3415                                                   BOOL *stop) {
3416    #pragma unused(stop)
3417    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3418    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
3419    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3420  }];
3421  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3422  result += tagSize * count;
3423  return result;
3424}
3425
3426- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3427                         asField:(GPBFieldDescriptor *)field {
3428  GPBDataType valueDataType = GPBGetFieldDataType(field);
3429  GPBDataType keyDataType = field.mapKeyDataType;
3430  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3431  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3432                                                   id aObject,
3433                                                   BOOL *stop) {
3434    #pragma unused(stop)
3435    // Write the tag.
3436    [outputStream writeInt32NoTag:tag];
3437    // Write the size of the message.
3438    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3439    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
3440    [outputStream writeInt32NoTag:(int32_t)msgSize];
3441    // Write the fields.
3442    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3443    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
3444  }];
3445}
3446
3447- (void)setGPBGenericValue:(GPBGenericValue *)value
3448     forGPBGenericValueKey:(GPBGenericValue *)key {
3449  [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
3450}
3451
3452- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3453  [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
3454      #pragma unused(stop)
3455      block([NSString stringWithFormat:@"%u", key], object);
3456  }];
3457}
3458
3459- (id)objectForKey:(uint32_t)key {
3460  id result = [_dictionary objectForKey:@(key)];
3461  return result;
3462}
3463
3464- (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
3465  if (otherDictionary) {
3466    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3467    if (_autocreator) {
3468      GPBAutocreatedDictionaryModified(_autocreator, self);
3469    }
3470  }
3471}
3472
3473- (void)setObject:(id)object forKey:(uint32_t)key {
3474  if (!object) {
3475    [NSException raise:NSInvalidArgumentException
3476                format:@"Attempting to add nil object to a Dictionary"];
3477  }
3478  [_dictionary setObject:object forKey:@(key)];
3479  if (_autocreator) {
3480    GPBAutocreatedDictionaryModified(_autocreator, self);
3481  }
3482}
3483
3484- (void)removeObjectForKey:(uint32_t)aKey {
3485  [_dictionary removeObjectForKey:@(aKey)];
3486}
3487
3488- (void)removeAll {
3489  [_dictionary removeAllObjects];
3490}
3491
3492@end
3493
3494//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
3495// This block of code is generated, do not edit it directly.
3496
3497#pragma mark - Int32 -> UInt32
3498
3499@implementation GPBInt32UInt32Dictionary {
3500 @package
3501  NSMutableDictionary *_dictionary;
3502}
3503
3504+ (instancetype)dictionary {
3505  return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
3506}
3507
3508+ (instancetype)dictionaryWithUInt32:(uint32_t)value
3509                              forKey:(int32_t)key {
3510  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
3511  // on to get the type correct.
3512  return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
3513                                                           forKeys:&key
3514                                                             count:1] autorelease];
3515}
3516
3517+ (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
3518                              forKeys:(const int32_t [])keys
3519                                count:(NSUInteger)count {
3520  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
3521  // on to get the type correct.
3522  return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
3523                                                          forKeys:keys
3524                                                            count:count] autorelease];
3525}
3526
3527+ (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
3528  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3529  // on to get the type correct.
3530  return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3531}
3532
3533+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3534  return [[[self alloc] initWithCapacity:numItems] autorelease];
3535}
3536
3537- (instancetype)init {
3538  return [self initWithUInt32s:NULL forKeys:NULL count:0];
3539}
3540
3541- (instancetype)initWithUInt32s:(const uint32_t [])values
3542                        forKeys:(const int32_t [])keys
3543                          count:(NSUInteger)count {
3544  self = [super init];
3545  if (self) {
3546    _dictionary = [[NSMutableDictionary alloc] init];
3547    if (count && values && keys) {
3548      for (NSUInteger i = 0; i < count; ++i) {
3549        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
3550      }
3551    }
3552  }
3553  return self;
3554}
3555
3556- (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
3557  self = [self initWithUInt32s:NULL forKeys:NULL count:0];
3558  if (self) {
3559    if (dictionary) {
3560      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3561    }
3562  }
3563  return self;
3564}
3565
3566- (instancetype)initWithCapacity:(NSUInteger)numItems {
3567  #pragma unused(numItems)
3568  return [self initWithUInt32s:NULL forKeys:NULL count:0];
3569}
3570
3571- (void)dealloc {
3572  NSAssert(!_autocreator,
3573           @"%@: Autocreator must be cleared before release, autocreator: %@",
3574           [self class], _autocreator);
3575  [_dictionary release];
3576  [super dealloc];
3577}
3578
3579- (instancetype)copyWithZone:(NSZone *)zone {
3580  return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
3581}
3582
3583- (BOOL)isEqual:(id)other {
3584  if (self == other) {
3585    return YES;
3586  }
3587  if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
3588    return NO;
3589  }
3590  GPBInt32UInt32Dictionary *otherDictionary = other;
3591  return [_dictionary isEqual:otherDictionary->_dictionary];
3592}
3593
3594- (NSUInteger)hash {
3595  return _dictionary.count;
3596}
3597
3598- (NSString *)description {
3599  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3600}
3601
3602- (NSUInteger)count {
3603  return _dictionary.count;
3604}
3605
3606- (void)enumerateKeysAndUInt32sUsingBlock:
3607    (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
3608  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3609                                                   NSNumber *aValue,
3610                                                   BOOL *stop) {
3611      block([aKey intValue], [aValue unsignedIntValue], stop);
3612  }];
3613}
3614
3615- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3616  NSUInteger count = _dictionary.count;
3617  if (count == 0) {
3618    return 0;
3619  }
3620
3621  GPBDataType valueDataType = GPBGetFieldDataType(field);
3622  GPBDataType keyDataType = field.mapKeyDataType;
3623  __block size_t result = 0;
3624  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3625                                                   NSNumber *aValue,
3626                                                   BOOL *stop) {
3627    #pragma unused(stop)
3628    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3629    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
3630    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3631  }];
3632  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3633  result += tagSize * count;
3634  return result;
3635}
3636
3637- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3638                         asField:(GPBFieldDescriptor *)field {
3639  GPBDataType valueDataType = GPBGetFieldDataType(field);
3640  GPBDataType keyDataType = field.mapKeyDataType;
3641  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3642  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3643                                                   NSNumber *aValue,
3644                                                   BOOL *stop) {
3645    #pragma unused(stop)
3646    // Write the tag.
3647    [outputStream writeInt32NoTag:tag];
3648    // Write the size of the message.
3649    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3650    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
3651    [outputStream writeInt32NoTag:(int32_t)msgSize];
3652    // Write the fields.
3653    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
3654    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
3655  }];
3656}
3657
3658- (void)setGPBGenericValue:(GPBGenericValue *)value
3659     forGPBGenericValueKey:(GPBGenericValue *)key {
3660  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
3661}
3662
3663- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3664  [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
3665      #pragma unused(stop)
3666      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
3667  }];
3668}
3669
3670- (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
3671  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3672  if (wrapped && value) {
3673    *value = [wrapped unsignedIntValue];
3674  }
3675  return (wrapped != NULL);
3676}
3677
3678- (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
3679  if (otherDictionary) {
3680    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3681    if (_autocreator) {
3682      GPBAutocreatedDictionaryModified(_autocreator, self);
3683    }
3684  }
3685}
3686
3687- (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
3688  [_dictionary setObject:@(value) forKey:@(key)];
3689  if (_autocreator) {
3690    GPBAutocreatedDictionaryModified(_autocreator, self);
3691  }
3692}
3693
3694- (void)removeUInt32ForKey:(int32_t)aKey {
3695  [_dictionary removeObjectForKey:@(aKey)];
3696}
3697
3698- (void)removeAll {
3699  [_dictionary removeAllObjects];
3700}
3701
3702@end
3703
3704#pragma mark - Int32 -> Int32
3705
3706@implementation GPBInt32Int32Dictionary {
3707 @package
3708  NSMutableDictionary *_dictionary;
3709}
3710
3711+ (instancetype)dictionary {
3712  return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
3713}
3714
3715+ (instancetype)dictionaryWithInt32:(int32_t)value
3716                             forKey:(int32_t)key {
3717  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
3718  // on to get the type correct.
3719  return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
3720                                                         forKeys:&key
3721                                                           count:1] autorelease];
3722}
3723
3724+ (instancetype)dictionaryWithInt32s:(const int32_t [])values
3725                             forKeys:(const int32_t [])keys
3726                               count:(NSUInteger)count {
3727  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
3728  // on to get the type correct.
3729  return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:values
3730                                                         forKeys:keys
3731                                                           count:count] autorelease];
3732}
3733
3734+ (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
3735  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3736  // on to get the type correct.
3737  return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3738}
3739
3740+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3741  return [[[self alloc] initWithCapacity:numItems] autorelease];
3742}
3743
3744- (instancetype)init {
3745  return [self initWithInt32s:NULL forKeys:NULL count:0];
3746}
3747
3748- (instancetype)initWithInt32s:(const int32_t [])values
3749                       forKeys:(const int32_t [])keys
3750                         count:(NSUInteger)count {
3751  self = [super init];
3752  if (self) {
3753    _dictionary = [[NSMutableDictionary alloc] init];
3754    if (count && values && keys) {
3755      for (NSUInteger i = 0; i < count; ++i) {
3756        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
3757      }
3758    }
3759  }
3760  return self;
3761}
3762
3763- (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
3764  self = [self initWithInt32s:NULL forKeys:NULL count:0];
3765  if (self) {
3766    if (dictionary) {
3767      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3768    }
3769  }
3770  return self;
3771}
3772
3773- (instancetype)initWithCapacity:(NSUInteger)numItems {
3774  #pragma unused(numItems)
3775  return [self initWithInt32s:NULL forKeys:NULL count:0];
3776}
3777
3778- (void)dealloc {
3779  NSAssert(!_autocreator,
3780           @"%@: Autocreator must be cleared before release, autocreator: %@",
3781           [self class], _autocreator);
3782  [_dictionary release];
3783  [super dealloc];
3784}
3785
3786- (instancetype)copyWithZone:(NSZone *)zone {
3787  return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
3788}
3789
3790- (BOOL)isEqual:(id)other {
3791  if (self == other) {
3792    return YES;
3793  }
3794  if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
3795    return NO;
3796  }
3797  GPBInt32Int32Dictionary *otherDictionary = other;
3798  return [_dictionary isEqual:otherDictionary->_dictionary];
3799}
3800
3801- (NSUInteger)hash {
3802  return _dictionary.count;
3803}
3804
3805- (NSString *)description {
3806  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3807}
3808
3809- (NSUInteger)count {
3810  return _dictionary.count;
3811}
3812
3813- (void)enumerateKeysAndInt32sUsingBlock:
3814    (void (^)(int32_t key, int32_t value, BOOL *stop))block {
3815  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3816                                                   NSNumber *aValue,
3817                                                   BOOL *stop) {
3818      block([aKey intValue], [aValue intValue], stop);
3819  }];
3820}
3821
3822- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3823  NSUInteger count = _dictionary.count;
3824  if (count == 0) {
3825    return 0;
3826  }
3827
3828  GPBDataType valueDataType = GPBGetFieldDataType(field);
3829  GPBDataType keyDataType = field.mapKeyDataType;
3830  __block size_t result = 0;
3831  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3832                                                   NSNumber *aValue,
3833                                                   BOOL *stop) {
3834    #pragma unused(stop)
3835    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3836    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3837    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3838  }];
3839  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3840  result += tagSize * count;
3841  return result;
3842}
3843
3844- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3845                         asField:(GPBFieldDescriptor *)field {
3846  GPBDataType valueDataType = GPBGetFieldDataType(field);
3847  GPBDataType keyDataType = field.mapKeyDataType;
3848  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3849  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3850                                                   NSNumber *aValue,
3851                                                   BOOL *stop) {
3852    #pragma unused(stop)
3853    // Write the tag.
3854    [outputStream writeInt32NoTag:tag];
3855    // Write the size of the message.
3856    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3857    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3858    [outputStream writeInt32NoTag:(int32_t)msgSize];
3859    // Write the fields.
3860    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
3861    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
3862  }];
3863}
3864
3865- (void)setGPBGenericValue:(GPBGenericValue *)value
3866     forGPBGenericValueKey:(GPBGenericValue *)key {
3867  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
3868}
3869
3870- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3871  [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
3872      #pragma unused(stop)
3873      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
3874  }];
3875}
3876
3877- (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
3878  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3879  if (wrapped && value) {
3880    *value = [wrapped intValue];
3881  }
3882  return (wrapped != NULL);
3883}
3884
3885- (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
3886  if (otherDictionary) {
3887    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3888    if (_autocreator) {
3889      GPBAutocreatedDictionaryModified(_autocreator, self);
3890    }
3891  }
3892}
3893
3894- (void)setInt32:(int32_t)value forKey:(int32_t)key {
3895  [_dictionary setObject:@(value) forKey:@(key)];
3896  if (_autocreator) {
3897    GPBAutocreatedDictionaryModified(_autocreator, self);
3898  }
3899}
3900
3901- (void)removeInt32ForKey:(int32_t)aKey {
3902  [_dictionary removeObjectForKey:@(aKey)];
3903}
3904
3905- (void)removeAll {
3906  [_dictionary removeAllObjects];
3907}
3908
3909@end
3910
3911#pragma mark - Int32 -> UInt64
3912
3913@implementation GPBInt32UInt64Dictionary {
3914 @package
3915  NSMutableDictionary *_dictionary;
3916}
3917
3918+ (instancetype)dictionary {
3919  return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
3920}
3921
3922+ (instancetype)dictionaryWithUInt64:(uint64_t)value
3923                              forKey:(int32_t)key {
3924  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
3925  // on to get the type correct.
3926  return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
3927                                                           forKeys:&key
3928                                                             count:1] autorelease];
3929}
3930
3931+ (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
3932                              forKeys:(const int32_t [])keys
3933                                count:(NSUInteger)count {
3934  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
3935  // on to get the type correct.
3936  return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
3937                                                          forKeys:keys
3938                                                            count:count] autorelease];
3939}
3940
3941+ (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
3942  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3943  // on to get the type correct.
3944  return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3945}
3946
3947+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3948  return [[[self alloc] initWithCapacity:numItems] autorelease];
3949}
3950
3951- (instancetype)init {
3952  return [self initWithUInt64s:NULL forKeys:NULL count:0];
3953}
3954
3955- (instancetype)initWithUInt64s:(const uint64_t [])values
3956                        forKeys:(const int32_t [])keys
3957                          count:(NSUInteger)count {
3958  self = [super init];
3959  if (self) {
3960    _dictionary = [[NSMutableDictionary alloc] init];
3961    if (count && values && keys) {
3962      for (NSUInteger i = 0; i < count; ++i) {
3963        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
3964      }
3965    }
3966  }
3967  return self;
3968}
3969
3970- (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
3971  self = [self initWithUInt64s:NULL forKeys:NULL count:0];
3972  if (self) {
3973    if (dictionary) {
3974      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3975    }
3976  }
3977  return self;
3978}
3979
3980- (instancetype)initWithCapacity:(NSUInteger)numItems {
3981  #pragma unused(numItems)
3982  return [self initWithUInt64s:NULL forKeys:NULL count:0];
3983}
3984
3985- (void)dealloc {
3986  NSAssert(!_autocreator,
3987           @"%@: Autocreator must be cleared before release, autocreator: %@",
3988           [self class], _autocreator);
3989  [_dictionary release];
3990  [super dealloc];
3991}
3992
3993- (instancetype)copyWithZone:(NSZone *)zone {
3994  return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
3995}
3996
3997- (BOOL)isEqual:(id)other {
3998  if (self == other) {
3999    return YES;
4000  }
4001  if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
4002    return NO;
4003  }
4004  GPBInt32UInt64Dictionary *otherDictionary = other;
4005  return [_dictionary isEqual:otherDictionary->_dictionary];
4006}
4007
4008- (NSUInteger)hash {
4009  return _dictionary.count;
4010}
4011
4012- (NSString *)description {
4013  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4014}
4015
4016- (NSUInteger)count {
4017  return _dictionary.count;
4018}
4019
4020- (void)enumerateKeysAndUInt64sUsingBlock:
4021    (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
4022  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4023                                                   NSNumber *aValue,
4024                                                   BOOL *stop) {
4025      block([aKey intValue], [aValue unsignedLongLongValue], stop);
4026  }];
4027}
4028
4029- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4030  NSUInteger count = _dictionary.count;
4031  if (count == 0) {
4032    return 0;
4033  }
4034
4035  GPBDataType valueDataType = GPBGetFieldDataType(field);
4036  GPBDataType keyDataType = field.mapKeyDataType;
4037  __block size_t result = 0;
4038  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4039                                                   NSNumber *aValue,
4040                                                   BOOL *stop) {
4041    #pragma unused(stop)
4042    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4043    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
4044    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4045  }];
4046  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4047  result += tagSize * count;
4048  return result;
4049}
4050
4051- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4052                         asField:(GPBFieldDescriptor *)field {
4053  GPBDataType valueDataType = GPBGetFieldDataType(field);
4054  GPBDataType keyDataType = field.mapKeyDataType;
4055  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4056  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4057                                                   NSNumber *aValue,
4058                                                   BOOL *stop) {
4059    #pragma unused(stop)
4060    // Write the tag.
4061    [outputStream writeInt32NoTag:tag];
4062    // Write the size of the message.
4063    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4064    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
4065    [outputStream writeInt32NoTag:(int32_t)msgSize];
4066    // Write the fields.
4067    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4068    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
4069  }];
4070}
4071
4072- (void)setGPBGenericValue:(GPBGenericValue *)value
4073     forGPBGenericValueKey:(GPBGenericValue *)key {
4074  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
4075}
4076
4077- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4078  [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
4079      #pragma unused(stop)
4080      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
4081  }];
4082}
4083
4084- (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
4085  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4086  if (wrapped && value) {
4087    *value = [wrapped unsignedLongLongValue];
4088  }
4089  return (wrapped != NULL);
4090}
4091
4092- (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
4093  if (otherDictionary) {
4094    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4095    if (_autocreator) {
4096      GPBAutocreatedDictionaryModified(_autocreator, self);
4097    }
4098  }
4099}
4100
4101- (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
4102  [_dictionary setObject:@(value) forKey:@(key)];
4103  if (_autocreator) {
4104    GPBAutocreatedDictionaryModified(_autocreator, self);
4105  }
4106}
4107
4108- (void)removeUInt64ForKey:(int32_t)aKey {
4109  [_dictionary removeObjectForKey:@(aKey)];
4110}
4111
4112- (void)removeAll {
4113  [_dictionary removeAllObjects];
4114}
4115
4116@end
4117
4118#pragma mark - Int32 -> Int64
4119
4120@implementation GPBInt32Int64Dictionary {
4121 @package
4122  NSMutableDictionary *_dictionary;
4123}
4124
4125+ (instancetype)dictionary {
4126  return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
4127}
4128
4129+ (instancetype)dictionaryWithInt64:(int64_t)value
4130                             forKey:(int32_t)key {
4131  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
4132  // on to get the type correct.
4133  return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
4134                                                         forKeys:&key
4135                                                           count:1] autorelease];
4136}
4137
4138+ (instancetype)dictionaryWithInt64s:(const int64_t [])values
4139                             forKeys:(const int32_t [])keys
4140                               count:(NSUInteger)count {
4141  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
4142  // on to get the type correct.
4143  return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:values
4144                                                         forKeys:keys
4145                                                           count:count] autorelease];
4146}
4147
4148+ (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
4149  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4150  // on to get the type correct.
4151  return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4152}
4153
4154+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4155  return [[[self alloc] initWithCapacity:numItems] autorelease];
4156}
4157
4158- (instancetype)init {
4159  return [self initWithInt64s:NULL forKeys:NULL count:0];
4160}
4161
4162- (instancetype)initWithInt64s:(const int64_t [])values
4163                       forKeys:(const int32_t [])keys
4164                         count:(NSUInteger)count {
4165  self = [super init];
4166  if (self) {
4167    _dictionary = [[NSMutableDictionary alloc] init];
4168    if (count && values && keys) {
4169      for (NSUInteger i = 0; i < count; ++i) {
4170        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4171      }
4172    }
4173  }
4174  return self;
4175}
4176
4177- (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
4178  self = [self initWithInt64s:NULL forKeys:NULL count:0];
4179  if (self) {
4180    if (dictionary) {
4181      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4182    }
4183  }
4184  return self;
4185}
4186
4187- (instancetype)initWithCapacity:(NSUInteger)numItems {
4188  #pragma unused(numItems)
4189  return [self initWithInt64s:NULL forKeys:NULL count:0];
4190}
4191
4192- (void)dealloc {
4193  NSAssert(!_autocreator,
4194           @"%@: Autocreator must be cleared before release, autocreator: %@",
4195           [self class], _autocreator);
4196  [_dictionary release];
4197  [super dealloc];
4198}
4199
4200- (instancetype)copyWithZone:(NSZone *)zone {
4201  return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
4202}
4203
4204- (BOOL)isEqual:(id)other {
4205  if (self == other) {
4206    return YES;
4207  }
4208  if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
4209    return NO;
4210  }
4211  GPBInt32Int64Dictionary *otherDictionary = other;
4212  return [_dictionary isEqual:otherDictionary->_dictionary];
4213}
4214
4215- (NSUInteger)hash {
4216  return _dictionary.count;
4217}
4218
4219- (NSString *)description {
4220  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4221}
4222
4223- (NSUInteger)count {
4224  return _dictionary.count;
4225}
4226
4227- (void)enumerateKeysAndInt64sUsingBlock:
4228    (void (^)(int32_t key, int64_t value, BOOL *stop))block {
4229  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4230                                                   NSNumber *aValue,
4231                                                   BOOL *stop) {
4232      block([aKey intValue], [aValue longLongValue], stop);
4233  }];
4234}
4235
4236- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4237  NSUInteger count = _dictionary.count;
4238  if (count == 0) {
4239    return 0;
4240  }
4241
4242  GPBDataType valueDataType = GPBGetFieldDataType(field);
4243  GPBDataType keyDataType = field.mapKeyDataType;
4244  __block size_t result = 0;
4245  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4246                                                   NSNumber *aValue,
4247                                                   BOOL *stop) {
4248    #pragma unused(stop)
4249    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4250    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
4251    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4252  }];
4253  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4254  result += tagSize * count;
4255  return result;
4256}
4257
4258- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4259                         asField:(GPBFieldDescriptor *)field {
4260  GPBDataType valueDataType = GPBGetFieldDataType(field);
4261  GPBDataType keyDataType = field.mapKeyDataType;
4262  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4263  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4264                                                   NSNumber *aValue,
4265                                                   BOOL *stop) {
4266    #pragma unused(stop)
4267    // Write the tag.
4268    [outputStream writeInt32NoTag:tag];
4269    // Write the size of the message.
4270    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4271    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
4272    [outputStream writeInt32NoTag:(int32_t)msgSize];
4273    // Write the fields.
4274    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4275    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
4276  }];
4277}
4278
4279- (void)setGPBGenericValue:(GPBGenericValue *)value
4280     forGPBGenericValueKey:(GPBGenericValue *)key {
4281  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
4282}
4283
4284- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4285  [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
4286      #pragma unused(stop)
4287      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
4288  }];
4289}
4290
4291- (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
4292  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4293  if (wrapped && value) {
4294    *value = [wrapped longLongValue];
4295  }
4296  return (wrapped != NULL);
4297}
4298
4299- (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
4300  if (otherDictionary) {
4301    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4302    if (_autocreator) {
4303      GPBAutocreatedDictionaryModified(_autocreator, self);
4304    }
4305  }
4306}
4307
4308- (void)setInt64:(int64_t)value forKey:(int32_t)key {
4309  [_dictionary setObject:@(value) forKey:@(key)];
4310  if (_autocreator) {
4311    GPBAutocreatedDictionaryModified(_autocreator, self);
4312  }
4313}
4314
4315- (void)removeInt64ForKey:(int32_t)aKey {
4316  [_dictionary removeObjectForKey:@(aKey)];
4317}
4318
4319- (void)removeAll {
4320  [_dictionary removeAllObjects];
4321}
4322
4323@end
4324
4325#pragma mark - Int32 -> Bool
4326
4327@implementation GPBInt32BoolDictionary {
4328 @package
4329  NSMutableDictionary *_dictionary;
4330}
4331
4332+ (instancetype)dictionary {
4333  return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
4334}
4335
4336+ (instancetype)dictionaryWithBool:(BOOL)value
4337                            forKey:(int32_t)key {
4338  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
4339  // on to get the type correct.
4340  return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:&value
4341                                                       forKeys:&key
4342                                                         count:1] autorelease];
4343}
4344
4345+ (instancetype)dictionaryWithBools:(const BOOL [])values
4346                            forKeys:(const int32_t [])keys
4347                              count:(NSUInteger)count {
4348  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
4349  // on to get the type correct.
4350  return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:values
4351                                                        forKeys:keys
4352                                                          count:count] autorelease];
4353}
4354
4355+ (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
4356  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4357  // on to get the type correct.
4358  return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4359}
4360
4361+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4362  return [[[self alloc] initWithCapacity:numItems] autorelease];
4363}
4364
4365- (instancetype)init {
4366  return [self initWithBools:NULL forKeys:NULL count:0];
4367}
4368
4369- (instancetype)initWithBools:(const BOOL [])values
4370                      forKeys:(const int32_t [])keys
4371                        count:(NSUInteger)count {
4372  self = [super init];
4373  if (self) {
4374    _dictionary = [[NSMutableDictionary alloc] init];
4375    if (count && values && keys) {
4376      for (NSUInteger i = 0; i < count; ++i) {
4377        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4378      }
4379    }
4380  }
4381  return self;
4382}
4383
4384- (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
4385  self = [self initWithBools:NULL forKeys:NULL count:0];
4386  if (self) {
4387    if (dictionary) {
4388      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4389    }
4390  }
4391  return self;
4392}
4393
4394- (instancetype)initWithCapacity:(NSUInteger)numItems {
4395  #pragma unused(numItems)
4396  return [self initWithBools:NULL forKeys:NULL count:0];
4397}
4398
4399- (void)dealloc {
4400  NSAssert(!_autocreator,
4401           @"%@: Autocreator must be cleared before release, autocreator: %@",
4402           [self class], _autocreator);
4403  [_dictionary release];
4404  [super dealloc];
4405}
4406
4407- (instancetype)copyWithZone:(NSZone *)zone {
4408  return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
4409}
4410
4411- (BOOL)isEqual:(id)other {
4412  if (self == other) {
4413    return YES;
4414  }
4415  if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
4416    return NO;
4417  }
4418  GPBInt32BoolDictionary *otherDictionary = other;
4419  return [_dictionary isEqual:otherDictionary->_dictionary];
4420}
4421
4422- (NSUInteger)hash {
4423  return _dictionary.count;
4424}
4425
4426- (NSString *)description {
4427  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4428}
4429
4430- (NSUInteger)count {
4431  return _dictionary.count;
4432}
4433
4434- (void)enumerateKeysAndBoolsUsingBlock:
4435    (void (^)(int32_t key, BOOL value, BOOL *stop))block {
4436  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4437                                                   NSNumber *aValue,
4438                                                   BOOL *stop) {
4439      block([aKey intValue], [aValue boolValue], stop);
4440  }];
4441}
4442
4443- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4444  NSUInteger count = _dictionary.count;
4445  if (count == 0) {
4446    return 0;
4447  }
4448
4449  GPBDataType valueDataType = GPBGetFieldDataType(field);
4450  GPBDataType keyDataType = field.mapKeyDataType;
4451  __block size_t result = 0;
4452  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4453                                                   NSNumber *aValue,
4454                                                   BOOL *stop) {
4455    #pragma unused(stop)
4456    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4457    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
4458    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4459  }];
4460  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4461  result += tagSize * count;
4462  return result;
4463}
4464
4465- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4466                         asField:(GPBFieldDescriptor *)field {
4467  GPBDataType valueDataType = GPBGetFieldDataType(field);
4468  GPBDataType keyDataType = field.mapKeyDataType;
4469  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4470  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4471                                                   NSNumber *aValue,
4472                                                   BOOL *stop) {
4473    #pragma unused(stop)
4474    // Write the tag.
4475    [outputStream writeInt32NoTag:tag];
4476    // Write the size of the message.
4477    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4478    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
4479    [outputStream writeInt32NoTag:(int32_t)msgSize];
4480    // Write the fields.
4481    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4482    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
4483  }];
4484}
4485
4486- (void)setGPBGenericValue:(GPBGenericValue *)value
4487     forGPBGenericValueKey:(GPBGenericValue *)key {
4488  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
4489}
4490
4491- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4492  [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
4493      #pragma unused(stop)
4494      block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
4495  }];
4496}
4497
4498- (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
4499  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4500  if (wrapped && value) {
4501    *value = [wrapped boolValue];
4502  }
4503  return (wrapped != NULL);
4504}
4505
4506- (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
4507  if (otherDictionary) {
4508    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4509    if (_autocreator) {
4510      GPBAutocreatedDictionaryModified(_autocreator, self);
4511    }
4512  }
4513}
4514
4515- (void)setBool:(BOOL)value forKey:(int32_t)key {
4516  [_dictionary setObject:@(value) forKey:@(key)];
4517  if (_autocreator) {
4518    GPBAutocreatedDictionaryModified(_autocreator, self);
4519  }
4520}
4521
4522- (void)removeBoolForKey:(int32_t)aKey {
4523  [_dictionary removeObjectForKey:@(aKey)];
4524}
4525
4526- (void)removeAll {
4527  [_dictionary removeAllObjects];
4528}
4529
4530@end
4531
4532#pragma mark - Int32 -> Float
4533
4534@implementation GPBInt32FloatDictionary {
4535 @package
4536  NSMutableDictionary *_dictionary;
4537}
4538
4539+ (instancetype)dictionary {
4540  return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
4541}
4542
4543+ (instancetype)dictionaryWithFloat:(float)value
4544                             forKey:(int32_t)key {
4545  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
4546  // on to get the type correct.
4547  return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:&value
4548                                                         forKeys:&key
4549                                                           count:1] autorelease];
4550}
4551
4552+ (instancetype)dictionaryWithFloats:(const float [])values
4553                             forKeys:(const int32_t [])keys
4554                               count:(NSUInteger)count {
4555  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
4556  // on to get the type correct.
4557  return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:values
4558                                                         forKeys:keys
4559                                                           count:count] autorelease];
4560}
4561
4562+ (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
4563  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4564  // on to get the type correct.
4565  return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4566}
4567
4568+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4569  return [[[self alloc] initWithCapacity:numItems] autorelease];
4570}
4571
4572- (instancetype)init {
4573  return [self initWithFloats:NULL forKeys:NULL count:0];
4574}
4575
4576- (instancetype)initWithFloats:(const float [])values
4577                       forKeys:(const int32_t [])keys
4578                         count:(NSUInteger)count {
4579  self = [super init];
4580  if (self) {
4581    _dictionary = [[NSMutableDictionary alloc] init];
4582    if (count && values && keys) {
4583      for (NSUInteger i = 0; i < count; ++i) {
4584        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4585      }
4586    }
4587  }
4588  return self;
4589}
4590
4591- (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
4592  self = [self initWithFloats:NULL forKeys:NULL count:0];
4593  if (self) {
4594    if (dictionary) {
4595      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4596    }
4597  }
4598  return self;
4599}
4600
4601- (instancetype)initWithCapacity:(NSUInteger)numItems {
4602  #pragma unused(numItems)
4603  return [self initWithFloats:NULL forKeys:NULL count:0];
4604}
4605
4606- (void)dealloc {
4607  NSAssert(!_autocreator,
4608           @"%@: Autocreator must be cleared before release, autocreator: %@",
4609           [self class], _autocreator);
4610  [_dictionary release];
4611  [super dealloc];
4612}
4613
4614- (instancetype)copyWithZone:(NSZone *)zone {
4615  return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
4616}
4617
4618- (BOOL)isEqual:(id)other {
4619  if (self == other) {
4620    return YES;
4621  }
4622  if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
4623    return NO;
4624  }
4625  GPBInt32FloatDictionary *otherDictionary = other;
4626  return [_dictionary isEqual:otherDictionary->_dictionary];
4627}
4628
4629- (NSUInteger)hash {
4630  return _dictionary.count;
4631}
4632
4633- (NSString *)description {
4634  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4635}
4636
4637- (NSUInteger)count {
4638  return _dictionary.count;
4639}
4640
4641- (void)enumerateKeysAndFloatsUsingBlock:
4642    (void (^)(int32_t key, float value, BOOL *stop))block {
4643  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4644                                                   NSNumber *aValue,
4645                                                   BOOL *stop) {
4646      block([aKey intValue], [aValue floatValue], stop);
4647  }];
4648}
4649
4650- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4651  NSUInteger count = _dictionary.count;
4652  if (count == 0) {
4653    return 0;
4654  }
4655
4656  GPBDataType valueDataType = GPBGetFieldDataType(field);
4657  GPBDataType keyDataType = field.mapKeyDataType;
4658  __block size_t result = 0;
4659  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4660                                                   NSNumber *aValue,
4661                                                   BOOL *stop) {
4662    #pragma unused(stop)
4663    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4664    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
4665    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4666  }];
4667  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4668  result += tagSize * count;
4669  return result;
4670}
4671
4672- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4673                         asField:(GPBFieldDescriptor *)field {
4674  GPBDataType valueDataType = GPBGetFieldDataType(field);
4675  GPBDataType keyDataType = field.mapKeyDataType;
4676  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4677  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4678                                                   NSNumber *aValue,
4679                                                   BOOL *stop) {
4680    #pragma unused(stop)
4681    // Write the tag.
4682    [outputStream writeInt32NoTag:tag];
4683    // Write the size of the message.
4684    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4685    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
4686    [outputStream writeInt32NoTag:(int32_t)msgSize];
4687    // Write the fields.
4688    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4689    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
4690  }];
4691}
4692
4693- (void)setGPBGenericValue:(GPBGenericValue *)value
4694     forGPBGenericValueKey:(GPBGenericValue *)key {
4695  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
4696}
4697
4698- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4699  [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) {
4700      #pragma unused(stop)
4701      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
4702  }];
4703}
4704
4705- (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
4706  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4707  if (wrapped && value) {
4708    *value = [wrapped floatValue];
4709  }
4710  return (wrapped != NULL);
4711}
4712
4713- (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
4714  if (otherDictionary) {
4715    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4716    if (_autocreator) {
4717      GPBAutocreatedDictionaryModified(_autocreator, self);
4718    }
4719  }
4720}
4721
4722- (void)setFloat:(float)value forKey:(int32_t)key {
4723  [_dictionary setObject:@(value) forKey:@(key)];
4724  if (_autocreator) {
4725    GPBAutocreatedDictionaryModified(_autocreator, self);
4726  }
4727}
4728
4729- (void)removeFloatForKey:(int32_t)aKey {
4730  [_dictionary removeObjectForKey:@(aKey)];
4731}
4732
4733- (void)removeAll {
4734  [_dictionary removeAllObjects];
4735}
4736
4737@end
4738
4739#pragma mark - Int32 -> Double
4740
4741@implementation GPBInt32DoubleDictionary {
4742 @package
4743  NSMutableDictionary *_dictionary;
4744}
4745
4746+ (instancetype)dictionary {
4747  return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
4748}
4749
4750+ (instancetype)dictionaryWithDouble:(double)value
4751                              forKey:(int32_t)key {
4752  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
4753  // on to get the type correct.
4754  return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
4755                                                           forKeys:&key
4756                                                             count:1] autorelease];
4757}
4758
4759+ (instancetype)dictionaryWithDoubles:(const double [])values
4760                              forKeys:(const int32_t [])keys
4761                                count:(NSUInteger)count {
4762  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
4763  // on to get the type correct.
4764  return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:values
4765                                                          forKeys:keys
4766                                                            count:count] autorelease];
4767}
4768
4769+ (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
4770  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4771  // on to get the type correct.
4772  return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4773}
4774
4775+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4776  return [[[self alloc] initWithCapacity:numItems] autorelease];
4777}
4778
4779- (instancetype)init {
4780  return [self initWithDoubles:NULL forKeys:NULL count:0];
4781}
4782
4783- (instancetype)initWithDoubles:(const double [])values
4784                        forKeys:(const int32_t [])keys
4785                          count:(NSUInteger)count {
4786  self = [super init];
4787  if (self) {
4788    _dictionary = [[NSMutableDictionary alloc] init];
4789    if (count && values && keys) {
4790      for (NSUInteger i = 0; i < count; ++i) {
4791        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4792      }
4793    }
4794  }
4795  return self;
4796}
4797
4798- (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
4799  self = [self initWithDoubles:NULL forKeys:NULL count:0];
4800  if (self) {
4801    if (dictionary) {
4802      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4803    }
4804  }
4805  return self;
4806}
4807
4808- (instancetype)initWithCapacity:(NSUInteger)numItems {
4809  #pragma unused(numItems)
4810  return [self initWithDoubles:NULL forKeys:NULL count:0];
4811}
4812
4813- (void)dealloc {
4814  NSAssert(!_autocreator,
4815           @"%@: Autocreator must be cleared before release, autocreator: %@",
4816           [self class], _autocreator);
4817  [_dictionary release];
4818  [super dealloc];
4819}
4820
4821- (instancetype)copyWithZone:(NSZone *)zone {
4822  return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
4823}
4824
4825- (BOOL)isEqual:(id)other {
4826  if (self == other) {
4827    return YES;
4828  }
4829  if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
4830    return NO;
4831  }
4832  GPBInt32DoubleDictionary *otherDictionary = other;
4833  return [_dictionary isEqual:otherDictionary->_dictionary];
4834}
4835
4836- (NSUInteger)hash {
4837  return _dictionary.count;
4838}
4839
4840- (NSString *)description {
4841  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4842}
4843
4844- (NSUInteger)count {
4845  return _dictionary.count;
4846}
4847
4848- (void)enumerateKeysAndDoublesUsingBlock:
4849    (void (^)(int32_t key, double value, BOOL *stop))block {
4850  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4851                                                   NSNumber *aValue,
4852                                                   BOOL *stop) {
4853      block([aKey intValue], [aValue doubleValue], stop);
4854  }];
4855}
4856
4857- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4858  NSUInteger count = _dictionary.count;
4859  if (count == 0) {
4860    return 0;
4861  }
4862
4863  GPBDataType valueDataType = GPBGetFieldDataType(field);
4864  GPBDataType keyDataType = field.mapKeyDataType;
4865  __block size_t result = 0;
4866  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4867                                                   NSNumber *aValue,
4868                                                   BOOL *stop) {
4869    #pragma unused(stop)
4870    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4871    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
4872    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4873  }];
4874  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4875  result += tagSize * count;
4876  return result;
4877}
4878
4879- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4880                         asField:(GPBFieldDescriptor *)field {
4881  GPBDataType valueDataType = GPBGetFieldDataType(field);
4882  GPBDataType keyDataType = field.mapKeyDataType;
4883  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4884  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4885                                                   NSNumber *aValue,
4886                                                   BOOL *stop) {
4887    #pragma unused(stop)
4888    // Write the tag.
4889    [outputStream writeInt32NoTag:tag];
4890    // Write the size of the message.
4891    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4892    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
4893    [outputStream writeInt32NoTag:(int32_t)msgSize];
4894    // Write the fields.
4895    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4896    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
4897  }];
4898}
4899
4900- (void)setGPBGenericValue:(GPBGenericValue *)value
4901     forGPBGenericValueKey:(GPBGenericValue *)key {
4902  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
4903}
4904
4905- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4906  [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) {
4907      #pragma unused(stop)
4908      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
4909  }];
4910}
4911
4912- (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
4913  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4914  if (wrapped && value) {
4915    *value = [wrapped doubleValue];
4916  }
4917  return (wrapped != NULL);
4918}
4919
4920- (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
4921  if (otherDictionary) {
4922    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4923    if (_autocreator) {
4924      GPBAutocreatedDictionaryModified(_autocreator, self);
4925    }
4926  }
4927}
4928
4929- (void)setDouble:(double)value forKey:(int32_t)key {
4930  [_dictionary setObject:@(value) forKey:@(key)];
4931  if (_autocreator) {
4932    GPBAutocreatedDictionaryModified(_autocreator, self);
4933  }
4934}
4935
4936- (void)removeDoubleForKey:(int32_t)aKey {
4937  [_dictionary removeObjectForKey:@(aKey)];
4938}
4939
4940- (void)removeAll {
4941  [_dictionary removeAllObjects];
4942}
4943
4944@end
4945
4946#pragma mark - Int32 -> Enum
4947
4948@implementation GPBInt32EnumDictionary {
4949 @package
4950  NSMutableDictionary *_dictionary;
4951  GPBEnumValidationFunc _validationFunc;
4952}
4953
4954@synthesize validationFunc = _validationFunc;
4955
4956+ (instancetype)dictionary {
4957  return [[[self alloc] initWithValidationFunction:NULL
4958                                         rawValues:NULL
4959                                           forKeys:NULL
4960                                             count:0] autorelease];
4961}
4962
4963+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
4964  return [[[self alloc] initWithValidationFunction:func
4965                                         rawValues:NULL
4966                                           forKeys:NULL
4967                                             count:0] autorelease];
4968}
4969
4970+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
4971                                        rawValue:(int32_t)rawValue
4972                                          forKey:(int32_t)key {
4973  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4974  // on to get the type correct.
4975  return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
4976                                                                  rawValues:&rawValue
4977                                                                    forKeys:&key
4978                                                                      count:1] autorelease];
4979}
4980
4981+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
4982                                       rawValues:(const int32_t [])rawValues
4983                                         forKeys:(const int32_t [])keys
4984                                           count:(NSUInteger)count {
4985  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4986  // on to get the type correct.
4987  return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
4988                                                                  rawValues:rawValues
4989                                                                    forKeys:keys
4990                                                                      count:count] autorelease];
4991}
4992
4993+ (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
4994  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4995  // on to get the type correct.
4996  return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4997}
4998
4999+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
5000                                        capacity:(NSUInteger)numItems {
5001  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
5002}
5003
5004- (instancetype)init {
5005  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
5006}
5007
5008- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
5009  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
5010}
5011
5012- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
5013                                 rawValues:(const int32_t [])rawValues
5014                                   forKeys:(const int32_t [])keys
5015                                     count:(NSUInteger)count {
5016  self = [super init];
5017  if (self) {
5018    _dictionary = [[NSMutableDictionary alloc] init];
5019    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
5020    if (count && rawValues && keys) {
5021      for (NSUInteger i = 0; i < count; ++i) {
5022        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
5023      }
5024    }
5025  }
5026  return self;
5027}
5028
5029- (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
5030  self = [self initWithValidationFunction:dictionary.validationFunc
5031                                rawValues:NULL
5032                                  forKeys:NULL
5033                                    count:0];
5034  if (self) {
5035    if (dictionary) {
5036      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5037    }
5038  }
5039  return self;
5040}
5041
5042- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
5043                                  capacity:(NSUInteger)numItems {
5044  #pragma unused(numItems)
5045  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
5046}
5047
5048- (void)dealloc {
5049  NSAssert(!_autocreator,
5050           @"%@: Autocreator must be cleared before release, autocreator: %@",
5051           [self class], _autocreator);
5052  [_dictionary release];
5053  [super dealloc];
5054}
5055
5056- (instancetype)copyWithZone:(NSZone *)zone {
5057  return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
5058}
5059
5060- (BOOL)isEqual:(id)other {
5061  if (self == other) {
5062    return YES;
5063  }
5064  if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
5065    return NO;
5066  }
5067  GPBInt32EnumDictionary *otherDictionary = other;
5068  return [_dictionary isEqual:otherDictionary->_dictionary];
5069}
5070
5071- (NSUInteger)hash {
5072  return _dictionary.count;
5073}
5074
5075- (NSString *)description {
5076  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5077}
5078
5079- (NSUInteger)count {
5080  return _dictionary.count;
5081}
5082
5083- (void)enumerateKeysAndRawValuesUsingBlock:
5084    (void (^)(int32_t key, int32_t value, BOOL *stop))block {
5085  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5086                                                   NSNumber *aValue,
5087                                                   BOOL *stop) {
5088      block([aKey intValue], [aValue intValue], stop);
5089  }];
5090}
5091
5092- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5093  NSUInteger count = _dictionary.count;
5094  if (count == 0) {
5095    return 0;
5096  }
5097
5098  GPBDataType valueDataType = GPBGetFieldDataType(field);
5099  GPBDataType keyDataType = field.mapKeyDataType;
5100  __block size_t result = 0;
5101  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5102                                                   NSNumber *aValue,
5103                                                   BOOL *stop) {
5104    #pragma unused(stop)
5105    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5106    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5107    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5108  }];
5109  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5110  result += tagSize * count;
5111  return result;
5112}
5113
5114- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5115                         asField:(GPBFieldDescriptor *)field {
5116  GPBDataType valueDataType = GPBGetFieldDataType(field);
5117  GPBDataType keyDataType = field.mapKeyDataType;
5118  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5119  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5120                                                   NSNumber *aValue,
5121                                                   BOOL *stop) {
5122    #pragma unused(stop)
5123    // Write the tag.
5124    [outputStream writeInt32NoTag:tag];
5125    // Write the size of the message.
5126    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5127    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5128    [outputStream writeInt32NoTag:(int32_t)msgSize];
5129    // Write the fields.
5130    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
5131    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
5132  }];
5133}
5134
5135- (NSData *)serializedDataForUnknownValue:(int32_t)value
5136                                   forKey:(GPBGenericValue *)key
5137                              keyDataType:(GPBDataType)keyDataType {
5138  size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
5139  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
5140  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
5141  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
5142  WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
5143  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
5144  [outputStream release];
5145  return data;
5146}
5147- (void)setGPBGenericValue:(GPBGenericValue *)value
5148     forGPBGenericValueKey:(GPBGenericValue *)key {
5149  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
5150}
5151
5152- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5153  [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
5154      #pragma unused(stop)
5155      block([NSString stringWithFormat:@"%d", key], @(value));
5156  }];
5157}
5158
5159- (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
5160  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5161  if (wrapped && value) {
5162    int32_t result = [wrapped intValue];
5163    if (!_validationFunc(result)) {
5164      result = kGPBUnrecognizedEnumeratorValue;
5165    }
5166    *value = result;
5167  }
5168  return (wrapped != NULL);
5169}
5170
5171- (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
5172  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5173  if (wrapped && rawValue) {
5174    *rawValue = [wrapped intValue];
5175  }
5176  return (wrapped != NULL);
5177}
5178
5179- (void)enumerateKeysAndEnumsUsingBlock:
5180    (void (^)(int32_t key, int32_t value, BOOL *stop))block {
5181  GPBEnumValidationFunc func = _validationFunc;
5182  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5183                                                   NSNumber *aValue,
5184                                                   BOOL *stop) {
5185      int32_t unwrapped = [aValue intValue];
5186      if (!func(unwrapped)) {
5187        unwrapped = kGPBUnrecognizedEnumeratorValue;
5188      }
5189      block([aKey intValue], unwrapped, stop);
5190  }];
5191}
5192
5193- (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
5194  if (otherDictionary) {
5195    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5196    if (_autocreator) {
5197      GPBAutocreatedDictionaryModified(_autocreator, self);
5198    }
5199  }
5200}
5201
5202- (void)setRawValue:(int32_t)value forKey:(int32_t)key {
5203  [_dictionary setObject:@(value) forKey:@(key)];
5204  if (_autocreator) {
5205    GPBAutocreatedDictionaryModified(_autocreator, self);
5206  }
5207}
5208
5209- (void)removeEnumForKey:(int32_t)aKey {
5210  [_dictionary removeObjectForKey:@(aKey)];
5211}
5212
5213- (void)removeAll {
5214  [_dictionary removeAllObjects];
5215}
5216
5217- (void)setEnum:(int32_t)value forKey:(int32_t)key {
5218  if (!_validationFunc(value)) {
5219    [NSException raise:NSInvalidArgumentException
5220                format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
5221                       value];
5222  }
5223
5224  [_dictionary setObject:@(value) forKey:@(key)];
5225  if (_autocreator) {
5226    GPBAutocreatedDictionaryModified(_autocreator, self);
5227  }
5228}
5229
5230@end
5231
5232#pragma mark - Int32 -> Object
5233
5234@implementation GPBInt32ObjectDictionary {
5235 @package
5236  NSMutableDictionary *_dictionary;
5237}
5238
5239+ (instancetype)dictionary {
5240  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
5241}
5242
5243+ (instancetype)dictionaryWithObject:(id)object
5244                              forKey:(int32_t)key {
5245  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
5246  // on to get the type correct.
5247  return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
5248                                                           forKeys:&key
5249                                                             count:1] autorelease];
5250}
5251
5252+ (instancetype)dictionaryWithObjects:(const id [])objects
5253                              forKeys:(const int32_t [])keys
5254                                count:(NSUInteger)count {
5255  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
5256  // on to get the type correct.
5257  return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
5258                                                          forKeys:keys
5259                                                            count:count] autorelease];
5260}
5261
5262+ (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
5263  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5264  // on to get the type correct.
5265  return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5266}
5267
5268+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5269  return [[[self alloc] initWithCapacity:numItems] autorelease];
5270}
5271
5272- (instancetype)init {
5273  return [self initWithObjects:NULL forKeys:NULL count:0];
5274}
5275
5276- (instancetype)initWithObjects:(const id [])objects
5277                        forKeys:(const int32_t [])keys
5278                          count:(NSUInteger)count {
5279  self = [super init];
5280  if (self) {
5281    _dictionary = [[NSMutableDictionary alloc] init];
5282    if (count && objects && keys) {
5283      for (NSUInteger i = 0; i < count; ++i) {
5284        if (!objects[i]) {
5285          [NSException raise:NSInvalidArgumentException
5286                      format:@"Attempting to add nil object to a Dictionary"];
5287        }
5288        [_dictionary setObject:objects[i] forKey:@(keys[i])];
5289      }
5290    }
5291  }
5292  return self;
5293}
5294
5295- (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
5296  self = [self initWithObjects:NULL forKeys:NULL count:0];
5297  if (self) {
5298    if (dictionary) {
5299      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5300    }
5301  }
5302  return self;
5303}
5304
5305- (instancetype)initWithCapacity:(NSUInteger)numItems {
5306  #pragma unused(numItems)
5307  return [self initWithObjects:NULL forKeys:NULL count:0];
5308}
5309
5310- (void)dealloc {
5311  NSAssert(!_autocreator,
5312           @"%@: Autocreator must be cleared before release, autocreator: %@",
5313           [self class], _autocreator);
5314  [_dictionary release];
5315  [super dealloc];
5316}
5317
5318- (instancetype)copyWithZone:(NSZone *)zone {
5319  return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
5320}
5321
5322- (BOOL)isEqual:(id)other {
5323  if (self == other) {
5324    return YES;
5325  }
5326  if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
5327    return NO;
5328  }
5329  GPBInt32ObjectDictionary *otherDictionary = other;
5330  return [_dictionary isEqual:otherDictionary->_dictionary];
5331}
5332
5333- (NSUInteger)hash {
5334  return _dictionary.count;
5335}
5336
5337- (NSString *)description {
5338  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5339}
5340
5341- (NSUInteger)count {
5342  return _dictionary.count;
5343}
5344
5345- (void)enumerateKeysAndObjectsUsingBlock:
5346    (void (^)(int32_t key, id object, BOOL *stop))block {
5347  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5348                                                   id aObject,
5349                                                   BOOL *stop) {
5350      block([aKey intValue], aObject, stop);
5351  }];
5352}
5353
5354- (BOOL)isInitialized {
5355  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
5356    if (!msg.initialized) {
5357      return NO;
5358    }
5359  }
5360  return YES;
5361}
5362
5363- (instancetype)deepCopyWithZone:(NSZone *)zone {
5364  GPBInt32ObjectDictionary *newDict =
5365      [[GPBInt32ObjectDictionary alloc] init];
5366  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
5367                                                   GPBMessage *msg,
5368                                                   BOOL *stop) {
5369    #pragma unused(stop)
5370    GPBMessage *copiedMsg = [msg copyWithZone:zone];
5371    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
5372    [copiedMsg release];
5373  }];
5374  return newDict;
5375}
5376
5377- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5378  NSUInteger count = _dictionary.count;
5379  if (count == 0) {
5380    return 0;
5381  }
5382
5383  GPBDataType valueDataType = GPBGetFieldDataType(field);
5384  GPBDataType keyDataType = field.mapKeyDataType;
5385  __block size_t result = 0;
5386  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5387                                                   id aObject,
5388                                                   BOOL *stop) {
5389    #pragma unused(stop)
5390    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5391    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
5392    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5393  }];
5394  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5395  result += tagSize * count;
5396  return result;
5397}
5398
5399- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5400                         asField:(GPBFieldDescriptor *)field {
5401  GPBDataType valueDataType = GPBGetFieldDataType(field);
5402  GPBDataType keyDataType = field.mapKeyDataType;
5403  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5404  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5405                                                   id aObject,
5406                                                   BOOL *stop) {
5407    #pragma unused(stop)
5408    // Write the tag.
5409    [outputStream writeInt32NoTag:tag];
5410    // Write the size of the message.
5411    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5412    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
5413    [outputStream writeInt32NoTag:(int32_t)msgSize];
5414    // Write the fields.
5415    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
5416    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
5417  }];
5418}
5419
5420- (void)setGPBGenericValue:(GPBGenericValue *)value
5421     forGPBGenericValueKey:(GPBGenericValue *)key {
5422  [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
5423}
5424
5425- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5426  [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
5427      #pragma unused(stop)
5428      block([NSString stringWithFormat:@"%d", key], object);
5429  }];
5430}
5431
5432- (id)objectForKey:(int32_t)key {
5433  id result = [_dictionary objectForKey:@(key)];
5434  return result;
5435}
5436
5437- (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
5438  if (otherDictionary) {
5439    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5440    if (_autocreator) {
5441      GPBAutocreatedDictionaryModified(_autocreator, self);
5442    }
5443  }
5444}
5445
5446- (void)setObject:(id)object forKey:(int32_t)key {
5447  if (!object) {
5448    [NSException raise:NSInvalidArgumentException
5449                format:@"Attempting to add nil object to a Dictionary"];
5450  }
5451  [_dictionary setObject:object forKey:@(key)];
5452  if (_autocreator) {
5453    GPBAutocreatedDictionaryModified(_autocreator, self);
5454  }
5455}
5456
5457- (void)removeObjectForKey:(int32_t)aKey {
5458  [_dictionary removeObjectForKey:@(aKey)];
5459}
5460
5461- (void)removeAll {
5462  [_dictionary removeAllObjects];
5463}
5464
5465@end
5466
5467//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
5468// This block of code is generated, do not edit it directly.
5469
5470#pragma mark - UInt64 -> UInt32
5471
5472@implementation GPBUInt64UInt32Dictionary {
5473 @package
5474  NSMutableDictionary *_dictionary;
5475}
5476
5477+ (instancetype)dictionary {
5478  return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
5479}
5480
5481+ (instancetype)dictionaryWithUInt32:(uint32_t)value
5482                              forKey:(uint64_t)key {
5483  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
5484  // on to get the type correct.
5485  return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
5486                                                            forKeys:&key
5487                                                              count:1] autorelease];
5488}
5489
5490+ (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
5491                              forKeys:(const uint64_t [])keys
5492                                count:(NSUInteger)count {
5493  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
5494  // on to get the type correct.
5495  return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
5496                                                           forKeys:keys
5497                                                             count:count] autorelease];
5498}
5499
5500+ (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
5501  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5502  // on to get the type correct.
5503  return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5504}
5505
5506+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5507  return [[[self alloc] initWithCapacity:numItems] autorelease];
5508}
5509
5510- (instancetype)init {
5511  return [self initWithUInt32s:NULL forKeys:NULL count:0];
5512}
5513
5514- (instancetype)initWithUInt32s:(const uint32_t [])values
5515                        forKeys:(const uint64_t [])keys
5516                          count:(NSUInteger)count {
5517  self = [super init];
5518  if (self) {
5519    _dictionary = [[NSMutableDictionary alloc] init];
5520    if (count && values && keys) {
5521      for (NSUInteger i = 0; i < count; ++i) {
5522        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
5523      }
5524    }
5525  }
5526  return self;
5527}
5528
5529- (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
5530  self = [self initWithUInt32s:NULL forKeys:NULL count:0];
5531  if (self) {
5532    if (dictionary) {
5533      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5534    }
5535  }
5536  return self;
5537}
5538
5539- (instancetype)initWithCapacity:(NSUInteger)numItems {
5540  #pragma unused(numItems)
5541  return [self initWithUInt32s:NULL forKeys:NULL count:0];
5542}
5543
5544- (void)dealloc {
5545  NSAssert(!_autocreator,
5546           @"%@: Autocreator must be cleared before release, autocreator: %@",
5547           [self class], _autocreator);
5548  [_dictionary release];
5549  [super dealloc];
5550}
5551
5552- (instancetype)copyWithZone:(NSZone *)zone {
5553  return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
5554}
5555
5556- (BOOL)isEqual:(id)other {
5557  if (self == other) {
5558    return YES;
5559  }
5560  if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
5561    return NO;
5562  }
5563  GPBUInt64UInt32Dictionary *otherDictionary = other;
5564  return [_dictionary isEqual:otherDictionary->_dictionary];
5565}
5566
5567- (NSUInteger)hash {
5568  return _dictionary.count;
5569}
5570
5571- (NSString *)description {
5572  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5573}
5574
5575- (NSUInteger)count {
5576  return _dictionary.count;
5577}
5578
5579- (void)enumerateKeysAndUInt32sUsingBlock:
5580    (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
5581  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5582                                                   NSNumber *aValue,
5583                                                   BOOL *stop) {
5584      block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
5585  }];
5586}
5587
5588- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5589  NSUInteger count = _dictionary.count;
5590  if (count == 0) {
5591    return 0;
5592  }
5593
5594  GPBDataType valueDataType = GPBGetFieldDataType(field);
5595  GPBDataType keyDataType = field.mapKeyDataType;
5596  __block size_t result = 0;
5597  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5598                                                   NSNumber *aValue,
5599                                                   BOOL *stop) {
5600    #pragma unused(stop)
5601    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5602    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
5603    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5604  }];
5605  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5606  result += tagSize * count;
5607  return result;
5608}
5609
5610- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5611                         asField:(GPBFieldDescriptor *)field {
5612  GPBDataType valueDataType = GPBGetFieldDataType(field);
5613  GPBDataType keyDataType = field.mapKeyDataType;
5614  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5615  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5616                                                   NSNumber *aValue,
5617                                                   BOOL *stop) {
5618    #pragma unused(stop)
5619    // Write the tag.
5620    [outputStream writeInt32NoTag:tag];
5621    // Write the size of the message.
5622    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5623    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
5624    [outputStream writeInt32NoTag:(int32_t)msgSize];
5625    // Write the fields.
5626    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5627    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
5628  }];
5629}
5630
5631- (void)setGPBGenericValue:(GPBGenericValue *)value
5632     forGPBGenericValueKey:(GPBGenericValue *)key {
5633  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
5634}
5635
5636- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5637  [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
5638      #pragma unused(stop)
5639      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
5640  }];
5641}
5642
5643- (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
5644  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5645  if (wrapped && value) {
5646    *value = [wrapped unsignedIntValue];
5647  }
5648  return (wrapped != NULL);
5649}
5650
5651- (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
5652  if (otherDictionary) {
5653    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5654    if (_autocreator) {
5655      GPBAutocreatedDictionaryModified(_autocreator, self);
5656    }
5657  }
5658}
5659
5660- (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
5661  [_dictionary setObject:@(value) forKey:@(key)];
5662  if (_autocreator) {
5663    GPBAutocreatedDictionaryModified(_autocreator, self);
5664  }
5665}
5666
5667- (void)removeUInt32ForKey:(uint64_t)aKey {
5668  [_dictionary removeObjectForKey:@(aKey)];
5669}
5670
5671- (void)removeAll {
5672  [_dictionary removeAllObjects];
5673}
5674
5675@end
5676
5677#pragma mark - UInt64 -> Int32
5678
5679@implementation GPBUInt64Int32Dictionary {
5680 @package
5681  NSMutableDictionary *_dictionary;
5682}
5683
5684+ (instancetype)dictionary {
5685  return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
5686}
5687
5688+ (instancetype)dictionaryWithInt32:(int32_t)value
5689                             forKey:(uint64_t)key {
5690  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
5691  // on to get the type correct.
5692  return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
5693                                                          forKeys:&key
5694                                                            count:1] autorelease];
5695}
5696
5697+ (instancetype)dictionaryWithInt32s:(const int32_t [])values
5698                             forKeys:(const uint64_t [])keys
5699                               count:(NSUInteger)count {
5700  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
5701  // on to get the type correct.
5702  return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:values
5703                                                          forKeys:keys
5704                                                            count:count] autorelease];
5705}
5706
5707+ (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
5708  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5709  // on to get the type correct.
5710  return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5711}
5712
5713+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5714  return [[[self alloc] initWithCapacity:numItems] autorelease];
5715}
5716
5717- (instancetype)init {
5718  return [self initWithInt32s:NULL forKeys:NULL count:0];
5719}
5720
5721- (instancetype)initWithInt32s:(const int32_t [])values
5722                       forKeys:(const uint64_t [])keys
5723                         count:(NSUInteger)count {
5724  self = [super init];
5725  if (self) {
5726    _dictionary = [[NSMutableDictionary alloc] init];
5727    if (count && values && keys) {
5728      for (NSUInteger i = 0; i < count; ++i) {
5729        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
5730      }
5731    }
5732  }
5733  return self;
5734}
5735
5736- (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
5737  self = [self initWithInt32s:NULL forKeys:NULL count:0];
5738  if (self) {
5739    if (dictionary) {
5740      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5741    }
5742  }
5743  return self;
5744}
5745
5746- (instancetype)initWithCapacity:(NSUInteger)numItems {
5747  #pragma unused(numItems)
5748  return [self initWithInt32s:NULL forKeys:NULL count:0];
5749}
5750
5751- (void)dealloc {
5752  NSAssert(!_autocreator,
5753           @"%@: Autocreator must be cleared before release, autocreator: %@",
5754           [self class], _autocreator);
5755  [_dictionary release];
5756  [super dealloc];
5757}
5758
5759- (instancetype)copyWithZone:(NSZone *)zone {
5760  return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
5761}
5762
5763- (BOOL)isEqual:(id)other {
5764  if (self == other) {
5765    return YES;
5766  }
5767  if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
5768    return NO;
5769  }
5770  GPBUInt64Int32Dictionary *otherDictionary = other;
5771  return [_dictionary isEqual:otherDictionary->_dictionary];
5772}
5773
5774- (NSUInteger)hash {
5775  return _dictionary.count;
5776}
5777
5778- (NSString *)description {
5779  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5780}
5781
5782- (NSUInteger)count {
5783  return _dictionary.count;
5784}
5785
5786- (void)enumerateKeysAndInt32sUsingBlock:
5787    (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
5788  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5789                                                   NSNumber *aValue,
5790                                                   BOOL *stop) {
5791      block([aKey unsignedLongLongValue], [aValue intValue], stop);
5792  }];
5793}
5794
5795- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5796  NSUInteger count = _dictionary.count;
5797  if (count == 0) {
5798    return 0;
5799  }
5800
5801  GPBDataType valueDataType = GPBGetFieldDataType(field);
5802  GPBDataType keyDataType = field.mapKeyDataType;
5803  __block size_t result = 0;
5804  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5805                                                   NSNumber *aValue,
5806                                                   BOOL *stop) {
5807    #pragma unused(stop)
5808    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5809    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5810    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5811  }];
5812  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5813  result += tagSize * count;
5814  return result;
5815}
5816
5817- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5818                         asField:(GPBFieldDescriptor *)field {
5819  GPBDataType valueDataType = GPBGetFieldDataType(field);
5820  GPBDataType keyDataType = field.mapKeyDataType;
5821  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5822  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5823                                                   NSNumber *aValue,
5824                                                   BOOL *stop) {
5825    #pragma unused(stop)
5826    // Write the tag.
5827    [outputStream writeInt32NoTag:tag];
5828    // Write the size of the message.
5829    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5830    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5831    [outputStream writeInt32NoTag:(int32_t)msgSize];
5832    // Write the fields.
5833    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5834    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
5835  }];
5836}
5837
5838- (void)setGPBGenericValue:(GPBGenericValue *)value
5839     forGPBGenericValueKey:(GPBGenericValue *)key {
5840  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
5841}
5842
5843- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5844  [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
5845      #pragma unused(stop)
5846      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
5847  }];
5848}
5849
5850- (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
5851  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5852  if (wrapped && value) {
5853    *value = [wrapped intValue];
5854  }
5855  return (wrapped != NULL);
5856}
5857
5858- (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
5859  if (otherDictionary) {
5860    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5861    if (_autocreator) {
5862      GPBAutocreatedDictionaryModified(_autocreator, self);
5863    }
5864  }
5865}
5866
5867- (void)setInt32:(int32_t)value forKey:(uint64_t)key {
5868  [_dictionary setObject:@(value) forKey:@(key)];
5869  if (_autocreator) {
5870    GPBAutocreatedDictionaryModified(_autocreator, self);
5871  }
5872}
5873
5874- (void)removeInt32ForKey:(uint64_t)aKey {
5875  [_dictionary removeObjectForKey:@(aKey)];
5876}
5877
5878- (void)removeAll {
5879  [_dictionary removeAllObjects];
5880}
5881
5882@end
5883
5884#pragma mark - UInt64 -> UInt64
5885
5886@implementation GPBUInt64UInt64Dictionary {
5887 @package
5888  NSMutableDictionary *_dictionary;
5889}
5890
5891+ (instancetype)dictionary {
5892  return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
5893}
5894
5895+ (instancetype)dictionaryWithUInt64:(uint64_t)value
5896                              forKey:(uint64_t)key {
5897  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
5898  // on to get the type correct.
5899  return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
5900                                                            forKeys:&key
5901                                                              count:1] autorelease];
5902}
5903
5904+ (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
5905                              forKeys:(const uint64_t [])keys
5906                                count:(NSUInteger)count {
5907  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
5908  // on to get the type correct.
5909  return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
5910                                                           forKeys:keys
5911                                                             count:count] autorelease];
5912}
5913
5914+ (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
5915  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5916  // on to get the type correct.
5917  return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5918}
5919
5920+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5921  return [[[self alloc] initWithCapacity:numItems] autorelease];
5922}
5923
5924- (instancetype)init {
5925  return [self initWithUInt64s:NULL forKeys:NULL count:0];
5926}
5927
5928- (instancetype)initWithUInt64s:(const uint64_t [])values
5929                        forKeys:(const uint64_t [])keys
5930                          count:(NSUInteger)count {
5931  self = [super init];
5932  if (self) {
5933    _dictionary = [[NSMutableDictionary alloc] init];
5934    if (count && values && keys) {
5935      for (NSUInteger i = 0; i < count; ++i) {
5936        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
5937      }
5938    }
5939  }
5940  return self;
5941}
5942
5943- (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
5944  self = [self initWithUInt64s:NULL forKeys:NULL count:0];
5945  if (self) {
5946    if (dictionary) {
5947      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5948    }
5949  }
5950  return self;
5951}
5952
5953- (instancetype)initWithCapacity:(NSUInteger)numItems {
5954  #pragma unused(numItems)
5955  return [self initWithUInt64s:NULL forKeys:NULL count:0];
5956}
5957
5958- (void)dealloc {
5959  NSAssert(!_autocreator,
5960           @"%@: Autocreator must be cleared before release, autocreator: %@",
5961           [self class], _autocreator);
5962  [_dictionary release];
5963  [super dealloc];
5964}
5965
5966- (instancetype)copyWithZone:(NSZone *)zone {
5967  return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
5968}
5969
5970- (BOOL)isEqual:(id)other {
5971  if (self == other) {
5972    return YES;
5973  }
5974  if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
5975    return NO;
5976  }
5977  GPBUInt64UInt64Dictionary *otherDictionary = other;
5978  return [_dictionary isEqual:otherDictionary->_dictionary];
5979}
5980
5981- (NSUInteger)hash {
5982  return _dictionary.count;
5983}
5984
5985- (NSString *)description {
5986  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5987}
5988
5989- (NSUInteger)count {
5990  return _dictionary.count;
5991}
5992
5993- (void)enumerateKeysAndUInt64sUsingBlock:
5994    (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
5995  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5996                                                   NSNumber *aValue,
5997                                                   BOOL *stop) {
5998      block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
5999  }];
6000}
6001
6002- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6003  NSUInteger count = _dictionary.count;
6004  if (count == 0) {
6005    return 0;
6006  }
6007
6008  GPBDataType valueDataType = GPBGetFieldDataType(field);
6009  GPBDataType keyDataType = field.mapKeyDataType;
6010  __block size_t result = 0;
6011  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6012                                                   NSNumber *aValue,
6013                                                   BOOL *stop) {
6014    #pragma unused(stop)
6015    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6016    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
6017    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6018  }];
6019  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6020  result += tagSize * count;
6021  return result;
6022}
6023
6024- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6025                         asField:(GPBFieldDescriptor *)field {
6026  GPBDataType valueDataType = GPBGetFieldDataType(field);
6027  GPBDataType keyDataType = field.mapKeyDataType;
6028  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6029  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6030                                                   NSNumber *aValue,
6031                                                   BOOL *stop) {
6032    #pragma unused(stop)
6033    // Write the tag.
6034    [outputStream writeInt32NoTag:tag];
6035    // Write the size of the message.
6036    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6037    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
6038    [outputStream writeInt32NoTag:(int32_t)msgSize];
6039    // Write the fields.
6040    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6041    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
6042  }];
6043}
6044
6045- (void)setGPBGenericValue:(GPBGenericValue *)value
6046     forGPBGenericValueKey:(GPBGenericValue *)key {
6047  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
6048}
6049
6050- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6051  [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
6052      #pragma unused(stop)
6053      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
6054  }];
6055}
6056
6057- (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
6058  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6059  if (wrapped && value) {
6060    *value = [wrapped unsignedLongLongValue];
6061  }
6062  return (wrapped != NULL);
6063}
6064
6065- (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
6066  if (otherDictionary) {
6067    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6068    if (_autocreator) {
6069      GPBAutocreatedDictionaryModified(_autocreator, self);
6070    }
6071  }
6072}
6073
6074- (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
6075  [_dictionary setObject:@(value) forKey:@(key)];
6076  if (_autocreator) {
6077    GPBAutocreatedDictionaryModified(_autocreator, self);
6078  }
6079}
6080
6081- (void)removeUInt64ForKey:(uint64_t)aKey {
6082  [_dictionary removeObjectForKey:@(aKey)];
6083}
6084
6085- (void)removeAll {
6086  [_dictionary removeAllObjects];
6087}
6088
6089@end
6090
6091#pragma mark - UInt64 -> Int64
6092
6093@implementation GPBUInt64Int64Dictionary {
6094 @package
6095  NSMutableDictionary *_dictionary;
6096}
6097
6098+ (instancetype)dictionary {
6099  return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
6100}
6101
6102+ (instancetype)dictionaryWithInt64:(int64_t)value
6103                             forKey:(uint64_t)key {
6104  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
6105  // on to get the type correct.
6106  return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
6107                                                          forKeys:&key
6108                                                            count:1] autorelease];
6109}
6110
6111+ (instancetype)dictionaryWithInt64s:(const int64_t [])values
6112                             forKeys:(const uint64_t [])keys
6113                               count:(NSUInteger)count {
6114  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
6115  // on to get the type correct.
6116  return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:values
6117                                                          forKeys:keys
6118                                                            count:count] autorelease];
6119}
6120
6121+ (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
6122  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6123  // on to get the type correct.
6124  return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6125}
6126
6127+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6128  return [[[self alloc] initWithCapacity:numItems] autorelease];
6129}
6130
6131- (instancetype)init {
6132  return [self initWithInt64s:NULL forKeys:NULL count:0];
6133}
6134
6135- (instancetype)initWithInt64s:(const int64_t [])values
6136                       forKeys:(const uint64_t [])keys
6137                         count:(NSUInteger)count {
6138  self = [super init];
6139  if (self) {
6140    _dictionary = [[NSMutableDictionary alloc] init];
6141    if (count && values && keys) {
6142      for (NSUInteger i = 0; i < count; ++i) {
6143        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6144      }
6145    }
6146  }
6147  return self;
6148}
6149
6150- (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
6151  self = [self initWithInt64s:NULL forKeys:NULL count:0];
6152  if (self) {
6153    if (dictionary) {
6154      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6155    }
6156  }
6157  return self;
6158}
6159
6160- (instancetype)initWithCapacity:(NSUInteger)numItems {
6161  #pragma unused(numItems)
6162  return [self initWithInt64s:NULL forKeys:NULL count:0];
6163}
6164
6165- (void)dealloc {
6166  NSAssert(!_autocreator,
6167           @"%@: Autocreator must be cleared before release, autocreator: %@",
6168           [self class], _autocreator);
6169  [_dictionary release];
6170  [super dealloc];
6171}
6172
6173- (instancetype)copyWithZone:(NSZone *)zone {
6174  return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
6175}
6176
6177- (BOOL)isEqual:(id)other {
6178  if (self == other) {
6179    return YES;
6180  }
6181  if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
6182    return NO;
6183  }
6184  GPBUInt64Int64Dictionary *otherDictionary = other;
6185  return [_dictionary isEqual:otherDictionary->_dictionary];
6186}
6187
6188- (NSUInteger)hash {
6189  return _dictionary.count;
6190}
6191
6192- (NSString *)description {
6193  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6194}
6195
6196- (NSUInteger)count {
6197  return _dictionary.count;
6198}
6199
6200- (void)enumerateKeysAndInt64sUsingBlock:
6201    (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
6202  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6203                                                   NSNumber *aValue,
6204                                                   BOOL *stop) {
6205      block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
6206  }];
6207}
6208
6209- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6210  NSUInteger count = _dictionary.count;
6211  if (count == 0) {
6212    return 0;
6213  }
6214
6215  GPBDataType valueDataType = GPBGetFieldDataType(field);
6216  GPBDataType keyDataType = field.mapKeyDataType;
6217  __block size_t result = 0;
6218  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6219                                                   NSNumber *aValue,
6220                                                   BOOL *stop) {
6221    #pragma unused(stop)
6222    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6223    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
6224    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6225  }];
6226  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6227  result += tagSize * count;
6228  return result;
6229}
6230
6231- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6232                         asField:(GPBFieldDescriptor *)field {
6233  GPBDataType valueDataType = GPBGetFieldDataType(field);
6234  GPBDataType keyDataType = field.mapKeyDataType;
6235  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6236  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6237                                                   NSNumber *aValue,
6238                                                   BOOL *stop) {
6239    #pragma unused(stop)
6240    // Write the tag.
6241    [outputStream writeInt32NoTag:tag];
6242    // Write the size of the message.
6243    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6244    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
6245    [outputStream writeInt32NoTag:(int32_t)msgSize];
6246    // Write the fields.
6247    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6248    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
6249  }];
6250}
6251
6252- (void)setGPBGenericValue:(GPBGenericValue *)value
6253     forGPBGenericValueKey:(GPBGenericValue *)key {
6254  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
6255}
6256
6257- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6258  [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
6259      #pragma unused(stop)
6260      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
6261  }];
6262}
6263
6264- (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
6265  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6266  if (wrapped && value) {
6267    *value = [wrapped longLongValue];
6268  }
6269  return (wrapped != NULL);
6270}
6271
6272- (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
6273  if (otherDictionary) {
6274    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6275    if (_autocreator) {
6276      GPBAutocreatedDictionaryModified(_autocreator, self);
6277    }
6278  }
6279}
6280
6281- (void)setInt64:(int64_t)value forKey:(uint64_t)key {
6282  [_dictionary setObject:@(value) forKey:@(key)];
6283  if (_autocreator) {
6284    GPBAutocreatedDictionaryModified(_autocreator, self);
6285  }
6286}
6287
6288- (void)removeInt64ForKey:(uint64_t)aKey {
6289  [_dictionary removeObjectForKey:@(aKey)];
6290}
6291
6292- (void)removeAll {
6293  [_dictionary removeAllObjects];
6294}
6295
6296@end
6297
6298#pragma mark - UInt64 -> Bool
6299
6300@implementation GPBUInt64BoolDictionary {
6301 @package
6302  NSMutableDictionary *_dictionary;
6303}
6304
6305+ (instancetype)dictionary {
6306  return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
6307}
6308
6309+ (instancetype)dictionaryWithBool:(BOOL)value
6310                            forKey:(uint64_t)key {
6311  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
6312  // on to get the type correct.
6313  return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:&value
6314                                                        forKeys:&key
6315                                                          count:1] autorelease];
6316}
6317
6318+ (instancetype)dictionaryWithBools:(const BOOL [])values
6319                            forKeys:(const uint64_t [])keys
6320                              count:(NSUInteger)count {
6321  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
6322  // on to get the type correct.
6323  return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:values
6324                                                         forKeys:keys
6325                                                           count:count] autorelease];
6326}
6327
6328+ (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
6329  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6330  // on to get the type correct.
6331  return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6332}
6333
6334+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6335  return [[[self alloc] initWithCapacity:numItems] autorelease];
6336}
6337
6338- (instancetype)init {
6339  return [self initWithBools:NULL forKeys:NULL count:0];
6340}
6341
6342- (instancetype)initWithBools:(const BOOL [])values
6343                      forKeys:(const uint64_t [])keys
6344                        count:(NSUInteger)count {
6345  self = [super init];
6346  if (self) {
6347    _dictionary = [[NSMutableDictionary alloc] init];
6348    if (count && values && keys) {
6349      for (NSUInteger i = 0; i < count; ++i) {
6350        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6351      }
6352    }
6353  }
6354  return self;
6355}
6356
6357- (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
6358  self = [self initWithBools:NULL forKeys:NULL count:0];
6359  if (self) {
6360    if (dictionary) {
6361      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6362    }
6363  }
6364  return self;
6365}
6366
6367- (instancetype)initWithCapacity:(NSUInteger)numItems {
6368  #pragma unused(numItems)
6369  return [self initWithBools:NULL forKeys:NULL count:0];
6370}
6371
6372- (void)dealloc {
6373  NSAssert(!_autocreator,
6374           @"%@: Autocreator must be cleared before release, autocreator: %@",
6375           [self class], _autocreator);
6376  [_dictionary release];
6377  [super dealloc];
6378}
6379
6380- (instancetype)copyWithZone:(NSZone *)zone {
6381  return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
6382}
6383
6384- (BOOL)isEqual:(id)other {
6385  if (self == other) {
6386    return YES;
6387  }
6388  if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
6389    return NO;
6390  }
6391  GPBUInt64BoolDictionary *otherDictionary = other;
6392  return [_dictionary isEqual:otherDictionary->_dictionary];
6393}
6394
6395- (NSUInteger)hash {
6396  return _dictionary.count;
6397}
6398
6399- (NSString *)description {
6400  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6401}
6402
6403- (NSUInteger)count {
6404  return _dictionary.count;
6405}
6406
6407- (void)enumerateKeysAndBoolsUsingBlock:
6408    (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
6409  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6410                                                   NSNumber *aValue,
6411                                                   BOOL *stop) {
6412      block([aKey unsignedLongLongValue], [aValue boolValue], stop);
6413  }];
6414}
6415
6416- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6417  NSUInteger count = _dictionary.count;
6418  if (count == 0) {
6419    return 0;
6420  }
6421
6422  GPBDataType valueDataType = GPBGetFieldDataType(field);
6423  GPBDataType keyDataType = field.mapKeyDataType;
6424  __block size_t result = 0;
6425  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6426                                                   NSNumber *aValue,
6427                                                   BOOL *stop) {
6428    #pragma unused(stop)
6429    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6430    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
6431    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6432  }];
6433  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6434  result += tagSize * count;
6435  return result;
6436}
6437
6438- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6439                         asField:(GPBFieldDescriptor *)field {
6440  GPBDataType valueDataType = GPBGetFieldDataType(field);
6441  GPBDataType keyDataType = field.mapKeyDataType;
6442  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6443  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6444                                                   NSNumber *aValue,
6445                                                   BOOL *stop) {
6446    #pragma unused(stop)
6447    // Write the tag.
6448    [outputStream writeInt32NoTag:tag];
6449    // Write the size of the message.
6450    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6451    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
6452    [outputStream writeInt32NoTag:(int32_t)msgSize];
6453    // Write the fields.
6454    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6455    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
6456  }];
6457}
6458
6459- (void)setGPBGenericValue:(GPBGenericValue *)value
6460     forGPBGenericValueKey:(GPBGenericValue *)key {
6461  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
6462}
6463
6464- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6465  [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
6466      #pragma unused(stop)
6467      block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
6468  }];
6469}
6470
6471- (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
6472  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6473  if (wrapped && value) {
6474    *value = [wrapped boolValue];
6475  }
6476  return (wrapped != NULL);
6477}
6478
6479- (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
6480  if (otherDictionary) {
6481    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6482    if (_autocreator) {
6483      GPBAutocreatedDictionaryModified(_autocreator, self);
6484    }
6485  }
6486}
6487
6488- (void)setBool:(BOOL)value forKey:(uint64_t)key {
6489  [_dictionary setObject:@(value) forKey:@(key)];
6490  if (_autocreator) {
6491    GPBAutocreatedDictionaryModified(_autocreator, self);
6492  }
6493}
6494
6495- (void)removeBoolForKey:(uint64_t)aKey {
6496  [_dictionary removeObjectForKey:@(aKey)];
6497}
6498
6499- (void)removeAll {
6500  [_dictionary removeAllObjects];
6501}
6502
6503@end
6504
6505#pragma mark - UInt64 -> Float
6506
6507@implementation GPBUInt64FloatDictionary {
6508 @package
6509  NSMutableDictionary *_dictionary;
6510}
6511
6512+ (instancetype)dictionary {
6513  return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
6514}
6515
6516+ (instancetype)dictionaryWithFloat:(float)value
6517                             forKey:(uint64_t)key {
6518  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
6519  // on to get the type correct.
6520  return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:&value
6521                                                          forKeys:&key
6522                                                            count:1] autorelease];
6523}
6524
6525+ (instancetype)dictionaryWithFloats:(const float [])values
6526                             forKeys:(const uint64_t [])keys
6527                               count:(NSUInteger)count {
6528  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
6529  // on to get the type correct.
6530  return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:values
6531                                                          forKeys:keys
6532                                                            count:count] autorelease];
6533}
6534
6535+ (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
6536  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6537  // on to get the type correct.
6538  return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6539}
6540
6541+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6542  return [[[self alloc] initWithCapacity:numItems] autorelease];
6543}
6544
6545- (instancetype)init {
6546  return [self initWithFloats:NULL forKeys:NULL count:0];
6547}
6548
6549- (instancetype)initWithFloats:(const float [])values
6550                       forKeys:(const uint64_t [])keys
6551                         count:(NSUInteger)count {
6552  self = [super init];
6553  if (self) {
6554    _dictionary = [[NSMutableDictionary alloc] init];
6555    if (count && values && keys) {
6556      for (NSUInteger i = 0; i < count; ++i) {
6557        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6558      }
6559    }
6560  }
6561  return self;
6562}
6563
6564- (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
6565  self = [self initWithFloats:NULL forKeys:NULL count:0];
6566  if (self) {
6567    if (dictionary) {
6568      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6569    }
6570  }
6571  return self;
6572}
6573
6574- (instancetype)initWithCapacity:(NSUInteger)numItems {
6575  #pragma unused(numItems)
6576  return [self initWithFloats:NULL forKeys:NULL count:0];
6577}
6578
6579- (void)dealloc {
6580  NSAssert(!_autocreator,
6581           @"%@: Autocreator must be cleared before release, autocreator: %@",
6582           [self class], _autocreator);
6583  [_dictionary release];
6584  [super dealloc];
6585}
6586
6587- (instancetype)copyWithZone:(NSZone *)zone {
6588  return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
6589}
6590
6591- (BOOL)isEqual:(id)other {
6592  if (self == other) {
6593    return YES;
6594  }
6595  if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
6596    return NO;
6597  }
6598  GPBUInt64FloatDictionary *otherDictionary = other;
6599  return [_dictionary isEqual:otherDictionary->_dictionary];
6600}
6601
6602- (NSUInteger)hash {
6603  return _dictionary.count;
6604}
6605
6606- (NSString *)description {
6607  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6608}
6609
6610- (NSUInteger)count {
6611  return _dictionary.count;
6612}
6613
6614- (void)enumerateKeysAndFloatsUsingBlock:
6615    (void (^)(uint64_t key, float value, BOOL *stop))block {
6616  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6617                                                   NSNumber *aValue,
6618                                                   BOOL *stop) {
6619      block([aKey unsignedLongLongValue], [aValue floatValue], stop);
6620  }];
6621}
6622
6623- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6624  NSUInteger count = _dictionary.count;
6625  if (count == 0) {
6626    return 0;
6627  }
6628
6629  GPBDataType valueDataType = GPBGetFieldDataType(field);
6630  GPBDataType keyDataType = field.mapKeyDataType;
6631  __block size_t result = 0;
6632  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6633                                                   NSNumber *aValue,
6634                                                   BOOL *stop) {
6635    #pragma unused(stop)
6636    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6637    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
6638    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6639  }];
6640  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6641  result += tagSize * count;
6642  return result;
6643}
6644
6645- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6646                         asField:(GPBFieldDescriptor *)field {
6647  GPBDataType valueDataType = GPBGetFieldDataType(field);
6648  GPBDataType keyDataType = field.mapKeyDataType;
6649  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6650  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6651                                                   NSNumber *aValue,
6652                                                   BOOL *stop) {
6653    #pragma unused(stop)
6654    // Write the tag.
6655    [outputStream writeInt32NoTag:tag];
6656    // Write the size of the message.
6657    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6658    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
6659    [outputStream writeInt32NoTag:(int32_t)msgSize];
6660    // Write the fields.
6661    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6662    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
6663  }];
6664}
6665
6666- (void)setGPBGenericValue:(GPBGenericValue *)value
6667     forGPBGenericValueKey:(GPBGenericValue *)key {
6668  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
6669}
6670
6671- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6672  [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) {
6673      #pragma unused(stop)
6674      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
6675  }];
6676}
6677
6678- (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
6679  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6680  if (wrapped && value) {
6681    *value = [wrapped floatValue];
6682  }
6683  return (wrapped != NULL);
6684}
6685
6686- (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
6687  if (otherDictionary) {
6688    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6689    if (_autocreator) {
6690      GPBAutocreatedDictionaryModified(_autocreator, self);
6691    }
6692  }
6693}
6694
6695- (void)setFloat:(float)value forKey:(uint64_t)key {
6696  [_dictionary setObject:@(value) forKey:@(key)];
6697  if (_autocreator) {
6698    GPBAutocreatedDictionaryModified(_autocreator, self);
6699  }
6700}
6701
6702- (void)removeFloatForKey:(uint64_t)aKey {
6703  [_dictionary removeObjectForKey:@(aKey)];
6704}
6705
6706- (void)removeAll {
6707  [_dictionary removeAllObjects];
6708}
6709
6710@end
6711
6712#pragma mark - UInt64 -> Double
6713
6714@implementation GPBUInt64DoubleDictionary {
6715 @package
6716  NSMutableDictionary *_dictionary;
6717}
6718
6719+ (instancetype)dictionary {
6720  return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
6721}
6722
6723+ (instancetype)dictionaryWithDouble:(double)value
6724                              forKey:(uint64_t)key {
6725  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
6726  // on to get the type correct.
6727  return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
6728                                                            forKeys:&key
6729                                                              count:1] autorelease];
6730}
6731
6732+ (instancetype)dictionaryWithDoubles:(const double [])values
6733                              forKeys:(const uint64_t [])keys
6734                                count:(NSUInteger)count {
6735  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
6736  // on to get the type correct.
6737  return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:values
6738                                                           forKeys:keys
6739                                                             count:count] autorelease];
6740}
6741
6742+ (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
6743  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6744  // on to get the type correct.
6745  return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6746}
6747
6748+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6749  return [[[self alloc] initWithCapacity:numItems] autorelease];
6750}
6751
6752- (instancetype)init {
6753  return [self initWithDoubles:NULL forKeys:NULL count:0];
6754}
6755
6756- (instancetype)initWithDoubles:(const double [])values
6757                        forKeys:(const uint64_t [])keys
6758                          count:(NSUInteger)count {
6759  self = [super init];
6760  if (self) {
6761    _dictionary = [[NSMutableDictionary alloc] init];
6762    if (count && values && keys) {
6763      for (NSUInteger i = 0; i < count; ++i) {
6764        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6765      }
6766    }
6767  }
6768  return self;
6769}
6770
6771- (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
6772  self = [self initWithDoubles:NULL forKeys:NULL count:0];
6773  if (self) {
6774    if (dictionary) {
6775      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6776    }
6777  }
6778  return self;
6779}
6780
6781- (instancetype)initWithCapacity:(NSUInteger)numItems {
6782  #pragma unused(numItems)
6783  return [self initWithDoubles:NULL forKeys:NULL count:0];
6784}
6785
6786- (void)dealloc {
6787  NSAssert(!_autocreator,
6788           @"%@: Autocreator must be cleared before release, autocreator: %@",
6789           [self class], _autocreator);
6790  [_dictionary release];
6791  [super dealloc];
6792}
6793
6794- (instancetype)copyWithZone:(NSZone *)zone {
6795  return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
6796}
6797
6798- (BOOL)isEqual:(id)other {
6799  if (self == other) {
6800    return YES;
6801  }
6802  if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
6803    return NO;
6804  }
6805  GPBUInt64DoubleDictionary *otherDictionary = other;
6806  return [_dictionary isEqual:otherDictionary->_dictionary];
6807}
6808
6809- (NSUInteger)hash {
6810  return _dictionary.count;
6811}
6812
6813- (NSString *)description {
6814  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6815}
6816
6817- (NSUInteger)count {
6818  return _dictionary.count;
6819}
6820
6821- (void)enumerateKeysAndDoublesUsingBlock:
6822    (void (^)(uint64_t key, double value, BOOL *stop))block {
6823  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6824                                                   NSNumber *aValue,
6825                                                   BOOL *stop) {
6826      block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
6827  }];
6828}
6829
6830- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6831  NSUInteger count = _dictionary.count;
6832  if (count == 0) {
6833    return 0;
6834  }
6835
6836  GPBDataType valueDataType = GPBGetFieldDataType(field);
6837  GPBDataType keyDataType = field.mapKeyDataType;
6838  __block size_t result = 0;
6839  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6840                                                   NSNumber *aValue,
6841                                                   BOOL *stop) {
6842    #pragma unused(stop)
6843    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6844    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
6845    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6846  }];
6847  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6848  result += tagSize * count;
6849  return result;
6850}
6851
6852- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6853                         asField:(GPBFieldDescriptor *)field {
6854  GPBDataType valueDataType = GPBGetFieldDataType(field);
6855  GPBDataType keyDataType = field.mapKeyDataType;
6856  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6857  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6858                                                   NSNumber *aValue,
6859                                                   BOOL *stop) {
6860    #pragma unused(stop)
6861    // Write the tag.
6862    [outputStream writeInt32NoTag:tag];
6863    // Write the size of the message.
6864    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6865    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
6866    [outputStream writeInt32NoTag:(int32_t)msgSize];
6867    // Write the fields.
6868    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6869    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
6870  }];
6871}
6872
6873- (void)setGPBGenericValue:(GPBGenericValue *)value
6874     forGPBGenericValueKey:(GPBGenericValue *)key {
6875  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
6876}
6877
6878- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6879  [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
6880      #pragma unused(stop)
6881      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
6882  }];
6883}
6884
6885- (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
6886  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6887  if (wrapped && value) {
6888    *value = [wrapped doubleValue];
6889  }
6890  return (wrapped != NULL);
6891}
6892
6893- (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
6894  if (otherDictionary) {
6895    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6896    if (_autocreator) {
6897      GPBAutocreatedDictionaryModified(_autocreator, self);
6898    }
6899  }
6900}
6901
6902- (void)setDouble:(double)value forKey:(uint64_t)key {
6903  [_dictionary setObject:@(value) forKey:@(key)];
6904  if (_autocreator) {
6905    GPBAutocreatedDictionaryModified(_autocreator, self);
6906  }
6907}
6908
6909- (void)removeDoubleForKey:(uint64_t)aKey {
6910  [_dictionary removeObjectForKey:@(aKey)];
6911}
6912
6913- (void)removeAll {
6914  [_dictionary removeAllObjects];
6915}
6916
6917@end
6918
6919#pragma mark - UInt64 -> Enum
6920
6921@implementation GPBUInt64EnumDictionary {
6922 @package
6923  NSMutableDictionary *_dictionary;
6924  GPBEnumValidationFunc _validationFunc;
6925}
6926
6927@synthesize validationFunc = _validationFunc;
6928
6929+ (instancetype)dictionary {
6930  return [[[self alloc] initWithValidationFunction:NULL
6931                                         rawValues:NULL
6932                                           forKeys:NULL
6933                                             count:0] autorelease];
6934}
6935
6936+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
6937  return [[[self alloc] initWithValidationFunction:func
6938                                         rawValues:NULL
6939                                           forKeys:NULL
6940                                             count:0] autorelease];
6941}
6942
6943+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
6944                                        rawValue:(int32_t)rawValue
6945                                          forKey:(uint64_t)key {
6946  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6947  // on to get the type correct.
6948  return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
6949                                                                   rawValues:&rawValue
6950                                                                     forKeys:&key
6951                                                                       count:1] autorelease];
6952}
6953
6954+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
6955                                       rawValues:(const int32_t [])rawValues
6956                                         forKeys:(const uint64_t [])keys
6957                                           count:(NSUInteger)count {
6958  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6959  // on to get the type correct.
6960  return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
6961                                                                   rawValues:rawValues
6962                                                                     forKeys:keys
6963                                                                       count:count] autorelease];
6964}
6965
6966+ (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
6967  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6968  // on to get the type correct.
6969  return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6970}
6971
6972+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
6973                                        capacity:(NSUInteger)numItems {
6974  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
6975}
6976
6977- (instancetype)init {
6978  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
6979}
6980
6981- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
6982  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
6983}
6984
6985- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
6986                                 rawValues:(const int32_t [])rawValues
6987                                   forKeys:(const uint64_t [])keys
6988                                     count:(NSUInteger)count {
6989  self = [super init];
6990  if (self) {
6991    _dictionary = [[NSMutableDictionary alloc] init];
6992    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
6993    if (count && rawValues && keys) {
6994      for (NSUInteger i = 0; i < count; ++i) {
6995        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
6996      }
6997    }
6998  }
6999  return self;
7000}
7001
7002- (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
7003  self = [self initWithValidationFunction:dictionary.validationFunc
7004                                rawValues:NULL
7005                                  forKeys:NULL
7006                                    count:0];
7007  if (self) {
7008    if (dictionary) {
7009      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7010    }
7011  }
7012  return self;
7013}
7014
7015- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
7016                                  capacity:(NSUInteger)numItems {
7017  #pragma unused(numItems)
7018  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
7019}
7020
7021- (void)dealloc {
7022  NSAssert(!_autocreator,
7023           @"%@: Autocreator must be cleared before release, autocreator: %@",
7024           [self class], _autocreator);
7025  [_dictionary release];
7026  [super dealloc];
7027}
7028
7029- (instancetype)copyWithZone:(NSZone *)zone {
7030  return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
7031}
7032
7033- (BOOL)isEqual:(id)other {
7034  if (self == other) {
7035    return YES;
7036  }
7037  if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
7038    return NO;
7039  }
7040  GPBUInt64EnumDictionary *otherDictionary = other;
7041  return [_dictionary isEqual:otherDictionary->_dictionary];
7042}
7043
7044- (NSUInteger)hash {
7045  return _dictionary.count;
7046}
7047
7048- (NSString *)description {
7049  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7050}
7051
7052- (NSUInteger)count {
7053  return _dictionary.count;
7054}
7055
7056- (void)enumerateKeysAndRawValuesUsingBlock:
7057    (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
7058  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7059                                                   NSNumber *aValue,
7060                                                   BOOL *stop) {
7061      block([aKey unsignedLongLongValue], [aValue intValue], stop);
7062  }];
7063}
7064
7065- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7066  NSUInteger count = _dictionary.count;
7067  if (count == 0) {
7068    return 0;
7069  }
7070
7071  GPBDataType valueDataType = GPBGetFieldDataType(field);
7072  GPBDataType keyDataType = field.mapKeyDataType;
7073  __block size_t result = 0;
7074  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7075                                                   NSNumber *aValue,
7076                                                   BOOL *stop) {
7077    #pragma unused(stop)
7078    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7079    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7080    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7081  }];
7082  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7083  result += tagSize * count;
7084  return result;
7085}
7086
7087- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7088                         asField:(GPBFieldDescriptor *)field {
7089  GPBDataType valueDataType = GPBGetFieldDataType(field);
7090  GPBDataType keyDataType = field.mapKeyDataType;
7091  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7092  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7093                                                   NSNumber *aValue,
7094                                                   BOOL *stop) {
7095    #pragma unused(stop)
7096    // Write the tag.
7097    [outputStream writeInt32NoTag:tag];
7098    // Write the size of the message.
7099    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7100    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7101    [outputStream writeInt32NoTag:(int32_t)msgSize];
7102    // Write the fields.
7103    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7104    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
7105  }];
7106}
7107
7108- (NSData *)serializedDataForUnknownValue:(int32_t)value
7109                                   forKey:(GPBGenericValue *)key
7110                              keyDataType:(GPBDataType)keyDataType {
7111  size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
7112  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
7113  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
7114  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
7115  WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
7116  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
7117  [outputStream release];
7118  return data;
7119}
7120- (void)setGPBGenericValue:(GPBGenericValue *)value
7121     forGPBGenericValueKey:(GPBGenericValue *)key {
7122  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
7123}
7124
7125- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7126  [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
7127      #pragma unused(stop)
7128      block([NSString stringWithFormat:@"%llu", key], @(value));
7129  }];
7130}
7131
7132- (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
7133  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7134  if (wrapped && value) {
7135    int32_t result = [wrapped intValue];
7136    if (!_validationFunc(result)) {
7137      result = kGPBUnrecognizedEnumeratorValue;
7138    }
7139    *value = result;
7140  }
7141  return (wrapped != NULL);
7142}
7143
7144- (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
7145  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7146  if (wrapped && rawValue) {
7147    *rawValue = [wrapped intValue];
7148  }
7149  return (wrapped != NULL);
7150}
7151
7152- (void)enumerateKeysAndEnumsUsingBlock:
7153    (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
7154  GPBEnumValidationFunc func = _validationFunc;
7155  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7156                                                   NSNumber *aValue,
7157                                                   BOOL *stop) {
7158      int32_t unwrapped = [aValue intValue];
7159      if (!func(unwrapped)) {
7160        unwrapped = kGPBUnrecognizedEnumeratorValue;
7161      }
7162      block([aKey unsignedLongLongValue], unwrapped, stop);
7163  }];
7164}
7165
7166- (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
7167  if (otherDictionary) {
7168    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7169    if (_autocreator) {
7170      GPBAutocreatedDictionaryModified(_autocreator, self);
7171    }
7172  }
7173}
7174
7175- (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
7176  [_dictionary setObject:@(value) forKey:@(key)];
7177  if (_autocreator) {
7178    GPBAutocreatedDictionaryModified(_autocreator, self);
7179  }
7180}
7181
7182- (void)removeEnumForKey:(uint64_t)aKey {
7183  [_dictionary removeObjectForKey:@(aKey)];
7184}
7185
7186- (void)removeAll {
7187  [_dictionary removeAllObjects];
7188}
7189
7190- (void)setEnum:(int32_t)value forKey:(uint64_t)key {
7191  if (!_validationFunc(value)) {
7192    [NSException raise:NSInvalidArgumentException
7193                format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
7194                       value];
7195  }
7196
7197  [_dictionary setObject:@(value) forKey:@(key)];
7198  if (_autocreator) {
7199    GPBAutocreatedDictionaryModified(_autocreator, self);
7200  }
7201}
7202
7203@end
7204
7205#pragma mark - UInt64 -> Object
7206
7207@implementation GPBUInt64ObjectDictionary {
7208 @package
7209  NSMutableDictionary *_dictionary;
7210}
7211
7212+ (instancetype)dictionary {
7213  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
7214}
7215
7216+ (instancetype)dictionaryWithObject:(id)object
7217                              forKey:(uint64_t)key {
7218  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
7219  // on to get the type correct.
7220  return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
7221                                                            forKeys:&key
7222                                                              count:1] autorelease];
7223}
7224
7225+ (instancetype)dictionaryWithObjects:(const id [])objects
7226                              forKeys:(const uint64_t [])keys
7227                                count:(NSUInteger)count {
7228  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
7229  // on to get the type correct.
7230  return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
7231                                                           forKeys:keys
7232                                                             count:count] autorelease];
7233}
7234
7235+ (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
7236  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7237  // on to get the type correct.
7238  return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7239}
7240
7241+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7242  return [[[self alloc] initWithCapacity:numItems] autorelease];
7243}
7244
7245- (instancetype)init {
7246  return [self initWithObjects:NULL forKeys:NULL count:0];
7247}
7248
7249- (instancetype)initWithObjects:(const id [])objects
7250                        forKeys:(const uint64_t [])keys
7251                          count:(NSUInteger)count {
7252  self = [super init];
7253  if (self) {
7254    _dictionary = [[NSMutableDictionary alloc] init];
7255    if (count && objects && keys) {
7256      for (NSUInteger i = 0; i < count; ++i) {
7257        if (!objects[i]) {
7258          [NSException raise:NSInvalidArgumentException
7259                      format:@"Attempting to add nil object to a Dictionary"];
7260        }
7261        [_dictionary setObject:objects[i] forKey:@(keys[i])];
7262      }
7263    }
7264  }
7265  return self;
7266}
7267
7268- (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
7269  self = [self initWithObjects:NULL forKeys:NULL count:0];
7270  if (self) {
7271    if (dictionary) {
7272      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7273    }
7274  }
7275  return self;
7276}
7277
7278- (instancetype)initWithCapacity:(NSUInteger)numItems {
7279  #pragma unused(numItems)
7280  return [self initWithObjects:NULL forKeys:NULL count:0];
7281}
7282
7283- (void)dealloc {
7284  NSAssert(!_autocreator,
7285           @"%@: Autocreator must be cleared before release, autocreator: %@",
7286           [self class], _autocreator);
7287  [_dictionary release];
7288  [super dealloc];
7289}
7290
7291- (instancetype)copyWithZone:(NSZone *)zone {
7292  return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
7293}
7294
7295- (BOOL)isEqual:(id)other {
7296  if (self == other) {
7297    return YES;
7298  }
7299  if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
7300    return NO;
7301  }
7302  GPBUInt64ObjectDictionary *otherDictionary = other;
7303  return [_dictionary isEqual:otherDictionary->_dictionary];
7304}
7305
7306- (NSUInteger)hash {
7307  return _dictionary.count;
7308}
7309
7310- (NSString *)description {
7311  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7312}
7313
7314- (NSUInteger)count {
7315  return _dictionary.count;
7316}
7317
7318- (void)enumerateKeysAndObjectsUsingBlock:
7319    (void (^)(uint64_t key, id object, BOOL *stop))block {
7320  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7321                                                   id aObject,
7322                                                   BOOL *stop) {
7323      block([aKey unsignedLongLongValue], aObject, stop);
7324  }];
7325}
7326
7327- (BOOL)isInitialized {
7328  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
7329    if (!msg.initialized) {
7330      return NO;
7331    }
7332  }
7333  return YES;
7334}
7335
7336- (instancetype)deepCopyWithZone:(NSZone *)zone {
7337  GPBUInt64ObjectDictionary *newDict =
7338      [[GPBUInt64ObjectDictionary alloc] init];
7339  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
7340                                                   GPBMessage *msg,
7341                                                   BOOL *stop) {
7342    #pragma unused(stop)
7343    GPBMessage *copiedMsg = [msg copyWithZone:zone];
7344    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
7345    [copiedMsg release];
7346  }];
7347  return newDict;
7348}
7349
7350- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7351  NSUInteger count = _dictionary.count;
7352  if (count == 0) {
7353    return 0;
7354  }
7355
7356  GPBDataType valueDataType = GPBGetFieldDataType(field);
7357  GPBDataType keyDataType = field.mapKeyDataType;
7358  __block size_t result = 0;
7359  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7360                                                   id aObject,
7361                                                   BOOL *stop) {
7362    #pragma unused(stop)
7363    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7364    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
7365    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7366  }];
7367  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7368  result += tagSize * count;
7369  return result;
7370}
7371
7372- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7373                         asField:(GPBFieldDescriptor *)field {
7374  GPBDataType valueDataType = GPBGetFieldDataType(field);
7375  GPBDataType keyDataType = field.mapKeyDataType;
7376  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7377  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7378                                                   id aObject,
7379                                                   BOOL *stop) {
7380    #pragma unused(stop)
7381    // Write the tag.
7382    [outputStream writeInt32NoTag:tag];
7383    // Write the size of the message.
7384    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7385    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
7386    [outputStream writeInt32NoTag:(int32_t)msgSize];
7387    // Write the fields.
7388    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7389    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
7390  }];
7391}
7392
7393- (void)setGPBGenericValue:(GPBGenericValue *)value
7394     forGPBGenericValueKey:(GPBGenericValue *)key {
7395  [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
7396}
7397
7398- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7399  [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
7400      #pragma unused(stop)
7401      block([NSString stringWithFormat:@"%llu", key], object);
7402  }];
7403}
7404
7405- (id)objectForKey:(uint64_t)key {
7406  id result = [_dictionary objectForKey:@(key)];
7407  return result;
7408}
7409
7410- (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
7411  if (otherDictionary) {
7412    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7413    if (_autocreator) {
7414      GPBAutocreatedDictionaryModified(_autocreator, self);
7415    }
7416  }
7417}
7418
7419- (void)setObject:(id)object forKey:(uint64_t)key {
7420  if (!object) {
7421    [NSException raise:NSInvalidArgumentException
7422                format:@"Attempting to add nil object to a Dictionary"];
7423  }
7424  [_dictionary setObject:object forKey:@(key)];
7425  if (_autocreator) {
7426    GPBAutocreatedDictionaryModified(_autocreator, self);
7427  }
7428}
7429
7430- (void)removeObjectForKey:(uint64_t)aKey {
7431  [_dictionary removeObjectForKey:@(aKey)];
7432}
7433
7434- (void)removeAll {
7435  [_dictionary removeAllObjects];
7436}
7437
7438@end
7439
7440//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
7441// This block of code is generated, do not edit it directly.
7442
7443#pragma mark - Int64 -> UInt32
7444
7445@implementation GPBInt64UInt32Dictionary {
7446 @package
7447  NSMutableDictionary *_dictionary;
7448}
7449
7450+ (instancetype)dictionary {
7451  return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
7452}
7453
7454+ (instancetype)dictionaryWithUInt32:(uint32_t)value
7455                              forKey:(int64_t)key {
7456  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
7457  // on to get the type correct.
7458  return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
7459                                                           forKeys:&key
7460                                                             count:1] autorelease];
7461}
7462
7463+ (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
7464                              forKeys:(const int64_t [])keys
7465                                count:(NSUInteger)count {
7466  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
7467  // on to get the type correct.
7468  return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
7469                                                          forKeys:keys
7470                                                            count:count] autorelease];
7471}
7472
7473+ (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
7474  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7475  // on to get the type correct.
7476  return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7477}
7478
7479+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7480  return [[[self alloc] initWithCapacity:numItems] autorelease];
7481}
7482
7483- (instancetype)init {
7484  return [self initWithUInt32s:NULL forKeys:NULL count:0];
7485}
7486
7487- (instancetype)initWithUInt32s:(const uint32_t [])values
7488                        forKeys:(const int64_t [])keys
7489                          count:(NSUInteger)count {
7490  self = [super init];
7491  if (self) {
7492    _dictionary = [[NSMutableDictionary alloc] init];
7493    if (count && values && keys) {
7494      for (NSUInteger i = 0; i < count; ++i) {
7495        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
7496      }
7497    }
7498  }
7499  return self;
7500}
7501
7502- (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
7503  self = [self initWithUInt32s:NULL forKeys:NULL count:0];
7504  if (self) {
7505    if (dictionary) {
7506      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7507    }
7508  }
7509  return self;
7510}
7511
7512- (instancetype)initWithCapacity:(NSUInteger)numItems {
7513  #pragma unused(numItems)
7514  return [self initWithUInt32s:NULL forKeys:NULL count:0];
7515}
7516
7517- (void)dealloc {
7518  NSAssert(!_autocreator,
7519           @"%@: Autocreator must be cleared before release, autocreator: %@",
7520           [self class], _autocreator);
7521  [_dictionary release];
7522  [super dealloc];
7523}
7524
7525- (instancetype)copyWithZone:(NSZone *)zone {
7526  return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
7527}
7528
7529- (BOOL)isEqual:(id)other {
7530  if (self == other) {
7531    return YES;
7532  }
7533  if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
7534    return NO;
7535  }
7536  GPBInt64UInt32Dictionary *otherDictionary = other;
7537  return [_dictionary isEqual:otherDictionary->_dictionary];
7538}
7539
7540- (NSUInteger)hash {
7541  return _dictionary.count;
7542}
7543
7544- (NSString *)description {
7545  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7546}
7547
7548- (NSUInteger)count {
7549  return _dictionary.count;
7550}
7551
7552- (void)enumerateKeysAndUInt32sUsingBlock:
7553    (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
7554  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7555                                                   NSNumber *aValue,
7556                                                   BOOL *stop) {
7557      block([aKey longLongValue], [aValue unsignedIntValue], stop);
7558  }];
7559}
7560
7561- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7562  NSUInteger count = _dictionary.count;
7563  if (count == 0) {
7564    return 0;
7565  }
7566
7567  GPBDataType valueDataType = GPBGetFieldDataType(field);
7568  GPBDataType keyDataType = field.mapKeyDataType;
7569  __block size_t result = 0;
7570  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7571                                                   NSNumber *aValue,
7572                                                   BOOL *stop) {
7573    #pragma unused(stop)
7574    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7575    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
7576    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7577  }];
7578  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7579  result += tagSize * count;
7580  return result;
7581}
7582
7583- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7584                         asField:(GPBFieldDescriptor *)field {
7585  GPBDataType valueDataType = GPBGetFieldDataType(field);
7586  GPBDataType keyDataType = field.mapKeyDataType;
7587  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7588  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7589                                                   NSNumber *aValue,
7590                                                   BOOL *stop) {
7591    #pragma unused(stop)
7592    // Write the tag.
7593    [outputStream writeInt32NoTag:tag];
7594    // Write the size of the message.
7595    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7596    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
7597    [outputStream writeInt32NoTag:(int32_t)msgSize];
7598    // Write the fields.
7599    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7600    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
7601  }];
7602}
7603
7604- (void)setGPBGenericValue:(GPBGenericValue *)value
7605     forGPBGenericValueKey:(GPBGenericValue *)key {
7606  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
7607}
7608
7609- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7610  [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
7611      #pragma unused(stop)
7612      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
7613  }];
7614}
7615
7616- (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
7617  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7618  if (wrapped && value) {
7619    *value = [wrapped unsignedIntValue];
7620  }
7621  return (wrapped != NULL);
7622}
7623
7624- (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
7625  if (otherDictionary) {
7626    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7627    if (_autocreator) {
7628      GPBAutocreatedDictionaryModified(_autocreator, self);
7629    }
7630  }
7631}
7632
7633- (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
7634  [_dictionary setObject:@(value) forKey:@(key)];
7635  if (_autocreator) {
7636    GPBAutocreatedDictionaryModified(_autocreator, self);
7637  }
7638}
7639
7640- (void)removeUInt32ForKey:(int64_t)aKey {
7641  [_dictionary removeObjectForKey:@(aKey)];
7642}
7643
7644- (void)removeAll {
7645  [_dictionary removeAllObjects];
7646}
7647
7648@end
7649
7650#pragma mark - Int64 -> Int32
7651
7652@implementation GPBInt64Int32Dictionary {
7653 @package
7654  NSMutableDictionary *_dictionary;
7655}
7656
7657+ (instancetype)dictionary {
7658  return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
7659}
7660
7661+ (instancetype)dictionaryWithInt32:(int32_t)value
7662                             forKey:(int64_t)key {
7663  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
7664  // on to get the type correct.
7665  return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
7666                                                         forKeys:&key
7667                                                           count:1] autorelease];
7668}
7669
7670+ (instancetype)dictionaryWithInt32s:(const int32_t [])values
7671                             forKeys:(const int64_t [])keys
7672                               count:(NSUInteger)count {
7673  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
7674  // on to get the type correct.
7675  return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:values
7676                                                         forKeys:keys
7677                                                           count:count] autorelease];
7678}
7679
7680+ (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
7681  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7682  // on to get the type correct.
7683  return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7684}
7685
7686+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7687  return [[[self alloc] initWithCapacity:numItems] autorelease];
7688}
7689
7690- (instancetype)init {
7691  return [self initWithInt32s:NULL forKeys:NULL count:0];
7692}
7693
7694- (instancetype)initWithInt32s:(const int32_t [])values
7695                       forKeys:(const int64_t [])keys
7696                         count:(NSUInteger)count {
7697  self = [super init];
7698  if (self) {
7699    _dictionary = [[NSMutableDictionary alloc] init];
7700    if (count && values && keys) {
7701      for (NSUInteger i = 0; i < count; ++i) {
7702        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
7703      }
7704    }
7705  }
7706  return self;
7707}
7708
7709- (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
7710  self = [self initWithInt32s:NULL forKeys:NULL count:0];
7711  if (self) {
7712    if (dictionary) {
7713      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7714    }
7715  }
7716  return self;
7717}
7718
7719- (instancetype)initWithCapacity:(NSUInteger)numItems {
7720  #pragma unused(numItems)
7721  return [self initWithInt32s:NULL forKeys:NULL count:0];
7722}
7723
7724- (void)dealloc {
7725  NSAssert(!_autocreator,
7726           @"%@: Autocreator must be cleared before release, autocreator: %@",
7727           [self class], _autocreator);
7728  [_dictionary release];
7729  [super dealloc];
7730}
7731
7732- (instancetype)copyWithZone:(NSZone *)zone {
7733  return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
7734}
7735
7736- (BOOL)isEqual:(id)other {
7737  if (self == other) {
7738    return YES;
7739  }
7740  if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
7741    return NO;
7742  }
7743  GPBInt64Int32Dictionary *otherDictionary = other;
7744  return [_dictionary isEqual:otherDictionary->_dictionary];
7745}
7746
7747- (NSUInteger)hash {
7748  return _dictionary.count;
7749}
7750
7751- (NSString *)description {
7752  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7753}
7754
7755- (NSUInteger)count {
7756  return _dictionary.count;
7757}
7758
7759- (void)enumerateKeysAndInt32sUsingBlock:
7760    (void (^)(int64_t key, int32_t value, BOOL *stop))block {
7761  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7762                                                   NSNumber *aValue,
7763                                                   BOOL *stop) {
7764      block([aKey longLongValue], [aValue intValue], stop);
7765  }];
7766}
7767
7768- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7769  NSUInteger count = _dictionary.count;
7770  if (count == 0) {
7771    return 0;
7772  }
7773
7774  GPBDataType valueDataType = GPBGetFieldDataType(field);
7775  GPBDataType keyDataType = field.mapKeyDataType;
7776  __block size_t result = 0;
7777  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7778                                                   NSNumber *aValue,
7779                                                   BOOL *stop) {
7780    #pragma unused(stop)
7781    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7782    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7783    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7784  }];
7785  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7786  result += tagSize * count;
7787  return result;
7788}
7789
7790- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7791                         asField:(GPBFieldDescriptor *)field {
7792  GPBDataType valueDataType = GPBGetFieldDataType(field);
7793  GPBDataType keyDataType = field.mapKeyDataType;
7794  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7795  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7796                                                   NSNumber *aValue,
7797                                                   BOOL *stop) {
7798    #pragma unused(stop)
7799    // Write the tag.
7800    [outputStream writeInt32NoTag:tag];
7801    // Write the size of the message.
7802    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7803    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7804    [outputStream writeInt32NoTag:(int32_t)msgSize];
7805    // Write the fields.
7806    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7807    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
7808  }];
7809}
7810
7811- (void)setGPBGenericValue:(GPBGenericValue *)value
7812     forGPBGenericValueKey:(GPBGenericValue *)key {
7813  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
7814}
7815
7816- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7817  [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
7818      #pragma unused(stop)
7819      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
7820  }];
7821}
7822
7823- (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
7824  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7825  if (wrapped && value) {
7826    *value = [wrapped intValue];
7827  }
7828  return (wrapped != NULL);
7829}
7830
7831- (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
7832  if (otherDictionary) {
7833    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7834    if (_autocreator) {
7835      GPBAutocreatedDictionaryModified(_autocreator, self);
7836    }
7837  }
7838}
7839
7840- (void)setInt32:(int32_t)value forKey:(int64_t)key {
7841  [_dictionary setObject:@(value) forKey:@(key)];
7842  if (_autocreator) {
7843    GPBAutocreatedDictionaryModified(_autocreator, self);
7844  }
7845}
7846
7847- (void)removeInt32ForKey:(int64_t)aKey {
7848  [_dictionary removeObjectForKey:@(aKey)];
7849}
7850
7851- (void)removeAll {
7852  [_dictionary removeAllObjects];
7853}
7854
7855@end
7856
7857#pragma mark - Int64 -> UInt64
7858
7859@implementation GPBInt64UInt64Dictionary {
7860 @package
7861  NSMutableDictionary *_dictionary;
7862}
7863
7864+ (instancetype)dictionary {
7865  return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
7866}
7867
7868+ (instancetype)dictionaryWithUInt64:(uint64_t)value
7869                              forKey:(int64_t)key {
7870  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
7871  // on to get the type correct.
7872  return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
7873                                                           forKeys:&key
7874                                                             count:1] autorelease];
7875}
7876
7877+ (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
7878                              forKeys:(const int64_t [])keys
7879                                count:(NSUInteger)count {
7880  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
7881  // on to get the type correct.
7882  return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
7883                                                          forKeys:keys
7884                                                            count:count] autorelease];
7885}
7886
7887+ (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
7888  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7889  // on to get the type correct.
7890  return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7891}
7892
7893+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7894  return [[[self alloc] initWithCapacity:numItems] autorelease];
7895}
7896
7897- (instancetype)init {
7898  return [self initWithUInt64s:NULL forKeys:NULL count:0];
7899}
7900
7901- (instancetype)initWithUInt64s:(const uint64_t [])values
7902                        forKeys:(const int64_t [])keys
7903                          count:(NSUInteger)count {
7904  self = [super init];
7905  if (self) {
7906    _dictionary = [[NSMutableDictionary alloc] init];
7907    if (count && values && keys) {
7908      for (NSUInteger i = 0; i < count; ++i) {
7909        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
7910      }
7911    }
7912  }
7913  return self;
7914}
7915
7916- (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
7917  self = [self initWithUInt64s:NULL forKeys:NULL count:0];
7918  if (self) {
7919    if (dictionary) {
7920      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7921    }
7922  }
7923  return self;
7924}
7925
7926- (instancetype)initWithCapacity:(NSUInteger)numItems {
7927  #pragma unused(numItems)
7928  return [self initWithUInt64s:NULL forKeys:NULL count:0];
7929}
7930
7931- (void)dealloc {
7932  NSAssert(!_autocreator,
7933           @"%@: Autocreator must be cleared before release, autocreator: %@",
7934           [self class], _autocreator);
7935  [_dictionary release];
7936  [super dealloc];
7937}
7938
7939- (instancetype)copyWithZone:(NSZone *)zone {
7940  return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
7941}
7942
7943- (BOOL)isEqual:(id)other {
7944  if (self == other) {
7945    return YES;
7946  }
7947  if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
7948    return NO;
7949  }
7950  GPBInt64UInt64Dictionary *otherDictionary = other;
7951  return [_dictionary isEqual:otherDictionary->_dictionary];
7952}
7953
7954- (NSUInteger)hash {
7955  return _dictionary.count;
7956}
7957
7958- (NSString *)description {
7959  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7960}
7961
7962- (NSUInteger)count {
7963  return _dictionary.count;
7964}
7965
7966- (void)enumerateKeysAndUInt64sUsingBlock:
7967    (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
7968  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7969                                                   NSNumber *aValue,
7970                                                   BOOL *stop) {
7971      block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
7972  }];
7973}
7974
7975- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7976  NSUInteger count = _dictionary.count;
7977  if (count == 0) {
7978    return 0;
7979  }
7980
7981  GPBDataType valueDataType = GPBGetFieldDataType(field);
7982  GPBDataType keyDataType = field.mapKeyDataType;
7983  __block size_t result = 0;
7984  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7985                                                   NSNumber *aValue,
7986                                                   BOOL *stop) {
7987    #pragma unused(stop)
7988    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7989    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
7990    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7991  }];
7992  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7993  result += tagSize * count;
7994  return result;
7995}
7996
7997- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7998                         asField:(GPBFieldDescriptor *)field {
7999  GPBDataType valueDataType = GPBGetFieldDataType(field);
8000  GPBDataType keyDataType = field.mapKeyDataType;
8001  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8002  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8003                                                   NSNumber *aValue,
8004                                                   BOOL *stop) {
8005    #pragma unused(stop)
8006    // Write the tag.
8007    [outputStream writeInt32NoTag:tag];
8008    // Write the size of the message.
8009    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8010    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
8011    [outputStream writeInt32NoTag:(int32_t)msgSize];
8012    // Write the fields.
8013    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8014    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
8015  }];
8016}
8017
8018- (void)setGPBGenericValue:(GPBGenericValue *)value
8019     forGPBGenericValueKey:(GPBGenericValue *)key {
8020  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
8021}
8022
8023- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8024  [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
8025      #pragma unused(stop)
8026      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
8027  }];
8028}
8029
8030- (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
8031  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8032  if (wrapped && value) {
8033    *value = [wrapped unsignedLongLongValue];
8034  }
8035  return (wrapped != NULL);
8036}
8037
8038- (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
8039  if (otherDictionary) {
8040    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8041    if (_autocreator) {
8042      GPBAutocreatedDictionaryModified(_autocreator, self);
8043    }
8044  }
8045}
8046
8047- (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
8048  [_dictionary setObject:@(value) forKey:@(key)];
8049  if (_autocreator) {
8050    GPBAutocreatedDictionaryModified(_autocreator, self);
8051  }
8052}
8053
8054- (void)removeUInt64ForKey:(int64_t)aKey {
8055  [_dictionary removeObjectForKey:@(aKey)];
8056}
8057
8058- (void)removeAll {
8059  [_dictionary removeAllObjects];
8060}
8061
8062@end
8063
8064#pragma mark - Int64 -> Int64
8065
8066@implementation GPBInt64Int64Dictionary {
8067 @package
8068  NSMutableDictionary *_dictionary;
8069}
8070
8071+ (instancetype)dictionary {
8072  return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
8073}
8074
8075+ (instancetype)dictionaryWithInt64:(int64_t)value
8076                             forKey:(int64_t)key {
8077  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
8078  // on to get the type correct.
8079  return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
8080                                                         forKeys:&key
8081                                                           count:1] autorelease];
8082}
8083
8084+ (instancetype)dictionaryWithInt64s:(const int64_t [])values
8085                             forKeys:(const int64_t [])keys
8086                               count:(NSUInteger)count {
8087  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
8088  // on to get the type correct.
8089  return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:values
8090                                                         forKeys:keys
8091                                                           count:count] autorelease];
8092}
8093
8094+ (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
8095  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8096  // on to get the type correct.
8097  return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8098}
8099
8100+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8101  return [[[self alloc] initWithCapacity:numItems] autorelease];
8102}
8103
8104- (instancetype)init {
8105  return [self initWithInt64s:NULL forKeys:NULL count:0];
8106}
8107
8108- (instancetype)initWithInt64s:(const int64_t [])values
8109                       forKeys:(const int64_t [])keys
8110                         count:(NSUInteger)count {
8111  self = [super init];
8112  if (self) {
8113    _dictionary = [[NSMutableDictionary alloc] init];
8114    if (count && values && keys) {
8115      for (NSUInteger i = 0; i < count; ++i) {
8116        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8117      }
8118    }
8119  }
8120  return self;
8121}
8122
8123- (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
8124  self = [self initWithInt64s:NULL forKeys:NULL count:0];
8125  if (self) {
8126    if (dictionary) {
8127      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8128    }
8129  }
8130  return self;
8131}
8132
8133- (instancetype)initWithCapacity:(NSUInteger)numItems {
8134  #pragma unused(numItems)
8135  return [self initWithInt64s:NULL forKeys:NULL count:0];
8136}
8137
8138- (void)dealloc {
8139  NSAssert(!_autocreator,
8140           @"%@: Autocreator must be cleared before release, autocreator: %@",
8141           [self class], _autocreator);
8142  [_dictionary release];
8143  [super dealloc];
8144}
8145
8146- (instancetype)copyWithZone:(NSZone *)zone {
8147  return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
8148}
8149
8150- (BOOL)isEqual:(id)other {
8151  if (self == other) {
8152    return YES;
8153  }
8154  if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
8155    return NO;
8156  }
8157  GPBInt64Int64Dictionary *otherDictionary = other;
8158  return [_dictionary isEqual:otherDictionary->_dictionary];
8159}
8160
8161- (NSUInteger)hash {
8162  return _dictionary.count;
8163}
8164
8165- (NSString *)description {
8166  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8167}
8168
8169- (NSUInteger)count {
8170  return _dictionary.count;
8171}
8172
8173- (void)enumerateKeysAndInt64sUsingBlock:
8174    (void (^)(int64_t key, int64_t value, BOOL *stop))block {
8175  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8176                                                   NSNumber *aValue,
8177                                                   BOOL *stop) {
8178      block([aKey longLongValue], [aValue longLongValue], stop);
8179  }];
8180}
8181
8182- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8183  NSUInteger count = _dictionary.count;
8184  if (count == 0) {
8185    return 0;
8186  }
8187
8188  GPBDataType valueDataType = GPBGetFieldDataType(field);
8189  GPBDataType keyDataType = field.mapKeyDataType;
8190  __block size_t result = 0;
8191  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8192                                                   NSNumber *aValue,
8193                                                   BOOL *stop) {
8194    #pragma unused(stop)
8195    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8196    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
8197    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8198  }];
8199  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8200  result += tagSize * count;
8201  return result;
8202}
8203
8204- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8205                         asField:(GPBFieldDescriptor *)field {
8206  GPBDataType valueDataType = GPBGetFieldDataType(field);
8207  GPBDataType keyDataType = field.mapKeyDataType;
8208  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8209  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8210                                                   NSNumber *aValue,
8211                                                   BOOL *stop) {
8212    #pragma unused(stop)
8213    // Write the tag.
8214    [outputStream writeInt32NoTag:tag];
8215    // Write the size of the message.
8216    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8217    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
8218    [outputStream writeInt32NoTag:(int32_t)msgSize];
8219    // Write the fields.
8220    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8221    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
8222  }];
8223}
8224
8225- (void)setGPBGenericValue:(GPBGenericValue *)value
8226     forGPBGenericValueKey:(GPBGenericValue *)key {
8227  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
8228}
8229
8230- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8231  [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
8232      #pragma unused(stop)
8233      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
8234  }];
8235}
8236
8237- (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
8238  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8239  if (wrapped && value) {
8240    *value = [wrapped longLongValue];
8241  }
8242  return (wrapped != NULL);
8243}
8244
8245- (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
8246  if (otherDictionary) {
8247    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8248    if (_autocreator) {
8249      GPBAutocreatedDictionaryModified(_autocreator, self);
8250    }
8251  }
8252}
8253
8254- (void)setInt64:(int64_t)value forKey:(int64_t)key {
8255  [_dictionary setObject:@(value) forKey:@(key)];
8256  if (_autocreator) {
8257    GPBAutocreatedDictionaryModified(_autocreator, self);
8258  }
8259}
8260
8261- (void)removeInt64ForKey:(int64_t)aKey {
8262  [_dictionary removeObjectForKey:@(aKey)];
8263}
8264
8265- (void)removeAll {
8266  [_dictionary removeAllObjects];
8267}
8268
8269@end
8270
8271#pragma mark - Int64 -> Bool
8272
8273@implementation GPBInt64BoolDictionary {
8274 @package
8275  NSMutableDictionary *_dictionary;
8276}
8277
8278+ (instancetype)dictionary {
8279  return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
8280}
8281
8282+ (instancetype)dictionaryWithBool:(BOOL)value
8283                            forKey:(int64_t)key {
8284  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
8285  // on to get the type correct.
8286  return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:&value
8287                                                       forKeys:&key
8288                                                         count:1] autorelease];
8289}
8290
8291+ (instancetype)dictionaryWithBools:(const BOOL [])values
8292                            forKeys:(const int64_t [])keys
8293                              count:(NSUInteger)count {
8294  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
8295  // on to get the type correct.
8296  return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:values
8297                                                        forKeys:keys
8298                                                          count:count] autorelease];
8299}
8300
8301+ (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
8302  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8303  // on to get the type correct.
8304  return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8305}
8306
8307+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8308  return [[[self alloc] initWithCapacity:numItems] autorelease];
8309}
8310
8311- (instancetype)init {
8312  return [self initWithBools:NULL forKeys:NULL count:0];
8313}
8314
8315- (instancetype)initWithBools:(const BOOL [])values
8316                      forKeys:(const int64_t [])keys
8317                        count:(NSUInteger)count {
8318  self = [super init];
8319  if (self) {
8320    _dictionary = [[NSMutableDictionary alloc] init];
8321    if (count && values && keys) {
8322      for (NSUInteger i = 0; i < count; ++i) {
8323        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8324      }
8325    }
8326  }
8327  return self;
8328}
8329
8330- (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
8331  self = [self initWithBools:NULL forKeys:NULL count:0];
8332  if (self) {
8333    if (dictionary) {
8334      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8335    }
8336  }
8337  return self;
8338}
8339
8340- (instancetype)initWithCapacity:(NSUInteger)numItems {
8341  #pragma unused(numItems)
8342  return [self initWithBools:NULL forKeys:NULL count:0];
8343}
8344
8345- (void)dealloc {
8346  NSAssert(!_autocreator,
8347           @"%@: Autocreator must be cleared before release, autocreator: %@",
8348           [self class], _autocreator);
8349  [_dictionary release];
8350  [super dealloc];
8351}
8352
8353- (instancetype)copyWithZone:(NSZone *)zone {
8354  return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
8355}
8356
8357- (BOOL)isEqual:(id)other {
8358  if (self == other) {
8359    return YES;
8360  }
8361  if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
8362    return NO;
8363  }
8364  GPBInt64BoolDictionary *otherDictionary = other;
8365  return [_dictionary isEqual:otherDictionary->_dictionary];
8366}
8367
8368- (NSUInteger)hash {
8369  return _dictionary.count;
8370}
8371
8372- (NSString *)description {
8373  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8374}
8375
8376- (NSUInteger)count {
8377  return _dictionary.count;
8378}
8379
8380- (void)enumerateKeysAndBoolsUsingBlock:
8381    (void (^)(int64_t key, BOOL value, BOOL *stop))block {
8382  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8383                                                   NSNumber *aValue,
8384                                                   BOOL *stop) {
8385      block([aKey longLongValue], [aValue boolValue], stop);
8386  }];
8387}
8388
8389- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8390  NSUInteger count = _dictionary.count;
8391  if (count == 0) {
8392    return 0;
8393  }
8394
8395  GPBDataType valueDataType = GPBGetFieldDataType(field);
8396  GPBDataType keyDataType = field.mapKeyDataType;
8397  __block size_t result = 0;
8398  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8399                                                   NSNumber *aValue,
8400                                                   BOOL *stop) {
8401    #pragma unused(stop)
8402    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8403    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
8404    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8405  }];
8406  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8407  result += tagSize * count;
8408  return result;
8409}
8410
8411- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8412                         asField:(GPBFieldDescriptor *)field {
8413  GPBDataType valueDataType = GPBGetFieldDataType(field);
8414  GPBDataType keyDataType = field.mapKeyDataType;
8415  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8416  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8417                                                   NSNumber *aValue,
8418                                                   BOOL *stop) {
8419    #pragma unused(stop)
8420    // Write the tag.
8421    [outputStream writeInt32NoTag:tag];
8422    // Write the size of the message.
8423    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8424    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
8425    [outputStream writeInt32NoTag:(int32_t)msgSize];
8426    // Write the fields.
8427    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8428    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
8429  }];
8430}
8431
8432- (void)setGPBGenericValue:(GPBGenericValue *)value
8433     forGPBGenericValueKey:(GPBGenericValue *)key {
8434  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
8435}
8436
8437- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8438  [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
8439      #pragma unused(stop)
8440      block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
8441  }];
8442}
8443
8444- (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
8445  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8446  if (wrapped && value) {
8447    *value = [wrapped boolValue];
8448  }
8449  return (wrapped != NULL);
8450}
8451
8452- (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
8453  if (otherDictionary) {
8454    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8455    if (_autocreator) {
8456      GPBAutocreatedDictionaryModified(_autocreator, self);
8457    }
8458  }
8459}
8460
8461- (void)setBool:(BOOL)value forKey:(int64_t)key {
8462  [_dictionary setObject:@(value) forKey:@(key)];
8463  if (_autocreator) {
8464    GPBAutocreatedDictionaryModified(_autocreator, self);
8465  }
8466}
8467
8468- (void)removeBoolForKey:(int64_t)aKey {
8469  [_dictionary removeObjectForKey:@(aKey)];
8470}
8471
8472- (void)removeAll {
8473  [_dictionary removeAllObjects];
8474}
8475
8476@end
8477
8478#pragma mark - Int64 -> Float
8479
8480@implementation GPBInt64FloatDictionary {
8481 @package
8482  NSMutableDictionary *_dictionary;
8483}
8484
8485+ (instancetype)dictionary {
8486  return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
8487}
8488
8489+ (instancetype)dictionaryWithFloat:(float)value
8490                             forKey:(int64_t)key {
8491  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
8492  // on to get the type correct.
8493  return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:&value
8494                                                         forKeys:&key
8495                                                           count:1] autorelease];
8496}
8497
8498+ (instancetype)dictionaryWithFloats:(const float [])values
8499                             forKeys:(const int64_t [])keys
8500                               count:(NSUInteger)count {
8501  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
8502  // on to get the type correct.
8503  return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:values
8504                                                         forKeys:keys
8505                                                           count:count] autorelease];
8506}
8507
8508+ (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
8509  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8510  // on to get the type correct.
8511  return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8512}
8513
8514+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8515  return [[[self alloc] initWithCapacity:numItems] autorelease];
8516}
8517
8518- (instancetype)init {
8519  return [self initWithFloats:NULL forKeys:NULL count:0];
8520}
8521
8522- (instancetype)initWithFloats:(const float [])values
8523                       forKeys:(const int64_t [])keys
8524                         count:(NSUInteger)count {
8525  self = [super init];
8526  if (self) {
8527    _dictionary = [[NSMutableDictionary alloc] init];
8528    if (count && values && keys) {
8529      for (NSUInteger i = 0; i < count; ++i) {
8530        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8531      }
8532    }
8533  }
8534  return self;
8535}
8536
8537- (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
8538  self = [self initWithFloats:NULL forKeys:NULL count:0];
8539  if (self) {
8540    if (dictionary) {
8541      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8542    }
8543  }
8544  return self;
8545}
8546
8547- (instancetype)initWithCapacity:(NSUInteger)numItems {
8548  #pragma unused(numItems)
8549  return [self initWithFloats:NULL forKeys:NULL count:0];
8550}
8551
8552- (void)dealloc {
8553  NSAssert(!_autocreator,
8554           @"%@: Autocreator must be cleared before release, autocreator: %@",
8555           [self class], _autocreator);
8556  [_dictionary release];
8557  [super dealloc];
8558}
8559
8560- (instancetype)copyWithZone:(NSZone *)zone {
8561  return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
8562}
8563
8564- (BOOL)isEqual:(id)other {
8565  if (self == other) {
8566    return YES;
8567  }
8568  if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
8569    return NO;
8570  }
8571  GPBInt64FloatDictionary *otherDictionary = other;
8572  return [_dictionary isEqual:otherDictionary->_dictionary];
8573}
8574
8575- (NSUInteger)hash {
8576  return _dictionary.count;
8577}
8578
8579- (NSString *)description {
8580  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8581}
8582
8583- (NSUInteger)count {
8584  return _dictionary.count;
8585}
8586
8587- (void)enumerateKeysAndFloatsUsingBlock:
8588    (void (^)(int64_t key, float value, BOOL *stop))block {
8589  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8590                                                   NSNumber *aValue,
8591                                                   BOOL *stop) {
8592      block([aKey longLongValue], [aValue floatValue], stop);
8593  }];
8594}
8595
8596- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8597  NSUInteger count = _dictionary.count;
8598  if (count == 0) {
8599    return 0;
8600  }
8601
8602  GPBDataType valueDataType = GPBGetFieldDataType(field);
8603  GPBDataType keyDataType = field.mapKeyDataType;
8604  __block size_t result = 0;
8605  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8606                                                   NSNumber *aValue,
8607                                                   BOOL *stop) {
8608    #pragma unused(stop)
8609    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8610    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
8611    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8612  }];
8613  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8614  result += tagSize * count;
8615  return result;
8616}
8617
8618- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8619                         asField:(GPBFieldDescriptor *)field {
8620  GPBDataType valueDataType = GPBGetFieldDataType(field);
8621  GPBDataType keyDataType = field.mapKeyDataType;
8622  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8623  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8624                                                   NSNumber *aValue,
8625                                                   BOOL *stop) {
8626    #pragma unused(stop)
8627    // Write the tag.
8628    [outputStream writeInt32NoTag:tag];
8629    // Write the size of the message.
8630    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8631    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
8632    [outputStream writeInt32NoTag:(int32_t)msgSize];
8633    // Write the fields.
8634    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8635    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
8636  }];
8637}
8638
8639- (void)setGPBGenericValue:(GPBGenericValue *)value
8640     forGPBGenericValueKey:(GPBGenericValue *)key {
8641  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
8642}
8643
8644- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8645  [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) {
8646      #pragma unused(stop)
8647      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
8648  }];
8649}
8650
8651- (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
8652  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8653  if (wrapped && value) {
8654    *value = [wrapped floatValue];
8655  }
8656  return (wrapped != NULL);
8657}
8658
8659- (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
8660  if (otherDictionary) {
8661    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8662    if (_autocreator) {
8663      GPBAutocreatedDictionaryModified(_autocreator, self);
8664    }
8665  }
8666}
8667
8668- (void)setFloat:(float)value forKey:(int64_t)key {
8669  [_dictionary setObject:@(value) forKey:@(key)];
8670  if (_autocreator) {
8671    GPBAutocreatedDictionaryModified(_autocreator, self);
8672  }
8673}
8674
8675- (void)removeFloatForKey:(int64_t)aKey {
8676  [_dictionary removeObjectForKey:@(aKey)];
8677}
8678
8679- (void)removeAll {
8680  [_dictionary removeAllObjects];
8681}
8682
8683@end
8684
8685#pragma mark - Int64 -> Double
8686
8687@implementation GPBInt64DoubleDictionary {
8688 @package
8689  NSMutableDictionary *_dictionary;
8690}
8691
8692+ (instancetype)dictionary {
8693  return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
8694}
8695
8696+ (instancetype)dictionaryWithDouble:(double)value
8697                              forKey:(int64_t)key {
8698  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
8699  // on to get the type correct.
8700  return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
8701                                                           forKeys:&key
8702                                                             count:1] autorelease];
8703}
8704
8705+ (instancetype)dictionaryWithDoubles:(const double [])values
8706                              forKeys:(const int64_t [])keys
8707                                count:(NSUInteger)count {
8708  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
8709  // on to get the type correct.
8710  return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:values
8711                                                          forKeys:keys
8712                                                            count:count] autorelease];
8713}
8714
8715+ (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
8716  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8717  // on to get the type correct.
8718  return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8719}
8720
8721+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8722  return [[[self alloc] initWithCapacity:numItems] autorelease];
8723}
8724
8725- (instancetype)init {
8726  return [self initWithDoubles:NULL forKeys:NULL count:0];
8727}
8728
8729- (instancetype)initWithDoubles:(const double [])values
8730                        forKeys:(const int64_t [])keys
8731                          count:(NSUInteger)count {
8732  self = [super init];
8733  if (self) {
8734    _dictionary = [[NSMutableDictionary alloc] init];
8735    if (count && values && keys) {
8736      for (NSUInteger i = 0; i < count; ++i) {
8737        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8738      }
8739    }
8740  }
8741  return self;
8742}
8743
8744- (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
8745  self = [self initWithDoubles:NULL forKeys:NULL count:0];
8746  if (self) {
8747    if (dictionary) {
8748      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8749    }
8750  }
8751  return self;
8752}
8753
8754- (instancetype)initWithCapacity:(NSUInteger)numItems {
8755  #pragma unused(numItems)
8756  return [self initWithDoubles:NULL forKeys:NULL count:0];
8757}
8758
8759- (void)dealloc {
8760  NSAssert(!_autocreator,
8761           @"%@: Autocreator must be cleared before release, autocreator: %@",
8762           [self class], _autocreator);
8763  [_dictionary release];
8764  [super dealloc];
8765}
8766
8767- (instancetype)copyWithZone:(NSZone *)zone {
8768  return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
8769}
8770
8771- (BOOL)isEqual:(id)other {
8772  if (self == other) {
8773    return YES;
8774  }
8775  if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
8776    return NO;
8777  }
8778  GPBInt64DoubleDictionary *otherDictionary = other;
8779  return [_dictionary isEqual:otherDictionary->_dictionary];
8780}
8781
8782- (NSUInteger)hash {
8783  return _dictionary.count;
8784}
8785
8786- (NSString *)description {
8787  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8788}
8789
8790- (NSUInteger)count {
8791  return _dictionary.count;
8792}
8793
8794- (void)enumerateKeysAndDoublesUsingBlock:
8795    (void (^)(int64_t key, double value, BOOL *stop))block {
8796  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8797                                                   NSNumber *aValue,
8798                                                   BOOL *stop) {
8799      block([aKey longLongValue], [aValue doubleValue], stop);
8800  }];
8801}
8802
8803- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8804  NSUInteger count = _dictionary.count;
8805  if (count == 0) {
8806    return 0;
8807  }
8808
8809  GPBDataType valueDataType = GPBGetFieldDataType(field);
8810  GPBDataType keyDataType = field.mapKeyDataType;
8811  __block size_t result = 0;
8812  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8813                                                   NSNumber *aValue,
8814                                                   BOOL *stop) {
8815    #pragma unused(stop)
8816    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8817    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
8818    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8819  }];
8820  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8821  result += tagSize * count;
8822  return result;
8823}
8824
8825- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8826                         asField:(GPBFieldDescriptor *)field {
8827  GPBDataType valueDataType = GPBGetFieldDataType(field);
8828  GPBDataType keyDataType = field.mapKeyDataType;
8829  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8830  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8831                                                   NSNumber *aValue,
8832                                                   BOOL *stop) {
8833    #pragma unused(stop)
8834    // Write the tag.
8835    [outputStream writeInt32NoTag:tag];
8836    // Write the size of the message.
8837    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8838    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
8839    [outputStream writeInt32NoTag:(int32_t)msgSize];
8840    // Write the fields.
8841    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8842    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
8843  }];
8844}
8845
8846- (void)setGPBGenericValue:(GPBGenericValue *)value
8847     forGPBGenericValueKey:(GPBGenericValue *)key {
8848  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
8849}
8850
8851- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8852  [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) {
8853      #pragma unused(stop)
8854      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
8855  }];
8856}
8857
8858- (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
8859  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8860  if (wrapped && value) {
8861    *value = [wrapped doubleValue];
8862  }
8863  return (wrapped != NULL);
8864}
8865
8866- (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
8867  if (otherDictionary) {
8868    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8869    if (_autocreator) {
8870      GPBAutocreatedDictionaryModified(_autocreator, self);
8871    }
8872  }
8873}
8874
8875- (void)setDouble:(double)value forKey:(int64_t)key {
8876  [_dictionary setObject:@(value) forKey:@(key)];
8877  if (_autocreator) {
8878    GPBAutocreatedDictionaryModified(_autocreator, self);
8879  }
8880}
8881
8882- (void)removeDoubleForKey:(int64_t)aKey {
8883  [_dictionary removeObjectForKey:@(aKey)];
8884}
8885
8886- (void)removeAll {
8887  [_dictionary removeAllObjects];
8888}
8889
8890@end
8891
8892#pragma mark - Int64 -> Enum
8893
8894@implementation GPBInt64EnumDictionary {
8895 @package
8896  NSMutableDictionary *_dictionary;
8897  GPBEnumValidationFunc _validationFunc;
8898}
8899
8900@synthesize validationFunc = _validationFunc;
8901
8902+ (instancetype)dictionary {
8903  return [[[self alloc] initWithValidationFunction:NULL
8904                                         rawValues:NULL
8905                                           forKeys:NULL
8906                                             count:0] autorelease];
8907}
8908
8909+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
8910  return [[[self alloc] initWithValidationFunction:func
8911                                         rawValues:NULL
8912                                           forKeys:NULL
8913                                             count:0] autorelease];
8914}
8915
8916+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
8917                                        rawValue:(int32_t)rawValue
8918                                          forKey:(int64_t)key {
8919  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8920  // on to get the type correct.
8921  return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
8922                                                                  rawValues:&rawValue
8923                                                                    forKeys:&key
8924                                                                      count:1] autorelease];
8925}
8926
8927+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
8928                                       rawValues:(const int32_t [])rawValues
8929                                         forKeys:(const int64_t [])keys
8930                                           count:(NSUInteger)count {
8931  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8932  // on to get the type correct.
8933  return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
8934                                                                  rawValues:rawValues
8935                                                                    forKeys:keys
8936                                                                      count:count] autorelease];
8937}
8938
8939+ (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
8940  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8941  // on to get the type correct.
8942  return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8943}
8944
8945+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
8946                                        capacity:(NSUInteger)numItems {
8947  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
8948}
8949
8950- (instancetype)init {
8951  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
8952}
8953
8954- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
8955  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
8956}
8957
8958- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
8959                                 rawValues:(const int32_t [])rawValues
8960                                   forKeys:(const int64_t [])keys
8961                                     count:(NSUInteger)count {
8962  self = [super init];
8963  if (self) {
8964    _dictionary = [[NSMutableDictionary alloc] init];
8965    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
8966    if (count && rawValues && keys) {
8967      for (NSUInteger i = 0; i < count; ++i) {
8968        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
8969      }
8970    }
8971  }
8972  return self;
8973}
8974
8975- (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
8976  self = [self initWithValidationFunction:dictionary.validationFunc
8977                                rawValues:NULL
8978                                  forKeys:NULL
8979                                    count:0];
8980  if (self) {
8981    if (dictionary) {
8982      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8983    }
8984  }
8985  return self;
8986}
8987
8988- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
8989                                  capacity:(NSUInteger)numItems {
8990  #pragma unused(numItems)
8991  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
8992}
8993
8994- (void)dealloc {
8995  NSAssert(!_autocreator,
8996           @"%@: Autocreator must be cleared before release, autocreator: %@",
8997           [self class], _autocreator);
8998  [_dictionary release];
8999  [super dealloc];
9000}
9001
9002- (instancetype)copyWithZone:(NSZone *)zone {
9003  return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
9004}
9005
9006- (BOOL)isEqual:(id)other {
9007  if (self == other) {
9008    return YES;
9009  }
9010  if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
9011    return NO;
9012  }
9013  GPBInt64EnumDictionary *otherDictionary = other;
9014  return [_dictionary isEqual:otherDictionary->_dictionary];
9015}
9016
9017- (NSUInteger)hash {
9018  return _dictionary.count;
9019}
9020
9021- (NSString *)description {
9022  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9023}
9024
9025- (NSUInteger)count {
9026  return _dictionary.count;
9027}
9028
9029- (void)enumerateKeysAndRawValuesUsingBlock:
9030    (void (^)(int64_t key, int32_t value, BOOL *stop))block {
9031  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9032                                                   NSNumber *aValue,
9033                                                   BOOL *stop) {
9034      block([aKey longLongValue], [aValue intValue], stop);
9035  }];
9036}
9037
9038- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9039  NSUInteger count = _dictionary.count;
9040  if (count == 0) {
9041    return 0;
9042  }
9043
9044  GPBDataType valueDataType = GPBGetFieldDataType(field);
9045  GPBDataType keyDataType = field.mapKeyDataType;
9046  __block size_t result = 0;
9047  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9048                                                   NSNumber *aValue,
9049                                                   BOOL *stop) {
9050    #pragma unused(stop)
9051    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9052    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9053    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9054  }];
9055  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9056  result += tagSize * count;
9057  return result;
9058}
9059
9060- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9061                         asField:(GPBFieldDescriptor *)field {
9062  GPBDataType valueDataType = GPBGetFieldDataType(field);
9063  GPBDataType keyDataType = field.mapKeyDataType;
9064  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9065  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9066                                                   NSNumber *aValue,
9067                                                   BOOL *stop) {
9068    #pragma unused(stop)
9069    // Write the tag.
9070    [outputStream writeInt32NoTag:tag];
9071    // Write the size of the message.
9072    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9073    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9074    [outputStream writeInt32NoTag:(int32_t)msgSize];
9075    // Write the fields.
9076    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9077    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
9078  }];
9079}
9080
9081- (NSData *)serializedDataForUnknownValue:(int32_t)value
9082                                   forKey:(GPBGenericValue *)key
9083                              keyDataType:(GPBDataType)keyDataType {
9084  size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
9085  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
9086  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
9087  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
9088  WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
9089  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
9090  [outputStream release];
9091  return data;
9092}
9093- (void)setGPBGenericValue:(GPBGenericValue *)value
9094     forGPBGenericValueKey:(GPBGenericValue *)key {
9095  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
9096}
9097
9098- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9099  [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
9100      #pragma unused(stop)
9101      block([NSString stringWithFormat:@"%lld", key], @(value));
9102  }];
9103}
9104
9105- (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
9106  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
9107  if (wrapped && value) {
9108    int32_t result = [wrapped intValue];
9109    if (!_validationFunc(result)) {
9110      result = kGPBUnrecognizedEnumeratorValue;
9111    }
9112    *value = result;
9113  }
9114  return (wrapped != NULL);
9115}
9116
9117- (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
9118  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
9119  if (wrapped && rawValue) {
9120    *rawValue = [wrapped intValue];
9121  }
9122  return (wrapped != NULL);
9123}
9124
9125- (void)enumerateKeysAndEnumsUsingBlock:
9126    (void (^)(int64_t key, int32_t value, BOOL *stop))block {
9127  GPBEnumValidationFunc func = _validationFunc;
9128  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9129                                                   NSNumber *aValue,
9130                                                   BOOL *stop) {
9131      int32_t unwrapped = [aValue intValue];
9132      if (!func(unwrapped)) {
9133        unwrapped = kGPBUnrecognizedEnumeratorValue;
9134      }
9135      block([aKey longLongValue], unwrapped, stop);
9136  }];
9137}
9138
9139- (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
9140  if (otherDictionary) {
9141    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9142    if (_autocreator) {
9143      GPBAutocreatedDictionaryModified(_autocreator, self);
9144    }
9145  }
9146}
9147
9148- (void)setRawValue:(int32_t)value forKey:(int64_t)key {
9149  [_dictionary setObject:@(value) forKey:@(key)];
9150  if (_autocreator) {
9151    GPBAutocreatedDictionaryModified(_autocreator, self);
9152  }
9153}
9154
9155- (void)removeEnumForKey:(int64_t)aKey {
9156  [_dictionary removeObjectForKey:@(aKey)];
9157}
9158
9159- (void)removeAll {
9160  [_dictionary removeAllObjects];
9161}
9162
9163- (void)setEnum:(int32_t)value forKey:(int64_t)key {
9164  if (!_validationFunc(value)) {
9165    [NSException raise:NSInvalidArgumentException
9166                format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
9167                       value];
9168  }
9169
9170  [_dictionary setObject:@(value) forKey:@(key)];
9171  if (_autocreator) {
9172    GPBAutocreatedDictionaryModified(_autocreator, self);
9173  }
9174}
9175
9176@end
9177
9178#pragma mark - Int64 -> Object
9179
9180@implementation GPBInt64ObjectDictionary {
9181 @package
9182  NSMutableDictionary *_dictionary;
9183}
9184
9185+ (instancetype)dictionary {
9186  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
9187}
9188
9189+ (instancetype)dictionaryWithObject:(id)object
9190                              forKey:(int64_t)key {
9191  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
9192  // on to get the type correct.
9193  return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
9194                                                           forKeys:&key
9195                                                             count:1] autorelease];
9196}
9197
9198+ (instancetype)dictionaryWithObjects:(const id [])objects
9199                              forKeys:(const int64_t [])keys
9200                                count:(NSUInteger)count {
9201  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
9202  // on to get the type correct.
9203  return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
9204                                                          forKeys:keys
9205                                                            count:count] autorelease];
9206}
9207
9208+ (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
9209  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9210  // on to get the type correct.
9211  return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9212}
9213
9214+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9215  return [[[self alloc] initWithCapacity:numItems] autorelease];
9216}
9217
9218- (instancetype)init {
9219  return [self initWithObjects:NULL forKeys:NULL count:0];
9220}
9221
9222- (instancetype)initWithObjects:(const id [])objects
9223                        forKeys:(const int64_t [])keys
9224                          count:(NSUInteger)count {
9225  self = [super init];
9226  if (self) {
9227    _dictionary = [[NSMutableDictionary alloc] init];
9228    if (count && objects && keys) {
9229      for (NSUInteger i = 0; i < count; ++i) {
9230        if (!objects[i]) {
9231          [NSException raise:NSInvalidArgumentException
9232                      format:@"Attempting to add nil object to a Dictionary"];
9233        }
9234        [_dictionary setObject:objects[i] forKey:@(keys[i])];
9235      }
9236    }
9237  }
9238  return self;
9239}
9240
9241- (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
9242  self = [self initWithObjects:NULL forKeys:NULL count:0];
9243  if (self) {
9244    if (dictionary) {
9245      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9246    }
9247  }
9248  return self;
9249}
9250
9251- (instancetype)initWithCapacity:(NSUInteger)numItems {
9252  #pragma unused(numItems)
9253  return [self initWithObjects:NULL forKeys:NULL count:0];
9254}
9255
9256- (void)dealloc {
9257  NSAssert(!_autocreator,
9258           @"%@: Autocreator must be cleared before release, autocreator: %@",
9259           [self class], _autocreator);
9260  [_dictionary release];
9261  [super dealloc];
9262}
9263
9264- (instancetype)copyWithZone:(NSZone *)zone {
9265  return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
9266}
9267
9268- (BOOL)isEqual:(id)other {
9269  if (self == other) {
9270    return YES;
9271  }
9272  if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
9273    return NO;
9274  }
9275  GPBInt64ObjectDictionary *otherDictionary = other;
9276  return [_dictionary isEqual:otherDictionary->_dictionary];
9277}
9278
9279- (NSUInteger)hash {
9280  return _dictionary.count;
9281}
9282
9283- (NSString *)description {
9284  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9285}
9286
9287- (NSUInteger)count {
9288  return _dictionary.count;
9289}
9290
9291- (void)enumerateKeysAndObjectsUsingBlock:
9292    (void (^)(int64_t key, id object, BOOL *stop))block {
9293  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9294                                                   id aObject,
9295                                                   BOOL *stop) {
9296      block([aKey longLongValue], aObject, stop);
9297  }];
9298}
9299
9300- (BOOL)isInitialized {
9301  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
9302    if (!msg.initialized) {
9303      return NO;
9304    }
9305  }
9306  return YES;
9307}
9308
9309- (instancetype)deepCopyWithZone:(NSZone *)zone {
9310  GPBInt64ObjectDictionary *newDict =
9311      [[GPBInt64ObjectDictionary alloc] init];
9312  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
9313                                                   GPBMessage *msg,
9314                                                   BOOL *stop) {
9315    #pragma unused(stop)
9316    GPBMessage *copiedMsg = [msg copyWithZone:zone];
9317    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
9318    [copiedMsg release];
9319  }];
9320  return newDict;
9321}
9322
9323- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9324  NSUInteger count = _dictionary.count;
9325  if (count == 0) {
9326    return 0;
9327  }
9328
9329  GPBDataType valueDataType = GPBGetFieldDataType(field);
9330  GPBDataType keyDataType = field.mapKeyDataType;
9331  __block size_t result = 0;
9332  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9333                                                   id aObject,
9334                                                   BOOL *stop) {
9335    #pragma unused(stop)
9336    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9337    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
9338    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9339  }];
9340  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9341  result += tagSize * count;
9342  return result;
9343}
9344
9345- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9346                         asField:(GPBFieldDescriptor *)field {
9347  GPBDataType valueDataType = GPBGetFieldDataType(field);
9348  GPBDataType keyDataType = field.mapKeyDataType;
9349  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9350  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9351                                                   id aObject,
9352                                                   BOOL *stop) {
9353    #pragma unused(stop)
9354    // Write the tag.
9355    [outputStream writeInt32NoTag:tag];
9356    // Write the size of the message.
9357    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9358    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
9359    [outputStream writeInt32NoTag:(int32_t)msgSize];
9360    // Write the fields.
9361    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9362    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
9363  }];
9364}
9365
9366- (void)setGPBGenericValue:(GPBGenericValue *)value
9367     forGPBGenericValueKey:(GPBGenericValue *)key {
9368  [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
9369}
9370
9371- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9372  [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
9373      #pragma unused(stop)
9374      block([NSString stringWithFormat:@"%lld", key], object);
9375  }];
9376}
9377
9378- (id)objectForKey:(int64_t)key {
9379  id result = [_dictionary objectForKey:@(key)];
9380  return result;
9381}
9382
9383- (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
9384  if (otherDictionary) {
9385    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9386    if (_autocreator) {
9387      GPBAutocreatedDictionaryModified(_autocreator, self);
9388    }
9389  }
9390}
9391
9392- (void)setObject:(id)object forKey:(int64_t)key {
9393  if (!object) {
9394    [NSException raise:NSInvalidArgumentException
9395                format:@"Attempting to add nil object to a Dictionary"];
9396  }
9397  [_dictionary setObject:object forKey:@(key)];
9398  if (_autocreator) {
9399    GPBAutocreatedDictionaryModified(_autocreator, self);
9400  }
9401}
9402
9403- (void)removeObjectForKey:(int64_t)aKey {
9404  [_dictionary removeObjectForKey:@(aKey)];
9405}
9406
9407- (void)removeAll {
9408  [_dictionary removeAllObjects];
9409}
9410
9411@end
9412
9413//%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
9414// This block of code is generated, do not edit it directly.
9415
9416#pragma mark - String -> UInt32
9417
9418@implementation GPBStringUInt32Dictionary {
9419 @package
9420  NSMutableDictionary *_dictionary;
9421}
9422
9423+ (instancetype)dictionary {
9424  return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
9425}
9426
9427+ (instancetype)dictionaryWithUInt32:(uint32_t)value
9428                              forKey:(NSString *)key {
9429  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
9430  // on to get the type correct.
9431  return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:&value
9432                                                            forKeys:&key
9433                                                              count:1] autorelease];
9434}
9435
9436+ (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
9437                              forKeys:(const NSString * [])keys
9438                                count:(NSUInteger)count {
9439  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
9440  // on to get the type correct.
9441  return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:values
9442                                                           forKeys:keys
9443                                                             count:count] autorelease];
9444}
9445
9446+ (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
9447  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9448  // on to get the type correct.
9449  return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9450}
9451
9452+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9453  return [[[self alloc] initWithCapacity:numItems] autorelease];
9454}
9455
9456- (instancetype)init {
9457  return [self initWithUInt32s:NULL forKeys:NULL count:0];
9458}
9459
9460- (instancetype)initWithUInt32s:(const uint32_t [])values
9461                        forKeys:(const NSString * [])keys
9462                          count:(NSUInteger)count {
9463  self = [super init];
9464  if (self) {
9465    _dictionary = [[NSMutableDictionary alloc] init];
9466    if (count && values && keys) {
9467      for (NSUInteger i = 0; i < count; ++i) {
9468        if (!keys[i]) {
9469          [NSException raise:NSInvalidArgumentException
9470                      format:@"Attempting to add nil key to a Dictionary"];
9471        }
9472        [_dictionary setObject:@(values[i]) forKey:keys[i]];
9473      }
9474    }
9475  }
9476  return self;
9477}
9478
9479- (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
9480  self = [self initWithUInt32s:NULL forKeys:NULL count:0];
9481  if (self) {
9482    if (dictionary) {
9483      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9484    }
9485  }
9486  return self;
9487}
9488
9489- (instancetype)initWithCapacity:(NSUInteger)numItems {
9490  #pragma unused(numItems)
9491  return [self initWithUInt32s:NULL forKeys:NULL count:0];
9492}
9493
9494- (void)dealloc {
9495  NSAssert(!_autocreator,
9496           @"%@: Autocreator must be cleared before release, autocreator: %@",
9497           [self class], _autocreator);
9498  [_dictionary release];
9499  [super dealloc];
9500}
9501
9502- (instancetype)copyWithZone:(NSZone *)zone {
9503  return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
9504}
9505
9506- (BOOL)isEqual:(id)other {
9507  if (self == other) {
9508    return YES;
9509  }
9510  if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
9511    return NO;
9512  }
9513  GPBStringUInt32Dictionary *otherDictionary = other;
9514  return [_dictionary isEqual:otherDictionary->_dictionary];
9515}
9516
9517- (NSUInteger)hash {
9518  return _dictionary.count;
9519}
9520
9521- (NSString *)description {
9522  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9523}
9524
9525- (NSUInteger)count {
9526  return _dictionary.count;
9527}
9528
9529- (void)enumerateKeysAndUInt32sUsingBlock:
9530    (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
9531  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9532                                                   NSNumber *aValue,
9533                                                   BOOL *stop) {
9534      block(aKey, [aValue unsignedIntValue], stop);
9535  }];
9536}
9537
9538- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9539  NSUInteger count = _dictionary.count;
9540  if (count == 0) {
9541    return 0;
9542  }
9543
9544  GPBDataType valueDataType = GPBGetFieldDataType(field);
9545  GPBDataType keyDataType = field.mapKeyDataType;
9546  __block size_t result = 0;
9547  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9548                                                   NSNumber *aValue,
9549                                                   BOOL *stop) {
9550    #pragma unused(stop)
9551    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9552    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
9553    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9554  }];
9555  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9556  result += tagSize * count;
9557  return result;
9558}
9559
9560- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9561                         asField:(GPBFieldDescriptor *)field {
9562  GPBDataType valueDataType = GPBGetFieldDataType(field);
9563  GPBDataType keyDataType = field.mapKeyDataType;
9564  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9565  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9566                                                   NSNumber *aValue,
9567                                                   BOOL *stop) {
9568    #pragma unused(stop)
9569    // Write the tag.
9570    [outputStream writeInt32NoTag:tag];
9571    // Write the size of the message.
9572    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9573    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
9574    [outputStream writeInt32NoTag:(int32_t)msgSize];
9575    // Write the fields.
9576    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
9577    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
9578  }];
9579}
9580
9581- (void)setGPBGenericValue:(GPBGenericValue *)value
9582     forGPBGenericValueKey:(GPBGenericValue *)key {
9583  [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
9584}
9585
9586- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9587  [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
9588      #pragma unused(stop)
9589      block(key, [NSString stringWithFormat:@"%u", value]);
9590  }];
9591}
9592
9593- (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
9594  NSNumber *wrapped = [_dictionary objectForKey:key];
9595  if (wrapped && value) {
9596    *value = [wrapped unsignedIntValue];
9597  }
9598  return (wrapped != NULL);
9599}
9600
9601- (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
9602  if (otherDictionary) {
9603    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9604    if (_autocreator) {
9605      GPBAutocreatedDictionaryModified(_autocreator, self);
9606    }
9607  }
9608}
9609
9610- (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
9611  if (!key) {
9612    [NSException raise:NSInvalidArgumentException
9613                format:@"Attempting to add nil key to a Dictionary"];
9614  }
9615  [_dictionary setObject:@(value) forKey:key];
9616  if (_autocreator) {
9617    GPBAutocreatedDictionaryModified(_autocreator, self);
9618  }
9619}
9620
9621- (void)removeUInt32ForKey:(NSString *)aKey {
9622  [_dictionary removeObjectForKey:aKey];
9623}
9624
9625- (void)removeAll {
9626  [_dictionary removeAllObjects];
9627}
9628
9629@end
9630
9631#pragma mark - String -> Int32
9632
9633@implementation GPBStringInt32Dictionary {
9634 @package
9635  NSMutableDictionary *_dictionary;
9636}
9637
9638+ (instancetype)dictionary {
9639  return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
9640}
9641
9642+ (instancetype)dictionaryWithInt32:(int32_t)value
9643                             forKey:(NSString *)key {
9644  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
9645  // on to get the type correct.
9646  return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:&value
9647                                                          forKeys:&key
9648                                                            count:1] autorelease];
9649}
9650
9651+ (instancetype)dictionaryWithInt32s:(const int32_t [])values
9652                             forKeys:(const NSString * [])keys
9653                               count:(NSUInteger)count {
9654  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
9655  // on to get the type correct.
9656  return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:values
9657                                                          forKeys:keys
9658                                                            count:count] autorelease];
9659}
9660
9661+ (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
9662  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9663  // on to get the type correct.
9664  return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9665}
9666
9667+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9668  return [[[self alloc] initWithCapacity:numItems] autorelease];
9669}
9670
9671- (instancetype)init {
9672  return [self initWithInt32s:NULL forKeys:NULL count:0];
9673}
9674
9675- (instancetype)initWithInt32s:(const int32_t [])values
9676                       forKeys:(const NSString * [])keys
9677                         count:(NSUInteger)count {
9678  self = [super init];
9679  if (self) {
9680    _dictionary = [[NSMutableDictionary alloc] init];
9681    if (count && values && keys) {
9682      for (NSUInteger i = 0; i < count; ++i) {
9683        if (!keys[i]) {
9684          [NSException raise:NSInvalidArgumentException
9685                      format:@"Attempting to add nil key to a Dictionary"];
9686        }
9687        [_dictionary setObject:@(values[i]) forKey:keys[i]];
9688      }
9689    }
9690  }
9691  return self;
9692}
9693
9694- (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
9695  self = [self initWithInt32s:NULL forKeys:NULL count:0];
9696  if (self) {
9697    if (dictionary) {
9698      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9699    }
9700  }
9701  return self;
9702}
9703
9704- (instancetype)initWithCapacity:(NSUInteger)numItems {
9705  #pragma unused(numItems)
9706  return [self initWithInt32s:NULL forKeys:NULL count:0];
9707}
9708
9709- (void)dealloc {
9710  NSAssert(!_autocreator,
9711           @"%@: Autocreator must be cleared before release, autocreator: %@",
9712           [self class], _autocreator);
9713  [_dictionary release];
9714  [super dealloc];
9715}
9716
9717- (instancetype)copyWithZone:(NSZone *)zone {
9718  return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
9719}
9720
9721- (BOOL)isEqual:(id)other {
9722  if (self == other) {
9723    return YES;
9724  }
9725  if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
9726    return NO;
9727  }
9728  GPBStringInt32Dictionary *otherDictionary = other;
9729  return [_dictionary isEqual:otherDictionary->_dictionary];
9730}
9731
9732- (NSUInteger)hash {
9733  return _dictionary.count;
9734}
9735
9736- (NSString *)description {
9737  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9738}
9739
9740- (NSUInteger)count {
9741  return _dictionary.count;
9742}
9743
9744- (void)enumerateKeysAndInt32sUsingBlock:
9745    (void (^)(NSString *key, int32_t value, BOOL *stop))block {
9746  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9747                                                   NSNumber *aValue,
9748                                                   BOOL *stop) {
9749      block(aKey, [aValue intValue], stop);
9750  }];
9751}
9752
9753- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9754  NSUInteger count = _dictionary.count;
9755  if (count == 0) {
9756    return 0;
9757  }
9758
9759  GPBDataType valueDataType = GPBGetFieldDataType(field);
9760  GPBDataType keyDataType = field.mapKeyDataType;
9761  __block size_t result = 0;
9762  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9763                                                   NSNumber *aValue,
9764                                                   BOOL *stop) {
9765    #pragma unused(stop)
9766    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9767    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9768    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9769  }];
9770  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9771  result += tagSize * count;
9772  return result;
9773}
9774
9775- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9776                         asField:(GPBFieldDescriptor *)field {
9777  GPBDataType valueDataType = GPBGetFieldDataType(field);
9778  GPBDataType keyDataType = field.mapKeyDataType;
9779  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9780  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9781                                                   NSNumber *aValue,
9782                                                   BOOL *stop) {
9783    #pragma unused(stop)
9784    // Write the tag.
9785    [outputStream writeInt32NoTag:tag];
9786    // Write the size of the message.
9787    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9788    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9789    [outputStream writeInt32NoTag:(int32_t)msgSize];
9790    // Write the fields.
9791    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
9792    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
9793  }];
9794}
9795
9796- (void)setGPBGenericValue:(GPBGenericValue *)value
9797     forGPBGenericValueKey:(GPBGenericValue *)key {
9798  [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
9799}
9800
9801- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9802  [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
9803      #pragma unused(stop)
9804      block(key, [NSString stringWithFormat:@"%d", value]);
9805  }];
9806}
9807
9808- (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
9809  NSNumber *wrapped = [_dictionary objectForKey:key];
9810  if (wrapped && value) {
9811    *value = [wrapped intValue];
9812  }
9813  return (wrapped != NULL);
9814}
9815
9816- (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
9817  if (otherDictionary) {
9818    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9819    if (_autocreator) {
9820      GPBAutocreatedDictionaryModified(_autocreator, self);
9821    }
9822  }
9823}
9824
9825- (void)setInt32:(int32_t)value forKey:(NSString *)key {
9826  if (!key) {
9827    [NSException raise:NSInvalidArgumentException
9828                format:@"Attempting to add nil key to a Dictionary"];
9829  }
9830  [_dictionary setObject:@(value) forKey:key];
9831  if (_autocreator) {
9832    GPBAutocreatedDictionaryModified(_autocreator, self);
9833  }
9834}
9835
9836- (void)removeInt32ForKey:(NSString *)aKey {
9837  [_dictionary removeObjectForKey:aKey];
9838}
9839
9840- (void)removeAll {
9841  [_dictionary removeAllObjects];
9842}
9843
9844@end
9845
9846#pragma mark - String -> UInt64
9847
9848@implementation GPBStringUInt64Dictionary {
9849 @package
9850  NSMutableDictionary *_dictionary;
9851}
9852
9853+ (instancetype)dictionary {
9854  return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
9855}
9856
9857+ (instancetype)dictionaryWithUInt64:(uint64_t)value
9858                              forKey:(NSString *)key {
9859  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
9860  // on to get the type correct.
9861  return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:&value
9862                                                            forKeys:&key
9863                                                              count:1] autorelease];
9864}
9865
9866+ (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
9867                              forKeys:(const NSString * [])keys
9868                                count:(NSUInteger)count {
9869  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
9870  // on to get the type correct.
9871  return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:values
9872                                                           forKeys:keys
9873                                                             count:count] autorelease];
9874}
9875
9876+ (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
9877  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9878  // on to get the type correct.
9879  return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9880}
9881
9882+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9883  return [[[self alloc] initWithCapacity:numItems] autorelease];
9884}
9885
9886- (instancetype)init {
9887  return [self initWithUInt64s:NULL forKeys:NULL count:0];
9888}
9889
9890- (instancetype)initWithUInt64s:(const uint64_t [])values
9891                        forKeys:(const NSString * [])keys
9892                          count:(NSUInteger)count {
9893  self = [super init];
9894  if (self) {
9895    _dictionary = [[NSMutableDictionary alloc] init];
9896    if (count && values && keys) {
9897      for (NSUInteger i = 0; i < count; ++i) {
9898        if (!keys[i]) {
9899          [NSException raise:NSInvalidArgumentException
9900                      format:@"Attempting to add nil key to a Dictionary"];
9901        }
9902        [_dictionary setObject:@(values[i]) forKey:keys[i]];
9903      }
9904    }
9905  }
9906  return self;
9907}
9908
9909- (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
9910  self = [self initWithUInt64s:NULL forKeys:NULL count:0];
9911  if (self) {
9912    if (dictionary) {
9913      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9914    }
9915  }
9916  return self;
9917}
9918
9919- (instancetype)initWithCapacity:(NSUInteger)numItems {
9920  #pragma unused(numItems)
9921  return [self initWithUInt64s:NULL forKeys:NULL count:0];
9922}
9923
9924- (void)dealloc {
9925  NSAssert(!_autocreator,
9926           @"%@: Autocreator must be cleared before release, autocreator: %@",
9927           [self class], _autocreator);
9928  [_dictionary release];
9929  [super dealloc];
9930}
9931
9932- (instancetype)copyWithZone:(NSZone *)zone {
9933  return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
9934}
9935
9936- (BOOL)isEqual:(id)other {
9937  if (self == other) {
9938    return YES;
9939  }
9940  if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
9941    return NO;
9942  }
9943  GPBStringUInt64Dictionary *otherDictionary = other;
9944  return [_dictionary isEqual:otherDictionary->_dictionary];
9945}
9946
9947- (NSUInteger)hash {
9948  return _dictionary.count;
9949}
9950
9951- (NSString *)description {
9952  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9953}
9954
9955- (NSUInteger)count {
9956  return _dictionary.count;
9957}
9958
9959- (void)enumerateKeysAndUInt64sUsingBlock:
9960    (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
9961  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9962                                                   NSNumber *aValue,
9963                                                   BOOL *stop) {
9964      block(aKey, [aValue unsignedLongLongValue], stop);
9965  }];
9966}
9967
9968- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9969  NSUInteger count = _dictionary.count;
9970  if (count == 0) {
9971    return 0;
9972  }
9973
9974  GPBDataType valueDataType = GPBGetFieldDataType(field);
9975  GPBDataType keyDataType = field.mapKeyDataType;
9976  __block size_t result = 0;
9977  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9978                                                   NSNumber *aValue,
9979                                                   BOOL *stop) {
9980    #pragma unused(stop)
9981    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9982    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
9983    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9984  }];
9985  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9986  result += tagSize * count;
9987  return result;
9988}
9989
9990- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9991                         asField:(GPBFieldDescriptor *)field {
9992  GPBDataType valueDataType = GPBGetFieldDataType(field);
9993  GPBDataType keyDataType = field.mapKeyDataType;
9994  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9995  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9996                                                   NSNumber *aValue,
9997                                                   BOOL *stop) {
9998    #pragma unused(stop)
9999    // Write the tag.
10000    [outputStream writeInt32NoTag:tag];
10001    // Write the size of the message.
10002    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10003    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
10004    [outputStream writeInt32NoTag:(int32_t)msgSize];
10005    // Write the fields.
10006    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10007    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
10008  }];
10009}
10010
10011- (void)setGPBGenericValue:(GPBGenericValue *)value
10012     forGPBGenericValueKey:(GPBGenericValue *)key {
10013  [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
10014}
10015
10016- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10017  [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
10018      #pragma unused(stop)
10019      block(key, [NSString stringWithFormat:@"%llu", value]);
10020  }];
10021}
10022
10023- (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
10024  NSNumber *wrapped = [_dictionary objectForKey:key];
10025  if (wrapped && value) {
10026    *value = [wrapped unsignedLongLongValue];
10027  }
10028  return (wrapped != NULL);
10029}
10030
10031- (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
10032  if (otherDictionary) {
10033    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10034    if (_autocreator) {
10035      GPBAutocreatedDictionaryModified(_autocreator, self);
10036    }
10037  }
10038}
10039
10040- (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
10041  if (!key) {
10042    [NSException raise:NSInvalidArgumentException
10043                format:@"Attempting to add nil key to a Dictionary"];
10044  }
10045  [_dictionary setObject:@(value) forKey:key];
10046  if (_autocreator) {
10047    GPBAutocreatedDictionaryModified(_autocreator, self);
10048  }
10049}
10050
10051- (void)removeUInt64ForKey:(NSString *)aKey {
10052  [_dictionary removeObjectForKey:aKey];
10053}
10054
10055- (void)removeAll {
10056  [_dictionary removeAllObjects];
10057}
10058
10059@end
10060
10061#pragma mark - String -> Int64
10062
10063@implementation GPBStringInt64Dictionary {
10064 @package
10065  NSMutableDictionary *_dictionary;
10066}
10067
10068+ (instancetype)dictionary {
10069  return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
10070}
10071
10072+ (instancetype)dictionaryWithInt64:(int64_t)value
10073                             forKey:(NSString *)key {
10074  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
10075  // on to get the type correct.
10076  return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:&value
10077                                                          forKeys:&key
10078                                                            count:1] autorelease];
10079}
10080
10081+ (instancetype)dictionaryWithInt64s:(const int64_t [])values
10082                             forKeys:(const NSString * [])keys
10083                               count:(NSUInteger)count {
10084  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
10085  // on to get the type correct.
10086  return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:values
10087                                                          forKeys:keys
10088                                                            count:count] autorelease];
10089}
10090
10091+ (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
10092  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10093  // on to get the type correct.
10094  return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10095}
10096
10097+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10098  return [[[self alloc] initWithCapacity:numItems] autorelease];
10099}
10100
10101- (instancetype)init {
10102  return [self initWithInt64s:NULL forKeys:NULL count:0];
10103}
10104
10105- (instancetype)initWithInt64s:(const int64_t [])values
10106                       forKeys:(const NSString * [])keys
10107                         count:(NSUInteger)count {
10108  self = [super init];
10109  if (self) {
10110    _dictionary = [[NSMutableDictionary alloc] init];
10111    if (count && values && keys) {
10112      for (NSUInteger i = 0; i < count; ++i) {
10113        if (!keys[i]) {
10114          [NSException raise:NSInvalidArgumentException
10115                      format:@"Attempting to add nil key to a Dictionary"];
10116        }
10117        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10118      }
10119    }
10120  }
10121  return self;
10122}
10123
10124- (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
10125  self = [self initWithInt64s:NULL forKeys:NULL count:0];
10126  if (self) {
10127    if (dictionary) {
10128      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10129    }
10130  }
10131  return self;
10132}
10133
10134- (instancetype)initWithCapacity:(NSUInteger)numItems {
10135  #pragma unused(numItems)
10136  return [self initWithInt64s:NULL forKeys:NULL count:0];
10137}
10138
10139- (void)dealloc {
10140  NSAssert(!_autocreator,
10141           @"%@: Autocreator must be cleared before release, autocreator: %@",
10142           [self class], _autocreator);
10143  [_dictionary release];
10144  [super dealloc];
10145}
10146
10147- (instancetype)copyWithZone:(NSZone *)zone {
10148  return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
10149}
10150
10151- (BOOL)isEqual:(id)other {
10152  if (self == other) {
10153    return YES;
10154  }
10155  if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
10156    return NO;
10157  }
10158  GPBStringInt64Dictionary *otherDictionary = other;
10159  return [_dictionary isEqual:otherDictionary->_dictionary];
10160}
10161
10162- (NSUInteger)hash {
10163  return _dictionary.count;
10164}
10165
10166- (NSString *)description {
10167  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10168}
10169
10170- (NSUInteger)count {
10171  return _dictionary.count;
10172}
10173
10174- (void)enumerateKeysAndInt64sUsingBlock:
10175    (void (^)(NSString *key, int64_t value, BOOL *stop))block {
10176  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10177                                                   NSNumber *aValue,
10178                                                   BOOL *stop) {
10179      block(aKey, [aValue longLongValue], stop);
10180  }];
10181}
10182
10183- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10184  NSUInteger count = _dictionary.count;
10185  if (count == 0) {
10186    return 0;
10187  }
10188
10189  GPBDataType valueDataType = GPBGetFieldDataType(field);
10190  GPBDataType keyDataType = field.mapKeyDataType;
10191  __block size_t result = 0;
10192  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10193                                                   NSNumber *aValue,
10194                                                   BOOL *stop) {
10195    #pragma unused(stop)
10196    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10197    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
10198    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10199  }];
10200  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10201  result += tagSize * count;
10202  return result;
10203}
10204
10205- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10206                         asField:(GPBFieldDescriptor *)field {
10207  GPBDataType valueDataType = GPBGetFieldDataType(field);
10208  GPBDataType keyDataType = field.mapKeyDataType;
10209  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10210  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10211                                                   NSNumber *aValue,
10212                                                   BOOL *stop) {
10213    #pragma unused(stop)
10214    // Write the tag.
10215    [outputStream writeInt32NoTag:tag];
10216    // Write the size of the message.
10217    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10218    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
10219    [outputStream writeInt32NoTag:(int32_t)msgSize];
10220    // Write the fields.
10221    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10222    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
10223  }];
10224}
10225
10226- (void)setGPBGenericValue:(GPBGenericValue *)value
10227     forGPBGenericValueKey:(GPBGenericValue *)key {
10228  [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
10229}
10230
10231- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10232  [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
10233      #pragma unused(stop)
10234      block(key, [NSString stringWithFormat:@"%lld", value]);
10235  }];
10236}
10237
10238- (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
10239  NSNumber *wrapped = [_dictionary objectForKey:key];
10240  if (wrapped && value) {
10241    *value = [wrapped longLongValue];
10242  }
10243  return (wrapped != NULL);
10244}
10245
10246- (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
10247  if (otherDictionary) {
10248    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10249    if (_autocreator) {
10250      GPBAutocreatedDictionaryModified(_autocreator, self);
10251    }
10252  }
10253}
10254
10255- (void)setInt64:(int64_t)value forKey:(NSString *)key {
10256  if (!key) {
10257    [NSException raise:NSInvalidArgumentException
10258                format:@"Attempting to add nil key to a Dictionary"];
10259  }
10260  [_dictionary setObject:@(value) forKey:key];
10261  if (_autocreator) {
10262    GPBAutocreatedDictionaryModified(_autocreator, self);
10263  }
10264}
10265
10266- (void)removeInt64ForKey:(NSString *)aKey {
10267  [_dictionary removeObjectForKey:aKey];
10268}
10269
10270- (void)removeAll {
10271  [_dictionary removeAllObjects];
10272}
10273
10274@end
10275
10276#pragma mark - String -> Bool
10277
10278@implementation GPBStringBoolDictionary {
10279 @package
10280  NSMutableDictionary *_dictionary;
10281}
10282
10283+ (instancetype)dictionary {
10284  return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
10285}
10286
10287+ (instancetype)dictionaryWithBool:(BOOL)value
10288                            forKey:(NSString *)key {
10289  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
10290  // on to get the type correct.
10291  return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:&value
10292                                                        forKeys:&key
10293                                                          count:1] autorelease];
10294}
10295
10296+ (instancetype)dictionaryWithBools:(const BOOL [])values
10297                            forKeys:(const NSString * [])keys
10298                              count:(NSUInteger)count {
10299  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
10300  // on to get the type correct.
10301  return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:values
10302                                                         forKeys:keys
10303                                                           count:count] autorelease];
10304}
10305
10306+ (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
10307  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10308  // on to get the type correct.
10309  return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10310}
10311
10312+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10313  return [[[self alloc] initWithCapacity:numItems] autorelease];
10314}
10315
10316- (instancetype)init {
10317  return [self initWithBools:NULL forKeys:NULL count:0];
10318}
10319
10320- (instancetype)initWithBools:(const BOOL [])values
10321                      forKeys:(const NSString * [])keys
10322                        count:(NSUInteger)count {
10323  self = [super init];
10324  if (self) {
10325    _dictionary = [[NSMutableDictionary alloc] init];
10326    if (count && values && keys) {
10327      for (NSUInteger i = 0; i < count; ++i) {
10328        if (!keys[i]) {
10329          [NSException raise:NSInvalidArgumentException
10330                      format:@"Attempting to add nil key to a Dictionary"];
10331        }
10332        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10333      }
10334    }
10335  }
10336  return self;
10337}
10338
10339- (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
10340  self = [self initWithBools:NULL forKeys:NULL count:0];
10341  if (self) {
10342    if (dictionary) {
10343      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10344    }
10345  }
10346  return self;
10347}
10348
10349- (instancetype)initWithCapacity:(NSUInteger)numItems {
10350  #pragma unused(numItems)
10351  return [self initWithBools:NULL forKeys:NULL count:0];
10352}
10353
10354- (void)dealloc {
10355  NSAssert(!_autocreator,
10356           @"%@: Autocreator must be cleared before release, autocreator: %@",
10357           [self class], _autocreator);
10358  [_dictionary release];
10359  [super dealloc];
10360}
10361
10362- (instancetype)copyWithZone:(NSZone *)zone {
10363  return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
10364}
10365
10366- (BOOL)isEqual:(id)other {
10367  if (self == other) {
10368    return YES;
10369  }
10370  if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
10371    return NO;
10372  }
10373  GPBStringBoolDictionary *otherDictionary = other;
10374  return [_dictionary isEqual:otherDictionary->_dictionary];
10375}
10376
10377- (NSUInteger)hash {
10378  return _dictionary.count;
10379}
10380
10381- (NSString *)description {
10382  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10383}
10384
10385- (NSUInteger)count {
10386  return _dictionary.count;
10387}
10388
10389- (void)enumerateKeysAndBoolsUsingBlock:
10390    (void (^)(NSString *key, BOOL value, BOOL *stop))block {
10391  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10392                                                   NSNumber *aValue,
10393                                                   BOOL *stop) {
10394      block(aKey, [aValue boolValue], stop);
10395  }];
10396}
10397
10398- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10399  NSUInteger count = _dictionary.count;
10400  if (count == 0) {
10401    return 0;
10402  }
10403
10404  GPBDataType valueDataType = GPBGetFieldDataType(field);
10405  GPBDataType keyDataType = field.mapKeyDataType;
10406  __block size_t result = 0;
10407  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10408                                                   NSNumber *aValue,
10409                                                   BOOL *stop) {
10410    #pragma unused(stop)
10411    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10412    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
10413    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10414  }];
10415  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10416  result += tagSize * count;
10417  return result;
10418}
10419
10420- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10421                         asField:(GPBFieldDescriptor *)field {
10422  GPBDataType valueDataType = GPBGetFieldDataType(field);
10423  GPBDataType keyDataType = field.mapKeyDataType;
10424  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10425  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10426                                                   NSNumber *aValue,
10427                                                   BOOL *stop) {
10428    #pragma unused(stop)
10429    // Write the tag.
10430    [outputStream writeInt32NoTag:tag];
10431    // Write the size of the message.
10432    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10433    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
10434    [outputStream writeInt32NoTag:(int32_t)msgSize];
10435    // Write the fields.
10436    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10437    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
10438  }];
10439}
10440
10441- (void)setGPBGenericValue:(GPBGenericValue *)value
10442     forGPBGenericValueKey:(GPBGenericValue *)key {
10443  [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
10444}
10445
10446- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10447  [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
10448      #pragma unused(stop)
10449      block(key, (value ? @"true" : @"false"));
10450  }];
10451}
10452
10453- (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
10454  NSNumber *wrapped = [_dictionary objectForKey:key];
10455  if (wrapped && value) {
10456    *value = [wrapped boolValue];
10457  }
10458  return (wrapped != NULL);
10459}
10460
10461- (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
10462  if (otherDictionary) {
10463    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10464    if (_autocreator) {
10465      GPBAutocreatedDictionaryModified(_autocreator, self);
10466    }
10467  }
10468}
10469
10470- (void)setBool:(BOOL)value forKey:(NSString *)key {
10471  if (!key) {
10472    [NSException raise:NSInvalidArgumentException
10473                format:@"Attempting to add nil key to a Dictionary"];
10474  }
10475  [_dictionary setObject:@(value) forKey:key];
10476  if (_autocreator) {
10477    GPBAutocreatedDictionaryModified(_autocreator, self);
10478  }
10479}
10480
10481- (void)removeBoolForKey:(NSString *)aKey {
10482  [_dictionary removeObjectForKey:aKey];
10483}
10484
10485- (void)removeAll {
10486  [_dictionary removeAllObjects];
10487}
10488
10489@end
10490
10491#pragma mark - String -> Float
10492
10493@implementation GPBStringFloatDictionary {
10494 @package
10495  NSMutableDictionary *_dictionary;
10496}
10497
10498+ (instancetype)dictionary {
10499  return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
10500}
10501
10502+ (instancetype)dictionaryWithFloat:(float)value
10503                             forKey:(NSString *)key {
10504  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
10505  // on to get the type correct.
10506  return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:&value
10507                                                          forKeys:&key
10508                                                            count:1] autorelease];
10509}
10510
10511+ (instancetype)dictionaryWithFloats:(const float [])values
10512                             forKeys:(const NSString * [])keys
10513                               count:(NSUInteger)count {
10514  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
10515  // on to get the type correct.
10516  return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:values
10517                                                          forKeys:keys
10518                                                            count:count] autorelease];
10519}
10520
10521+ (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
10522  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10523  // on to get the type correct.
10524  return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10525}
10526
10527+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10528  return [[[self alloc] initWithCapacity:numItems] autorelease];
10529}
10530
10531- (instancetype)init {
10532  return [self initWithFloats:NULL forKeys:NULL count:0];
10533}
10534
10535- (instancetype)initWithFloats:(const float [])values
10536                       forKeys:(const NSString * [])keys
10537                         count:(NSUInteger)count {
10538  self = [super init];
10539  if (self) {
10540    _dictionary = [[NSMutableDictionary alloc] init];
10541    if (count && values && keys) {
10542      for (NSUInteger i = 0; i < count; ++i) {
10543        if (!keys[i]) {
10544          [NSException raise:NSInvalidArgumentException
10545                      format:@"Attempting to add nil key to a Dictionary"];
10546        }
10547        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10548      }
10549    }
10550  }
10551  return self;
10552}
10553
10554- (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
10555  self = [self initWithFloats:NULL forKeys:NULL count:0];
10556  if (self) {
10557    if (dictionary) {
10558      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10559    }
10560  }
10561  return self;
10562}
10563
10564- (instancetype)initWithCapacity:(NSUInteger)numItems {
10565  #pragma unused(numItems)
10566  return [self initWithFloats:NULL forKeys:NULL count:0];
10567}
10568
10569- (void)dealloc {
10570  NSAssert(!_autocreator,
10571           @"%@: Autocreator must be cleared before release, autocreator: %@",
10572           [self class], _autocreator);
10573  [_dictionary release];
10574  [super dealloc];
10575}
10576
10577- (instancetype)copyWithZone:(NSZone *)zone {
10578  return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
10579}
10580
10581- (BOOL)isEqual:(id)other {
10582  if (self == other) {
10583    return YES;
10584  }
10585  if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
10586    return NO;
10587  }
10588  GPBStringFloatDictionary *otherDictionary = other;
10589  return [_dictionary isEqual:otherDictionary->_dictionary];
10590}
10591
10592- (NSUInteger)hash {
10593  return _dictionary.count;
10594}
10595
10596- (NSString *)description {
10597  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10598}
10599
10600- (NSUInteger)count {
10601  return _dictionary.count;
10602}
10603
10604- (void)enumerateKeysAndFloatsUsingBlock:
10605    (void (^)(NSString *key, float value, BOOL *stop))block {
10606  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10607                                                   NSNumber *aValue,
10608                                                   BOOL *stop) {
10609      block(aKey, [aValue floatValue], stop);
10610  }];
10611}
10612
10613- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10614  NSUInteger count = _dictionary.count;
10615  if (count == 0) {
10616    return 0;
10617  }
10618
10619  GPBDataType valueDataType = GPBGetFieldDataType(field);
10620  GPBDataType keyDataType = field.mapKeyDataType;
10621  __block size_t result = 0;
10622  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10623                                                   NSNumber *aValue,
10624                                                   BOOL *stop) {
10625    #pragma unused(stop)
10626    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10627    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
10628    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10629  }];
10630  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10631  result += tagSize * count;
10632  return result;
10633}
10634
10635- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10636                         asField:(GPBFieldDescriptor *)field {
10637  GPBDataType valueDataType = GPBGetFieldDataType(field);
10638  GPBDataType keyDataType = field.mapKeyDataType;
10639  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10640  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10641                                                   NSNumber *aValue,
10642                                                   BOOL *stop) {
10643    #pragma unused(stop)
10644    // Write the tag.
10645    [outputStream writeInt32NoTag:tag];
10646    // Write the size of the message.
10647    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10648    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
10649    [outputStream writeInt32NoTag:(int32_t)msgSize];
10650    // Write the fields.
10651    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10652    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
10653  }];
10654}
10655
10656- (void)setGPBGenericValue:(GPBGenericValue *)value
10657     forGPBGenericValueKey:(GPBGenericValue *)key {
10658  [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
10659}
10660
10661- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10662  [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) {
10663      #pragma unused(stop)
10664      block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
10665  }];
10666}
10667
10668- (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
10669  NSNumber *wrapped = [_dictionary objectForKey:key];
10670  if (wrapped && value) {
10671    *value = [wrapped floatValue];
10672  }
10673  return (wrapped != NULL);
10674}
10675
10676- (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
10677  if (otherDictionary) {
10678    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10679    if (_autocreator) {
10680      GPBAutocreatedDictionaryModified(_autocreator, self);
10681    }
10682  }
10683}
10684
10685- (void)setFloat:(float)value forKey:(NSString *)key {
10686  if (!key) {
10687    [NSException raise:NSInvalidArgumentException
10688                format:@"Attempting to add nil key to a Dictionary"];
10689  }
10690  [_dictionary setObject:@(value) forKey:key];
10691  if (_autocreator) {
10692    GPBAutocreatedDictionaryModified(_autocreator, self);
10693  }
10694}
10695
10696- (void)removeFloatForKey:(NSString *)aKey {
10697  [_dictionary removeObjectForKey:aKey];
10698}
10699
10700- (void)removeAll {
10701  [_dictionary removeAllObjects];
10702}
10703
10704@end
10705
10706#pragma mark - String -> Double
10707
10708@implementation GPBStringDoubleDictionary {
10709 @package
10710  NSMutableDictionary *_dictionary;
10711}
10712
10713+ (instancetype)dictionary {
10714  return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
10715}
10716
10717+ (instancetype)dictionaryWithDouble:(double)value
10718                              forKey:(NSString *)key {
10719  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
10720  // on to get the type correct.
10721  return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:&value
10722                                                            forKeys:&key
10723                                                              count:1] autorelease];
10724}
10725
10726+ (instancetype)dictionaryWithDoubles:(const double [])values
10727                              forKeys:(const NSString * [])keys
10728                                count:(NSUInteger)count {
10729  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
10730  // on to get the type correct.
10731  return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:values
10732                                                           forKeys:keys
10733                                                             count:count] autorelease];
10734}
10735
10736+ (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
10737  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10738  // on to get the type correct.
10739  return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10740}
10741
10742+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10743  return [[[self alloc] initWithCapacity:numItems] autorelease];
10744}
10745
10746- (instancetype)init {
10747  return [self initWithDoubles:NULL forKeys:NULL count:0];
10748}
10749
10750- (instancetype)initWithDoubles:(const double [])values
10751                        forKeys:(const NSString * [])keys
10752                          count:(NSUInteger)count {
10753  self = [super init];
10754  if (self) {
10755    _dictionary = [[NSMutableDictionary alloc] init];
10756    if (count && values && keys) {
10757      for (NSUInteger i = 0; i < count; ++i) {
10758        if (!keys[i]) {
10759          [NSException raise:NSInvalidArgumentException
10760                      format:@"Attempting to add nil key to a Dictionary"];
10761        }
10762        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10763      }
10764    }
10765  }
10766  return self;
10767}
10768
10769- (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
10770  self = [self initWithDoubles:NULL forKeys:NULL count:0];
10771  if (self) {
10772    if (dictionary) {
10773      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10774    }
10775  }
10776  return self;
10777}
10778
10779- (instancetype)initWithCapacity:(NSUInteger)numItems {
10780  #pragma unused(numItems)
10781  return [self initWithDoubles:NULL forKeys:NULL count:0];
10782}
10783
10784- (void)dealloc {
10785  NSAssert(!_autocreator,
10786           @"%@: Autocreator must be cleared before release, autocreator: %@",
10787           [self class], _autocreator);
10788  [_dictionary release];
10789  [super dealloc];
10790}
10791
10792- (instancetype)copyWithZone:(NSZone *)zone {
10793  return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
10794}
10795
10796- (BOOL)isEqual:(id)other {
10797  if (self == other) {
10798    return YES;
10799  }
10800  if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
10801    return NO;
10802  }
10803  GPBStringDoubleDictionary *otherDictionary = other;
10804  return [_dictionary isEqual:otherDictionary->_dictionary];
10805}
10806
10807- (NSUInteger)hash {
10808  return _dictionary.count;
10809}
10810
10811- (NSString *)description {
10812  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10813}
10814
10815- (NSUInteger)count {
10816  return _dictionary.count;
10817}
10818
10819- (void)enumerateKeysAndDoublesUsingBlock:
10820    (void (^)(NSString *key, double value, BOOL *stop))block {
10821  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10822                                                   NSNumber *aValue,
10823                                                   BOOL *stop) {
10824      block(aKey, [aValue doubleValue], stop);
10825  }];
10826}
10827
10828- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10829  NSUInteger count = _dictionary.count;
10830  if (count == 0) {
10831    return 0;
10832  }
10833
10834  GPBDataType valueDataType = GPBGetFieldDataType(field);
10835  GPBDataType keyDataType = field.mapKeyDataType;
10836  __block size_t result = 0;
10837  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10838                                                   NSNumber *aValue,
10839                                                   BOOL *stop) {
10840    #pragma unused(stop)
10841    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10842    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
10843    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10844  }];
10845  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10846  result += tagSize * count;
10847  return result;
10848}
10849
10850- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10851                         asField:(GPBFieldDescriptor *)field {
10852  GPBDataType valueDataType = GPBGetFieldDataType(field);
10853  GPBDataType keyDataType = field.mapKeyDataType;
10854  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10855  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10856                                                   NSNumber *aValue,
10857                                                   BOOL *stop) {
10858    #pragma unused(stop)
10859    // Write the tag.
10860    [outputStream writeInt32NoTag:tag];
10861    // Write the size of the message.
10862    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10863    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
10864    [outputStream writeInt32NoTag:(int32_t)msgSize];
10865    // Write the fields.
10866    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10867    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
10868  }];
10869}
10870
10871- (void)setGPBGenericValue:(GPBGenericValue *)value
10872     forGPBGenericValueKey:(GPBGenericValue *)key {
10873  [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
10874}
10875
10876- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10877  [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) {
10878      #pragma unused(stop)
10879      block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
10880  }];
10881}
10882
10883- (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
10884  NSNumber *wrapped = [_dictionary objectForKey:key];
10885  if (wrapped && value) {
10886    *value = [wrapped doubleValue];
10887  }
10888  return (wrapped != NULL);
10889}
10890
10891- (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
10892  if (otherDictionary) {
10893    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10894    if (_autocreator) {
10895      GPBAutocreatedDictionaryModified(_autocreator, self);
10896    }
10897  }
10898}
10899
10900- (void)setDouble:(double)value forKey:(NSString *)key {
10901  if (!key) {
10902    [NSException raise:NSInvalidArgumentException
10903                format:@"Attempting to add nil key to a Dictionary"];
10904  }
10905  [_dictionary setObject:@(value) forKey:key];
10906  if (_autocreator) {
10907    GPBAutocreatedDictionaryModified(_autocreator, self);
10908  }
10909}
10910
10911- (void)removeDoubleForKey:(NSString *)aKey {
10912  [_dictionary removeObjectForKey:aKey];
10913}
10914
10915- (void)removeAll {
10916  [_dictionary removeAllObjects];
10917}
10918
10919@end
10920
10921#pragma mark - String -> Enum
10922
10923@implementation GPBStringEnumDictionary {
10924 @package
10925  NSMutableDictionary *_dictionary;
10926  GPBEnumValidationFunc _validationFunc;
10927}
10928
10929@synthesize validationFunc = _validationFunc;
10930
10931+ (instancetype)dictionary {
10932  return [[[self alloc] initWithValidationFunction:NULL
10933                                         rawValues:NULL
10934                                           forKeys:NULL
10935                                             count:0] autorelease];
10936}
10937
10938+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
10939  return [[[self alloc] initWithValidationFunction:func
10940                                         rawValues:NULL
10941                                           forKeys:NULL
10942                                             count:0] autorelease];
10943}
10944
10945+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
10946                                        rawValue:(int32_t)rawValue
10947                                          forKey:(NSString *)key {
10948  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10949  // on to get the type correct.
10950  return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
10951                                                                   rawValues:&rawValue
10952                                                                     forKeys:&key
10953                                                                       count:1] autorelease];
10954}
10955
10956+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
10957                                       rawValues:(const int32_t [])rawValues
10958                                         forKeys:(const NSString * [])keys
10959                                           count:(NSUInteger)count {
10960  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10961  // on to get the type correct.
10962  return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
10963                                                                   rawValues:rawValues
10964                                                                     forKeys:keys
10965                                                                       count:count] autorelease];
10966}
10967
10968+ (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
10969  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10970  // on to get the type correct.
10971  return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10972}
10973
10974+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
10975                                        capacity:(NSUInteger)numItems {
10976  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
10977}
10978
10979- (instancetype)init {
10980  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
10981}
10982
10983- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
10984  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
10985}
10986
10987- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
10988                                 rawValues:(const int32_t [])rawValues
10989                                   forKeys:(const NSString * [])keys
10990                                     count:(NSUInteger)count {
10991  self = [super init];
10992  if (self) {
10993    _dictionary = [[NSMutableDictionary alloc] init];
10994    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
10995    if (count && rawValues && keys) {
10996      for (NSUInteger i = 0; i < count; ++i) {
10997        if (!keys[i]) {
10998          [NSException raise:NSInvalidArgumentException
10999                      format:@"Attempting to add nil key to a Dictionary"];
11000        }
11001        [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
11002      }
11003    }
11004  }
11005  return self;
11006}
11007
11008- (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
11009  self = [self initWithValidationFunction:dictionary.validationFunc
11010                                rawValues:NULL
11011                                  forKeys:NULL
11012                                    count:0];
11013  if (self) {
11014    if (dictionary) {
11015      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
11016    }
11017  }
11018  return self;
11019}
11020
11021- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
11022                                  capacity:(NSUInteger)numItems {
11023  #pragma unused(numItems)
11024  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
11025}
11026
11027- (void)dealloc {
11028  NSAssert(!_autocreator,
11029           @"%@: Autocreator must be cleared before release, autocreator: %@",
11030           [self class], _autocreator);
11031  [_dictionary release];
11032  [super dealloc];
11033}
11034
11035- (instancetype)copyWithZone:(NSZone *)zone {
11036  return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
11037}
11038
11039- (BOOL)isEqual:(id)other {
11040  if (self == other) {
11041    return YES;
11042  }
11043  if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
11044    return NO;
11045  }
11046  GPBStringEnumDictionary *otherDictionary = other;
11047  return [_dictionary isEqual:otherDictionary->_dictionary];
11048}
11049
11050- (NSUInteger)hash {
11051  return _dictionary.count;
11052}
11053
11054- (NSString *)description {
11055  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
11056}
11057
11058- (NSUInteger)count {
11059  return _dictionary.count;
11060}
11061
11062- (void)enumerateKeysAndRawValuesUsingBlock:
11063    (void (^)(NSString *key, int32_t value, BOOL *stop))block {
11064  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11065                                                   NSNumber *aValue,
11066                                                   BOOL *stop) {
11067      block(aKey, [aValue intValue], stop);
11068  }];
11069}
11070
11071- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11072  NSUInteger count = _dictionary.count;
11073  if (count == 0) {
11074    return 0;
11075  }
11076
11077  GPBDataType valueDataType = GPBGetFieldDataType(field);
11078  GPBDataType keyDataType = field.mapKeyDataType;
11079  __block size_t result = 0;
11080  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11081                                                   NSNumber *aValue,
11082                                                   BOOL *stop) {
11083    #pragma unused(stop)
11084    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
11085    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
11086    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11087  }];
11088  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11089  result += tagSize * count;
11090  return result;
11091}
11092
11093- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11094                         asField:(GPBFieldDescriptor *)field {
11095  GPBDataType valueDataType = GPBGetFieldDataType(field);
11096  GPBDataType keyDataType = field.mapKeyDataType;
11097  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11098  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11099                                                   NSNumber *aValue,
11100                                                   BOOL *stop) {
11101    #pragma unused(stop)
11102    // Write the tag.
11103    [outputStream writeInt32NoTag:tag];
11104    // Write the size of the message.
11105    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
11106    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
11107    [outputStream writeInt32NoTag:(int32_t)msgSize];
11108    // Write the fields.
11109    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
11110    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
11111  }];
11112}
11113
11114- (NSData *)serializedDataForUnknownValue:(int32_t)value
11115                                   forKey:(GPBGenericValue *)key
11116                              keyDataType:(GPBDataType)keyDataType {
11117  size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
11118  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
11119  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
11120  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
11121  WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
11122  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
11123  [outputStream release];
11124  return data;
11125}
11126- (void)setGPBGenericValue:(GPBGenericValue *)value
11127     forGPBGenericValueKey:(GPBGenericValue *)key {
11128  [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
11129}
11130
11131- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11132  [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
11133      #pragma unused(stop)
11134      block(key, @(value));
11135  }];
11136}
11137
11138- (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
11139  NSNumber *wrapped = [_dictionary objectForKey:key];
11140  if (wrapped && value) {
11141    int32_t result = [wrapped intValue];
11142    if (!_validationFunc(result)) {
11143      result = kGPBUnrecognizedEnumeratorValue;
11144    }
11145    *value = result;
11146  }
11147  return (wrapped != NULL);
11148}
11149
11150- (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
11151  NSNumber *wrapped = [_dictionary objectForKey:key];
11152  if (wrapped && rawValue) {
11153    *rawValue = [wrapped intValue];
11154  }
11155  return (wrapped != NULL);
11156}
11157
11158- (void)enumerateKeysAndEnumsUsingBlock:
11159    (void (^)(NSString *key, int32_t value, BOOL *stop))block {
11160  GPBEnumValidationFunc func = _validationFunc;
11161  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11162                                                   NSNumber *aValue,
11163                                                   BOOL *stop) {
11164      int32_t unwrapped = [aValue intValue];
11165      if (!func(unwrapped)) {
11166        unwrapped = kGPBUnrecognizedEnumeratorValue;
11167      }
11168      block(aKey, unwrapped, stop);
11169  }];
11170}
11171
11172- (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
11173  if (otherDictionary) {
11174    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
11175    if (_autocreator) {
11176      GPBAutocreatedDictionaryModified(_autocreator, self);
11177    }
11178  }
11179}
11180
11181- (void)setRawValue:(int32_t)value forKey:(NSString *)key {
11182  if (!key) {
11183    [NSException raise:NSInvalidArgumentException
11184                format:@"Attempting to add nil key to a Dictionary"];
11185  }
11186  [_dictionary setObject:@(value) forKey:key];
11187  if (_autocreator) {
11188    GPBAutocreatedDictionaryModified(_autocreator, self);
11189  }
11190}
11191
11192- (void)removeEnumForKey:(NSString *)aKey {
11193  [_dictionary removeObjectForKey:aKey];
11194}
11195
11196- (void)removeAll {
11197  [_dictionary removeAllObjects];
11198}
11199
11200- (void)setEnum:(int32_t)value forKey:(NSString *)key {
11201  if (!key) {
11202    [NSException raise:NSInvalidArgumentException
11203                format:@"Attempting to add nil key to a Dictionary"];
11204  }
11205  if (!_validationFunc(value)) {
11206    [NSException raise:NSInvalidArgumentException
11207                format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
11208                       value];
11209  }
11210
11211  [_dictionary setObject:@(value) forKey:key];
11212  if (_autocreator) {
11213    GPBAutocreatedDictionaryModified(_autocreator, self);
11214  }
11215}
11216
11217@end
11218
11219//%PDDM-EXPAND-END (5 expansions)
11220
11221
11222//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
11223// This block of code is generated, do not edit it directly.
11224
11225#pragma mark - Bool -> UInt32
11226
11227@implementation GPBBoolUInt32Dictionary {
11228 @package
11229  uint32_t _values[2];
11230  BOOL _valueSet[2];
11231}
11232
11233+ (instancetype)dictionary {
11234  return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
11235}
11236
11237+ (instancetype)dictionaryWithUInt32:(uint32_t)value
11238                              forKey:(BOOL)key {
11239  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
11240  // on to get the type correct.
11241  return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:&value
11242                                                          forKeys:&key
11243                                                            count:1] autorelease];
11244}
11245
11246+ (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
11247                              forKeys:(const BOOL [])keys
11248                                count:(NSUInteger)count {
11249  // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
11250  // on to get the type correct.
11251  return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:values
11252                                                          forKeys:keys
11253                                                            count:count] autorelease];
11254}
11255
11256+ (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
11257  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11258  // on to get the type correct.
11259  return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11260}
11261
11262+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11263  return [[[self alloc] initWithCapacity:numItems] autorelease];
11264}
11265
11266- (instancetype)init {
11267  return [self initWithUInt32s:NULL forKeys:NULL count:0];
11268}
11269
11270- (instancetype)initWithUInt32s:(const uint32_t [])values
11271                        forKeys:(const BOOL [])keys
11272                          count:(NSUInteger)count {
11273  self = [super init];
11274  if (self) {
11275    for (NSUInteger i = 0; i < count; ++i) {
11276      int idx = keys[i] ? 1 : 0;
11277      _values[idx] = values[i];
11278      _valueSet[idx] = YES;
11279    }
11280  }
11281  return self;
11282}
11283
11284- (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
11285  self = [self initWithUInt32s:NULL forKeys:NULL count:0];
11286  if (self) {
11287    if (dictionary) {
11288      for (int i = 0; i < 2; ++i) {
11289        if (dictionary->_valueSet[i]) {
11290          _values[i] = dictionary->_values[i];
11291          _valueSet[i] = YES;
11292        }
11293      }
11294    }
11295  }
11296  return self;
11297}
11298
11299- (instancetype)initWithCapacity:(NSUInteger)numItems {
11300  #pragma unused(numItems)
11301  return [self initWithUInt32s:NULL forKeys:NULL count:0];
11302}
11303
11304#if !defined(NS_BLOCK_ASSERTIONS)
11305- (void)dealloc {
11306  NSAssert(!_autocreator,
11307           @"%@: Autocreator must be cleared before release, autocreator: %@",
11308           [self class], _autocreator);
11309  [super dealloc];
11310}
11311#endif  // !defined(NS_BLOCK_ASSERTIONS)
11312
11313- (instancetype)copyWithZone:(NSZone *)zone {
11314  return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
11315}
11316
11317- (BOOL)isEqual:(id)other {
11318  if (self == other) {
11319    return YES;
11320  }
11321  if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
11322    return NO;
11323  }
11324  GPBBoolUInt32Dictionary *otherDictionary = other;
11325  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
11326      (_valueSet[1] != otherDictionary->_valueSet[1])) {
11327    return NO;
11328  }
11329  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
11330      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
11331    return NO;
11332  }
11333  return YES;
11334}
11335
11336- (NSUInteger)hash {
11337  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11338}
11339
11340- (NSString *)description {
11341  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
11342  if (_valueSet[0]) {
11343    [result appendFormat:@"NO: %u", _values[0]];
11344  }
11345  if (_valueSet[1]) {
11346    [result appendFormat:@"YES: %u", _values[1]];
11347  }
11348  [result appendString:@" }"];
11349  return result;
11350}
11351
11352- (NSUInteger)count {
11353  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11354}
11355
11356- (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
11357  int idx = (key ? 1 : 0);
11358  if (_valueSet[idx]) {
11359    if (value) {
11360      *value = _values[idx];
11361    }
11362    return YES;
11363  }
11364  return NO;
11365}
11366
11367- (void)setGPBGenericValue:(GPBGenericValue *)value
11368     forGPBGenericValueKey:(GPBGenericValue *)key {
11369  int idx = (key->valueBool ? 1 : 0);
11370  _values[idx] = value->valueUInt32;
11371  _valueSet[idx] = YES;
11372}
11373
11374- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11375  if (_valueSet[0]) {
11376    block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
11377  }
11378  if (_valueSet[1]) {
11379    block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
11380  }
11381}
11382
11383- (void)enumerateKeysAndUInt32sUsingBlock:
11384    (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
11385  BOOL stop = NO;
11386  if (_valueSet[0]) {
11387    block(NO, _values[0], &stop);
11388  }
11389  if (!stop && _valueSet[1]) {
11390    block(YES, _values[1], &stop);
11391  }
11392}
11393
11394- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11395  GPBDataType valueDataType = GPBGetFieldDataType(field);
11396  NSUInteger count = 0;
11397  size_t result = 0;
11398  for (int i = 0; i < 2; ++i) {
11399    if (_valueSet[i]) {
11400      ++count;
11401      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11402      msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11403      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11404    }
11405  }
11406  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11407  result += tagSize * count;
11408  return result;
11409}
11410
11411- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11412                         asField:(GPBFieldDescriptor *)field {
11413  GPBDataType valueDataType = GPBGetFieldDataType(field);
11414  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11415  for (int i = 0; i < 2; ++i) {
11416    if (_valueSet[i]) {
11417      // Write the tag.
11418      [outputStream writeInt32NoTag:tag];
11419      // Write the size of the message.
11420      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11421      msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11422      [outputStream writeInt32NoTag:(int32_t)msgSize];
11423      // Write the fields.
11424      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11425      WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
11426    }
11427  }
11428}
11429
11430- (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
11431  if (otherDictionary) {
11432    for (int i = 0; i < 2; ++i) {
11433      if (otherDictionary->_valueSet[i]) {
11434        _valueSet[i] = YES;
11435        _values[i] = otherDictionary->_values[i];
11436      }
11437    }
11438    if (_autocreator) {
11439      GPBAutocreatedDictionaryModified(_autocreator, self);
11440    }
11441  }
11442}
11443
11444- (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
11445  int idx = (key ? 1 : 0);
11446  _values[idx] = value;
11447  _valueSet[idx] = YES;
11448  if (_autocreator) {
11449    GPBAutocreatedDictionaryModified(_autocreator, self);
11450  }
11451}
11452
11453- (void)removeUInt32ForKey:(BOOL)aKey {
11454  _valueSet[aKey ? 1 : 0] = NO;
11455}
11456
11457- (void)removeAll {
11458  _valueSet[0] = NO;
11459  _valueSet[1] = NO;
11460}
11461
11462@end
11463
11464//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
11465// This block of code is generated, do not edit it directly.
11466
11467#pragma mark - Bool -> Int32
11468
11469@implementation GPBBoolInt32Dictionary {
11470 @package
11471  int32_t _values[2];
11472  BOOL _valueSet[2];
11473}
11474
11475+ (instancetype)dictionary {
11476  return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
11477}
11478
11479+ (instancetype)dictionaryWithInt32:(int32_t)value
11480                             forKey:(BOOL)key {
11481  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
11482  // on to get the type correct.
11483  return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:&value
11484                                                        forKeys:&key
11485                                                          count:1] autorelease];
11486}
11487
11488+ (instancetype)dictionaryWithInt32s:(const int32_t [])values
11489                             forKeys:(const BOOL [])keys
11490                               count:(NSUInteger)count {
11491  // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
11492  // on to get the type correct.
11493  return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:values
11494                                                        forKeys:keys
11495                                                          count:count] autorelease];
11496}
11497
11498+ (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
11499  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11500  // on to get the type correct.
11501  return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11502}
11503
11504+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11505  return [[[self alloc] initWithCapacity:numItems] autorelease];
11506}
11507
11508- (instancetype)init {
11509  return [self initWithInt32s:NULL forKeys:NULL count:0];
11510}
11511
11512- (instancetype)initWithInt32s:(const int32_t [])values
11513                       forKeys:(const BOOL [])keys
11514                         count:(NSUInteger)count {
11515  self = [super init];
11516  if (self) {
11517    for (NSUInteger i = 0; i < count; ++i) {
11518      int idx = keys[i] ? 1 : 0;
11519      _values[idx] = values[i];
11520      _valueSet[idx] = YES;
11521    }
11522  }
11523  return self;
11524}
11525
11526- (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
11527  self = [self initWithInt32s:NULL forKeys:NULL count:0];
11528  if (self) {
11529    if (dictionary) {
11530      for (int i = 0; i < 2; ++i) {
11531        if (dictionary->_valueSet[i]) {
11532          _values[i] = dictionary->_values[i];
11533          _valueSet[i] = YES;
11534        }
11535      }
11536    }
11537  }
11538  return self;
11539}
11540
11541- (instancetype)initWithCapacity:(NSUInteger)numItems {
11542  #pragma unused(numItems)
11543  return [self initWithInt32s:NULL forKeys:NULL count:0];
11544}
11545
11546#if !defined(NS_BLOCK_ASSERTIONS)
11547- (void)dealloc {
11548  NSAssert(!_autocreator,
11549           @"%@: Autocreator must be cleared before release, autocreator: %@",
11550           [self class], _autocreator);
11551  [super dealloc];
11552}
11553#endif  // !defined(NS_BLOCK_ASSERTIONS)
11554
11555- (instancetype)copyWithZone:(NSZone *)zone {
11556  return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
11557}
11558
11559- (BOOL)isEqual:(id)other {
11560  if (self == other) {
11561    return YES;
11562  }
11563  if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
11564    return NO;
11565  }
11566  GPBBoolInt32Dictionary *otherDictionary = other;
11567  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
11568      (_valueSet[1] != otherDictionary->_valueSet[1])) {
11569    return NO;
11570  }
11571  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
11572      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
11573    return NO;
11574  }
11575  return YES;
11576}
11577
11578- (NSUInteger)hash {
11579  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11580}
11581
11582- (NSString *)description {
11583  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
11584  if (_valueSet[0]) {
11585    [result appendFormat:@"NO: %d", _values[0]];
11586  }
11587  if (_valueSet[1]) {
11588    [result appendFormat:@"YES: %d", _values[1]];
11589  }
11590  [result appendString:@" }"];
11591  return result;
11592}
11593
11594- (NSUInteger)count {
11595  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11596}
11597
11598- (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
11599  int idx = (key ? 1 : 0);
11600  if (_valueSet[idx]) {
11601    if (value) {
11602      *value = _values[idx];
11603    }
11604    return YES;
11605  }
11606  return NO;
11607}
11608
11609- (void)setGPBGenericValue:(GPBGenericValue *)value
11610     forGPBGenericValueKey:(GPBGenericValue *)key {
11611  int idx = (key->valueBool ? 1 : 0);
11612  _values[idx] = value->valueInt32;
11613  _valueSet[idx] = YES;
11614}
11615
11616- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11617  if (_valueSet[0]) {
11618    block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
11619  }
11620  if (_valueSet[1]) {
11621    block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
11622  }
11623}
11624
11625- (void)enumerateKeysAndInt32sUsingBlock:
11626    (void (^)(BOOL key, int32_t value, BOOL *stop))block {
11627  BOOL stop = NO;
11628  if (_valueSet[0]) {
11629    block(NO, _values[0], &stop);
11630  }
11631  if (!stop && _valueSet[1]) {
11632    block(YES, _values[1], &stop);
11633  }
11634}
11635
11636- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11637  GPBDataType valueDataType = GPBGetFieldDataType(field);
11638  NSUInteger count = 0;
11639  size_t result = 0;
11640  for (int i = 0; i < 2; ++i) {
11641    if (_valueSet[i]) {
11642      ++count;
11643      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11644      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11645      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11646    }
11647  }
11648  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11649  result += tagSize * count;
11650  return result;
11651}
11652
11653- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11654                         asField:(GPBFieldDescriptor *)field {
11655  GPBDataType valueDataType = GPBGetFieldDataType(field);
11656  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11657  for (int i = 0; i < 2; ++i) {
11658    if (_valueSet[i]) {
11659      // Write the tag.
11660      [outputStream writeInt32NoTag:tag];
11661      // Write the size of the message.
11662      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11663      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11664      [outputStream writeInt32NoTag:(int32_t)msgSize];
11665      // Write the fields.
11666      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11667      WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
11668    }
11669  }
11670}
11671
11672- (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
11673  if (otherDictionary) {
11674    for (int i = 0; i < 2; ++i) {
11675      if (otherDictionary->_valueSet[i]) {
11676        _valueSet[i] = YES;
11677        _values[i] = otherDictionary->_values[i];
11678      }
11679    }
11680    if (_autocreator) {
11681      GPBAutocreatedDictionaryModified(_autocreator, self);
11682    }
11683  }
11684}
11685
11686- (void)setInt32:(int32_t)value forKey:(BOOL)key {
11687  int idx = (key ? 1 : 0);
11688  _values[idx] = value;
11689  _valueSet[idx] = YES;
11690  if (_autocreator) {
11691    GPBAutocreatedDictionaryModified(_autocreator, self);
11692  }
11693}
11694
11695- (void)removeInt32ForKey:(BOOL)aKey {
11696  _valueSet[aKey ? 1 : 0] = NO;
11697}
11698
11699- (void)removeAll {
11700  _valueSet[0] = NO;
11701  _valueSet[1] = NO;
11702}
11703
11704@end
11705
11706//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
11707// This block of code is generated, do not edit it directly.
11708
11709#pragma mark - Bool -> UInt64
11710
11711@implementation GPBBoolUInt64Dictionary {
11712 @package
11713  uint64_t _values[2];
11714  BOOL _valueSet[2];
11715}
11716
11717+ (instancetype)dictionary {
11718  return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
11719}
11720
11721+ (instancetype)dictionaryWithUInt64:(uint64_t)value
11722                              forKey:(BOOL)key {
11723  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
11724  // on to get the type correct.
11725  return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:&value
11726                                                          forKeys:&key
11727                                                            count:1] autorelease];
11728}
11729
11730+ (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
11731                              forKeys:(const BOOL [])keys
11732                                count:(NSUInteger)count {
11733  // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
11734  // on to get the type correct.
11735  return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:values
11736                                                          forKeys:keys
11737                                                            count:count] autorelease];
11738}
11739
11740+ (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
11741  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11742  // on to get the type correct.
11743  return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11744}
11745
11746+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11747  return [[[self alloc] initWithCapacity:numItems] autorelease];
11748}
11749
11750- (instancetype)init {
11751  return [self initWithUInt64s:NULL forKeys:NULL count:0];
11752}
11753
11754- (instancetype)initWithUInt64s:(const uint64_t [])values
11755                        forKeys:(const BOOL [])keys
11756                          count:(NSUInteger)count {
11757  self = [super init];
11758  if (self) {
11759    for (NSUInteger i = 0; i < count; ++i) {
11760      int idx = keys[i] ? 1 : 0;
11761      _values[idx] = values[i];
11762      _valueSet[idx] = YES;
11763    }
11764  }
11765  return self;
11766}
11767
11768- (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
11769  self = [self initWithUInt64s:NULL forKeys:NULL count:0];
11770  if (self) {
11771    if (dictionary) {
11772      for (int i = 0; i < 2; ++i) {
11773        if (dictionary->_valueSet[i]) {
11774          _values[i] = dictionary->_values[i];
11775          _valueSet[i] = YES;
11776        }
11777      }
11778    }
11779  }
11780  return self;
11781}
11782
11783- (instancetype)initWithCapacity:(NSUInteger)numItems {
11784  #pragma unused(numItems)
11785  return [self initWithUInt64s:NULL forKeys:NULL count:0];
11786}
11787
11788#if !defined(NS_BLOCK_ASSERTIONS)
11789- (void)dealloc {
11790  NSAssert(!_autocreator,
11791           @"%@: Autocreator must be cleared before release, autocreator: %@",
11792           [self class], _autocreator);
11793  [super dealloc];
11794}
11795#endif  // !defined(NS_BLOCK_ASSERTIONS)
11796
11797- (instancetype)copyWithZone:(NSZone *)zone {
11798  return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
11799}
11800
11801- (BOOL)isEqual:(id)other {
11802  if (self == other) {
11803    return YES;
11804  }
11805  if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
11806    return NO;
11807  }
11808  GPBBoolUInt64Dictionary *otherDictionary = other;
11809  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
11810      (_valueSet[1] != otherDictionary->_valueSet[1])) {
11811    return NO;
11812  }
11813  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
11814      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
11815    return NO;
11816  }
11817  return YES;
11818}
11819
11820- (NSUInteger)hash {
11821  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11822}
11823
11824- (NSString *)description {
11825  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
11826  if (_valueSet[0]) {
11827    [result appendFormat:@"NO: %llu", _values[0]];
11828  }
11829  if (_valueSet[1]) {
11830    [result appendFormat:@"YES: %llu", _values[1]];
11831  }
11832  [result appendString:@" }"];
11833  return result;
11834}
11835
11836- (NSUInteger)count {
11837  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11838}
11839
11840- (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
11841  int idx = (key ? 1 : 0);
11842  if (_valueSet[idx]) {
11843    if (value) {
11844      *value = _values[idx];
11845    }
11846    return YES;
11847  }
11848  return NO;
11849}
11850
11851- (void)setGPBGenericValue:(GPBGenericValue *)value
11852     forGPBGenericValueKey:(GPBGenericValue *)key {
11853  int idx = (key->valueBool ? 1 : 0);
11854  _values[idx] = value->valueUInt64;
11855  _valueSet[idx] = YES;
11856}
11857
11858- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11859  if (_valueSet[0]) {
11860    block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
11861  }
11862  if (_valueSet[1]) {
11863    block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
11864  }
11865}
11866
11867- (void)enumerateKeysAndUInt64sUsingBlock:
11868    (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
11869  BOOL stop = NO;
11870  if (_valueSet[0]) {
11871    block(NO, _values[0], &stop);
11872  }
11873  if (!stop && _valueSet[1]) {
11874    block(YES, _values[1], &stop);
11875  }
11876}
11877
11878- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11879  GPBDataType valueDataType = GPBGetFieldDataType(field);
11880  NSUInteger count = 0;
11881  size_t result = 0;
11882  for (int i = 0; i < 2; ++i) {
11883    if (_valueSet[i]) {
11884      ++count;
11885      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11886      msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11887      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11888    }
11889  }
11890  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11891  result += tagSize * count;
11892  return result;
11893}
11894
11895- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11896                         asField:(GPBFieldDescriptor *)field {
11897  GPBDataType valueDataType = GPBGetFieldDataType(field);
11898  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11899  for (int i = 0; i < 2; ++i) {
11900    if (_valueSet[i]) {
11901      // Write the tag.
11902      [outputStream writeInt32NoTag:tag];
11903      // Write the size of the message.
11904      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11905      msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11906      [outputStream writeInt32NoTag:(int32_t)msgSize];
11907      // Write the fields.
11908      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11909      WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
11910    }
11911  }
11912}
11913
11914- (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
11915  if (otherDictionary) {
11916    for (int i = 0; i < 2; ++i) {
11917      if (otherDictionary->_valueSet[i]) {
11918        _valueSet[i] = YES;
11919        _values[i] = otherDictionary->_values[i];
11920      }
11921    }
11922    if (_autocreator) {
11923      GPBAutocreatedDictionaryModified(_autocreator, self);
11924    }
11925  }
11926}
11927
11928- (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
11929  int idx = (key ? 1 : 0);
11930  _values[idx] = value;
11931  _valueSet[idx] = YES;
11932  if (_autocreator) {
11933    GPBAutocreatedDictionaryModified(_autocreator, self);
11934  }
11935}
11936
11937- (void)removeUInt64ForKey:(BOOL)aKey {
11938  _valueSet[aKey ? 1 : 0] = NO;
11939}
11940
11941- (void)removeAll {
11942  _valueSet[0] = NO;
11943  _valueSet[1] = NO;
11944}
11945
11946@end
11947
11948//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
11949// This block of code is generated, do not edit it directly.
11950
11951#pragma mark - Bool -> Int64
11952
11953@implementation GPBBoolInt64Dictionary {
11954 @package
11955  int64_t _values[2];
11956  BOOL _valueSet[2];
11957}
11958
11959+ (instancetype)dictionary {
11960  return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
11961}
11962
11963+ (instancetype)dictionaryWithInt64:(int64_t)value
11964                             forKey:(BOOL)key {
11965  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
11966  // on to get the type correct.
11967  return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:&value
11968                                                        forKeys:&key
11969                                                          count:1] autorelease];
11970}
11971
11972+ (instancetype)dictionaryWithInt64s:(const int64_t [])values
11973                             forKeys:(const BOOL [])keys
11974                               count:(NSUInteger)count {
11975  // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
11976  // on to get the type correct.
11977  return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:values
11978                                                        forKeys:keys
11979                                                          count:count] autorelease];
11980}
11981
11982+ (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
11983  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11984  // on to get the type correct.
11985  return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11986}
11987
11988+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11989  return [[[self alloc] initWithCapacity:numItems] autorelease];
11990}
11991
11992- (instancetype)init {
11993  return [self initWithInt64s:NULL forKeys:NULL count:0];
11994}
11995
11996- (instancetype)initWithInt64s:(const int64_t [])values
11997                       forKeys:(const BOOL [])keys
11998                         count:(NSUInteger)count {
11999  self = [super init];
12000  if (self) {
12001    for (NSUInteger i = 0; i < count; ++i) {
12002      int idx = keys[i] ? 1 : 0;
12003      _values[idx] = values[i];
12004      _valueSet[idx] = YES;
12005    }
12006  }
12007  return self;
12008}
12009
12010- (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
12011  self = [self initWithInt64s:NULL forKeys:NULL count:0];
12012  if (self) {
12013    if (dictionary) {
12014      for (int i = 0; i < 2; ++i) {
12015        if (dictionary->_valueSet[i]) {
12016          _values[i] = dictionary->_values[i];
12017          _valueSet[i] = YES;
12018        }
12019      }
12020    }
12021  }
12022  return self;
12023}
12024
12025- (instancetype)initWithCapacity:(NSUInteger)numItems {
12026  #pragma unused(numItems)
12027  return [self initWithInt64s:NULL forKeys:NULL count:0];
12028}
12029
12030#if !defined(NS_BLOCK_ASSERTIONS)
12031- (void)dealloc {
12032  NSAssert(!_autocreator,
12033           @"%@: Autocreator must be cleared before release, autocreator: %@",
12034           [self class], _autocreator);
12035  [super dealloc];
12036}
12037#endif  // !defined(NS_BLOCK_ASSERTIONS)
12038
12039- (instancetype)copyWithZone:(NSZone *)zone {
12040  return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
12041}
12042
12043- (BOOL)isEqual:(id)other {
12044  if (self == other) {
12045    return YES;
12046  }
12047  if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
12048    return NO;
12049  }
12050  GPBBoolInt64Dictionary *otherDictionary = other;
12051  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
12052      (_valueSet[1] != otherDictionary->_valueSet[1])) {
12053    return NO;
12054  }
12055  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
12056      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
12057    return NO;
12058  }
12059  return YES;
12060}
12061
12062- (NSUInteger)hash {
12063  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12064}
12065
12066- (NSString *)description {
12067  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12068  if (_valueSet[0]) {
12069    [result appendFormat:@"NO: %lld", _values[0]];
12070  }
12071  if (_valueSet[1]) {
12072    [result appendFormat:@"YES: %lld", _values[1]];
12073  }
12074  [result appendString:@" }"];
12075  return result;
12076}
12077
12078- (NSUInteger)count {
12079  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12080}
12081
12082- (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
12083  int idx = (key ? 1 : 0);
12084  if (_valueSet[idx]) {
12085    if (value) {
12086      *value = _values[idx];
12087    }
12088    return YES;
12089  }
12090  return NO;
12091}
12092
12093- (void)setGPBGenericValue:(GPBGenericValue *)value
12094     forGPBGenericValueKey:(GPBGenericValue *)key {
12095  int idx = (key->valueBool ? 1 : 0);
12096  _values[idx] = value->valueInt64;
12097  _valueSet[idx] = YES;
12098}
12099
12100- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12101  if (_valueSet[0]) {
12102    block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
12103  }
12104  if (_valueSet[1]) {
12105    block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
12106  }
12107}
12108
12109- (void)enumerateKeysAndInt64sUsingBlock:
12110    (void (^)(BOOL key, int64_t value, BOOL *stop))block {
12111  BOOL stop = NO;
12112  if (_valueSet[0]) {
12113    block(NO, _values[0], &stop);
12114  }
12115  if (!stop && _valueSet[1]) {
12116    block(YES, _values[1], &stop);
12117  }
12118}
12119
12120- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12121  GPBDataType valueDataType = GPBGetFieldDataType(field);
12122  NSUInteger count = 0;
12123  size_t result = 0;
12124  for (int i = 0; i < 2; ++i) {
12125    if (_valueSet[i]) {
12126      ++count;
12127      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12128      msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12129      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12130    }
12131  }
12132  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12133  result += tagSize * count;
12134  return result;
12135}
12136
12137- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12138                         asField:(GPBFieldDescriptor *)field {
12139  GPBDataType valueDataType = GPBGetFieldDataType(field);
12140  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12141  for (int i = 0; i < 2; ++i) {
12142    if (_valueSet[i]) {
12143      // Write the tag.
12144      [outputStream writeInt32NoTag:tag];
12145      // Write the size of the message.
12146      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12147      msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12148      [outputStream writeInt32NoTag:(int32_t)msgSize];
12149      // Write the fields.
12150      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12151      WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12152    }
12153  }
12154}
12155
12156- (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
12157  if (otherDictionary) {
12158    for (int i = 0; i < 2; ++i) {
12159      if (otherDictionary->_valueSet[i]) {
12160        _valueSet[i] = YES;
12161        _values[i] = otherDictionary->_values[i];
12162      }
12163    }
12164    if (_autocreator) {
12165      GPBAutocreatedDictionaryModified(_autocreator, self);
12166    }
12167  }
12168}
12169
12170- (void)setInt64:(int64_t)value forKey:(BOOL)key {
12171  int idx = (key ? 1 : 0);
12172  _values[idx] = value;
12173  _valueSet[idx] = YES;
12174  if (_autocreator) {
12175    GPBAutocreatedDictionaryModified(_autocreator, self);
12176  }
12177}
12178
12179- (void)removeInt64ForKey:(BOOL)aKey {
12180  _valueSet[aKey ? 1 : 0] = NO;
12181}
12182
12183- (void)removeAll {
12184  _valueSet[0] = NO;
12185  _valueSet[1] = NO;
12186}
12187
12188@end
12189
12190//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
12191// This block of code is generated, do not edit it directly.
12192
12193#pragma mark - Bool -> Bool
12194
12195@implementation GPBBoolBoolDictionary {
12196 @package
12197  BOOL _values[2];
12198  BOOL _valueSet[2];
12199}
12200
12201+ (instancetype)dictionary {
12202  return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
12203}
12204
12205+ (instancetype)dictionaryWithBool:(BOOL)value
12206                            forKey:(BOOL)key {
12207  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
12208  // on to get the type correct.
12209  return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:&value
12210                                                      forKeys:&key
12211                                                        count:1] autorelease];
12212}
12213
12214+ (instancetype)dictionaryWithBools:(const BOOL [])values
12215                            forKeys:(const BOOL [])keys
12216                              count:(NSUInteger)count {
12217  // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
12218  // on to get the type correct.
12219  return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:values
12220                                                      forKeys:keys
12221                                                        count:count] autorelease];
12222}
12223
12224+ (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
12225  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12226  // on to get the type correct.
12227  return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12228}
12229
12230+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12231  return [[[self alloc] initWithCapacity:numItems] autorelease];
12232}
12233
12234- (instancetype)init {
12235  return [self initWithBools:NULL forKeys:NULL count:0];
12236}
12237
12238- (instancetype)initWithBools:(const BOOL [])values
12239                      forKeys:(const BOOL [])keys
12240                        count:(NSUInteger)count {
12241  self = [super init];
12242  if (self) {
12243    for (NSUInteger i = 0; i < count; ++i) {
12244      int idx = keys[i] ? 1 : 0;
12245      _values[idx] = values[i];
12246      _valueSet[idx] = YES;
12247    }
12248  }
12249  return self;
12250}
12251
12252- (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
12253  self = [self initWithBools:NULL forKeys:NULL count:0];
12254  if (self) {
12255    if (dictionary) {
12256      for (int i = 0; i < 2; ++i) {
12257        if (dictionary->_valueSet[i]) {
12258          _values[i] = dictionary->_values[i];
12259          _valueSet[i] = YES;
12260        }
12261      }
12262    }
12263  }
12264  return self;
12265}
12266
12267- (instancetype)initWithCapacity:(NSUInteger)numItems {
12268  #pragma unused(numItems)
12269  return [self initWithBools:NULL forKeys:NULL count:0];
12270}
12271
12272#if !defined(NS_BLOCK_ASSERTIONS)
12273- (void)dealloc {
12274  NSAssert(!_autocreator,
12275           @"%@: Autocreator must be cleared before release, autocreator: %@",
12276           [self class], _autocreator);
12277  [super dealloc];
12278}
12279#endif  // !defined(NS_BLOCK_ASSERTIONS)
12280
12281- (instancetype)copyWithZone:(NSZone *)zone {
12282  return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
12283}
12284
12285- (BOOL)isEqual:(id)other {
12286  if (self == other) {
12287    return YES;
12288  }
12289  if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
12290    return NO;
12291  }
12292  GPBBoolBoolDictionary *otherDictionary = other;
12293  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
12294      (_valueSet[1] != otherDictionary->_valueSet[1])) {
12295    return NO;
12296  }
12297  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
12298      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
12299    return NO;
12300  }
12301  return YES;
12302}
12303
12304- (NSUInteger)hash {
12305  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12306}
12307
12308- (NSString *)description {
12309  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12310  if (_valueSet[0]) {
12311    [result appendFormat:@"NO: %d", _values[0]];
12312  }
12313  if (_valueSet[1]) {
12314    [result appendFormat:@"YES: %d", _values[1]];
12315  }
12316  [result appendString:@" }"];
12317  return result;
12318}
12319
12320- (NSUInteger)count {
12321  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12322}
12323
12324- (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
12325  int idx = (key ? 1 : 0);
12326  if (_valueSet[idx]) {
12327    if (value) {
12328      *value = _values[idx];
12329    }
12330    return YES;
12331  }
12332  return NO;
12333}
12334
12335- (void)setGPBGenericValue:(GPBGenericValue *)value
12336     forGPBGenericValueKey:(GPBGenericValue *)key {
12337  int idx = (key->valueBool ? 1 : 0);
12338  _values[idx] = value->valueBool;
12339  _valueSet[idx] = YES;
12340}
12341
12342- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12343  if (_valueSet[0]) {
12344    block(@"false", (_values[0] ? @"true" : @"false"));
12345  }
12346  if (_valueSet[1]) {
12347    block(@"true", (_values[1] ? @"true" : @"false"));
12348  }
12349}
12350
12351- (void)enumerateKeysAndBoolsUsingBlock:
12352    (void (^)(BOOL key, BOOL value, BOOL *stop))block {
12353  BOOL stop = NO;
12354  if (_valueSet[0]) {
12355    block(NO, _values[0], &stop);
12356  }
12357  if (!stop && _valueSet[1]) {
12358    block(YES, _values[1], &stop);
12359  }
12360}
12361
12362- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12363  GPBDataType valueDataType = GPBGetFieldDataType(field);
12364  NSUInteger count = 0;
12365  size_t result = 0;
12366  for (int i = 0; i < 2; ++i) {
12367    if (_valueSet[i]) {
12368      ++count;
12369      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12370      msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12371      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12372    }
12373  }
12374  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12375  result += tagSize * count;
12376  return result;
12377}
12378
12379- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12380                         asField:(GPBFieldDescriptor *)field {
12381  GPBDataType valueDataType = GPBGetFieldDataType(field);
12382  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12383  for (int i = 0; i < 2; ++i) {
12384    if (_valueSet[i]) {
12385      // Write the tag.
12386      [outputStream writeInt32NoTag:tag];
12387      // Write the size of the message.
12388      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12389      msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12390      [outputStream writeInt32NoTag:(int32_t)msgSize];
12391      // Write the fields.
12392      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12393      WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12394    }
12395  }
12396}
12397
12398- (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
12399  if (otherDictionary) {
12400    for (int i = 0; i < 2; ++i) {
12401      if (otherDictionary->_valueSet[i]) {
12402        _valueSet[i] = YES;
12403        _values[i] = otherDictionary->_values[i];
12404      }
12405    }
12406    if (_autocreator) {
12407      GPBAutocreatedDictionaryModified(_autocreator, self);
12408    }
12409  }
12410}
12411
12412- (void)setBool:(BOOL)value forKey:(BOOL)key {
12413  int idx = (key ? 1 : 0);
12414  _values[idx] = value;
12415  _valueSet[idx] = YES;
12416  if (_autocreator) {
12417    GPBAutocreatedDictionaryModified(_autocreator, self);
12418  }
12419}
12420
12421- (void)removeBoolForKey:(BOOL)aKey {
12422  _valueSet[aKey ? 1 : 0] = NO;
12423}
12424
12425- (void)removeAll {
12426  _valueSet[0] = NO;
12427  _valueSet[1] = NO;
12428}
12429
12430@end
12431
12432//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
12433// This block of code is generated, do not edit it directly.
12434
12435#pragma mark - Bool -> Float
12436
12437@implementation GPBBoolFloatDictionary {
12438 @package
12439  float _values[2];
12440  BOOL _valueSet[2];
12441}
12442
12443+ (instancetype)dictionary {
12444  return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
12445}
12446
12447+ (instancetype)dictionaryWithFloat:(float)value
12448                             forKey:(BOOL)key {
12449  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
12450  // on to get the type correct.
12451  return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:&value
12452                                                        forKeys:&key
12453                                                          count:1] autorelease];
12454}
12455
12456+ (instancetype)dictionaryWithFloats:(const float [])values
12457                             forKeys:(const BOOL [])keys
12458                               count:(NSUInteger)count {
12459  // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
12460  // on to get the type correct.
12461  return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:values
12462                                                        forKeys:keys
12463                                                          count:count] autorelease];
12464}
12465
12466+ (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
12467  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12468  // on to get the type correct.
12469  return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12470}
12471
12472+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12473  return [[[self alloc] initWithCapacity:numItems] autorelease];
12474}
12475
12476- (instancetype)init {
12477  return [self initWithFloats:NULL forKeys:NULL count:0];
12478}
12479
12480- (instancetype)initWithFloats:(const float [])values
12481                       forKeys:(const BOOL [])keys
12482                         count:(NSUInteger)count {
12483  self = [super init];
12484  if (self) {
12485    for (NSUInteger i = 0; i < count; ++i) {
12486      int idx = keys[i] ? 1 : 0;
12487      _values[idx] = values[i];
12488      _valueSet[idx] = YES;
12489    }
12490  }
12491  return self;
12492}
12493
12494- (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
12495  self = [self initWithFloats:NULL forKeys:NULL count:0];
12496  if (self) {
12497    if (dictionary) {
12498      for (int i = 0; i < 2; ++i) {
12499        if (dictionary->_valueSet[i]) {
12500          _values[i] = dictionary->_values[i];
12501          _valueSet[i] = YES;
12502        }
12503      }
12504    }
12505  }
12506  return self;
12507}
12508
12509- (instancetype)initWithCapacity:(NSUInteger)numItems {
12510  #pragma unused(numItems)
12511  return [self initWithFloats:NULL forKeys:NULL count:0];
12512}
12513
12514#if !defined(NS_BLOCK_ASSERTIONS)
12515- (void)dealloc {
12516  NSAssert(!_autocreator,
12517           @"%@: Autocreator must be cleared before release, autocreator: %@",
12518           [self class], _autocreator);
12519  [super dealloc];
12520}
12521#endif  // !defined(NS_BLOCK_ASSERTIONS)
12522
12523- (instancetype)copyWithZone:(NSZone *)zone {
12524  return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
12525}
12526
12527- (BOOL)isEqual:(id)other {
12528  if (self == other) {
12529    return YES;
12530  }
12531  if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
12532    return NO;
12533  }
12534  GPBBoolFloatDictionary *otherDictionary = other;
12535  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
12536      (_valueSet[1] != otherDictionary->_valueSet[1])) {
12537    return NO;
12538  }
12539  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
12540      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
12541    return NO;
12542  }
12543  return YES;
12544}
12545
12546- (NSUInteger)hash {
12547  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12548}
12549
12550- (NSString *)description {
12551  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12552  if (_valueSet[0]) {
12553    [result appendFormat:@"NO: %f", _values[0]];
12554  }
12555  if (_valueSet[1]) {
12556    [result appendFormat:@"YES: %f", _values[1]];
12557  }
12558  [result appendString:@" }"];
12559  return result;
12560}
12561
12562- (NSUInteger)count {
12563  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12564}
12565
12566- (BOOL)getFloat:(float *)value forKey:(BOOL)key {
12567  int idx = (key ? 1 : 0);
12568  if (_valueSet[idx]) {
12569    if (value) {
12570      *value = _values[idx];
12571    }
12572    return YES;
12573  }
12574  return NO;
12575}
12576
12577- (void)setGPBGenericValue:(GPBGenericValue *)value
12578     forGPBGenericValueKey:(GPBGenericValue *)key {
12579  int idx = (key->valueBool ? 1 : 0);
12580  _values[idx] = value->valueFloat;
12581  _valueSet[idx] = YES;
12582}
12583
12584- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12585  if (_valueSet[0]) {
12586    block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
12587  }
12588  if (_valueSet[1]) {
12589    block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
12590  }
12591}
12592
12593- (void)enumerateKeysAndFloatsUsingBlock:
12594    (void (^)(BOOL key, float value, BOOL *stop))block {
12595  BOOL stop = NO;
12596  if (_valueSet[0]) {
12597    block(NO, _values[0], &stop);
12598  }
12599  if (!stop && _valueSet[1]) {
12600    block(YES, _values[1], &stop);
12601  }
12602}
12603
12604- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12605  GPBDataType valueDataType = GPBGetFieldDataType(field);
12606  NSUInteger count = 0;
12607  size_t result = 0;
12608  for (int i = 0; i < 2; ++i) {
12609    if (_valueSet[i]) {
12610      ++count;
12611      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12612      msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12613      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12614    }
12615  }
12616  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12617  result += tagSize * count;
12618  return result;
12619}
12620
12621- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12622                         asField:(GPBFieldDescriptor *)field {
12623  GPBDataType valueDataType = GPBGetFieldDataType(field);
12624  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12625  for (int i = 0; i < 2; ++i) {
12626    if (_valueSet[i]) {
12627      // Write the tag.
12628      [outputStream writeInt32NoTag:tag];
12629      // Write the size of the message.
12630      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12631      msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12632      [outputStream writeInt32NoTag:(int32_t)msgSize];
12633      // Write the fields.
12634      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12635      WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12636    }
12637  }
12638}
12639
12640- (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
12641  if (otherDictionary) {
12642    for (int i = 0; i < 2; ++i) {
12643      if (otherDictionary->_valueSet[i]) {
12644        _valueSet[i] = YES;
12645        _values[i] = otherDictionary->_values[i];
12646      }
12647    }
12648    if (_autocreator) {
12649      GPBAutocreatedDictionaryModified(_autocreator, self);
12650    }
12651  }
12652}
12653
12654- (void)setFloat:(float)value forKey:(BOOL)key {
12655  int idx = (key ? 1 : 0);
12656  _values[idx] = value;
12657  _valueSet[idx] = YES;
12658  if (_autocreator) {
12659    GPBAutocreatedDictionaryModified(_autocreator, self);
12660  }
12661}
12662
12663- (void)removeFloatForKey:(BOOL)aKey {
12664  _valueSet[aKey ? 1 : 0] = NO;
12665}
12666
12667- (void)removeAll {
12668  _valueSet[0] = NO;
12669  _valueSet[1] = NO;
12670}
12671
12672@end
12673
12674//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
12675// This block of code is generated, do not edit it directly.
12676
12677#pragma mark - Bool -> Double
12678
12679@implementation GPBBoolDoubleDictionary {
12680 @package
12681  double _values[2];
12682  BOOL _valueSet[2];
12683}
12684
12685+ (instancetype)dictionary {
12686  return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
12687}
12688
12689+ (instancetype)dictionaryWithDouble:(double)value
12690                              forKey:(BOOL)key {
12691  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
12692  // on to get the type correct.
12693  return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:&value
12694                                                          forKeys:&key
12695                                                            count:1] autorelease];
12696}
12697
12698+ (instancetype)dictionaryWithDoubles:(const double [])values
12699                              forKeys:(const BOOL [])keys
12700                                count:(NSUInteger)count {
12701  // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
12702  // on to get the type correct.
12703  return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:values
12704                                                          forKeys:keys
12705                                                            count:count] autorelease];
12706}
12707
12708+ (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
12709  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12710  // on to get the type correct.
12711  return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12712}
12713
12714+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12715  return [[[self alloc] initWithCapacity:numItems] autorelease];
12716}
12717
12718- (instancetype)init {
12719  return [self initWithDoubles:NULL forKeys:NULL count:0];
12720}
12721
12722- (instancetype)initWithDoubles:(const double [])values
12723                        forKeys:(const BOOL [])keys
12724                          count:(NSUInteger)count {
12725  self = [super init];
12726  if (self) {
12727    for (NSUInteger i = 0; i < count; ++i) {
12728      int idx = keys[i] ? 1 : 0;
12729      _values[idx] = values[i];
12730      _valueSet[idx] = YES;
12731    }
12732  }
12733  return self;
12734}
12735
12736- (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
12737  self = [self initWithDoubles:NULL forKeys:NULL count:0];
12738  if (self) {
12739    if (dictionary) {
12740      for (int i = 0; i < 2; ++i) {
12741        if (dictionary->_valueSet[i]) {
12742          _values[i] = dictionary->_values[i];
12743          _valueSet[i] = YES;
12744        }
12745      }
12746    }
12747  }
12748  return self;
12749}
12750
12751- (instancetype)initWithCapacity:(NSUInteger)numItems {
12752  #pragma unused(numItems)
12753  return [self initWithDoubles:NULL forKeys:NULL count:0];
12754}
12755
12756#if !defined(NS_BLOCK_ASSERTIONS)
12757- (void)dealloc {
12758  NSAssert(!_autocreator,
12759           @"%@: Autocreator must be cleared before release, autocreator: %@",
12760           [self class], _autocreator);
12761  [super dealloc];
12762}
12763#endif  // !defined(NS_BLOCK_ASSERTIONS)
12764
12765- (instancetype)copyWithZone:(NSZone *)zone {
12766  return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
12767}
12768
12769- (BOOL)isEqual:(id)other {
12770  if (self == other) {
12771    return YES;
12772  }
12773  if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
12774    return NO;
12775  }
12776  GPBBoolDoubleDictionary *otherDictionary = other;
12777  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
12778      (_valueSet[1] != otherDictionary->_valueSet[1])) {
12779    return NO;
12780  }
12781  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
12782      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
12783    return NO;
12784  }
12785  return YES;
12786}
12787
12788- (NSUInteger)hash {
12789  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12790}
12791
12792- (NSString *)description {
12793  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12794  if (_valueSet[0]) {
12795    [result appendFormat:@"NO: %lf", _values[0]];
12796  }
12797  if (_valueSet[1]) {
12798    [result appendFormat:@"YES: %lf", _values[1]];
12799  }
12800  [result appendString:@" }"];
12801  return result;
12802}
12803
12804- (NSUInteger)count {
12805  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12806}
12807
12808- (BOOL)getDouble:(double *)value forKey:(BOOL)key {
12809  int idx = (key ? 1 : 0);
12810  if (_valueSet[idx]) {
12811    if (value) {
12812      *value = _values[idx];
12813    }
12814    return YES;
12815  }
12816  return NO;
12817}
12818
12819- (void)setGPBGenericValue:(GPBGenericValue *)value
12820     forGPBGenericValueKey:(GPBGenericValue *)key {
12821  int idx = (key->valueBool ? 1 : 0);
12822  _values[idx] = value->valueDouble;
12823  _valueSet[idx] = YES;
12824}
12825
12826- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12827  if (_valueSet[0]) {
12828    block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
12829  }
12830  if (_valueSet[1]) {
12831    block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
12832  }
12833}
12834
12835- (void)enumerateKeysAndDoublesUsingBlock:
12836    (void (^)(BOOL key, double value, BOOL *stop))block {
12837  BOOL stop = NO;
12838  if (_valueSet[0]) {
12839    block(NO, _values[0], &stop);
12840  }
12841  if (!stop && _valueSet[1]) {
12842    block(YES, _values[1], &stop);
12843  }
12844}
12845
12846- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12847  GPBDataType valueDataType = GPBGetFieldDataType(field);
12848  NSUInteger count = 0;
12849  size_t result = 0;
12850  for (int i = 0; i < 2; ++i) {
12851    if (_valueSet[i]) {
12852      ++count;
12853      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12854      msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12855      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12856    }
12857  }
12858  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12859  result += tagSize * count;
12860  return result;
12861}
12862
12863- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12864                         asField:(GPBFieldDescriptor *)field {
12865  GPBDataType valueDataType = GPBGetFieldDataType(field);
12866  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12867  for (int i = 0; i < 2; ++i) {
12868    if (_valueSet[i]) {
12869      // Write the tag.
12870      [outputStream writeInt32NoTag:tag];
12871      // Write the size of the message.
12872      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12873      msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12874      [outputStream writeInt32NoTag:(int32_t)msgSize];
12875      // Write the fields.
12876      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12877      WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12878    }
12879  }
12880}
12881
12882- (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
12883  if (otherDictionary) {
12884    for (int i = 0; i < 2; ++i) {
12885      if (otherDictionary->_valueSet[i]) {
12886        _valueSet[i] = YES;
12887        _values[i] = otherDictionary->_values[i];
12888      }
12889    }
12890    if (_autocreator) {
12891      GPBAutocreatedDictionaryModified(_autocreator, self);
12892    }
12893  }
12894}
12895
12896- (void)setDouble:(double)value forKey:(BOOL)key {
12897  int idx = (key ? 1 : 0);
12898  _values[idx] = value;
12899  _valueSet[idx] = YES;
12900  if (_autocreator) {
12901    GPBAutocreatedDictionaryModified(_autocreator, self);
12902  }
12903}
12904
12905- (void)removeDoubleForKey:(BOOL)aKey {
12906  _valueSet[aKey ? 1 : 0] = NO;
12907}
12908
12909- (void)removeAll {
12910  _valueSet[0] = NO;
12911  _valueSet[1] = NO;
12912}
12913
12914@end
12915
12916//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
12917// This block of code is generated, do not edit it directly.
12918
12919#pragma mark - Bool -> Object
12920
12921@implementation GPBBoolObjectDictionary {
12922 @package
12923  id _values[2];
12924}
12925
12926+ (instancetype)dictionary {
12927  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
12928}
12929
12930+ (instancetype)dictionaryWithObject:(id)object
12931                              forKey:(BOOL)key {
12932  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
12933  // on to get the type correct.
12934  return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
12935                                                          forKeys:&key
12936                                                            count:1] autorelease];
12937}
12938
12939+ (instancetype)dictionaryWithObjects:(const id [])objects
12940                              forKeys:(const BOOL [])keys
12941                                count:(NSUInteger)count {
12942  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
12943  // on to get the type correct.
12944  return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
12945                                                          forKeys:keys
12946                                                            count:count] autorelease];
12947}
12948
12949+ (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
12950  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12951  // on to get the type correct.
12952  return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12953}
12954
12955+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12956  return [[[self alloc] initWithCapacity:numItems] autorelease];
12957}
12958
12959- (instancetype)init {
12960  return [self initWithObjects:NULL forKeys:NULL count:0];
12961}
12962
12963- (instancetype)initWithObjects:(const id [])objects
12964                        forKeys:(const BOOL [])keys
12965                          count:(NSUInteger)count {
12966  self = [super init];
12967  if (self) {
12968    for (NSUInteger i = 0; i < count; ++i) {
12969      if (!objects[i]) {
12970        [NSException raise:NSInvalidArgumentException
12971                    format:@"Attempting to add nil object to a Dictionary"];
12972      }
12973      int idx = keys[i] ? 1 : 0;
12974      [_values[idx] release];
12975      _values[idx] = (id)[objects[i] retain];
12976    }
12977  }
12978  return self;
12979}
12980
12981- (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
12982  self = [self initWithObjects:NULL forKeys:NULL count:0];
12983  if (self) {
12984    if (dictionary) {
12985      _values[0] = [dictionary->_values[0] retain];
12986      _values[1] = [dictionary->_values[1] retain];
12987    }
12988  }
12989  return self;
12990}
12991
12992- (instancetype)initWithCapacity:(NSUInteger)numItems {
12993  #pragma unused(numItems)
12994  return [self initWithObjects:NULL forKeys:NULL count:0];
12995}
12996
12997- (void)dealloc {
12998  NSAssert(!_autocreator,
12999           @"%@: Autocreator must be cleared before release, autocreator: %@",
13000           [self class], _autocreator);
13001  [_values[0] release];
13002  [_values[1] release];
13003  [super dealloc];
13004}
13005
13006- (instancetype)copyWithZone:(NSZone *)zone {
13007  return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
13008}
13009
13010- (BOOL)isEqual:(id)other {
13011  if (self == other) {
13012    return YES;
13013  }
13014  if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
13015    return NO;
13016  }
13017  GPBBoolObjectDictionary *otherDictionary = other;
13018  if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
13019      ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
13020    return NO;
13021  }
13022  if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
13023      ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
13024    return NO;
13025  }
13026  return YES;
13027}
13028
13029- (NSUInteger)hash {
13030  return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
13031}
13032
13033- (NSString *)description {
13034  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
13035  if ((_values[0] != nil)) {
13036    [result appendFormat:@"NO: %@", _values[0]];
13037  }
13038  if ((_values[1] != nil)) {
13039    [result appendFormat:@"YES: %@", _values[1]];
13040  }
13041  [result appendString:@" }"];
13042  return result;
13043}
13044
13045- (NSUInteger)count {
13046  return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
13047}
13048
13049- (id)objectForKey:(BOOL)key {
13050  return _values[key ? 1 : 0];
13051}
13052
13053- (void)setGPBGenericValue:(GPBGenericValue *)value
13054     forGPBGenericValueKey:(GPBGenericValue *)key {
13055  int idx = (key->valueBool ? 1 : 0);
13056  [_values[idx] release];
13057  _values[idx] = [value->valueString retain];
13058}
13059
13060- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
13061  if (_values[0] != nil) {
13062    block(@"false", _values[0]);
13063  }
13064  if ((_values[1] != nil)) {
13065    block(@"true", _values[1]);
13066  }
13067}
13068
13069- (void)enumerateKeysAndObjectsUsingBlock:
13070    (void (^)(BOOL key, id object, BOOL *stop))block {
13071  BOOL stop = NO;
13072  if (_values[0] != nil) {
13073    block(NO, _values[0], &stop);
13074  }
13075  if (!stop && (_values[1] != nil)) {
13076    block(YES, _values[1], &stop);
13077  }
13078}
13079
13080- (BOOL)isInitialized {
13081  if (_values[0] && ![_values[0] isInitialized]) {
13082    return NO;
13083  }
13084  if (_values[1] && ![_values[1] isInitialized]) {
13085    return NO;
13086  }
13087  return YES;
13088}
13089
13090- (instancetype)deepCopyWithZone:(NSZone *)zone {
13091  GPBBoolObjectDictionary *newDict =
13092      [[GPBBoolObjectDictionary alloc] init];
13093  for (int i = 0; i < 2; ++i) {
13094    if (_values[i] != nil) {
13095      newDict->_values[i] = [_values[i] copyWithZone:zone];
13096    }
13097  }
13098  return newDict;
13099}
13100
13101- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
13102  GPBDataType valueDataType = GPBGetFieldDataType(field);
13103  NSUInteger count = 0;
13104  size_t result = 0;
13105  for (int i = 0; i < 2; ++i) {
13106    if (_values[i] != nil) {
13107      ++count;
13108      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13109      msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13110      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
13111    }
13112  }
13113  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
13114  result += tagSize * count;
13115  return result;
13116}
13117
13118- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
13119                         asField:(GPBFieldDescriptor *)field {
13120  GPBDataType valueDataType = GPBGetFieldDataType(field);
13121  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
13122  for (int i = 0; i < 2; ++i) {
13123    if (_values[i] != nil) {
13124      // Write the tag.
13125      [outputStream writeInt32NoTag:tag];
13126      // Write the size of the message.
13127      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13128      msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13129      [outputStream writeInt32NoTag:(int32_t)msgSize];
13130      // Write the fields.
13131      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13132      WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
13133    }
13134  }
13135}
13136
13137- (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
13138  if (otherDictionary) {
13139    for (int i = 0; i < 2; ++i) {
13140      if (otherDictionary->_values[i] != nil) {
13141        [_values[i] release];
13142        _values[i] = [otherDictionary->_values[i] retain];
13143      }
13144    }
13145    if (_autocreator) {
13146      GPBAutocreatedDictionaryModified(_autocreator, self);
13147    }
13148  }
13149}
13150
13151- (void)setObject:(id)object forKey:(BOOL)key {
13152  if (!object) {
13153    [NSException raise:NSInvalidArgumentException
13154                format:@"Attempting to add nil object to a Dictionary"];
13155  }
13156  int idx = (key ? 1 : 0);
13157  [_values[idx] release];
13158  _values[idx] = [object retain];
13159  if (_autocreator) {
13160    GPBAutocreatedDictionaryModified(_autocreator, self);
13161  }
13162}
13163
13164- (void)removeObjectForKey:(BOOL)aKey {
13165  int idx = (aKey ? 1 : 0);
13166  [_values[idx] release];
13167  _values[idx] = nil;
13168}
13169
13170- (void)removeAll {
13171  for (int i = 0; i < 2; ++i) {
13172    [_values[i] release];
13173    _values[i] = nil;
13174  }
13175}
13176
13177@end
13178
13179//%PDDM-EXPAND-END (8 expansions)
13180
13181#pragma mark - Bool -> Enum
13182
13183@implementation GPBBoolEnumDictionary {
13184 @package
13185  GPBEnumValidationFunc _validationFunc;
13186  int32_t _values[2];
13187  BOOL _valueSet[2];
13188}
13189
13190@synthesize validationFunc = _validationFunc;
13191
13192+ (instancetype)dictionary {
13193  return [[[self alloc] initWithValidationFunction:NULL
13194                                         rawValues:NULL
13195                                           forKeys:NULL
13196                                             count:0] autorelease];
13197}
13198
13199+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
13200  return [[[self alloc] initWithValidationFunction:func
13201                                         rawValues:NULL
13202                                           forKeys:NULL
13203                                             count:0] autorelease];
13204}
13205
13206+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
13207                                       rawValue:(int32_t)rawValue
13208                                          forKey:(BOOL)key {
13209  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
13210  // on to get the type correct.
13211  return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
13212                                                                 rawValues:&rawValue
13213                                                                   forKeys:&key
13214                                                                     count:1] autorelease];
13215}
13216
13217+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
13218                                       rawValues:(const int32_t [])values
13219                                         forKeys:(const BOOL [])keys
13220                                           count:(NSUInteger)count {
13221  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
13222  // on to get the type correct.
13223  return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
13224                                                                 rawValues:values
13225                                                                   forKeys:keys
13226                                                                     count:count] autorelease];
13227}
13228
13229+ (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
13230  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
13231  // on to get the type correct.
13232  return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
13233}
13234
13235+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
13236                                        capacity:(NSUInteger)numItems {
13237  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
13238}
13239
13240- (instancetype)init {
13241  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
13242}
13243
13244- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
13245  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
13246}
13247
13248- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
13249                                rawValues:(const int32_t [])rawValues
13250                                   forKeys:(const BOOL [])keys
13251                                     count:(NSUInteger)count {
13252  self = [super init];
13253  if (self) {
13254    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
13255    for (NSUInteger i = 0; i < count; ++i) {
13256      int idx = keys[i] ? 1 : 0;
13257      _values[idx] = rawValues[i];
13258      _valueSet[idx] = YES;
13259    }
13260  }
13261  return self;
13262}
13263
13264- (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
13265  self = [self initWithValidationFunction:dictionary.validationFunc
13266                                rawValues:NULL
13267                                  forKeys:NULL
13268                                    count:0];
13269  if (self) {
13270    if (dictionary) {
13271      for (int i = 0; i < 2; ++i) {
13272        if (dictionary->_valueSet[i]) {
13273          _values[i] = dictionary->_values[i];
13274          _valueSet[i] = YES;
13275        }
13276      }
13277    }
13278  }
13279  return self;
13280}
13281
13282- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
13283                                  capacity:(NSUInteger)numItems {
13284#pragma unused(numItems)
13285  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
13286}
13287
13288#if !defined(NS_BLOCK_ASSERTIONS)
13289- (void)dealloc {
13290  NSAssert(!_autocreator,
13291           @"%@: Autocreator must be cleared before release, autocreator: %@",
13292           [self class], _autocreator);
13293  [super dealloc];
13294}
13295#endif  // !defined(NS_BLOCK_ASSERTIONS)
13296
13297- (instancetype)copyWithZone:(NSZone *)zone {
13298  return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
13299}
13300
13301- (BOOL)isEqual:(id)other {
13302  if (self == other) {
13303    return YES;
13304  }
13305  if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
13306    return NO;
13307  }
13308  GPBBoolEnumDictionary *otherDictionary = other;
13309  if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
13310      (_valueSet[1] != otherDictionary->_valueSet[1])) {
13311    return NO;
13312  }
13313  if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
13314      (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
13315    return NO;
13316  }
13317  return YES;
13318}
13319
13320- (NSUInteger)hash {
13321  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
13322}
13323
13324- (NSString *)description {
13325  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
13326  if (_valueSet[0]) {
13327    [result appendFormat:@"NO: %d", _values[0]];
13328  }
13329  if (_valueSet[1]) {
13330    [result appendFormat:@"YES: %d", _values[1]];
13331  }
13332  [result appendString:@" }"];
13333  return result;
13334}
13335
13336- (NSUInteger)count {
13337  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
13338}
13339
13340- (BOOL)getEnum:(int32_t*)value forKey:(BOOL)key {
13341  int idx = (key ? 1 : 0);
13342  if (_valueSet[idx]) {
13343    if (value) {
13344      int32_t result = _values[idx];
13345      if (!_validationFunc(result)) {
13346        result = kGPBUnrecognizedEnumeratorValue;
13347      }
13348      *value = result;
13349    }
13350    return YES;
13351  }
13352  return NO;
13353}
13354
13355- (BOOL)getRawValue:(int32_t*)rawValue forKey:(BOOL)key {
13356  int idx = (key ? 1 : 0);
13357  if (_valueSet[idx]) {
13358    if (rawValue) {
13359      *rawValue = _values[idx];
13360    }
13361    return YES;
13362  }
13363  return NO;
13364}
13365
13366- (void)enumerateKeysAndRawValuesUsingBlock:
13367    (void (^)(BOOL key, int32_t value, BOOL *stop))block {
13368  BOOL stop = NO;
13369  if (_valueSet[0]) {
13370    block(NO, _values[0], &stop);
13371  }
13372  if (!stop && _valueSet[1]) {
13373    block(YES, _values[1], &stop);
13374  }
13375}
13376
13377- (void)enumerateKeysAndEnumsUsingBlock:
13378    (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
13379  BOOL stop = NO;
13380  GPBEnumValidationFunc func = _validationFunc;
13381  int32_t validatedValue;
13382  if (_valueSet[0]) {
13383    validatedValue = _values[0];
13384    if (!func(validatedValue)) {
13385      validatedValue = kGPBUnrecognizedEnumeratorValue;
13386    }
13387    block(NO, validatedValue, &stop);
13388  }
13389  if (!stop && _valueSet[1]) {
13390    validatedValue = _values[1];
13391    if (!func(validatedValue)) {
13392      validatedValue = kGPBUnrecognizedEnumeratorValue;
13393    }
13394    block(YES, validatedValue, &stop);
13395  }
13396}
13397
13398//%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
13399// This block of code is generated, do not edit it directly.
13400
13401- (NSData *)serializedDataForUnknownValue:(int32_t)value
13402                                   forKey:(GPBGenericValue *)key
13403                              keyDataType:(GPBDataType)keyDataType {
13404  size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
13405  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
13406  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
13407  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
13408  WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
13409  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
13410  [outputStream release];
13411  return data;
13412}
13413
13414//%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
13415
13416- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
13417  GPBDataType valueDataType = GPBGetFieldDataType(field);
13418  NSUInteger count = 0;
13419  size_t result = 0;
13420  for (int i = 0; i < 2; ++i) {
13421    if (_valueSet[i]) {
13422      ++count;
13423      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13424      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13425      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
13426    }
13427  }
13428  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
13429  result += tagSize * count;
13430  return result;
13431}
13432
13433- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
13434                         asField:(GPBFieldDescriptor *)field {
13435  GPBDataType valueDataType = GPBGetFieldDataType(field);
13436  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
13437  for (int i = 0; i < 2; ++i) {
13438    if (_valueSet[i]) {
13439      // Write the tag.
13440      [outputStream writeInt32NoTag:tag];
13441      // Write the size of the message.
13442      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13443      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13444      [outputStream writeInt32NoTag:(int32_t)msgSize];
13445      // Write the fields.
13446      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13447      WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
13448    }
13449  }
13450}
13451
13452- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
13453  if (_valueSet[0]) {
13454    block(@"false", @(_values[0]));
13455  }
13456  if (_valueSet[1]) {
13457    block(@"true", @(_values[1]));
13458  }
13459}
13460
13461- (void)setGPBGenericValue:(GPBGenericValue *)value
13462     forGPBGenericValueKey:(GPBGenericValue *)key {
13463  int idx = (key->valueBool ? 1 : 0);
13464  _values[idx] = value->valueInt32;
13465  _valueSet[idx] = YES;
13466}
13467
13468- (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
13469  if (otherDictionary) {
13470    for (int i = 0; i < 2; ++i) {
13471      if (otherDictionary->_valueSet[i]) {
13472        _valueSet[i] = YES;
13473        _values[i] = otherDictionary->_values[i];
13474      }
13475    }
13476    if (_autocreator) {
13477      GPBAutocreatedDictionaryModified(_autocreator, self);
13478    }
13479  }
13480}
13481
13482- (void)setEnum:(int32_t)value forKey:(BOOL)key {
13483  if (!_validationFunc(value)) {
13484    [NSException raise:NSInvalidArgumentException
13485                format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
13486     value];
13487  }
13488  int idx = (key ? 1 : 0);
13489  _values[idx] = value;
13490  _valueSet[idx] = YES;
13491  if (_autocreator) {
13492    GPBAutocreatedDictionaryModified(_autocreator, self);
13493  }
13494}
13495
13496- (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
13497  int idx = (key ? 1 : 0);
13498  _values[idx] = rawValue;
13499  _valueSet[idx] = YES;
13500  if (_autocreator) {
13501    GPBAutocreatedDictionaryModified(_autocreator, self);
13502  }
13503}
13504
13505- (void)removeEnumForKey:(BOOL)aKey {
13506  _valueSet[aKey ? 1 : 0] = NO;
13507}
13508
13509- (void)removeAll {
13510  _valueSet[0] = NO;
13511  _valueSet[1] = NO;
13512}
13513
13514@end
13515
13516#pragma mark - NSDictionary Subclass
13517
13518@implementation GPBAutocreatedDictionary {
13519  NSMutableDictionary *_dictionary;
13520}
13521
13522- (void)dealloc {
13523  NSAssert(!_autocreator,
13524           @"%@: Autocreator must be cleared before release, autocreator: %@",
13525           [self class], _autocreator);
13526  [_dictionary release];
13527  [super dealloc];
13528}
13529
13530#pragma mark Required NSDictionary overrides
13531
13532- (instancetype)initWithObjects:(const id [])objects
13533                        forKeys:(const id<NSCopying> [])keys
13534                          count:(NSUInteger)count {
13535  self = [super init];
13536  if (self) {
13537    _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
13538                                                       forKeys:keys
13539                                                         count:count];
13540  }
13541  return self;
13542}
13543
13544- (NSUInteger)count {
13545  return [_dictionary count];
13546}
13547
13548- (id)objectForKey:(id)aKey {
13549  return [_dictionary objectForKey:aKey];
13550}
13551
13552- (NSEnumerator *)keyEnumerator {
13553  if (_dictionary == nil) {
13554    _dictionary = [[NSMutableDictionary alloc] init];
13555  }
13556  return [_dictionary keyEnumerator];
13557}
13558
13559#pragma mark Required NSMutableDictionary overrides
13560
13561// Only need to call GPBAutocreatedDictionaryModified() when adding things
13562// since we only autocreate empty dictionaries.
13563
13564- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
13565  if (_dictionary == nil) {
13566    _dictionary = [[NSMutableDictionary alloc] init];
13567  }
13568  [_dictionary setObject:anObject forKey:aKey];
13569  if (_autocreator) {
13570    GPBAutocreatedDictionaryModified(_autocreator, self);
13571  }
13572}
13573
13574- (void)removeObjectForKey:(id)aKey {
13575  [_dictionary removeObjectForKey:aKey];
13576}
13577
13578#pragma mark Extra things hooked
13579
13580- (id)copyWithZone:(NSZone *)zone {
13581  if (_dictionary == nil) {
13582    _dictionary = [[NSMutableDictionary alloc] init];
13583  }
13584  return [_dictionary copyWithZone:zone];
13585}
13586
13587- (id)mutableCopyWithZone:(NSZone *)zone {
13588  if (_dictionary == nil) {
13589    _dictionary = [[NSMutableDictionary alloc] init];
13590  }
13591  return [_dictionary mutableCopyWithZone:zone];
13592}
13593
13594- (id)objectForKeyedSubscript:(id)key {
13595  return [_dictionary objectForKeyedSubscript:key];
13596}
13597
13598- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
13599  if (_dictionary == nil) {
13600    _dictionary = [[NSMutableDictionary alloc] init];
13601  }
13602  [_dictionary setObject:obj forKeyedSubscript:key];
13603  if (_autocreator) {
13604    GPBAutocreatedDictionaryModified(_autocreator, self);
13605  }
13606}
13607
13608- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
13609                                                    id obj,
13610                                                    BOOL *stop))block {
13611  [_dictionary enumerateKeysAndObjectsUsingBlock:block];
13612}
13613
13614- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
13615                                usingBlock:(void (^)(id key,
13616                                                     id obj,
13617                                                     BOOL *stop))block {
13618  [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
13619}
13620
13621@end
13622
13623#pragma clang diagnostic pop
13624