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// Used to include code only visible to specific versions of the static
49// analyzer. Useful for wrapping code that only exists to silence the analyzer.
50// Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
51// END_APPLE_BUILD_VERSION using:
52//   xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
53// Example usage:
54//  #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
55#define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
56    (defined(__clang_analyzer__) && \
57     (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
58      __apple_build_version__ <= END_APPLE_BUILD_VERSION))
59
60enum {
61  kMapKeyFieldNumber = 1,
62  kMapValueFieldNumber = 2,
63};
64
65static BOOL DictDefault_IsValidValue(int32_t value) {
66  // Anything but the bad value marker is allowed.
67  return (value != kGPBUnrecognizedEnumeratorValue);
68}
69
70//%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
71//%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
72//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
73//%    return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
74//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
75//%    return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
76//%  } else {
77//%    NSCAssert(NO, @"Unexpected type %d", dataType);
78//%    return 0;
79//%  }
80//%}
81//%
82//%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
83//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
84//%    [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
85//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
86//%    [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
87//%  } else {
88//%    NSCAssert(NO, @"Unexpected type %d", dataType);
89//%  }
90//%}
91//%
92//%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
93//%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
94//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
95//%    return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
96//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
97//%    return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
98//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
99//%    return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
100//%  } else {
101//%    NSCAssert(NO, @"Unexpected type %d", dataType);
102//%    return 0;
103//%  }
104//%}
105//%
106//%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
107//%  if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
108//%    [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
109//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
110//%    [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
111//%  } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
112//%    [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
113//%  } else {
114//%    NSCAssert(NO, @"Unexpected type %d", dataType);
115//%  }
116//%}
117//%
118//%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
119//%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
120//%  NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
121//%  #pragma unused(dataType)  // For when asserts are off in release.
122//%  return GPBCompute##VALUE_NAME##Size(fieldNum, value);
123//%}
124//%
125//%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, 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//%  [stream write##VALUE_NAME##:fieldNum value:value];
129//%}
130//%
131//%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
132//%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
133//%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
134//%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
135//%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
136//%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
137//%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
138//%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
139//%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
140//%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
141//%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
142//%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
143// This block of code is generated, do not edit it directly.
144
145static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
146  if (dataType == GPBDataTypeInt32) {
147    return GPBComputeInt32Size(fieldNum, value);
148  } else if (dataType == GPBDataTypeSInt32) {
149    return GPBComputeSInt32Size(fieldNum, value);
150  } else if (dataType == GPBDataTypeSFixed32) {
151    return GPBComputeSFixed32Size(fieldNum, value);
152  } else {
153    NSCAssert(NO, @"Unexpected type %d", dataType);
154    return 0;
155  }
156}
157
158static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
159  if (dataType == GPBDataTypeInt32) {
160    [stream writeInt32:fieldNum value:value];
161  } else if (dataType == GPBDataTypeSInt32) {
162    [stream writeSInt32:fieldNum value:value];
163  } else if (dataType == GPBDataTypeSFixed32) {
164    [stream writeSFixed32:fieldNum value:value];
165  } else {
166    NSCAssert(NO, @"Unexpected type %d", dataType);
167  }
168}
169
170static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
171  if (dataType == GPBDataTypeUInt32) {
172    return GPBComputeUInt32Size(fieldNum, value);
173  } else if (dataType == GPBDataTypeFixed32) {
174    return GPBComputeFixed32Size(fieldNum, value);
175  } else {
176    NSCAssert(NO, @"Unexpected type %d", dataType);
177    return 0;
178  }
179}
180
181static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
182  if (dataType == GPBDataTypeUInt32) {
183    [stream writeUInt32:fieldNum value:value];
184  } else if (dataType == GPBDataTypeFixed32) {
185    [stream writeFixed32:fieldNum value:value];
186  } else {
187    NSCAssert(NO, @"Unexpected type %d", dataType);
188  }
189}
190
191static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
192  if (dataType == GPBDataTypeInt64) {
193    return GPBComputeInt64Size(fieldNum, value);
194  } else if (dataType == GPBDataTypeSInt64) {
195    return GPBComputeSInt64Size(fieldNum, value);
196  } else if (dataType == GPBDataTypeSFixed64) {
197    return GPBComputeSFixed64Size(fieldNum, value);
198  } else {
199    NSCAssert(NO, @"Unexpected type %d", dataType);
200    return 0;
201  }
202}
203
204static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
205  if (dataType == GPBDataTypeInt64) {
206    [stream writeInt64:fieldNum value:value];
207  } else if (dataType == GPBDataTypeSInt64) {
208    [stream writeSInt64:fieldNum value:value];
209  } else if (dataType == GPBDataTypeSFixed64) {
210    [stream writeSFixed64:fieldNum value:value];
211  } else {
212    NSCAssert(NO, @"Unexpected type %d", dataType);
213  }
214}
215
216static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
217  if (dataType == GPBDataTypeUInt64) {
218    return GPBComputeUInt64Size(fieldNum, value);
219  } else if (dataType == GPBDataTypeFixed64) {
220    return GPBComputeFixed64Size(fieldNum, value);
221  } else {
222    NSCAssert(NO, @"Unexpected type %d", dataType);
223    return 0;
224  }
225}
226
227static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
228  if (dataType == GPBDataTypeUInt64) {
229    [stream writeUInt64:fieldNum value:value];
230  } else if (dataType == GPBDataTypeFixed64) {
231    [stream writeFixed64:fieldNum value:value];
232  } else {
233    NSCAssert(NO, @"Unexpected type %d", dataType);
234  }
235}
236
237static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
238  NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
239  #pragma unused(dataType)  // For when asserts are off in release.
240  return GPBComputeBoolSize(fieldNum, value);
241}
242
243static void WriteDictBoolField(GPBCodedOutputStream *stream, 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  [stream writeBool:fieldNum value:value];
247}
248
249static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
250  NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
251  #pragma unused(dataType)  // For when asserts are off in release.
252  return GPBComputeEnumSize(fieldNum, value);
253}
254
255static void WriteDictEnumField(GPBCodedOutputStream *stream, 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  [stream writeEnum:fieldNum value:value];
259}
260
261static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
262  NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
263  #pragma unused(dataType)  // For when asserts are off in release.
264  return GPBComputeFloatSize(fieldNum, value);
265}
266
267static void WriteDictFloatField(GPBCodedOutputStream *stream, 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  [stream writeFloat:fieldNum value:value];
271}
272
273static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
274  NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
275  #pragma unused(dataType)  // For when asserts are off in release.
276  return GPBComputeDoubleSize(fieldNum, value);
277}
278
279static void WriteDictDoubleField(GPBCodedOutputStream *stream, 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  [stream writeDouble:fieldNum value:value];
283}
284
285static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
286  NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
287  #pragma unused(dataType)  // For when asserts are off in release.
288  return GPBComputeStringSize(fieldNum, value);
289}
290
291static void WriteDictStringField(GPBCodedOutputStream *stream, 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  [stream writeString:fieldNum value:value];
295}
296
297static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
298  if (dataType == GPBDataTypeMessage) {
299    return GPBComputeMessageSize(fieldNum, value);
300  } else if (dataType == GPBDataTypeString) {
301    return GPBComputeStringSize(fieldNum, value);
302  } else if (dataType == GPBDataTypeBytes) {
303    return GPBComputeBytesSize(fieldNum, value);
304  } else {
305    NSCAssert(NO, @"Unexpected type %d", dataType);
306    return 0;
307  }
308}
309
310static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
311  if (dataType == GPBDataTypeMessage) {
312    [stream writeMessage:fieldNum value:value];
313  } else if (dataType == GPBDataTypeString) {
314    [stream writeString:fieldNum value:value];
315  } else if (dataType == GPBDataTypeBytes) {
316    [stream writeBytes:fieldNum value:value];
317  } else {
318    NSCAssert(NO, @"Unexpected type %d", dataType);
319  }
320}
321
322//%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
323
324size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
325  GPBDataType mapValueType = GPBGetFieldDataType(field);
326  __block size_t result = 0;
327  [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
328    #pragma unused(stop)
329    size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
330    msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
331    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
332  }];
333  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
334  result += tagSize * dict.count;
335  return result;
336}
337
338void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
339                                              NSDictionary *dict,
340                                              GPBFieldDescriptor *field) {
341  NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
342  GPBDataType mapValueType = GPBGetFieldDataType(field);
343  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
344  [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
345    #pragma unused(stop)
346    // Write the tag.
347    [outputStream writeInt32NoTag:tag];
348    // Write the size of the message.
349    size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
350    msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
351
352    // Write the size and fields.
353    [outputStream writeInt32NoTag:(int32_t)msgSize];
354    [outputStream writeString:kMapKeyFieldNumber value:key];
355    WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
356  }];
357}
358
359BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
360  NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
361  NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
362  #pragma unused(field)  // For when asserts are off in release.
363  for (GPBMessage *msg in [dict objectEnumerator]) {
364    if (!msg.initialized) {
365      return NO;
366    }
367  }
368  return YES;
369}
370
371// Note: if the type is an object, it the retain pass back to the caller.
372static void ReadValue(GPBCodedInputStream *stream,
373                      GPBGenericValue *valueToFill,
374                      GPBDataType type,
375                      GPBExtensionRegistry *registry,
376                      GPBFieldDescriptor *field) {
377  switch (type) {
378    case GPBDataTypeBool:
379      valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
380      break;
381    case GPBDataTypeFixed32:
382      valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
383      break;
384    case GPBDataTypeSFixed32:
385      valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
386      break;
387    case GPBDataTypeFloat:
388      valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
389      break;
390    case GPBDataTypeFixed64:
391      valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
392      break;
393    case GPBDataTypeSFixed64:
394      valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
395      break;
396    case GPBDataTypeDouble:
397      valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
398      break;
399    case GPBDataTypeInt32:
400      valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
401      break;
402    case GPBDataTypeInt64:
403      valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
404      break;
405    case GPBDataTypeSInt32:
406      valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
407      break;
408    case GPBDataTypeSInt64:
409      valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
410      break;
411    case GPBDataTypeUInt32:
412      valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
413      break;
414    case GPBDataTypeUInt64:
415      valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
416      break;
417    case GPBDataTypeBytes:
418      [valueToFill->valueData release];
419      valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
420      break;
421    case GPBDataTypeString:
422      [valueToFill->valueString release];
423      valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
424      break;
425    case GPBDataTypeMessage: {
426      GPBMessage *message = [[field.msgClass alloc] init];
427      [stream readMessage:message extensionRegistry:registry];
428      [valueToFill->valueMessage release];
429      valueToFill->valueMessage = message;
430      break;
431    }
432    case GPBDataTypeGroup:
433      NSCAssert(NO, @"Can't happen");
434      break;
435    case GPBDataTypeEnum:
436      valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
437      break;
438  }
439}
440
441void GPBDictionaryReadEntry(id mapDictionary,
442                            GPBCodedInputStream *stream,
443                            GPBExtensionRegistry *registry,
444                            GPBFieldDescriptor *field,
445                            GPBMessage *parentMessage) {
446  GPBDataType keyDataType = field.mapKeyDataType;
447  GPBDataType valueDataType = GPBGetFieldDataType(field);
448
449  GPBGenericValue key;
450  GPBGenericValue value;
451  // Zero them (but pick up any enum default for proto2).
452  key.valueString = value.valueString = nil;
453  if (valueDataType == GPBDataTypeEnum) {
454    value = field.defaultValue;
455  }
456
457  GPBCodedInputStreamState *state = &stream->state_;
458  uint32_t keyTag =
459      GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
460  uint32_t valueTag =
461      GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
462
463  BOOL hitError = NO;
464  while (YES) {
465    uint32_t tag = GPBCodedInputStreamReadTag(state);
466    if (tag == keyTag) {
467      ReadValue(stream, &key, keyDataType, registry, field);
468    } else if (tag == valueTag) {
469      ReadValue(stream, &value, valueDataType, registry, field);
470    } else if (tag == 0) {
471      // zero signals EOF / limit reached
472      break;
473    } else {  // Unknown
474      if (![stream skipField:tag]){
475        hitError = YES;
476        break;
477      }
478    }
479  }
480
481  if (!hitError) {
482    // Handle the special defaults and/or missing key/value.
483    if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
484      key.valueString = [@"" retain];
485    }
486    if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
487      switch (valueDataType) {
488        case GPBDataTypeString:
489          value.valueString = [@"" retain];
490          break;
491        case GPBDataTypeBytes:
492          value.valueData = [GPBEmptyNSData() retain];
493          break;
494#if defined(__clang_analyzer__)
495        case GPBDataTypeGroup:
496          // Maps can't really have Groups as the value type, but this case is needed
497          // so the analyzer won't report the posibility of send nil in for the value
498          // in the NSMutableDictionary case below.
499#endif
500        case GPBDataTypeMessage: {
501          value.valueMessage = [[field.msgClass alloc] init];
502          break;
503        }
504        default:
505          // Nothing
506          break;
507      }
508    }
509
510    if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
511#if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
512     // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
513     // be raised as needed for new Xcodes.
514     //
515     // This is only needed on a "shallow" analyze; on a "deep" analyze, the
516     // existing code path gets this correct. In shallow, the analyzer decides
517     // GPBDataTypeIsObject(valueDataType) is both false and true on a single
518     // path through this function, allowing nil to be used for the
519     // setObject:forKey:.
520     if (value.valueString == nil) {
521       value.valueString = [@"" retain];
522     }
523#endif
524      // mapDictionary is an NSMutableDictionary
525      [(NSMutableDictionary *)mapDictionary setObject:value.valueString
526                                               forKey:key.valueString];
527    } else {
528      if (valueDataType == GPBDataTypeEnum) {
529        if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
530            [field isValidEnumValue:value.valueEnum]) {
531          [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
532        } else {
533          NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
534                                                               forKey:&key
535                                                          keyDataType:keyDataType];
536          [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
537        }
538      } else {
539        [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
540      }
541    }
542  }
543
544  if (GPBDataTypeIsObject(keyDataType)) {
545    [key.valueString release];
546  }
547  if (GPBDataTypeIsObject(valueDataType)) {
548    [value.valueString release];
549  }
550}
551
552//
553// Macros for the common basic cases.
554//
555
556//%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
557//%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
558//%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
559
560//%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
561//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
562//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
563//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
564//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
565//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
566//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
567//%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
568//%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
569
570//%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
571//%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, value)
572
573//%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
574//%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, object)
575
576//%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME)
577//%#pragma mark - KEY_NAME -> VALUE_NAME
578//%
579//%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
580//% @package
581//%  NSMutableDictionary *_dictionary;
582//%}
583//%
584//%+ (instancetype)dictionary {
585//%  return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
586//%}
587//%
588//%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)##VNAME
589//%                      ##VNAME$S##  forKey:(KEY_TYPE##KisP$S##KisP)key {
590//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
591//%  // on to get the type correct.
592//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
593//%               KEY_NAME$S VALUE_NAME$S                        ##VNAME$S##  forKeys:&key
594//%               KEY_NAME$S VALUE_NAME$S                        ##VNAME$S##    count:1] autorelease];
595//%}
596//%
597//%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
598//%                      ##VNAME$S##  forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
599//%                      ##VNAME$S##    count:(NSUInteger)count {
600//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
601//%  // on to get the type correct.
602//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
603//%               KEY_NAME$S VALUE_NAME$S                               forKeys:keys
604//%               KEY_NAME$S VALUE_NAME$S                                 count:count] autorelease];
605//%}
606//%
607//%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
608//%  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
609//%  // on to get the type correct.
610//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
611//%}
612//%
613//%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
614//%  return [[[self alloc] initWithCapacity:numItems] autorelease];
615//%}
616//%
617//%- (instancetype)init {
618//%  return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
619//%}
620//%
621//%- (instancetype)initWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
622//%                ##VNAME$S##  forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
623//%                ##VNAME$S##    count:(NSUInteger)count {
624//%  self = [super init];
625//%  if (self) {
626//%    _dictionary = [[NSMutableDictionary alloc] init];
627//%    if (count && VNAME##s && keys) {
628//%      for (NSUInteger i = 0; i < count; ++i) {
629//%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______)        [_dictionary setObject:WRAPPED##VHELPER(VNAME##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
630//%      }
631//%    }
632//%  }
633//%  return self;
634//%}
635//%
636//%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
637//%  self = [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
638//%  if (self) {
639//%    if (dictionary) {
640//%      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
641//%    }
642//%  }
643//%  return self;
644//%}
645//%
646//%- (instancetype)initWithCapacity:(NSUInteger)numItems {
647//%  #pragma unused(numItems)
648//%  return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
649//%}
650//%
651//%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
652//%
653//%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
654//%
655//%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
656//%
657//%@end
658//%
659
660//%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
661//%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
662//%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
663//%#pragma mark - KEY_NAME -> VALUE_NAME
664//%
665//%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
666//% @package
667//%  NSMutableDictionary *_dictionary;
668//%  GPBEnumValidationFunc _validationFunc;
669//%}
670//%
671//%@synthesize validationFunc = _validationFunc;
672//%
673//%+ (instancetype)dictionary {
674//%  return [[[self alloc] initWithValidationFunction:NULL
675//%                                         rawValues:NULL
676//%                                           forKeys:NULL
677//%                                             count:0] autorelease];
678//%}
679//%
680//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
681//%  return [[[self alloc] initWithValidationFunction:func
682//%                                         rawValues:NULL
683//%                                           forKeys:NULL
684//%                                             count:0] autorelease];
685//%}
686//%
687//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
688//%                                        rawValue:(VALUE_TYPE)rawValue
689//%                                          forKey:(KEY_TYPE##KisP$S##KisP)key {
690//%  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
691//%  // on to get the type correct.
692//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
693//%               KEY_NAME$S VALUE_NAME$S                                         rawValues:&rawValue
694//%               KEY_NAME$S VALUE_NAME$S                                           forKeys:&key
695//%               KEY_NAME$S VALUE_NAME$S                                             count:1] autorelease];
696//%}
697//%
698//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
699//%                                       rawValues:(const VALUE_TYPE [])rawValues
700//%                                         forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
701//%                                           count:(NSUInteger)count {
702//%  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
703//%  // on to get the type correct.
704//%  return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
705//%               KEY_NAME$S VALUE_NAME$S                                         rawValues:rawValues
706//%               KEY_NAME$S VALUE_NAME$S                                           forKeys:keys
707//%               KEY_NAME$S VALUE_NAME$S                                             count:count] autorelease];
708//%}
709//%
710//%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
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] initWithDictionary:dictionary] autorelease];
714//%}
715//%
716//%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
717//%                                        capacity:(NSUInteger)numItems {
718//%  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
719//%}
720//%
721//%- (instancetype)init {
722//%  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
723//%}
724//%
725//%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
726//%  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
727//%}
728//%
729//%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
730//%                                 rawValues:(const VALUE_TYPE [])rawValues
731//%                                   forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
732//%                                     count:(NSUInteger)count {
733//%  self = [super init];
734//%  if (self) {
735//%    _dictionary = [[NSMutableDictionary alloc] init];
736//%    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
737//%    if (count && rawValues && keys) {
738//%      for (NSUInteger i = 0; i < count; ++i) {
739//%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______)        [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
740//%      }
741//%    }
742//%  }
743//%  return self;
744//%}
745//%
746//%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
747//%  self = [self initWithValidationFunction:dictionary.validationFunc
748//%                                rawValues:NULL
749//%                                  forKeys:NULL
750//%                                    count:0];
751//%  if (self) {
752//%    if (dictionary) {
753//%      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
754//%    }
755//%  }
756//%  return self;
757//%}
758//%
759//%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
760//%                                  capacity:(NSUInteger)numItems {
761//%  #pragma unused(numItems)
762//%  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
763//%}
764//%
765//%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
766//%
767//%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
768//%  NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
769//%  if (wrapped && value) {
770//%    VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
771//%    if (!_validationFunc(result)) {
772//%      result = kGPBUnrecognizedEnumeratorValue;
773//%    }
774//%    *value = result;
775//%  }
776//%  return (wrapped != NULL);
777//%}
778//%
779//%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
780//%  NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
781//%  if (wrapped && rawValue) {
782//%    *rawValue = UNWRAP##VALUE_NAME(wrapped);
783//%  }
784//%  return (wrapped != NULL);
785//%}
786//%
787//%- (void)enumerateKeysAndValuesUsingBlock:
788//%    (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
789//%  GPBEnumValidationFunc func = _validationFunc;
790//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
791//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
792//%                                                   BOOL *stop) {
793//%      VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
794//%      if (!func(unwrapped)) {
795//%        unwrapped = kGPBUnrecognizedEnumeratorValue;
796//%      }
797//%      block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
798//%  }];
799//%}
800//%
801//%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
802//%
803//%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
804//%DICTIONARY_VALIDATE_KEY_##KHELPER(key, )  if (!_validationFunc(value)) {
805//%    [NSException raise:NSInvalidArgumentException
806//%                format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
807//%                       value];
808//%  }
809//%
810//%  [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
811//%  if (_autocreator) {
812//%    GPBAutocreatedDictionaryModified(_autocreator, self);
813//%  }
814//%}
815//%
816//%@end
817//%
818
819//%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
820//%- (void)dealloc {
821//%  NSAssert(!_autocreator,
822//%           @"%@: Autocreator must be cleared before release, autocreator: %@",
823//%           [self class], _autocreator);
824//%  [_dictionary release];
825//%  [super dealloc];
826//%}
827//%
828//%- (instancetype)copyWithZone:(NSZone *)zone {
829//%  return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
830//%}
831//%
832//%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
833//%  if (self == other) {
834//%    return YES;
835//%  }
836//%  if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
837//%    return NO;
838//%  }
839//%  return [_dictionary isEqual:other->_dictionary];
840//%}
841//%
842//%- (NSUInteger)hash {
843//%  return _dictionary.count;
844//%}
845//%
846//%- (NSString *)description {
847//%  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
848//%}
849//%
850//%- (NSUInteger)count {
851//%  return _dictionary.count;
852//%}
853//%
854//%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:
855//%    (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop))block {
856//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
857//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
858//%                                                   BOOL *stop) {
859//%      block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME$u), stop);
860//%  }];
861//%}
862//%
863//%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
864//%  NSUInteger count = _dictionary.count;
865//%  if (count == 0) {
866//%    return 0;
867//%  }
868//%
869//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
870//%  GPBDataType keyDataType = field.mapKeyDataType;
871//%  __block size_t result = 0;
872//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
873//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u##,
874//%                                                   BOOL *stop) {
875//%    #pragma unused(stop)
876//%    size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
877//%    msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
878//%    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
879//%  }];
880//%  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
881//%  result += tagSize * count;
882//%  return result;
883//%}
884//%
885//%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
886//%                         asField:(GPBFieldDescriptor *)field {
887//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
888//%  GPBDataType keyDataType = field.mapKeyDataType;
889//%  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
890//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
891//%                                                   ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
892//%                                                   BOOL *stop) {
893//%    #pragma unused(stop)
894//%    // Write the tag.
895//%    [outputStream writeInt32NoTag:tag];
896//%    // Write the size of the message.
897//%    size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
898//%    msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
899//%    [outputStream writeInt32NoTag:(int32_t)msgSize];
900//%    // Write the fields.
901//%    WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
902//%    WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
903//%  }];
904//%}
905//%
906//%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
907//%     forGPBGenericValueKey:(GPBGenericValue *)key {
908//%  [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
909//%}
910//%
911//%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
912//%  [self enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop) {
913//%      #pragma unused(stop)
914//%      block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME));
915//%  }];
916//%}
917//%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
918//%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
919//%  if (otherDictionary) {
920//%    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
921//%    if (_autocreator) {
922//%      GPBAutocreatedDictionaryModified(_autocreator, self);
923//%    }
924//%  }
925//%}
926//%
927//%- (void)set##ACCESSOR_NAME##VNAME$u##:(VALUE_TYPE)VNAME forKey:(KEY_TYPE##KisP$S##KisP)key {
928//%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, )  [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)];
929//%  if (_autocreator) {
930//%    GPBAutocreatedDictionaryModified(_autocreator, self);
931//%  }
932//%}
933//%
934//%- (void)remove##VNAME$u##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
935//%  [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
936//%}
937//%
938//%- (void)removeAll {
939//%  [_dictionary removeAllObjects];
940//%}
941
942//
943// Custom Generation for Bool keys
944//
945
946//%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
947//%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, value)
948//%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
949//%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, object)
950
951//%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME)
952//%#pragma mark - Bool -> VALUE_NAME
953//%
954//%@implementation GPBBool##VALUE_NAME##Dictionary {
955//% @package
956//%  VALUE_TYPE _values[2];
957//%BOOL_DICT_HAS_STORAGE_##HELPER()}
958//%
959//%+ (instancetype)dictionary {
960//%  return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
961//%}
962//%
963//%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)VNAME
964//%                      ##VNAME$S##  forKey:(BOOL)key {
965//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
966//%  // on to get the type correct.
967//%  return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
968//%                    VALUE_NAME$S                        ##VNAME$S##  forKeys:&key
969//%                    VALUE_NAME$S                        ##VNAME$S##    count:1] autorelease];
970//%}
971//%
972//%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
973//%                      ##VNAME$S##  forKeys:(const BOOL [])keys
974//%                      ##VNAME$S##    count:(NSUInteger)count {
975//%  // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
976//%  // on to get the type correct.
977//%  return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
978//%                    VALUE_NAME$S                        ##VNAME$S##  forKeys:keys
979//%                    VALUE_NAME$S                        ##VNAME$S##    count:count] autorelease];
980//%}
981//%
982//%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
983//%  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
984//%  // on to get the type correct.
985//%  return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
986//%}
987//%
988//%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
989//%  return [[[self alloc] initWithCapacity:numItems] autorelease];
990//%}
991//%
992//%- (instancetype)init {
993//%  return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
994//%}
995//%
996//%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
997//%
998//%- (instancetype)initWithCapacity:(NSUInteger)numItems {
999//%  #pragma unused(numItems)
1000//%  return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
1001//%}
1002//%
1003//%BOOL_DICT_DEALLOC##HELPER()
1004//%
1005//%- (instancetype)copyWithZone:(NSZone *)zone {
1006//%  return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
1007//%}
1008//%
1009//%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
1010//%  if (self == other) {
1011//%    return YES;
1012//%  }
1013//%  if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
1014//%    return NO;
1015//%  }
1016//%  if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
1017//%      (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
1018//%    return NO;
1019//%  }
1020//%  if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
1021//%      (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
1022//%    return NO;
1023//%  }
1024//%  return YES;
1025//%}
1026//%
1027//%- (NSUInteger)hash {
1028//%  return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
1029//%}
1030//%
1031//%- (NSString *)description {
1032//%  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
1033//%  if (BOOL_DICT_W_HAS##HELPER(0, )) {
1034//%    [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
1035//%  }
1036//%  if (BOOL_DICT_W_HAS##HELPER(1, )) {
1037//%    [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
1038//%  }
1039//%  [result appendString:@" }"];
1040//%  return result;
1041//%}
1042//%
1043//%- (NSUInteger)count {
1044//%  return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
1045//%}
1046//%
1047//%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
1048//%
1049//%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
1050//%
1051//%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
1052//%  if (BOOL_DICT_HAS##HELPER(0, )) {
1053//%    block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
1054//%  }
1055//%  if (BOOL_DICT_W_HAS##HELPER(1, )) {
1056//%    block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
1057//%  }
1058//%}
1059//%
1060//%- (void)enumerateKeysAnd##VNAME$u##sUsingBlock:
1061//%    (void (^)(BOOL key, VALUE_TYPE VNAME, BOOL *stop))block {
1062//%  BOOL stop = NO;
1063//%  if (BOOL_DICT_HAS##HELPER(0, )) {
1064//%    block(NO, _values[0], &stop);
1065//%  }
1066//%  if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
1067//%    block(YES, _values[1], &stop);
1068//%  }
1069//%}
1070//%
1071//%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
1072//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
1073//%  NSUInteger count = 0;
1074//%  size_t result = 0;
1075//%  for (int i = 0; i < 2; ++i) {
1076//%    if (BOOL_DICT_HAS##HELPER(i, )) {
1077//%      ++count;
1078//%      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
1079//%      msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
1080//%      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
1081//%    }
1082//%  }
1083//%  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
1084//%  result += tagSize * count;
1085//%  return result;
1086//%}
1087//%
1088//%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
1089//%                         asField:(GPBFieldDescriptor *)field {
1090//%  GPBDataType valueDataType = GPBGetFieldDataType(field);
1091//%  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
1092//%  for (int i = 0; i < 2; ++i) {
1093//%    if (BOOL_DICT_HAS##HELPER(i, )) {
1094//%      // Write the tag.
1095//%      [outputStream writeInt32NoTag:tag];
1096//%      // Write the size of the message.
1097//%      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
1098//%      msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
1099//%      [outputStream writeInt32NoTag:(int32_t)msgSize];
1100//%      // Write the fields.
1101//%      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
1102//%      WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
1103//%    }
1104//%  }
1105//%}
1106//%
1107//%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
1108//%
1109//%@end
1110//%
1111
1112
1113//
1114// Helpers for PODs
1115//
1116
1117//%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
1118//%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
1119//%  NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
1120//%  if (wrapped && value) {
1121//%    *value = UNWRAP##VALUE_NAME(wrapped);
1122//%  }
1123//%  return (wrapped != NULL);
1124//%}
1125//%PDDM-DEFINE WRAPPEDPOD(VALUE)
1126//%@(VALUE)
1127//%PDDM-DEFINE UNWRAPUInt32(VALUE)
1128//%[VALUE unsignedIntValue]
1129//%PDDM-DEFINE UNWRAPInt32(VALUE)
1130//%[VALUE intValue]
1131//%PDDM-DEFINE UNWRAPUInt64(VALUE)
1132//%[VALUE unsignedLongLongValue]
1133//%PDDM-DEFINE UNWRAPInt64(VALUE)
1134//%[VALUE longLongValue]
1135//%PDDM-DEFINE UNWRAPBool(VALUE)
1136//%[VALUE boolValue]
1137//%PDDM-DEFINE UNWRAPFloat(VALUE)
1138//%[VALUE floatValue]
1139//%PDDM-DEFINE UNWRAPDouble(VALUE)
1140//%[VALUE doubleValue]
1141//%PDDM-DEFINE UNWRAPEnum(VALUE)
1142//%[VALUE intValue]
1143//%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
1144//%[NSString stringWithFormat:@"%u", VALUE]
1145//%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
1146//%[NSString stringWithFormat:@"%d", VALUE]
1147//%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
1148//%[NSString stringWithFormat:@"%llu", VALUE]
1149//%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
1150//%[NSString stringWithFormat:@"%lld", VALUE]
1151//%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
1152//%(VALUE ? @"true" : @"false")
1153//%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
1154//%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
1155//%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
1156//%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
1157//%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
1158//%@(VALUE)
1159//%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
1160//%NSNumber *
1161//%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
1162//%VAL1 != VAL2
1163//%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
1164// Empty
1165//%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
1166// Empty
1167//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
1168//%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
1169//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
1170// Empty
1171//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
1172// Empty
1173//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
1174// Empty
1175//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
1176// Empty
1177//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
1178// Empty
1179//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
1180// Empty
1181//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
1182// Empty
1183//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
1184//%- (NSData *)serializedDataForUnknownValue:(int32_t)value
1185//%                                   forKey:(GPBGenericValue *)key
1186//%                              keyDataType:(GPBDataType)keyDataType {
1187//%  size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
1188//%  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
1189//%  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
1190//%  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
1191//%  WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
1192//%  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
1193//%  [outputStream release];
1194//%  return data;
1195//%}
1196//%
1197//%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
1198//%value##VALUE_NAME
1199//%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
1200// Empty
1201//%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
1202// Empty
1203
1204//%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
1205//%  BOOL _valueSet[2];
1206//%
1207//%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
1208//%- (instancetype)initWithValues:(const VALUE_TYPE [])values
1209//%                       forKeys:(const BOOL [])keys
1210//%                         count:(NSUInteger)count {
1211//%  self = [super init];
1212//%  if (self) {
1213//%    for (NSUInteger i = 0; i < count; ++i) {
1214//%      int idx = keys[i] ? 1 : 0;
1215//%      _values[idx] = values[i];
1216//%      _valueSet[idx] = YES;
1217//%    }
1218//%  }
1219//%  return self;
1220//%}
1221//%
1222//%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
1223//%  self = [self initWithValues:NULL forKeys:NULL count:0];
1224//%  if (self) {
1225//%    if (dictionary) {
1226//%      for (int i = 0; i < 2; ++i) {
1227//%        if (dictionary->_valueSet[i]) {
1228//%          _values[i] = dictionary->_values[i];
1229//%          _valueSet[i] = YES;
1230//%        }
1231//%      }
1232//%    }
1233//%  }
1234//%  return self;
1235//%}
1236//%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
1237//%#if !defined(NS_BLOCK_ASSERTIONS)
1238//%- (void)dealloc {
1239//%  NSAssert(!_autocreator,
1240//%           @"%@: Autocreator must be cleared before release, autocreator: %@",
1241//%           [self class], _autocreator);
1242//%  [super dealloc];
1243//%}
1244//%#endif  // !defined(NS_BLOCK_ASSERTIONS)
1245//%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
1246//%BOOL_DICT_HASPOD(IDX, REF)
1247//%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
1248//%REF##_valueSet[IDX]
1249//%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
1250//%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
1251//%  int idx = (key ? 1 : 0);
1252//%  if (_valueSet[idx]) {
1253//%    if (value) {
1254//%      *value = _values[idx];
1255//%    }
1256//%    return YES;
1257//%  }
1258//%  return NO;
1259//%}
1260//%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
1261//%- (void)setGPBGenericValue:(GPBGenericValue *)value
1262//%     forGPBGenericValueKey:(GPBGenericValue *)key {
1263//%  int idx = (key->valueBool ? 1 : 0);
1264//%  _values[idx] = value->value##VALUE_NAME;
1265//%  _valueSet[idx] = YES;
1266//%}
1267//%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
1268//%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
1269//%  if (otherDictionary) {
1270//%    for (int i = 0; i < 2; ++i) {
1271//%      if (otherDictionary->_valueSet[i]) {
1272//%        _valueSet[i] = YES;
1273//%        _values[i] = otherDictionary->_values[i];
1274//%      }
1275//%    }
1276//%    if (_autocreator) {
1277//%      GPBAutocreatedDictionaryModified(_autocreator, self);
1278//%    }
1279//%  }
1280//%}
1281//%
1282//%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
1283//%  int idx = (key ? 1 : 0);
1284//%  _values[idx] = value;
1285//%  _valueSet[idx] = YES;
1286//%  if (_autocreator) {
1287//%    GPBAutocreatedDictionaryModified(_autocreator, self);
1288//%  }
1289//%}
1290//%
1291//%- (void)removeValueForKey:(BOOL)aKey {
1292//%  _valueSet[aKey ? 1 : 0] = NO;
1293//%}
1294//%
1295//%- (void)removeAll {
1296//%  _valueSet[0] = NO;
1297//%  _valueSet[1] = NO;
1298//%}
1299//%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
1300//%STR_FORMAT_##VALUE_NAME()
1301//%PDDM-DEFINE STR_FORMAT_UInt32()
1302//%%u
1303//%PDDM-DEFINE STR_FORMAT_Int32()
1304//%%d
1305//%PDDM-DEFINE STR_FORMAT_UInt64()
1306//%%llu
1307//%PDDM-DEFINE STR_FORMAT_Int64()
1308//%%lld
1309//%PDDM-DEFINE STR_FORMAT_Bool()
1310//%%d
1311//%PDDM-DEFINE STR_FORMAT_Float()
1312//%%f
1313//%PDDM-DEFINE STR_FORMAT_Double()
1314//%%lf
1315
1316//
1317// Helpers for Objects
1318//
1319
1320//%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
1321//%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
1322//%  VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
1323//%  return result;
1324//%}
1325//%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
1326//%VALUE
1327//%PDDM-DEFINE UNWRAPString(VALUE)
1328//%VALUE
1329//%PDDM-DEFINE UNWRAPObject(VALUE)
1330//%VALUE
1331//%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
1332//%VALUE
1333//%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
1334//%VALUE
1335//%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
1336//%ENUM_TYPEOBJECT_##TYPE()
1337//%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
1338//%NSString *
1339//%PDDM-DEFINE ENUM_TYPEOBJECT_id()
1340//%id ##
1341//%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
1342//%![VAL1 isEqual:VAL2]
1343//%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
1344//%- (BOOL)isInitialized {
1345//%  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
1346//%    if (!msg.initialized) {
1347//%      return NO;
1348//%    }
1349//%  }
1350//%  return YES;
1351//%}
1352//%
1353//%- (instancetype)deepCopyWithZone:(NSZone *)zone {
1354//%  GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
1355//%      [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
1356//%  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
1357//%                                                   GPBMessage *msg,
1358//%                                                   BOOL *stop) {
1359//%    #pragma unused(stop)
1360//%    GPBMessage *copiedMsg = [msg copyWithZone:zone];
1361//%    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
1362//%    [copiedMsg release];
1363//%  }];
1364//%  return newDict;
1365//%}
1366//%
1367//%
1368//%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
1369//%- (BOOL)isInitialized {
1370//%  if (_values[0] && ![_values[0] isInitialized]) {
1371//%    return NO;
1372//%  }
1373//%  if (_values[1] && ![_values[1] isInitialized]) {
1374//%    return NO;
1375//%  }
1376//%  return YES;
1377//%}
1378//%
1379//%- (instancetype)deepCopyWithZone:(NSZone *)zone {
1380//%  GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
1381//%      [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
1382//%  for (int i = 0; i < 2; ++i) {
1383//%    if (_values[i] != nil) {
1384//%      newDict->_values[i] = [_values[i] copyWithZone:zone];
1385//%    }
1386//%  }
1387//%  return newDict;
1388//%}
1389//%
1390//%
1391//%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
1392// Empty
1393//%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
1394//%valueString
1395//%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
1396//%##EXTRA_INDENT$S##  if (!##VALUE_NAME) {
1397//%##EXTRA_INDENT$S##    [NSException raise:NSInvalidArgumentException
1398//%##EXTRA_INDENT$S##                format:@"Attempting to add nil object to a Dictionary"];
1399//%##EXTRA_INDENT$S##  }
1400//%
1401//%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
1402//%##EXTRA_INDENT$S##  if (!##KEY_NAME) {
1403//%##EXTRA_INDENT$S##    [NSException raise:NSInvalidArgumentException
1404//%##EXTRA_INDENT$S##                format:@"Attempting to add nil key to a Dictionary"];
1405//%##EXTRA_INDENT$S##  }
1406//%
1407
1408//%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
1409// Empty
1410//%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
1411//%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
1412//%                        forKeys:(const BOOL [])keys
1413//%                          count:(NSUInteger)count {
1414//%  self = [super init];
1415//%  if (self) {
1416//%    for (NSUInteger i = 0; i < count; ++i) {
1417//%      if (!objects[i]) {
1418//%        [NSException raise:NSInvalidArgumentException
1419//%                    format:@"Attempting to add nil object to a Dictionary"];
1420//%      }
1421//%      int idx = keys[i] ? 1 : 0;
1422//%      [_values[idx] release];
1423//%      _values[idx] = (VALUE_TYPE)[objects[i] retain];
1424//%    }
1425//%  }
1426//%  return self;
1427//%}
1428//%
1429//%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
1430//%  self = [self initWithObjects:NULL forKeys:NULL count:0];
1431//%  if (self) {
1432//%    if (dictionary) {
1433//%      _values[0] = [dictionary->_values[0] retain];
1434//%      _values[1] = [dictionary->_values[1] retain];
1435//%    }
1436//%  }
1437//%  return self;
1438//%}
1439//%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
1440//%- (void)dealloc {
1441//%  NSAssert(!_autocreator,
1442//%           @"%@: Autocreator must be cleared before release, autocreator: %@",
1443//%           [self class], _autocreator);
1444//%  [_values[0] release];
1445//%  [_values[1] release];
1446//%  [super dealloc];
1447//%}
1448//%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
1449//%(BOOL_DICT_HASOBJECT(IDX, REF))
1450//%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
1451//%REF##_values[IDX] != nil
1452//%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
1453//%- (VALUE_TYPE)objectForKey:(BOOL)key {
1454//%  return _values[key ? 1 : 0];
1455//%}
1456//%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
1457//%- (void)setGPBGenericValue:(GPBGenericValue *)value
1458//%     forGPBGenericValueKey:(GPBGenericValue *)key {
1459//%  int idx = (key->valueBool ? 1 : 0);
1460//%  [_values[idx] release];
1461//%  _values[idx] = [value->valueString retain];
1462//%}
1463
1464//%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
1465//%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
1466//%  if (otherDictionary) {
1467//%    for (int i = 0; i < 2; ++i) {
1468//%      if (otherDictionary->_values[i] != nil) {
1469//%        [_values[i] release];
1470//%        _values[i] = [otherDictionary->_values[i] retain];
1471//%      }
1472//%    }
1473//%    if (_autocreator) {
1474//%      GPBAutocreatedDictionaryModified(_autocreator, self);
1475//%    }
1476//%  }
1477//%}
1478//%
1479//%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
1480//%  if (!object) {
1481//%    [NSException raise:NSInvalidArgumentException
1482//%                format:@"Attempting to add nil object to a Dictionary"];
1483//%  }
1484//%  int idx = (key ? 1 : 0);
1485//%  [_values[idx] release];
1486//%  _values[idx] = [object retain];
1487//%  if (_autocreator) {
1488//%    GPBAutocreatedDictionaryModified(_autocreator, self);
1489//%  }
1490//%}
1491//%
1492//%- (void)removeObjectForKey:(BOOL)aKey {
1493//%  int idx = (aKey ? 1 : 0);
1494//%  [_values[idx] release];
1495//%  _values[idx] = nil;
1496//%}
1497//%
1498//%- (void)removeAll {
1499//%  for (int i = 0; i < 2; ++i) {
1500//%    [_values[i] release];
1501//%    _values[i] = nil;
1502//%  }
1503//%}
1504//%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
1505//%%@
1506
1507
1508//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
1509// This block of code is generated, do not edit it directly.
1510
1511#pragma mark - UInt32 -> UInt32
1512
1513@implementation GPBUInt32UInt32Dictionary {
1514 @package
1515  NSMutableDictionary *_dictionary;
1516}
1517
1518+ (instancetype)dictionary {
1519  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
1520}
1521
1522+ (instancetype)dictionaryWithValue:(uint32_t)value
1523                             forKey:(uint32_t)key {
1524  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
1525  // on to get the type correct.
1526  return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
1527                                                           forKeys:&key
1528                                                             count:1] autorelease];
1529}
1530
1531+ (instancetype)dictionaryWithValues:(const uint32_t [])values
1532                             forKeys:(const uint32_t [])keys
1533                               count:(NSUInteger)count {
1534  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
1535  // on to get the type correct.
1536  return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
1537                                                           forKeys:keys
1538                                                             count:count] autorelease];
1539}
1540
1541+ (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
1542  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
1543  // on to get the type correct.
1544  return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
1545}
1546
1547+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1548  return [[[self alloc] initWithCapacity:numItems] autorelease];
1549}
1550
1551- (instancetype)init {
1552  return [self initWithValues:NULL forKeys:NULL count:0];
1553}
1554
1555- (instancetype)initWithValues:(const uint32_t [])values
1556                       forKeys:(const uint32_t [])keys
1557                         count:(NSUInteger)count {
1558  self = [super init];
1559  if (self) {
1560    _dictionary = [[NSMutableDictionary alloc] init];
1561    if (count && values && keys) {
1562      for (NSUInteger i = 0; i < count; ++i) {
1563        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
1564      }
1565    }
1566  }
1567  return self;
1568}
1569
1570- (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
1571  self = [self initWithValues:NULL forKeys:NULL count:0];
1572  if (self) {
1573    if (dictionary) {
1574      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
1575    }
1576  }
1577  return self;
1578}
1579
1580- (instancetype)initWithCapacity:(NSUInteger)numItems {
1581  #pragma unused(numItems)
1582  return [self initWithValues:NULL forKeys:NULL count:0];
1583}
1584
1585- (void)dealloc {
1586  NSAssert(!_autocreator,
1587           @"%@: Autocreator must be cleared before release, autocreator: %@",
1588           [self class], _autocreator);
1589  [_dictionary release];
1590  [super dealloc];
1591}
1592
1593- (instancetype)copyWithZone:(NSZone *)zone {
1594  return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
1595}
1596
1597- (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
1598  if (self == other) {
1599    return YES;
1600  }
1601  if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
1602    return NO;
1603  }
1604  return [_dictionary isEqual:other->_dictionary];
1605}
1606
1607- (NSUInteger)hash {
1608  return _dictionary.count;
1609}
1610
1611- (NSString *)description {
1612  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
1613}
1614
1615- (NSUInteger)count {
1616  return _dictionary.count;
1617}
1618
1619- (void)enumerateKeysAndValuesUsingBlock:
1620    (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
1621  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1622                                                   NSNumber *aValue,
1623                                                   BOOL *stop) {
1624      block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
1625  }];
1626}
1627
1628- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
1629  NSUInteger count = _dictionary.count;
1630  if (count == 0) {
1631    return 0;
1632  }
1633
1634  GPBDataType valueDataType = GPBGetFieldDataType(field);
1635  GPBDataType keyDataType = field.mapKeyDataType;
1636  __block size_t result = 0;
1637  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1638                                                   NSNumber *aValue,
1639                                                   BOOL *stop) {
1640    #pragma unused(stop)
1641    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1642    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
1643    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
1644  }];
1645  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
1646  result += tagSize * count;
1647  return result;
1648}
1649
1650- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
1651                         asField:(GPBFieldDescriptor *)field {
1652  GPBDataType valueDataType = GPBGetFieldDataType(field);
1653  GPBDataType keyDataType = field.mapKeyDataType;
1654  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
1655  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1656                                                   NSNumber *aValue,
1657                                                   BOOL *stop) {
1658    #pragma unused(stop)
1659    // Write the tag.
1660    [outputStream writeInt32NoTag:tag];
1661    // Write the size of the message.
1662    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1663    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
1664    [outputStream writeInt32NoTag:(int32_t)msgSize];
1665    // Write the fields.
1666    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1667    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
1668  }];
1669}
1670
1671- (void)setGPBGenericValue:(GPBGenericValue *)value
1672     forGPBGenericValueKey:(GPBGenericValue *)key {
1673  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
1674}
1675
1676- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
1677  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
1678      #pragma unused(stop)
1679      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
1680  }];
1681}
1682
1683- (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
1684  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
1685  if (wrapped && value) {
1686    *value = [wrapped unsignedIntValue];
1687  }
1688  return (wrapped != NULL);
1689}
1690
1691- (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
1692  if (otherDictionary) {
1693    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
1694    if (_autocreator) {
1695      GPBAutocreatedDictionaryModified(_autocreator, self);
1696    }
1697  }
1698}
1699
1700- (void)setValue:(uint32_t)value forKey:(uint32_t)key {
1701  [_dictionary setObject:@(value) forKey:@(key)];
1702  if (_autocreator) {
1703    GPBAutocreatedDictionaryModified(_autocreator, self);
1704  }
1705}
1706
1707- (void)removeValueForKey:(uint32_t)aKey {
1708  [_dictionary removeObjectForKey:@(aKey)];
1709}
1710
1711- (void)removeAll {
1712  [_dictionary removeAllObjects];
1713}
1714
1715@end
1716
1717#pragma mark - UInt32 -> Int32
1718
1719@implementation GPBUInt32Int32Dictionary {
1720 @package
1721  NSMutableDictionary *_dictionary;
1722}
1723
1724+ (instancetype)dictionary {
1725  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
1726}
1727
1728+ (instancetype)dictionaryWithValue:(int32_t)value
1729                             forKey:(uint32_t)key {
1730  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
1731  // on to get the type correct.
1732  return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
1733                                                          forKeys:&key
1734                                                            count:1] autorelease];
1735}
1736
1737+ (instancetype)dictionaryWithValues:(const int32_t [])values
1738                             forKeys:(const uint32_t [])keys
1739                               count:(NSUInteger)count {
1740  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
1741  // on to get the type correct.
1742  return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
1743                                                          forKeys:keys
1744                                                            count:count] autorelease];
1745}
1746
1747+ (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
1748  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
1749  // on to get the type correct.
1750  return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
1751}
1752
1753+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1754  return [[[self alloc] initWithCapacity:numItems] autorelease];
1755}
1756
1757- (instancetype)init {
1758  return [self initWithValues:NULL forKeys:NULL count:0];
1759}
1760
1761- (instancetype)initWithValues:(const int32_t [])values
1762                       forKeys:(const uint32_t [])keys
1763                         count:(NSUInteger)count {
1764  self = [super init];
1765  if (self) {
1766    _dictionary = [[NSMutableDictionary alloc] init];
1767    if (count && values && keys) {
1768      for (NSUInteger i = 0; i < count; ++i) {
1769        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
1770      }
1771    }
1772  }
1773  return self;
1774}
1775
1776- (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
1777  self = [self initWithValues:NULL forKeys:NULL count:0];
1778  if (self) {
1779    if (dictionary) {
1780      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
1781    }
1782  }
1783  return self;
1784}
1785
1786- (instancetype)initWithCapacity:(NSUInteger)numItems {
1787  #pragma unused(numItems)
1788  return [self initWithValues:NULL forKeys:NULL count:0];
1789}
1790
1791- (void)dealloc {
1792  NSAssert(!_autocreator,
1793           @"%@: Autocreator must be cleared before release, autocreator: %@",
1794           [self class], _autocreator);
1795  [_dictionary release];
1796  [super dealloc];
1797}
1798
1799- (instancetype)copyWithZone:(NSZone *)zone {
1800  return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
1801}
1802
1803- (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
1804  if (self == other) {
1805    return YES;
1806  }
1807  if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
1808    return NO;
1809  }
1810  return [_dictionary isEqual:other->_dictionary];
1811}
1812
1813- (NSUInteger)hash {
1814  return _dictionary.count;
1815}
1816
1817- (NSString *)description {
1818  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
1819}
1820
1821- (NSUInteger)count {
1822  return _dictionary.count;
1823}
1824
1825- (void)enumerateKeysAndValuesUsingBlock:
1826    (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
1827  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1828                                                   NSNumber *aValue,
1829                                                   BOOL *stop) {
1830      block([aKey unsignedIntValue], [aValue intValue], stop);
1831  }];
1832}
1833
1834- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
1835  NSUInteger count = _dictionary.count;
1836  if (count == 0) {
1837    return 0;
1838  }
1839
1840  GPBDataType valueDataType = GPBGetFieldDataType(field);
1841  GPBDataType keyDataType = field.mapKeyDataType;
1842  __block size_t result = 0;
1843  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1844                                                   NSNumber *aValue,
1845                                                   BOOL *stop) {
1846    #pragma unused(stop)
1847    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1848    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
1849    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
1850  }];
1851  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
1852  result += tagSize * count;
1853  return result;
1854}
1855
1856- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
1857                         asField:(GPBFieldDescriptor *)field {
1858  GPBDataType valueDataType = GPBGetFieldDataType(field);
1859  GPBDataType keyDataType = field.mapKeyDataType;
1860  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
1861  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
1862                                                   NSNumber *aValue,
1863                                                   BOOL *stop) {
1864    #pragma unused(stop)
1865    // Write the tag.
1866    [outputStream writeInt32NoTag:tag];
1867    // Write the size of the message.
1868    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1869    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
1870    [outputStream writeInt32NoTag:(int32_t)msgSize];
1871    // Write the fields.
1872    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
1873    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
1874  }];
1875}
1876
1877- (void)setGPBGenericValue:(GPBGenericValue *)value
1878     forGPBGenericValueKey:(GPBGenericValue *)key {
1879  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
1880}
1881
1882- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
1883  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
1884      #pragma unused(stop)
1885      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
1886  }];
1887}
1888
1889- (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
1890  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
1891  if (wrapped && value) {
1892    *value = [wrapped intValue];
1893  }
1894  return (wrapped != NULL);
1895}
1896
1897- (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
1898  if (otherDictionary) {
1899    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
1900    if (_autocreator) {
1901      GPBAutocreatedDictionaryModified(_autocreator, self);
1902    }
1903  }
1904}
1905
1906- (void)setValue:(int32_t)value forKey:(uint32_t)key {
1907  [_dictionary setObject:@(value) forKey:@(key)];
1908  if (_autocreator) {
1909    GPBAutocreatedDictionaryModified(_autocreator, self);
1910  }
1911}
1912
1913- (void)removeValueForKey:(uint32_t)aKey {
1914  [_dictionary removeObjectForKey:@(aKey)];
1915}
1916
1917- (void)removeAll {
1918  [_dictionary removeAllObjects];
1919}
1920
1921@end
1922
1923#pragma mark - UInt32 -> UInt64
1924
1925@implementation GPBUInt32UInt64Dictionary {
1926 @package
1927  NSMutableDictionary *_dictionary;
1928}
1929
1930+ (instancetype)dictionary {
1931  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
1932}
1933
1934+ (instancetype)dictionaryWithValue:(uint64_t)value
1935                             forKey:(uint32_t)key {
1936  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
1937  // on to get the type correct.
1938  return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
1939                                                           forKeys:&key
1940                                                             count:1] autorelease];
1941}
1942
1943+ (instancetype)dictionaryWithValues:(const uint64_t [])values
1944                             forKeys:(const uint32_t [])keys
1945                               count:(NSUInteger)count {
1946  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
1947  // on to get the type correct.
1948  return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
1949                                                           forKeys:keys
1950                                                             count:count] autorelease];
1951}
1952
1953+ (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
1954  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
1955  // on to get the type correct.
1956  return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
1957}
1958
1959+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
1960  return [[[self alloc] initWithCapacity:numItems] autorelease];
1961}
1962
1963- (instancetype)init {
1964  return [self initWithValues:NULL forKeys:NULL count:0];
1965}
1966
1967- (instancetype)initWithValues:(const uint64_t [])values
1968                       forKeys:(const uint32_t [])keys
1969                         count:(NSUInteger)count {
1970  self = [super init];
1971  if (self) {
1972    _dictionary = [[NSMutableDictionary alloc] init];
1973    if (count && values && keys) {
1974      for (NSUInteger i = 0; i < count; ++i) {
1975        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
1976      }
1977    }
1978  }
1979  return self;
1980}
1981
1982- (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
1983  self = [self initWithValues:NULL forKeys:NULL count:0];
1984  if (self) {
1985    if (dictionary) {
1986      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
1987    }
1988  }
1989  return self;
1990}
1991
1992- (instancetype)initWithCapacity:(NSUInteger)numItems {
1993  #pragma unused(numItems)
1994  return [self initWithValues:NULL forKeys:NULL count:0];
1995}
1996
1997- (void)dealloc {
1998  NSAssert(!_autocreator,
1999           @"%@: Autocreator must be cleared before release, autocreator: %@",
2000           [self class], _autocreator);
2001  [_dictionary release];
2002  [super dealloc];
2003}
2004
2005- (instancetype)copyWithZone:(NSZone *)zone {
2006  return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
2007}
2008
2009- (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
2010  if (self == other) {
2011    return YES;
2012  }
2013  if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
2014    return NO;
2015  }
2016  return [_dictionary isEqual:other->_dictionary];
2017}
2018
2019- (NSUInteger)hash {
2020  return _dictionary.count;
2021}
2022
2023- (NSString *)description {
2024  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2025}
2026
2027- (NSUInteger)count {
2028  return _dictionary.count;
2029}
2030
2031- (void)enumerateKeysAndValuesUsingBlock:
2032    (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
2033  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2034                                                   NSNumber *aValue,
2035                                                   BOOL *stop) {
2036      block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
2037  }];
2038}
2039
2040- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2041  NSUInteger count = _dictionary.count;
2042  if (count == 0) {
2043    return 0;
2044  }
2045
2046  GPBDataType valueDataType = GPBGetFieldDataType(field);
2047  GPBDataType keyDataType = field.mapKeyDataType;
2048  __block size_t result = 0;
2049  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2050                                                   NSNumber *aValue,
2051                                                   BOOL *stop) {
2052    #pragma unused(stop)
2053    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2054    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
2055    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2056  }];
2057  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2058  result += tagSize * count;
2059  return result;
2060}
2061
2062- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2063                         asField:(GPBFieldDescriptor *)field {
2064  GPBDataType valueDataType = GPBGetFieldDataType(field);
2065  GPBDataType keyDataType = field.mapKeyDataType;
2066  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2067  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2068                                                   NSNumber *aValue,
2069                                                   BOOL *stop) {
2070    #pragma unused(stop)
2071    // Write the tag.
2072    [outputStream writeInt32NoTag:tag];
2073    // Write the size of the message.
2074    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2075    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
2076    [outputStream writeInt32NoTag:(int32_t)msgSize];
2077    // Write the fields.
2078    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2079    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
2080  }];
2081}
2082
2083- (void)setGPBGenericValue:(GPBGenericValue *)value
2084     forGPBGenericValueKey:(GPBGenericValue *)key {
2085  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
2086}
2087
2088- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2089  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
2090      #pragma unused(stop)
2091      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
2092  }];
2093}
2094
2095- (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
2096  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2097  if (wrapped && value) {
2098    *value = [wrapped unsignedLongLongValue];
2099  }
2100  return (wrapped != NULL);
2101}
2102
2103- (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
2104  if (otherDictionary) {
2105    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2106    if (_autocreator) {
2107      GPBAutocreatedDictionaryModified(_autocreator, self);
2108    }
2109  }
2110}
2111
2112- (void)setValue:(uint64_t)value forKey:(uint32_t)key {
2113  [_dictionary setObject:@(value) forKey:@(key)];
2114  if (_autocreator) {
2115    GPBAutocreatedDictionaryModified(_autocreator, self);
2116  }
2117}
2118
2119- (void)removeValueForKey:(uint32_t)aKey {
2120  [_dictionary removeObjectForKey:@(aKey)];
2121}
2122
2123- (void)removeAll {
2124  [_dictionary removeAllObjects];
2125}
2126
2127@end
2128
2129#pragma mark - UInt32 -> Int64
2130
2131@implementation GPBUInt32Int64Dictionary {
2132 @package
2133  NSMutableDictionary *_dictionary;
2134}
2135
2136+ (instancetype)dictionary {
2137  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
2138}
2139
2140+ (instancetype)dictionaryWithValue:(int64_t)value
2141                             forKey:(uint32_t)key {
2142  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2143  // on to get the type correct.
2144  return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
2145                                                          forKeys:&key
2146                                                            count:1] autorelease];
2147}
2148
2149+ (instancetype)dictionaryWithValues:(const int64_t [])values
2150                             forKeys:(const uint32_t [])keys
2151                               count:(NSUInteger)count {
2152  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2153  // on to get the type correct.
2154  return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
2155                                                          forKeys:keys
2156                                                            count:count] autorelease];
2157}
2158
2159+ (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
2160  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2161  // on to get the type correct.
2162  return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2163}
2164
2165+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2166  return [[[self alloc] initWithCapacity:numItems] autorelease];
2167}
2168
2169- (instancetype)init {
2170  return [self initWithValues:NULL forKeys:NULL count:0];
2171}
2172
2173- (instancetype)initWithValues:(const int64_t [])values
2174                       forKeys:(const uint32_t [])keys
2175                         count:(NSUInteger)count {
2176  self = [super init];
2177  if (self) {
2178    _dictionary = [[NSMutableDictionary alloc] init];
2179    if (count && values && keys) {
2180      for (NSUInteger i = 0; i < count; ++i) {
2181        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2182      }
2183    }
2184  }
2185  return self;
2186}
2187
2188- (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
2189  self = [self initWithValues:NULL forKeys:NULL count:0];
2190  if (self) {
2191    if (dictionary) {
2192      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2193    }
2194  }
2195  return self;
2196}
2197
2198- (instancetype)initWithCapacity:(NSUInteger)numItems {
2199  #pragma unused(numItems)
2200  return [self initWithValues:NULL forKeys:NULL count:0];
2201}
2202
2203- (void)dealloc {
2204  NSAssert(!_autocreator,
2205           @"%@: Autocreator must be cleared before release, autocreator: %@",
2206           [self class], _autocreator);
2207  [_dictionary release];
2208  [super dealloc];
2209}
2210
2211- (instancetype)copyWithZone:(NSZone *)zone {
2212  return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
2213}
2214
2215- (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
2216  if (self == other) {
2217    return YES;
2218  }
2219  if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
2220    return NO;
2221  }
2222  return [_dictionary isEqual:other->_dictionary];
2223}
2224
2225- (NSUInteger)hash {
2226  return _dictionary.count;
2227}
2228
2229- (NSString *)description {
2230  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2231}
2232
2233- (NSUInteger)count {
2234  return _dictionary.count;
2235}
2236
2237- (void)enumerateKeysAndValuesUsingBlock:
2238    (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
2239  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2240                                                   NSNumber *aValue,
2241                                                   BOOL *stop) {
2242      block([aKey unsignedIntValue], [aValue longLongValue], stop);
2243  }];
2244}
2245
2246- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2247  NSUInteger count = _dictionary.count;
2248  if (count == 0) {
2249    return 0;
2250  }
2251
2252  GPBDataType valueDataType = GPBGetFieldDataType(field);
2253  GPBDataType keyDataType = field.mapKeyDataType;
2254  __block size_t result = 0;
2255  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2256                                                   NSNumber *aValue,
2257                                                   BOOL *stop) {
2258    #pragma unused(stop)
2259    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2260    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
2261    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2262  }];
2263  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2264  result += tagSize * count;
2265  return result;
2266}
2267
2268- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2269                         asField:(GPBFieldDescriptor *)field {
2270  GPBDataType valueDataType = GPBGetFieldDataType(field);
2271  GPBDataType keyDataType = field.mapKeyDataType;
2272  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2273  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2274                                                   NSNumber *aValue,
2275                                                   BOOL *stop) {
2276    #pragma unused(stop)
2277    // Write the tag.
2278    [outputStream writeInt32NoTag:tag];
2279    // Write the size of the message.
2280    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2281    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
2282    [outputStream writeInt32NoTag:(int32_t)msgSize];
2283    // Write the fields.
2284    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2285    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
2286  }];
2287}
2288
2289- (void)setGPBGenericValue:(GPBGenericValue *)value
2290     forGPBGenericValueKey:(GPBGenericValue *)key {
2291  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
2292}
2293
2294- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2295  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
2296      #pragma unused(stop)
2297      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
2298  }];
2299}
2300
2301- (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
2302  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2303  if (wrapped && value) {
2304    *value = [wrapped longLongValue];
2305  }
2306  return (wrapped != NULL);
2307}
2308
2309- (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
2310  if (otherDictionary) {
2311    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2312    if (_autocreator) {
2313      GPBAutocreatedDictionaryModified(_autocreator, self);
2314    }
2315  }
2316}
2317
2318- (void)setValue:(int64_t)value forKey:(uint32_t)key {
2319  [_dictionary setObject:@(value) forKey:@(key)];
2320  if (_autocreator) {
2321    GPBAutocreatedDictionaryModified(_autocreator, self);
2322  }
2323}
2324
2325- (void)removeValueForKey:(uint32_t)aKey {
2326  [_dictionary removeObjectForKey:@(aKey)];
2327}
2328
2329- (void)removeAll {
2330  [_dictionary removeAllObjects];
2331}
2332
2333@end
2334
2335#pragma mark - UInt32 -> Bool
2336
2337@implementation GPBUInt32BoolDictionary {
2338 @package
2339  NSMutableDictionary *_dictionary;
2340}
2341
2342+ (instancetype)dictionary {
2343  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
2344}
2345
2346+ (instancetype)dictionaryWithValue:(BOOL)value
2347                             forKey:(uint32_t)key {
2348  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2349  // on to get the type correct.
2350  return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
2351                                                         forKeys:&key
2352                                                           count:1] autorelease];
2353}
2354
2355+ (instancetype)dictionaryWithValues:(const BOOL [])values
2356                             forKeys:(const uint32_t [])keys
2357                               count:(NSUInteger)count {
2358  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2359  // on to get the type correct.
2360  return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
2361                                                         forKeys:keys
2362                                                           count:count] autorelease];
2363}
2364
2365+ (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
2366  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2367  // on to get the type correct.
2368  return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2369}
2370
2371+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2372  return [[[self alloc] initWithCapacity:numItems] autorelease];
2373}
2374
2375- (instancetype)init {
2376  return [self initWithValues:NULL forKeys:NULL count:0];
2377}
2378
2379- (instancetype)initWithValues:(const BOOL [])values
2380                       forKeys:(const uint32_t [])keys
2381                         count:(NSUInteger)count {
2382  self = [super init];
2383  if (self) {
2384    _dictionary = [[NSMutableDictionary alloc] init];
2385    if (count && values && keys) {
2386      for (NSUInteger i = 0; i < count; ++i) {
2387        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2388      }
2389    }
2390  }
2391  return self;
2392}
2393
2394- (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
2395  self = [self initWithValues:NULL forKeys:NULL count:0];
2396  if (self) {
2397    if (dictionary) {
2398      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2399    }
2400  }
2401  return self;
2402}
2403
2404- (instancetype)initWithCapacity:(NSUInteger)numItems {
2405  #pragma unused(numItems)
2406  return [self initWithValues:NULL forKeys:NULL count:0];
2407}
2408
2409- (void)dealloc {
2410  NSAssert(!_autocreator,
2411           @"%@: Autocreator must be cleared before release, autocreator: %@",
2412           [self class], _autocreator);
2413  [_dictionary release];
2414  [super dealloc];
2415}
2416
2417- (instancetype)copyWithZone:(NSZone *)zone {
2418  return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
2419}
2420
2421- (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
2422  if (self == other) {
2423    return YES;
2424  }
2425  if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
2426    return NO;
2427  }
2428  return [_dictionary isEqual:other->_dictionary];
2429}
2430
2431- (NSUInteger)hash {
2432  return _dictionary.count;
2433}
2434
2435- (NSString *)description {
2436  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2437}
2438
2439- (NSUInteger)count {
2440  return _dictionary.count;
2441}
2442
2443- (void)enumerateKeysAndValuesUsingBlock:
2444    (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
2445  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2446                                                   NSNumber *aValue,
2447                                                   BOOL *stop) {
2448      block([aKey unsignedIntValue], [aValue boolValue], stop);
2449  }];
2450}
2451
2452- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2453  NSUInteger count = _dictionary.count;
2454  if (count == 0) {
2455    return 0;
2456  }
2457
2458  GPBDataType valueDataType = GPBGetFieldDataType(field);
2459  GPBDataType keyDataType = field.mapKeyDataType;
2460  __block size_t result = 0;
2461  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2462                                                   NSNumber *aValue,
2463                                                   BOOL *stop) {
2464    #pragma unused(stop)
2465    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2466    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
2467    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2468  }];
2469  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2470  result += tagSize * count;
2471  return result;
2472}
2473
2474- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2475                         asField:(GPBFieldDescriptor *)field {
2476  GPBDataType valueDataType = GPBGetFieldDataType(field);
2477  GPBDataType keyDataType = field.mapKeyDataType;
2478  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2479  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2480                                                   NSNumber *aValue,
2481                                                   BOOL *stop) {
2482    #pragma unused(stop)
2483    // Write the tag.
2484    [outputStream writeInt32NoTag:tag];
2485    // Write the size of the message.
2486    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2487    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
2488    [outputStream writeInt32NoTag:(int32_t)msgSize];
2489    // Write the fields.
2490    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2491    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
2492  }];
2493}
2494
2495- (void)setGPBGenericValue:(GPBGenericValue *)value
2496     forGPBGenericValueKey:(GPBGenericValue *)key {
2497  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
2498}
2499
2500- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2501  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
2502      #pragma unused(stop)
2503      block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
2504  }];
2505}
2506
2507- (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
2508  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2509  if (wrapped && value) {
2510    *value = [wrapped boolValue];
2511  }
2512  return (wrapped != NULL);
2513}
2514
2515- (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
2516  if (otherDictionary) {
2517    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2518    if (_autocreator) {
2519      GPBAutocreatedDictionaryModified(_autocreator, self);
2520    }
2521  }
2522}
2523
2524- (void)setValue:(BOOL)value forKey:(uint32_t)key {
2525  [_dictionary setObject:@(value) forKey:@(key)];
2526  if (_autocreator) {
2527    GPBAutocreatedDictionaryModified(_autocreator, self);
2528  }
2529}
2530
2531- (void)removeValueForKey:(uint32_t)aKey {
2532  [_dictionary removeObjectForKey:@(aKey)];
2533}
2534
2535- (void)removeAll {
2536  [_dictionary removeAllObjects];
2537}
2538
2539@end
2540
2541#pragma mark - UInt32 -> Float
2542
2543@implementation GPBUInt32FloatDictionary {
2544 @package
2545  NSMutableDictionary *_dictionary;
2546}
2547
2548+ (instancetype)dictionary {
2549  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
2550}
2551
2552+ (instancetype)dictionaryWithValue:(float)value
2553                             forKey:(uint32_t)key {
2554  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2555  // on to get the type correct.
2556  return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
2557                                                          forKeys:&key
2558                                                            count:1] autorelease];
2559}
2560
2561+ (instancetype)dictionaryWithValues:(const float [])values
2562                             forKeys:(const uint32_t [])keys
2563                               count:(NSUInteger)count {
2564  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2565  // on to get the type correct.
2566  return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
2567                                                          forKeys:keys
2568                                                            count:count] autorelease];
2569}
2570
2571+ (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
2572  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2573  // on to get the type correct.
2574  return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2575}
2576
2577+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2578  return [[[self alloc] initWithCapacity:numItems] autorelease];
2579}
2580
2581- (instancetype)init {
2582  return [self initWithValues:NULL forKeys:NULL count:0];
2583}
2584
2585- (instancetype)initWithValues:(const float [])values
2586                       forKeys:(const uint32_t [])keys
2587                         count:(NSUInteger)count {
2588  self = [super init];
2589  if (self) {
2590    _dictionary = [[NSMutableDictionary alloc] init];
2591    if (count && values && keys) {
2592      for (NSUInteger i = 0; i < count; ++i) {
2593        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2594      }
2595    }
2596  }
2597  return self;
2598}
2599
2600- (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
2601  self = [self initWithValues:NULL forKeys:NULL count:0];
2602  if (self) {
2603    if (dictionary) {
2604      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2605    }
2606  }
2607  return self;
2608}
2609
2610- (instancetype)initWithCapacity:(NSUInteger)numItems {
2611  #pragma unused(numItems)
2612  return [self initWithValues:NULL forKeys:NULL count:0];
2613}
2614
2615- (void)dealloc {
2616  NSAssert(!_autocreator,
2617           @"%@: Autocreator must be cleared before release, autocreator: %@",
2618           [self class], _autocreator);
2619  [_dictionary release];
2620  [super dealloc];
2621}
2622
2623- (instancetype)copyWithZone:(NSZone *)zone {
2624  return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
2625}
2626
2627- (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
2628  if (self == other) {
2629    return YES;
2630  }
2631  if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
2632    return NO;
2633  }
2634  return [_dictionary isEqual:other->_dictionary];
2635}
2636
2637- (NSUInteger)hash {
2638  return _dictionary.count;
2639}
2640
2641- (NSString *)description {
2642  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2643}
2644
2645- (NSUInteger)count {
2646  return _dictionary.count;
2647}
2648
2649- (void)enumerateKeysAndValuesUsingBlock:
2650    (void (^)(uint32_t key, float value, BOOL *stop))block {
2651  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2652                                                   NSNumber *aValue,
2653                                                   BOOL *stop) {
2654      block([aKey unsignedIntValue], [aValue floatValue], stop);
2655  }];
2656}
2657
2658- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2659  NSUInteger count = _dictionary.count;
2660  if (count == 0) {
2661    return 0;
2662  }
2663
2664  GPBDataType valueDataType = GPBGetFieldDataType(field);
2665  GPBDataType keyDataType = field.mapKeyDataType;
2666  __block size_t result = 0;
2667  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2668                                                   NSNumber *aValue,
2669                                                   BOOL *stop) {
2670    #pragma unused(stop)
2671    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2672    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
2673    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2674  }];
2675  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2676  result += tagSize * count;
2677  return result;
2678}
2679
2680- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2681                         asField:(GPBFieldDescriptor *)field {
2682  GPBDataType valueDataType = GPBGetFieldDataType(field);
2683  GPBDataType keyDataType = field.mapKeyDataType;
2684  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2685  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2686                                                   NSNumber *aValue,
2687                                                   BOOL *stop) {
2688    #pragma unused(stop)
2689    // Write the tag.
2690    [outputStream writeInt32NoTag:tag];
2691    // Write the size of the message.
2692    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2693    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
2694    [outputStream writeInt32NoTag:(int32_t)msgSize];
2695    // Write the fields.
2696    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2697    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
2698  }];
2699}
2700
2701- (void)setGPBGenericValue:(GPBGenericValue *)value
2702     forGPBGenericValueKey:(GPBGenericValue *)key {
2703  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
2704}
2705
2706- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2707  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
2708      #pragma unused(stop)
2709      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
2710  }];
2711}
2712
2713- (BOOL)valueForKey:(uint32_t)key value:(float *)value {
2714  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2715  if (wrapped && value) {
2716    *value = [wrapped floatValue];
2717  }
2718  return (wrapped != NULL);
2719}
2720
2721- (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
2722  if (otherDictionary) {
2723    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2724    if (_autocreator) {
2725      GPBAutocreatedDictionaryModified(_autocreator, self);
2726    }
2727  }
2728}
2729
2730- (void)setValue:(float)value forKey:(uint32_t)key {
2731  [_dictionary setObject:@(value) forKey:@(key)];
2732  if (_autocreator) {
2733    GPBAutocreatedDictionaryModified(_autocreator, self);
2734  }
2735}
2736
2737- (void)removeValueForKey:(uint32_t)aKey {
2738  [_dictionary removeObjectForKey:@(aKey)];
2739}
2740
2741- (void)removeAll {
2742  [_dictionary removeAllObjects];
2743}
2744
2745@end
2746
2747#pragma mark - UInt32 -> Double
2748
2749@implementation GPBUInt32DoubleDictionary {
2750 @package
2751  NSMutableDictionary *_dictionary;
2752}
2753
2754+ (instancetype)dictionary {
2755  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
2756}
2757
2758+ (instancetype)dictionaryWithValue:(double)value
2759                             forKey:(uint32_t)key {
2760  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2761  // on to get the type correct.
2762  return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
2763                                                           forKeys:&key
2764                                                             count:1] autorelease];
2765}
2766
2767+ (instancetype)dictionaryWithValues:(const double [])values
2768                             forKeys:(const uint32_t [])keys
2769                               count:(NSUInteger)count {
2770  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2771  // on to get the type correct.
2772  return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
2773                                                           forKeys:keys
2774                                                             count:count] autorelease];
2775}
2776
2777+ (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
2778  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
2779  // on to get the type correct.
2780  return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
2781}
2782
2783+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
2784  return [[[self alloc] initWithCapacity:numItems] autorelease];
2785}
2786
2787- (instancetype)init {
2788  return [self initWithValues:NULL forKeys:NULL count:0];
2789}
2790
2791- (instancetype)initWithValues:(const double [])values
2792                       forKeys:(const uint32_t [])keys
2793                         count:(NSUInteger)count {
2794  self = [super init];
2795  if (self) {
2796    _dictionary = [[NSMutableDictionary alloc] init];
2797    if (count && values && keys) {
2798      for (NSUInteger i = 0; i < count; ++i) {
2799        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
2800      }
2801    }
2802  }
2803  return self;
2804}
2805
2806- (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
2807  self = [self initWithValues:NULL forKeys:NULL count:0];
2808  if (self) {
2809    if (dictionary) {
2810      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
2811    }
2812  }
2813  return self;
2814}
2815
2816- (instancetype)initWithCapacity:(NSUInteger)numItems {
2817  #pragma unused(numItems)
2818  return [self initWithValues:NULL forKeys:NULL count:0];
2819}
2820
2821- (void)dealloc {
2822  NSAssert(!_autocreator,
2823           @"%@: Autocreator must be cleared before release, autocreator: %@",
2824           [self class], _autocreator);
2825  [_dictionary release];
2826  [super dealloc];
2827}
2828
2829- (instancetype)copyWithZone:(NSZone *)zone {
2830  return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
2831}
2832
2833- (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
2834  if (self == other) {
2835    return YES;
2836  }
2837  if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
2838    return NO;
2839  }
2840  return [_dictionary isEqual:other->_dictionary];
2841}
2842
2843- (NSUInteger)hash {
2844  return _dictionary.count;
2845}
2846
2847- (NSString *)description {
2848  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
2849}
2850
2851- (NSUInteger)count {
2852  return _dictionary.count;
2853}
2854
2855- (void)enumerateKeysAndValuesUsingBlock:
2856    (void (^)(uint32_t key, double value, BOOL *stop))block {
2857  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2858                                                   NSNumber *aValue,
2859                                                   BOOL *stop) {
2860      block([aKey unsignedIntValue], [aValue doubleValue], stop);
2861  }];
2862}
2863
2864- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
2865  NSUInteger count = _dictionary.count;
2866  if (count == 0) {
2867    return 0;
2868  }
2869
2870  GPBDataType valueDataType = GPBGetFieldDataType(field);
2871  GPBDataType keyDataType = field.mapKeyDataType;
2872  __block size_t result = 0;
2873  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2874                                                   NSNumber *aValue,
2875                                                   BOOL *stop) {
2876    #pragma unused(stop)
2877    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2878    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
2879    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
2880  }];
2881  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
2882  result += tagSize * count;
2883  return result;
2884}
2885
2886- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
2887                         asField:(GPBFieldDescriptor *)field {
2888  GPBDataType valueDataType = GPBGetFieldDataType(field);
2889  GPBDataType keyDataType = field.mapKeyDataType;
2890  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
2891  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
2892                                                   NSNumber *aValue,
2893                                                   BOOL *stop) {
2894    #pragma unused(stop)
2895    // Write the tag.
2896    [outputStream writeInt32NoTag:tag];
2897    // Write the size of the message.
2898    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2899    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
2900    [outputStream writeInt32NoTag:(int32_t)msgSize];
2901    // Write the fields.
2902    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
2903    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
2904  }];
2905}
2906
2907- (void)setGPBGenericValue:(GPBGenericValue *)value
2908     forGPBGenericValueKey:(GPBGenericValue *)key {
2909  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
2910}
2911
2912- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
2913  [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
2914      #pragma unused(stop)
2915      block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
2916  }];
2917}
2918
2919- (BOOL)valueForKey:(uint32_t)key value:(double *)value {
2920  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
2921  if (wrapped && value) {
2922    *value = [wrapped doubleValue];
2923  }
2924  return (wrapped != NULL);
2925}
2926
2927- (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
2928  if (otherDictionary) {
2929    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
2930    if (_autocreator) {
2931      GPBAutocreatedDictionaryModified(_autocreator, self);
2932    }
2933  }
2934}
2935
2936- (void)setValue:(double)value forKey:(uint32_t)key {
2937  [_dictionary setObject:@(value) forKey:@(key)];
2938  if (_autocreator) {
2939    GPBAutocreatedDictionaryModified(_autocreator, self);
2940  }
2941}
2942
2943- (void)removeValueForKey:(uint32_t)aKey {
2944  [_dictionary removeObjectForKey:@(aKey)];
2945}
2946
2947- (void)removeAll {
2948  [_dictionary removeAllObjects];
2949}
2950
2951@end
2952
2953#pragma mark - UInt32 -> Enum
2954
2955@implementation GPBUInt32EnumDictionary {
2956 @package
2957  NSMutableDictionary *_dictionary;
2958  GPBEnumValidationFunc _validationFunc;
2959}
2960
2961@synthesize validationFunc = _validationFunc;
2962
2963+ (instancetype)dictionary {
2964  return [[[self alloc] initWithValidationFunction:NULL
2965                                         rawValues:NULL
2966                                           forKeys:NULL
2967                                             count:0] autorelease];
2968}
2969
2970+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
2971  return [[[self alloc] initWithValidationFunction:func
2972                                         rawValues:NULL
2973                                           forKeys:NULL
2974                                             count:0] autorelease];
2975}
2976
2977+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
2978                                        rawValue:(int32_t)rawValue
2979                                          forKey:(uint32_t)key {
2980  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2981  // on to get the type correct.
2982  return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
2983                                                                   rawValues:&rawValue
2984                                                                     forKeys:&key
2985                                                                       count:1] autorelease];
2986}
2987
2988+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
2989                                       rawValues:(const int32_t [])rawValues
2990                                         forKeys:(const uint32_t [])keys
2991                                           count:(NSUInteger)count {
2992  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
2993  // on to get the type correct.
2994  return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
2995                                                                   rawValues:rawValues
2996                                                                     forKeys:keys
2997                                                                       count:count] autorelease];
2998}
2999
3000+ (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
3001  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3002  // on to get the type correct.
3003  return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3004}
3005
3006+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
3007                                        capacity:(NSUInteger)numItems {
3008  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
3009}
3010
3011- (instancetype)init {
3012  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
3013}
3014
3015- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
3016  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
3017}
3018
3019- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
3020                                 rawValues:(const int32_t [])rawValues
3021                                   forKeys:(const uint32_t [])keys
3022                                     count:(NSUInteger)count {
3023  self = [super init];
3024  if (self) {
3025    _dictionary = [[NSMutableDictionary alloc] init];
3026    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
3027    if (count && rawValues && keys) {
3028      for (NSUInteger i = 0; i < count; ++i) {
3029        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
3030      }
3031    }
3032  }
3033  return self;
3034}
3035
3036- (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
3037  self = [self initWithValidationFunction:dictionary.validationFunc
3038                                rawValues:NULL
3039                                  forKeys:NULL
3040                                    count:0];
3041  if (self) {
3042    if (dictionary) {
3043      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3044    }
3045  }
3046  return self;
3047}
3048
3049- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
3050                                  capacity:(NSUInteger)numItems {
3051  #pragma unused(numItems)
3052  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
3053}
3054
3055- (void)dealloc {
3056  NSAssert(!_autocreator,
3057           @"%@: Autocreator must be cleared before release, autocreator: %@",
3058           [self class], _autocreator);
3059  [_dictionary release];
3060  [super dealloc];
3061}
3062
3063- (instancetype)copyWithZone:(NSZone *)zone {
3064  return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
3065}
3066
3067- (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
3068  if (self == other) {
3069    return YES;
3070  }
3071  if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
3072    return NO;
3073  }
3074  return [_dictionary isEqual:other->_dictionary];
3075}
3076
3077- (NSUInteger)hash {
3078  return _dictionary.count;
3079}
3080
3081- (NSString *)description {
3082  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3083}
3084
3085- (NSUInteger)count {
3086  return _dictionary.count;
3087}
3088
3089- (void)enumerateKeysAndRawValuesUsingBlock:
3090    (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
3091  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3092                                                   NSNumber *aValue,
3093                                                   BOOL *stop) {
3094      block([aKey unsignedIntValue], [aValue intValue], stop);
3095  }];
3096}
3097
3098- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3099  NSUInteger count = _dictionary.count;
3100  if (count == 0) {
3101    return 0;
3102  }
3103
3104  GPBDataType valueDataType = GPBGetFieldDataType(field);
3105  GPBDataType keyDataType = field.mapKeyDataType;
3106  __block size_t result = 0;
3107  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3108                                                   NSNumber *aValue,
3109                                                   BOOL *stop) {
3110    #pragma unused(stop)
3111    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3112    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3113    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3114  }];
3115  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3116  result += tagSize * count;
3117  return result;
3118}
3119
3120- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3121                         asField:(GPBFieldDescriptor *)field {
3122  GPBDataType valueDataType = GPBGetFieldDataType(field);
3123  GPBDataType keyDataType = field.mapKeyDataType;
3124  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3125  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3126                                                   NSNumber *aValue,
3127                                                   BOOL *stop) {
3128    #pragma unused(stop)
3129    // Write the tag.
3130    [outputStream writeInt32NoTag:tag];
3131    // Write the size of the message.
3132    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3133    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3134    [outputStream writeInt32NoTag:(int32_t)msgSize];
3135    // Write the fields.
3136    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3137    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
3138  }];
3139}
3140
3141- (NSData *)serializedDataForUnknownValue:(int32_t)value
3142                                   forKey:(GPBGenericValue *)key
3143                              keyDataType:(GPBDataType)keyDataType {
3144  size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
3145  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
3146  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
3147  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
3148  WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
3149  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
3150  [outputStream release];
3151  return data;
3152}
3153- (void)setGPBGenericValue:(GPBGenericValue *)value
3154     forGPBGenericValueKey:(GPBGenericValue *)key {
3155  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
3156}
3157
3158- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3159  [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
3160      #pragma unused(stop)
3161      block([NSString stringWithFormat:@"%u", key], @(value));
3162  }];
3163}
3164
3165- (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
3166  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3167  if (wrapped && value) {
3168    int32_t result = [wrapped intValue];
3169    if (!_validationFunc(result)) {
3170      result = kGPBUnrecognizedEnumeratorValue;
3171    }
3172    *value = result;
3173  }
3174  return (wrapped != NULL);
3175}
3176
3177- (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
3178  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3179  if (wrapped && rawValue) {
3180    *rawValue = [wrapped intValue];
3181  }
3182  return (wrapped != NULL);
3183}
3184
3185- (void)enumerateKeysAndValuesUsingBlock:
3186    (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
3187  GPBEnumValidationFunc func = _validationFunc;
3188  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3189                                                   NSNumber *aValue,
3190                                                   BOOL *stop) {
3191      int32_t unwrapped = [aValue intValue];
3192      if (!func(unwrapped)) {
3193        unwrapped = kGPBUnrecognizedEnumeratorValue;
3194      }
3195      block([aKey unsignedIntValue], unwrapped, stop);
3196  }];
3197}
3198
3199- (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
3200  if (otherDictionary) {
3201    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3202    if (_autocreator) {
3203      GPBAutocreatedDictionaryModified(_autocreator, self);
3204    }
3205  }
3206}
3207
3208- (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
3209  [_dictionary setObject:@(value) forKey:@(key)];
3210  if (_autocreator) {
3211    GPBAutocreatedDictionaryModified(_autocreator, self);
3212  }
3213}
3214
3215- (void)removeValueForKey:(uint32_t)aKey {
3216  [_dictionary removeObjectForKey:@(aKey)];
3217}
3218
3219- (void)removeAll {
3220  [_dictionary removeAllObjects];
3221}
3222
3223- (void)setValue:(int32_t)value forKey:(uint32_t)key {
3224  if (!_validationFunc(value)) {
3225    [NSException raise:NSInvalidArgumentException
3226                format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
3227                       value];
3228  }
3229
3230  [_dictionary setObject:@(value) forKey:@(key)];
3231  if (_autocreator) {
3232    GPBAutocreatedDictionaryModified(_autocreator, self);
3233  }
3234}
3235
3236@end
3237
3238#pragma mark - UInt32 -> Object
3239
3240@implementation GPBUInt32ObjectDictionary {
3241 @package
3242  NSMutableDictionary *_dictionary;
3243}
3244
3245+ (instancetype)dictionary {
3246  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
3247}
3248
3249+ (instancetype)dictionaryWithObject:(id)object
3250                              forKey:(uint32_t)key {
3251  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
3252  // on to get the type correct.
3253  return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
3254                                                            forKeys:&key
3255                                                              count:1] autorelease];
3256}
3257
3258+ (instancetype)dictionaryWithObjects:(const id [])objects
3259                              forKeys:(const uint32_t [])keys
3260                                count:(NSUInteger)count {
3261  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
3262  // on to get the type correct.
3263  return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
3264                                                           forKeys:keys
3265                                                             count:count] autorelease];
3266}
3267
3268+ (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
3269  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3270  // on to get the type correct.
3271  return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3272}
3273
3274+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3275  return [[[self alloc] initWithCapacity:numItems] autorelease];
3276}
3277
3278- (instancetype)init {
3279  return [self initWithObjects:NULL forKeys:NULL count:0];
3280}
3281
3282- (instancetype)initWithObjects:(const id [])objects
3283                        forKeys:(const uint32_t [])keys
3284                          count:(NSUInteger)count {
3285  self = [super init];
3286  if (self) {
3287    _dictionary = [[NSMutableDictionary alloc] init];
3288    if (count && objects && keys) {
3289      for (NSUInteger i = 0; i < count; ++i) {
3290        if (!objects[i]) {
3291          [NSException raise:NSInvalidArgumentException
3292                      format:@"Attempting to add nil object to a Dictionary"];
3293        }
3294        [_dictionary setObject:objects[i] forKey:@(keys[i])];
3295      }
3296    }
3297  }
3298  return self;
3299}
3300
3301- (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
3302  self = [self initWithObjects:NULL forKeys:NULL count:0];
3303  if (self) {
3304    if (dictionary) {
3305      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3306    }
3307  }
3308  return self;
3309}
3310
3311- (instancetype)initWithCapacity:(NSUInteger)numItems {
3312  #pragma unused(numItems)
3313  return [self initWithObjects:NULL forKeys:NULL count:0];
3314}
3315
3316- (void)dealloc {
3317  NSAssert(!_autocreator,
3318           @"%@: Autocreator must be cleared before release, autocreator: %@",
3319           [self class], _autocreator);
3320  [_dictionary release];
3321  [super dealloc];
3322}
3323
3324- (instancetype)copyWithZone:(NSZone *)zone {
3325  return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
3326}
3327
3328- (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
3329  if (self == other) {
3330    return YES;
3331  }
3332  if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
3333    return NO;
3334  }
3335  return [_dictionary isEqual:other->_dictionary];
3336}
3337
3338- (NSUInteger)hash {
3339  return _dictionary.count;
3340}
3341
3342- (NSString *)description {
3343  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3344}
3345
3346- (NSUInteger)count {
3347  return _dictionary.count;
3348}
3349
3350- (void)enumerateKeysAndObjectsUsingBlock:
3351    (void (^)(uint32_t key, id object, BOOL *stop))block {
3352  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3353                                                   id aObject,
3354                                                   BOOL *stop) {
3355      block([aKey unsignedIntValue], aObject, stop);
3356  }];
3357}
3358
3359- (BOOL)isInitialized {
3360  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
3361    if (!msg.initialized) {
3362      return NO;
3363    }
3364  }
3365  return YES;
3366}
3367
3368- (instancetype)deepCopyWithZone:(NSZone *)zone {
3369  GPBUInt32ObjectDictionary *newDict =
3370      [[GPBUInt32ObjectDictionary alloc] init];
3371  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
3372                                                   GPBMessage *msg,
3373                                                   BOOL *stop) {
3374    #pragma unused(stop)
3375    GPBMessage *copiedMsg = [msg copyWithZone:zone];
3376    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
3377    [copiedMsg release];
3378  }];
3379  return newDict;
3380}
3381
3382- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3383  NSUInteger count = _dictionary.count;
3384  if (count == 0) {
3385    return 0;
3386  }
3387
3388  GPBDataType valueDataType = GPBGetFieldDataType(field);
3389  GPBDataType keyDataType = field.mapKeyDataType;
3390  __block size_t result = 0;
3391  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3392                                                   id aObject,
3393                                                   BOOL *stop) {
3394    #pragma unused(stop)
3395    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3396    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
3397    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3398  }];
3399  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3400  result += tagSize * count;
3401  return result;
3402}
3403
3404- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3405                         asField:(GPBFieldDescriptor *)field {
3406  GPBDataType valueDataType = GPBGetFieldDataType(field);
3407  GPBDataType keyDataType = field.mapKeyDataType;
3408  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3409  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3410                                                   id aObject,
3411                                                   BOOL *stop) {
3412    #pragma unused(stop)
3413    // Write the tag.
3414    [outputStream writeInt32NoTag:tag];
3415    // Write the size of the message.
3416    size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3417    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
3418    [outputStream writeInt32NoTag:(int32_t)msgSize];
3419    // Write the fields.
3420    WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
3421    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
3422  }];
3423}
3424
3425- (void)setGPBGenericValue:(GPBGenericValue *)value
3426     forGPBGenericValueKey:(GPBGenericValue *)key {
3427  [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
3428}
3429
3430- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3431  [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
3432      #pragma unused(stop)
3433      block([NSString stringWithFormat:@"%u", key], object);
3434  }];
3435}
3436
3437- (id)objectForKey:(uint32_t)key {
3438  id result = [_dictionary objectForKey:@(key)];
3439  return result;
3440}
3441
3442- (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
3443  if (otherDictionary) {
3444    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3445    if (_autocreator) {
3446      GPBAutocreatedDictionaryModified(_autocreator, self);
3447    }
3448  }
3449}
3450
3451- (void)setObject:(id)object forKey:(uint32_t)key {
3452  if (!object) {
3453    [NSException raise:NSInvalidArgumentException
3454                format:@"Attempting to add nil object to a Dictionary"];
3455  }
3456  [_dictionary setObject:object forKey:@(key)];
3457  if (_autocreator) {
3458    GPBAutocreatedDictionaryModified(_autocreator, self);
3459  }
3460}
3461
3462- (void)removeObjectForKey:(uint32_t)aKey {
3463  [_dictionary removeObjectForKey:@(aKey)];
3464}
3465
3466- (void)removeAll {
3467  [_dictionary removeAllObjects];
3468}
3469
3470@end
3471
3472//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
3473// This block of code is generated, do not edit it directly.
3474
3475#pragma mark - Int32 -> UInt32
3476
3477@implementation GPBInt32UInt32Dictionary {
3478 @package
3479  NSMutableDictionary *_dictionary;
3480}
3481
3482+ (instancetype)dictionary {
3483  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
3484}
3485
3486+ (instancetype)dictionaryWithValue:(uint32_t)value
3487                             forKey:(int32_t)key {
3488  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3489  // on to get the type correct.
3490  return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
3491                                                          forKeys:&key
3492                                                            count:1] autorelease];
3493}
3494
3495+ (instancetype)dictionaryWithValues:(const uint32_t [])values
3496                             forKeys:(const int32_t [])keys
3497                               count:(NSUInteger)count {
3498  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3499  // on to get the type correct.
3500  return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
3501                                                          forKeys:keys
3502                                                            count:count] autorelease];
3503}
3504
3505+ (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
3506  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3507  // on to get the type correct.
3508  return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3509}
3510
3511+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3512  return [[[self alloc] initWithCapacity:numItems] autorelease];
3513}
3514
3515- (instancetype)init {
3516  return [self initWithValues:NULL forKeys:NULL count:0];
3517}
3518
3519- (instancetype)initWithValues:(const uint32_t [])values
3520                       forKeys:(const int32_t [])keys
3521                         count:(NSUInteger)count {
3522  self = [super init];
3523  if (self) {
3524    _dictionary = [[NSMutableDictionary alloc] init];
3525    if (count && values && keys) {
3526      for (NSUInteger i = 0; i < count; ++i) {
3527        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
3528      }
3529    }
3530  }
3531  return self;
3532}
3533
3534- (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
3535  self = [self initWithValues:NULL forKeys:NULL count:0];
3536  if (self) {
3537    if (dictionary) {
3538      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3539    }
3540  }
3541  return self;
3542}
3543
3544- (instancetype)initWithCapacity:(NSUInteger)numItems {
3545  #pragma unused(numItems)
3546  return [self initWithValues:NULL forKeys:NULL count:0];
3547}
3548
3549- (void)dealloc {
3550  NSAssert(!_autocreator,
3551           @"%@: Autocreator must be cleared before release, autocreator: %@",
3552           [self class], _autocreator);
3553  [_dictionary release];
3554  [super dealloc];
3555}
3556
3557- (instancetype)copyWithZone:(NSZone *)zone {
3558  return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
3559}
3560
3561- (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
3562  if (self == other) {
3563    return YES;
3564  }
3565  if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
3566    return NO;
3567  }
3568  return [_dictionary isEqual:other->_dictionary];
3569}
3570
3571- (NSUInteger)hash {
3572  return _dictionary.count;
3573}
3574
3575- (NSString *)description {
3576  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3577}
3578
3579- (NSUInteger)count {
3580  return _dictionary.count;
3581}
3582
3583- (void)enumerateKeysAndValuesUsingBlock:
3584    (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
3585  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3586                                                   NSNumber *aValue,
3587                                                   BOOL *stop) {
3588      block([aKey intValue], [aValue unsignedIntValue], stop);
3589  }];
3590}
3591
3592- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3593  NSUInteger count = _dictionary.count;
3594  if (count == 0) {
3595    return 0;
3596  }
3597
3598  GPBDataType valueDataType = GPBGetFieldDataType(field);
3599  GPBDataType keyDataType = field.mapKeyDataType;
3600  __block size_t result = 0;
3601  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3602                                                   NSNumber *aValue,
3603                                                   BOOL *stop) {
3604    #pragma unused(stop)
3605    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3606    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
3607    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3608  }];
3609  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3610  result += tagSize * count;
3611  return result;
3612}
3613
3614- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3615                         asField:(GPBFieldDescriptor *)field {
3616  GPBDataType valueDataType = GPBGetFieldDataType(field);
3617  GPBDataType keyDataType = field.mapKeyDataType;
3618  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3619  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3620                                                   NSNumber *aValue,
3621                                                   BOOL *stop) {
3622    #pragma unused(stop)
3623    // Write the tag.
3624    [outputStream writeInt32NoTag:tag];
3625    // Write the size of the message.
3626    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3627    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
3628    [outputStream writeInt32NoTag:(int32_t)msgSize];
3629    // Write the fields.
3630    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
3631    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
3632  }];
3633}
3634
3635- (void)setGPBGenericValue:(GPBGenericValue *)value
3636     forGPBGenericValueKey:(GPBGenericValue *)key {
3637  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
3638}
3639
3640- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3641  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
3642      #pragma unused(stop)
3643      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
3644  }];
3645}
3646
3647- (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
3648  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3649  if (wrapped && value) {
3650    *value = [wrapped unsignedIntValue];
3651  }
3652  return (wrapped != NULL);
3653}
3654
3655- (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
3656  if (otherDictionary) {
3657    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3658    if (_autocreator) {
3659      GPBAutocreatedDictionaryModified(_autocreator, self);
3660    }
3661  }
3662}
3663
3664- (void)setValue:(uint32_t)value forKey:(int32_t)key {
3665  [_dictionary setObject:@(value) forKey:@(key)];
3666  if (_autocreator) {
3667    GPBAutocreatedDictionaryModified(_autocreator, self);
3668  }
3669}
3670
3671- (void)removeValueForKey:(int32_t)aKey {
3672  [_dictionary removeObjectForKey:@(aKey)];
3673}
3674
3675- (void)removeAll {
3676  [_dictionary removeAllObjects];
3677}
3678
3679@end
3680
3681#pragma mark - Int32 -> Int32
3682
3683@implementation GPBInt32Int32Dictionary {
3684 @package
3685  NSMutableDictionary *_dictionary;
3686}
3687
3688+ (instancetype)dictionary {
3689  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
3690}
3691
3692+ (instancetype)dictionaryWithValue:(int32_t)value
3693                             forKey:(int32_t)key {
3694  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3695  // on to get the type correct.
3696  return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
3697                                                         forKeys:&key
3698                                                           count:1] autorelease];
3699}
3700
3701+ (instancetype)dictionaryWithValues:(const int32_t [])values
3702                             forKeys:(const int32_t [])keys
3703                               count:(NSUInteger)count {
3704  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3705  // on to get the type correct.
3706  return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
3707                                                         forKeys:keys
3708                                                           count:count] autorelease];
3709}
3710
3711+ (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
3712  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3713  // on to get the type correct.
3714  return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3715}
3716
3717+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3718  return [[[self alloc] initWithCapacity:numItems] autorelease];
3719}
3720
3721- (instancetype)init {
3722  return [self initWithValues:NULL forKeys:NULL count:0];
3723}
3724
3725- (instancetype)initWithValues:(const int32_t [])values
3726                       forKeys:(const int32_t [])keys
3727                         count:(NSUInteger)count {
3728  self = [super init];
3729  if (self) {
3730    _dictionary = [[NSMutableDictionary alloc] init];
3731    if (count && values && keys) {
3732      for (NSUInteger i = 0; i < count; ++i) {
3733        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
3734      }
3735    }
3736  }
3737  return self;
3738}
3739
3740- (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
3741  self = [self initWithValues:NULL forKeys:NULL count:0];
3742  if (self) {
3743    if (dictionary) {
3744      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3745    }
3746  }
3747  return self;
3748}
3749
3750- (instancetype)initWithCapacity:(NSUInteger)numItems {
3751  #pragma unused(numItems)
3752  return [self initWithValues:NULL forKeys:NULL count:0];
3753}
3754
3755- (void)dealloc {
3756  NSAssert(!_autocreator,
3757           @"%@: Autocreator must be cleared before release, autocreator: %@",
3758           [self class], _autocreator);
3759  [_dictionary release];
3760  [super dealloc];
3761}
3762
3763- (instancetype)copyWithZone:(NSZone *)zone {
3764  return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
3765}
3766
3767- (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
3768  if (self == other) {
3769    return YES;
3770  }
3771  if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
3772    return NO;
3773  }
3774  return [_dictionary isEqual:other->_dictionary];
3775}
3776
3777- (NSUInteger)hash {
3778  return _dictionary.count;
3779}
3780
3781- (NSString *)description {
3782  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3783}
3784
3785- (NSUInteger)count {
3786  return _dictionary.count;
3787}
3788
3789- (void)enumerateKeysAndValuesUsingBlock:
3790    (void (^)(int32_t key, int32_t value, BOOL *stop))block {
3791  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3792                                                   NSNumber *aValue,
3793                                                   BOOL *stop) {
3794      block([aKey intValue], [aValue intValue], stop);
3795  }];
3796}
3797
3798- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
3799  NSUInteger count = _dictionary.count;
3800  if (count == 0) {
3801    return 0;
3802  }
3803
3804  GPBDataType valueDataType = GPBGetFieldDataType(field);
3805  GPBDataType keyDataType = field.mapKeyDataType;
3806  __block size_t result = 0;
3807  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3808                                                   NSNumber *aValue,
3809                                                   BOOL *stop) {
3810    #pragma unused(stop)
3811    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3812    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3813    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
3814  }];
3815  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
3816  result += tagSize * count;
3817  return result;
3818}
3819
3820- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
3821                         asField:(GPBFieldDescriptor *)field {
3822  GPBDataType valueDataType = GPBGetFieldDataType(field);
3823  GPBDataType keyDataType = field.mapKeyDataType;
3824  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
3825  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3826                                                   NSNumber *aValue,
3827                                                   BOOL *stop) {
3828    #pragma unused(stop)
3829    // Write the tag.
3830    [outputStream writeInt32NoTag:tag];
3831    // Write the size of the message.
3832    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
3833    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
3834    [outputStream writeInt32NoTag:(int32_t)msgSize];
3835    // Write the fields.
3836    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
3837    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
3838  }];
3839}
3840
3841- (void)setGPBGenericValue:(GPBGenericValue *)value
3842     forGPBGenericValueKey:(GPBGenericValue *)key {
3843  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
3844}
3845
3846- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
3847  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
3848      #pragma unused(stop)
3849      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
3850  }];
3851}
3852
3853- (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
3854  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
3855  if (wrapped && value) {
3856    *value = [wrapped intValue];
3857  }
3858  return (wrapped != NULL);
3859}
3860
3861- (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
3862  if (otherDictionary) {
3863    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
3864    if (_autocreator) {
3865      GPBAutocreatedDictionaryModified(_autocreator, self);
3866    }
3867  }
3868}
3869
3870- (void)setValue:(int32_t)value forKey:(int32_t)key {
3871  [_dictionary setObject:@(value) forKey:@(key)];
3872  if (_autocreator) {
3873    GPBAutocreatedDictionaryModified(_autocreator, self);
3874  }
3875}
3876
3877- (void)removeValueForKey:(int32_t)aKey {
3878  [_dictionary removeObjectForKey:@(aKey)];
3879}
3880
3881- (void)removeAll {
3882  [_dictionary removeAllObjects];
3883}
3884
3885@end
3886
3887#pragma mark - Int32 -> UInt64
3888
3889@implementation GPBInt32UInt64Dictionary {
3890 @package
3891  NSMutableDictionary *_dictionary;
3892}
3893
3894+ (instancetype)dictionary {
3895  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
3896}
3897
3898+ (instancetype)dictionaryWithValue:(uint64_t)value
3899                             forKey:(int32_t)key {
3900  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3901  // on to get the type correct.
3902  return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
3903                                                          forKeys:&key
3904                                                            count:1] autorelease];
3905}
3906
3907+ (instancetype)dictionaryWithValues:(const uint64_t [])values
3908                             forKeys:(const int32_t [])keys
3909                               count:(NSUInteger)count {
3910  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
3911  // on to get the type correct.
3912  return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
3913                                                          forKeys:keys
3914                                                            count:count] autorelease];
3915}
3916
3917+ (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
3918  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
3919  // on to get the type correct.
3920  return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
3921}
3922
3923+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
3924  return [[[self alloc] initWithCapacity:numItems] autorelease];
3925}
3926
3927- (instancetype)init {
3928  return [self initWithValues:NULL forKeys:NULL count:0];
3929}
3930
3931- (instancetype)initWithValues:(const uint64_t [])values
3932                       forKeys:(const int32_t [])keys
3933                         count:(NSUInteger)count {
3934  self = [super init];
3935  if (self) {
3936    _dictionary = [[NSMutableDictionary alloc] init];
3937    if (count && values && keys) {
3938      for (NSUInteger i = 0; i < count; ++i) {
3939        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
3940      }
3941    }
3942  }
3943  return self;
3944}
3945
3946- (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
3947  self = [self initWithValues:NULL forKeys:NULL count:0];
3948  if (self) {
3949    if (dictionary) {
3950      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
3951    }
3952  }
3953  return self;
3954}
3955
3956- (instancetype)initWithCapacity:(NSUInteger)numItems {
3957  #pragma unused(numItems)
3958  return [self initWithValues:NULL forKeys:NULL count:0];
3959}
3960
3961- (void)dealloc {
3962  NSAssert(!_autocreator,
3963           @"%@: Autocreator must be cleared before release, autocreator: %@",
3964           [self class], _autocreator);
3965  [_dictionary release];
3966  [super dealloc];
3967}
3968
3969- (instancetype)copyWithZone:(NSZone *)zone {
3970  return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
3971}
3972
3973- (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
3974  if (self == other) {
3975    return YES;
3976  }
3977  if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
3978    return NO;
3979  }
3980  return [_dictionary isEqual:other->_dictionary];
3981}
3982
3983- (NSUInteger)hash {
3984  return _dictionary.count;
3985}
3986
3987- (NSString *)description {
3988  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
3989}
3990
3991- (NSUInteger)count {
3992  return _dictionary.count;
3993}
3994
3995- (void)enumerateKeysAndValuesUsingBlock:
3996    (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
3997  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
3998                                                   NSNumber *aValue,
3999                                                   BOOL *stop) {
4000      block([aKey intValue], [aValue unsignedLongLongValue], stop);
4001  }];
4002}
4003
4004- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4005  NSUInteger count = _dictionary.count;
4006  if (count == 0) {
4007    return 0;
4008  }
4009
4010  GPBDataType valueDataType = GPBGetFieldDataType(field);
4011  GPBDataType keyDataType = field.mapKeyDataType;
4012  __block size_t result = 0;
4013  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4014                                                   NSNumber *aValue,
4015                                                   BOOL *stop) {
4016    #pragma unused(stop)
4017    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4018    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
4019    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4020  }];
4021  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4022  result += tagSize * count;
4023  return result;
4024}
4025
4026- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4027                         asField:(GPBFieldDescriptor *)field {
4028  GPBDataType valueDataType = GPBGetFieldDataType(field);
4029  GPBDataType keyDataType = field.mapKeyDataType;
4030  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4031  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4032                                                   NSNumber *aValue,
4033                                                   BOOL *stop) {
4034    #pragma unused(stop)
4035    // Write the tag.
4036    [outputStream writeInt32NoTag:tag];
4037    // Write the size of the message.
4038    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4039    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
4040    [outputStream writeInt32NoTag:(int32_t)msgSize];
4041    // Write the fields.
4042    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4043    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
4044  }];
4045}
4046
4047- (void)setGPBGenericValue:(GPBGenericValue *)value
4048     forGPBGenericValueKey:(GPBGenericValue *)key {
4049  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
4050}
4051
4052- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4053  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
4054      #pragma unused(stop)
4055      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
4056  }];
4057}
4058
4059- (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
4060  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4061  if (wrapped && value) {
4062    *value = [wrapped unsignedLongLongValue];
4063  }
4064  return (wrapped != NULL);
4065}
4066
4067- (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
4068  if (otherDictionary) {
4069    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4070    if (_autocreator) {
4071      GPBAutocreatedDictionaryModified(_autocreator, self);
4072    }
4073  }
4074}
4075
4076- (void)setValue:(uint64_t)value forKey:(int32_t)key {
4077  [_dictionary setObject:@(value) forKey:@(key)];
4078  if (_autocreator) {
4079    GPBAutocreatedDictionaryModified(_autocreator, self);
4080  }
4081}
4082
4083- (void)removeValueForKey:(int32_t)aKey {
4084  [_dictionary removeObjectForKey:@(aKey)];
4085}
4086
4087- (void)removeAll {
4088  [_dictionary removeAllObjects];
4089}
4090
4091@end
4092
4093#pragma mark - Int32 -> Int64
4094
4095@implementation GPBInt32Int64Dictionary {
4096 @package
4097  NSMutableDictionary *_dictionary;
4098}
4099
4100+ (instancetype)dictionary {
4101  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
4102}
4103
4104+ (instancetype)dictionaryWithValue:(int64_t)value
4105                             forKey:(int32_t)key {
4106  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4107  // on to get the type correct.
4108  return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
4109                                                         forKeys:&key
4110                                                           count:1] autorelease];
4111}
4112
4113+ (instancetype)dictionaryWithValues:(const int64_t [])values
4114                             forKeys:(const int32_t [])keys
4115                               count:(NSUInteger)count {
4116  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4117  // on to get the type correct.
4118  return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
4119                                                         forKeys:keys
4120                                                           count:count] autorelease];
4121}
4122
4123+ (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
4124  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4125  // on to get the type correct.
4126  return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4127}
4128
4129+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4130  return [[[self alloc] initWithCapacity:numItems] autorelease];
4131}
4132
4133- (instancetype)init {
4134  return [self initWithValues:NULL forKeys:NULL count:0];
4135}
4136
4137- (instancetype)initWithValues:(const int64_t [])values
4138                       forKeys:(const int32_t [])keys
4139                         count:(NSUInteger)count {
4140  self = [super init];
4141  if (self) {
4142    _dictionary = [[NSMutableDictionary alloc] init];
4143    if (count && values && keys) {
4144      for (NSUInteger i = 0; i < count; ++i) {
4145        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4146      }
4147    }
4148  }
4149  return self;
4150}
4151
4152- (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
4153  self = [self initWithValues:NULL forKeys:NULL count:0];
4154  if (self) {
4155    if (dictionary) {
4156      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4157    }
4158  }
4159  return self;
4160}
4161
4162- (instancetype)initWithCapacity:(NSUInteger)numItems {
4163  #pragma unused(numItems)
4164  return [self initWithValues:NULL forKeys:NULL count:0];
4165}
4166
4167- (void)dealloc {
4168  NSAssert(!_autocreator,
4169           @"%@: Autocreator must be cleared before release, autocreator: %@",
4170           [self class], _autocreator);
4171  [_dictionary release];
4172  [super dealloc];
4173}
4174
4175- (instancetype)copyWithZone:(NSZone *)zone {
4176  return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
4177}
4178
4179- (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
4180  if (self == other) {
4181    return YES;
4182  }
4183  if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
4184    return NO;
4185  }
4186  return [_dictionary isEqual:other->_dictionary];
4187}
4188
4189- (NSUInteger)hash {
4190  return _dictionary.count;
4191}
4192
4193- (NSString *)description {
4194  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4195}
4196
4197- (NSUInteger)count {
4198  return _dictionary.count;
4199}
4200
4201- (void)enumerateKeysAndValuesUsingBlock:
4202    (void (^)(int32_t key, int64_t value, BOOL *stop))block {
4203  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4204                                                   NSNumber *aValue,
4205                                                   BOOL *stop) {
4206      block([aKey intValue], [aValue longLongValue], stop);
4207  }];
4208}
4209
4210- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4211  NSUInteger count = _dictionary.count;
4212  if (count == 0) {
4213    return 0;
4214  }
4215
4216  GPBDataType valueDataType = GPBGetFieldDataType(field);
4217  GPBDataType keyDataType = field.mapKeyDataType;
4218  __block size_t result = 0;
4219  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4220                                                   NSNumber *aValue,
4221                                                   BOOL *stop) {
4222    #pragma unused(stop)
4223    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4224    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
4225    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4226  }];
4227  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4228  result += tagSize * count;
4229  return result;
4230}
4231
4232- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4233                         asField:(GPBFieldDescriptor *)field {
4234  GPBDataType valueDataType = GPBGetFieldDataType(field);
4235  GPBDataType keyDataType = field.mapKeyDataType;
4236  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4237  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4238                                                   NSNumber *aValue,
4239                                                   BOOL *stop) {
4240    #pragma unused(stop)
4241    // Write the tag.
4242    [outputStream writeInt32NoTag:tag];
4243    // Write the size of the message.
4244    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4245    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
4246    [outputStream writeInt32NoTag:(int32_t)msgSize];
4247    // Write the fields.
4248    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4249    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
4250  }];
4251}
4252
4253- (void)setGPBGenericValue:(GPBGenericValue *)value
4254     forGPBGenericValueKey:(GPBGenericValue *)key {
4255  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
4256}
4257
4258- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4259  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
4260      #pragma unused(stop)
4261      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
4262  }];
4263}
4264
4265- (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
4266  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4267  if (wrapped && value) {
4268    *value = [wrapped longLongValue];
4269  }
4270  return (wrapped != NULL);
4271}
4272
4273- (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
4274  if (otherDictionary) {
4275    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4276    if (_autocreator) {
4277      GPBAutocreatedDictionaryModified(_autocreator, self);
4278    }
4279  }
4280}
4281
4282- (void)setValue:(int64_t)value forKey:(int32_t)key {
4283  [_dictionary setObject:@(value) forKey:@(key)];
4284  if (_autocreator) {
4285    GPBAutocreatedDictionaryModified(_autocreator, self);
4286  }
4287}
4288
4289- (void)removeValueForKey:(int32_t)aKey {
4290  [_dictionary removeObjectForKey:@(aKey)];
4291}
4292
4293- (void)removeAll {
4294  [_dictionary removeAllObjects];
4295}
4296
4297@end
4298
4299#pragma mark - Int32 -> Bool
4300
4301@implementation GPBInt32BoolDictionary {
4302 @package
4303  NSMutableDictionary *_dictionary;
4304}
4305
4306+ (instancetype)dictionary {
4307  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
4308}
4309
4310+ (instancetype)dictionaryWithValue:(BOOL)value
4311                             forKey:(int32_t)key {
4312  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4313  // on to get the type correct.
4314  return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
4315                                                        forKeys:&key
4316                                                          count:1] autorelease];
4317}
4318
4319+ (instancetype)dictionaryWithValues:(const BOOL [])values
4320                             forKeys:(const int32_t [])keys
4321                               count:(NSUInteger)count {
4322  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4323  // on to get the type correct.
4324  return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
4325                                                        forKeys:keys
4326                                                          count:count] autorelease];
4327}
4328
4329+ (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
4330  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4331  // on to get the type correct.
4332  return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4333}
4334
4335+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4336  return [[[self alloc] initWithCapacity:numItems] autorelease];
4337}
4338
4339- (instancetype)init {
4340  return [self initWithValues:NULL forKeys:NULL count:0];
4341}
4342
4343- (instancetype)initWithValues:(const BOOL [])values
4344                       forKeys:(const int32_t [])keys
4345                         count:(NSUInteger)count {
4346  self = [super init];
4347  if (self) {
4348    _dictionary = [[NSMutableDictionary alloc] init];
4349    if (count && values && keys) {
4350      for (NSUInteger i = 0; i < count; ++i) {
4351        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4352      }
4353    }
4354  }
4355  return self;
4356}
4357
4358- (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
4359  self = [self initWithValues:NULL forKeys:NULL count:0];
4360  if (self) {
4361    if (dictionary) {
4362      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4363    }
4364  }
4365  return self;
4366}
4367
4368- (instancetype)initWithCapacity:(NSUInteger)numItems {
4369  #pragma unused(numItems)
4370  return [self initWithValues:NULL forKeys:NULL count:0];
4371}
4372
4373- (void)dealloc {
4374  NSAssert(!_autocreator,
4375           @"%@: Autocreator must be cleared before release, autocreator: %@",
4376           [self class], _autocreator);
4377  [_dictionary release];
4378  [super dealloc];
4379}
4380
4381- (instancetype)copyWithZone:(NSZone *)zone {
4382  return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
4383}
4384
4385- (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
4386  if (self == other) {
4387    return YES;
4388  }
4389  if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
4390    return NO;
4391  }
4392  return [_dictionary isEqual:other->_dictionary];
4393}
4394
4395- (NSUInteger)hash {
4396  return _dictionary.count;
4397}
4398
4399- (NSString *)description {
4400  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4401}
4402
4403- (NSUInteger)count {
4404  return _dictionary.count;
4405}
4406
4407- (void)enumerateKeysAndValuesUsingBlock:
4408    (void (^)(int32_t key, BOOL value, BOOL *stop))block {
4409  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4410                                                   NSNumber *aValue,
4411                                                   BOOL *stop) {
4412      block([aKey intValue], [aValue boolValue], stop);
4413  }];
4414}
4415
4416- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4417  NSUInteger count = _dictionary.count;
4418  if (count == 0) {
4419    return 0;
4420  }
4421
4422  GPBDataType valueDataType = GPBGetFieldDataType(field);
4423  GPBDataType keyDataType = field.mapKeyDataType;
4424  __block size_t result = 0;
4425  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4426                                                   NSNumber *aValue,
4427                                                   BOOL *stop) {
4428    #pragma unused(stop)
4429    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4430    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
4431    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4432  }];
4433  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4434  result += tagSize * count;
4435  return result;
4436}
4437
4438- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4439                         asField:(GPBFieldDescriptor *)field {
4440  GPBDataType valueDataType = GPBGetFieldDataType(field);
4441  GPBDataType keyDataType = field.mapKeyDataType;
4442  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4443  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4444                                                   NSNumber *aValue,
4445                                                   BOOL *stop) {
4446    #pragma unused(stop)
4447    // Write the tag.
4448    [outputStream writeInt32NoTag:tag];
4449    // Write the size of the message.
4450    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4451    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
4452    [outputStream writeInt32NoTag:(int32_t)msgSize];
4453    // Write the fields.
4454    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4455    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
4456  }];
4457}
4458
4459- (void)setGPBGenericValue:(GPBGenericValue *)value
4460     forGPBGenericValueKey:(GPBGenericValue *)key {
4461  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
4462}
4463
4464- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4465  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
4466      #pragma unused(stop)
4467      block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
4468  }];
4469}
4470
4471- (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
4472  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4473  if (wrapped && value) {
4474    *value = [wrapped boolValue];
4475  }
4476  return (wrapped != NULL);
4477}
4478
4479- (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
4480  if (otherDictionary) {
4481    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4482    if (_autocreator) {
4483      GPBAutocreatedDictionaryModified(_autocreator, self);
4484    }
4485  }
4486}
4487
4488- (void)setValue:(BOOL)value forKey:(int32_t)key {
4489  [_dictionary setObject:@(value) forKey:@(key)];
4490  if (_autocreator) {
4491    GPBAutocreatedDictionaryModified(_autocreator, self);
4492  }
4493}
4494
4495- (void)removeValueForKey:(int32_t)aKey {
4496  [_dictionary removeObjectForKey:@(aKey)];
4497}
4498
4499- (void)removeAll {
4500  [_dictionary removeAllObjects];
4501}
4502
4503@end
4504
4505#pragma mark - Int32 -> Float
4506
4507@implementation GPBInt32FloatDictionary {
4508 @package
4509  NSMutableDictionary *_dictionary;
4510}
4511
4512+ (instancetype)dictionary {
4513  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
4514}
4515
4516+ (instancetype)dictionaryWithValue:(float)value
4517                             forKey:(int32_t)key {
4518  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4519  // on to get the type correct.
4520  return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
4521                                                         forKeys:&key
4522                                                           count:1] autorelease];
4523}
4524
4525+ (instancetype)dictionaryWithValues:(const float [])values
4526                             forKeys:(const int32_t [])keys
4527                               count:(NSUInteger)count {
4528  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4529  // on to get the type correct.
4530  return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
4531                                                         forKeys:keys
4532                                                           count:count] autorelease];
4533}
4534
4535+ (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
4536  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4537  // on to get the type correct.
4538  return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4539}
4540
4541+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4542  return [[[self alloc] initWithCapacity:numItems] autorelease];
4543}
4544
4545- (instancetype)init {
4546  return [self initWithValues:NULL forKeys:NULL count:0];
4547}
4548
4549- (instancetype)initWithValues:(const float [])values
4550                       forKeys:(const int32_t [])keys
4551                         count:(NSUInteger)count {
4552  self = [super init];
4553  if (self) {
4554    _dictionary = [[NSMutableDictionary alloc] init];
4555    if (count && values && keys) {
4556      for (NSUInteger i = 0; i < count; ++i) {
4557        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4558      }
4559    }
4560  }
4561  return self;
4562}
4563
4564- (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
4565  self = [self initWithValues:NULL forKeys:NULL count:0];
4566  if (self) {
4567    if (dictionary) {
4568      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4569    }
4570  }
4571  return self;
4572}
4573
4574- (instancetype)initWithCapacity:(NSUInteger)numItems {
4575  #pragma unused(numItems)
4576  return [self initWithValues:NULL forKeys:NULL count:0];
4577}
4578
4579- (void)dealloc {
4580  NSAssert(!_autocreator,
4581           @"%@: Autocreator must be cleared before release, autocreator: %@",
4582           [self class], _autocreator);
4583  [_dictionary release];
4584  [super dealloc];
4585}
4586
4587- (instancetype)copyWithZone:(NSZone *)zone {
4588  return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
4589}
4590
4591- (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
4592  if (self == other) {
4593    return YES;
4594  }
4595  if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
4596    return NO;
4597  }
4598  return [_dictionary isEqual:other->_dictionary];
4599}
4600
4601- (NSUInteger)hash {
4602  return _dictionary.count;
4603}
4604
4605- (NSString *)description {
4606  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4607}
4608
4609- (NSUInteger)count {
4610  return _dictionary.count;
4611}
4612
4613- (void)enumerateKeysAndValuesUsingBlock:
4614    (void (^)(int32_t key, float value, BOOL *stop))block {
4615  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4616                                                   NSNumber *aValue,
4617                                                   BOOL *stop) {
4618      block([aKey intValue], [aValue floatValue], stop);
4619  }];
4620}
4621
4622- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4623  NSUInteger count = _dictionary.count;
4624  if (count == 0) {
4625    return 0;
4626  }
4627
4628  GPBDataType valueDataType = GPBGetFieldDataType(field);
4629  GPBDataType keyDataType = field.mapKeyDataType;
4630  __block size_t result = 0;
4631  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4632                                                   NSNumber *aValue,
4633                                                   BOOL *stop) {
4634    #pragma unused(stop)
4635    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4636    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
4637    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4638  }];
4639  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4640  result += tagSize * count;
4641  return result;
4642}
4643
4644- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4645                         asField:(GPBFieldDescriptor *)field {
4646  GPBDataType valueDataType = GPBGetFieldDataType(field);
4647  GPBDataType keyDataType = field.mapKeyDataType;
4648  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4649  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4650                                                   NSNumber *aValue,
4651                                                   BOOL *stop) {
4652    #pragma unused(stop)
4653    // Write the tag.
4654    [outputStream writeInt32NoTag:tag];
4655    // Write the size of the message.
4656    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4657    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
4658    [outputStream writeInt32NoTag:(int32_t)msgSize];
4659    // Write the fields.
4660    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4661    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
4662  }];
4663}
4664
4665- (void)setGPBGenericValue:(GPBGenericValue *)value
4666     forGPBGenericValueKey:(GPBGenericValue *)key {
4667  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
4668}
4669
4670- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4671  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
4672      #pragma unused(stop)
4673      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
4674  }];
4675}
4676
4677- (BOOL)valueForKey:(int32_t)key value:(float *)value {
4678  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4679  if (wrapped && value) {
4680    *value = [wrapped floatValue];
4681  }
4682  return (wrapped != NULL);
4683}
4684
4685- (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
4686  if (otherDictionary) {
4687    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4688    if (_autocreator) {
4689      GPBAutocreatedDictionaryModified(_autocreator, self);
4690    }
4691  }
4692}
4693
4694- (void)setValue:(float)value forKey:(int32_t)key {
4695  [_dictionary setObject:@(value) forKey:@(key)];
4696  if (_autocreator) {
4697    GPBAutocreatedDictionaryModified(_autocreator, self);
4698  }
4699}
4700
4701- (void)removeValueForKey:(int32_t)aKey {
4702  [_dictionary removeObjectForKey:@(aKey)];
4703}
4704
4705- (void)removeAll {
4706  [_dictionary removeAllObjects];
4707}
4708
4709@end
4710
4711#pragma mark - Int32 -> Double
4712
4713@implementation GPBInt32DoubleDictionary {
4714 @package
4715  NSMutableDictionary *_dictionary;
4716}
4717
4718+ (instancetype)dictionary {
4719  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
4720}
4721
4722+ (instancetype)dictionaryWithValue:(double)value
4723                             forKey:(int32_t)key {
4724  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4725  // on to get the type correct.
4726  return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
4727                                                          forKeys:&key
4728                                                            count:1] autorelease];
4729}
4730
4731+ (instancetype)dictionaryWithValues:(const double [])values
4732                             forKeys:(const int32_t [])keys
4733                               count:(NSUInteger)count {
4734  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4735  // on to get the type correct.
4736  return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
4737                                                          forKeys:keys
4738                                                            count:count] autorelease];
4739}
4740
4741+ (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
4742  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
4743  // on to get the type correct.
4744  return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4745}
4746
4747+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
4748  return [[[self alloc] initWithCapacity:numItems] autorelease];
4749}
4750
4751- (instancetype)init {
4752  return [self initWithValues:NULL forKeys:NULL count:0];
4753}
4754
4755- (instancetype)initWithValues:(const double [])values
4756                       forKeys:(const int32_t [])keys
4757                         count:(NSUInteger)count {
4758  self = [super init];
4759  if (self) {
4760    _dictionary = [[NSMutableDictionary alloc] init];
4761    if (count && values && keys) {
4762      for (NSUInteger i = 0; i < count; ++i) {
4763        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
4764      }
4765    }
4766  }
4767  return self;
4768}
4769
4770- (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
4771  self = [self initWithValues:NULL forKeys:NULL count:0];
4772  if (self) {
4773    if (dictionary) {
4774      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
4775    }
4776  }
4777  return self;
4778}
4779
4780- (instancetype)initWithCapacity:(NSUInteger)numItems {
4781  #pragma unused(numItems)
4782  return [self initWithValues:NULL forKeys:NULL count:0];
4783}
4784
4785- (void)dealloc {
4786  NSAssert(!_autocreator,
4787           @"%@: Autocreator must be cleared before release, autocreator: %@",
4788           [self class], _autocreator);
4789  [_dictionary release];
4790  [super dealloc];
4791}
4792
4793- (instancetype)copyWithZone:(NSZone *)zone {
4794  return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
4795}
4796
4797- (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
4798  if (self == other) {
4799    return YES;
4800  }
4801  if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
4802    return NO;
4803  }
4804  return [_dictionary isEqual:other->_dictionary];
4805}
4806
4807- (NSUInteger)hash {
4808  return _dictionary.count;
4809}
4810
4811- (NSString *)description {
4812  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
4813}
4814
4815- (NSUInteger)count {
4816  return _dictionary.count;
4817}
4818
4819- (void)enumerateKeysAndValuesUsingBlock:
4820    (void (^)(int32_t key, double value, BOOL *stop))block {
4821  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4822                                                   NSNumber *aValue,
4823                                                   BOOL *stop) {
4824      block([aKey intValue], [aValue doubleValue], stop);
4825  }];
4826}
4827
4828- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
4829  NSUInteger count = _dictionary.count;
4830  if (count == 0) {
4831    return 0;
4832  }
4833
4834  GPBDataType valueDataType = GPBGetFieldDataType(field);
4835  GPBDataType keyDataType = field.mapKeyDataType;
4836  __block size_t result = 0;
4837  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4838                                                   NSNumber *aValue,
4839                                                   BOOL *stop) {
4840    #pragma unused(stop)
4841    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4842    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
4843    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
4844  }];
4845  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
4846  result += tagSize * count;
4847  return result;
4848}
4849
4850- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
4851                         asField:(GPBFieldDescriptor *)field {
4852  GPBDataType valueDataType = GPBGetFieldDataType(field);
4853  GPBDataType keyDataType = field.mapKeyDataType;
4854  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
4855  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
4856                                                   NSNumber *aValue,
4857                                                   BOOL *stop) {
4858    #pragma unused(stop)
4859    // Write the tag.
4860    [outputStream writeInt32NoTag:tag];
4861    // Write the size of the message.
4862    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
4863    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
4864    [outputStream writeInt32NoTag:(int32_t)msgSize];
4865    // Write the fields.
4866    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
4867    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
4868  }];
4869}
4870
4871- (void)setGPBGenericValue:(GPBGenericValue *)value
4872     forGPBGenericValueKey:(GPBGenericValue *)key {
4873  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
4874}
4875
4876- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
4877  [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
4878      #pragma unused(stop)
4879      block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
4880  }];
4881}
4882
4883- (BOOL)valueForKey:(int32_t)key value:(double *)value {
4884  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
4885  if (wrapped && value) {
4886    *value = [wrapped doubleValue];
4887  }
4888  return (wrapped != NULL);
4889}
4890
4891- (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
4892  if (otherDictionary) {
4893    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
4894    if (_autocreator) {
4895      GPBAutocreatedDictionaryModified(_autocreator, self);
4896    }
4897  }
4898}
4899
4900- (void)setValue:(double)value forKey:(int32_t)key {
4901  [_dictionary setObject:@(value) forKey:@(key)];
4902  if (_autocreator) {
4903    GPBAutocreatedDictionaryModified(_autocreator, self);
4904  }
4905}
4906
4907- (void)removeValueForKey:(int32_t)aKey {
4908  [_dictionary removeObjectForKey:@(aKey)];
4909}
4910
4911- (void)removeAll {
4912  [_dictionary removeAllObjects];
4913}
4914
4915@end
4916
4917#pragma mark - Int32 -> Enum
4918
4919@implementation GPBInt32EnumDictionary {
4920 @package
4921  NSMutableDictionary *_dictionary;
4922  GPBEnumValidationFunc _validationFunc;
4923}
4924
4925@synthesize validationFunc = _validationFunc;
4926
4927+ (instancetype)dictionary {
4928  return [[[self alloc] initWithValidationFunction:NULL
4929                                         rawValues:NULL
4930                                           forKeys:NULL
4931                                             count:0] autorelease];
4932}
4933
4934+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
4935  return [[[self alloc] initWithValidationFunction:func
4936                                         rawValues:NULL
4937                                           forKeys:NULL
4938                                             count:0] autorelease];
4939}
4940
4941+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
4942                                        rawValue:(int32_t)rawValue
4943                                          forKey:(int32_t)key {
4944  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4945  // on to get the type correct.
4946  return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
4947                                                                  rawValues:&rawValue
4948                                                                    forKeys:&key
4949                                                                      count:1] autorelease];
4950}
4951
4952+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
4953                                       rawValues:(const int32_t [])rawValues
4954                                         forKeys:(const int32_t [])keys
4955                                           count:(NSUInteger)count {
4956  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4957  // on to get the type correct.
4958  return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
4959                                                                  rawValues:rawValues
4960                                                                    forKeys:keys
4961                                                                      count:count] autorelease];
4962}
4963
4964+ (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
4965  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
4966  // on to get the type correct.
4967  return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
4968}
4969
4970+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
4971                                        capacity:(NSUInteger)numItems {
4972  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
4973}
4974
4975- (instancetype)init {
4976  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
4977}
4978
4979- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
4980  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
4981}
4982
4983- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
4984                                 rawValues:(const int32_t [])rawValues
4985                                   forKeys:(const int32_t [])keys
4986                                     count:(NSUInteger)count {
4987  self = [super init];
4988  if (self) {
4989    _dictionary = [[NSMutableDictionary alloc] init];
4990    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
4991    if (count && rawValues && keys) {
4992      for (NSUInteger i = 0; i < count; ++i) {
4993        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
4994      }
4995    }
4996  }
4997  return self;
4998}
4999
5000- (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
5001  self = [self initWithValidationFunction:dictionary.validationFunc
5002                                rawValues:NULL
5003                                  forKeys:NULL
5004                                    count:0];
5005  if (self) {
5006    if (dictionary) {
5007      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5008    }
5009  }
5010  return self;
5011}
5012
5013- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
5014                                  capacity:(NSUInteger)numItems {
5015  #pragma unused(numItems)
5016  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
5017}
5018
5019- (void)dealloc {
5020  NSAssert(!_autocreator,
5021           @"%@: Autocreator must be cleared before release, autocreator: %@",
5022           [self class], _autocreator);
5023  [_dictionary release];
5024  [super dealloc];
5025}
5026
5027- (instancetype)copyWithZone:(NSZone *)zone {
5028  return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
5029}
5030
5031- (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
5032  if (self == other) {
5033    return YES;
5034  }
5035  if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
5036    return NO;
5037  }
5038  return [_dictionary isEqual:other->_dictionary];
5039}
5040
5041- (NSUInteger)hash {
5042  return _dictionary.count;
5043}
5044
5045- (NSString *)description {
5046  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5047}
5048
5049- (NSUInteger)count {
5050  return _dictionary.count;
5051}
5052
5053- (void)enumerateKeysAndRawValuesUsingBlock:
5054    (void (^)(int32_t key, int32_t value, BOOL *stop))block {
5055  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5056                                                   NSNumber *aValue,
5057                                                   BOOL *stop) {
5058      block([aKey intValue], [aValue intValue], stop);
5059  }];
5060}
5061
5062- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5063  NSUInteger count = _dictionary.count;
5064  if (count == 0) {
5065    return 0;
5066  }
5067
5068  GPBDataType valueDataType = GPBGetFieldDataType(field);
5069  GPBDataType keyDataType = field.mapKeyDataType;
5070  __block size_t result = 0;
5071  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5072                                                   NSNumber *aValue,
5073                                                   BOOL *stop) {
5074    #pragma unused(stop)
5075    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5076    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5077    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5078  }];
5079  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5080  result += tagSize * count;
5081  return result;
5082}
5083
5084- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5085                         asField:(GPBFieldDescriptor *)field {
5086  GPBDataType valueDataType = GPBGetFieldDataType(field);
5087  GPBDataType keyDataType = field.mapKeyDataType;
5088  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5089  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5090                                                   NSNumber *aValue,
5091                                                   BOOL *stop) {
5092    #pragma unused(stop)
5093    // Write the tag.
5094    [outputStream writeInt32NoTag:tag];
5095    // Write the size of the message.
5096    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5097    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5098    [outputStream writeInt32NoTag:(int32_t)msgSize];
5099    // Write the fields.
5100    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
5101    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
5102  }];
5103}
5104
5105- (NSData *)serializedDataForUnknownValue:(int32_t)value
5106                                   forKey:(GPBGenericValue *)key
5107                              keyDataType:(GPBDataType)keyDataType {
5108  size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
5109  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
5110  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
5111  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
5112  WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
5113  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
5114  [outputStream release];
5115  return data;
5116}
5117- (void)setGPBGenericValue:(GPBGenericValue *)value
5118     forGPBGenericValueKey:(GPBGenericValue *)key {
5119  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
5120}
5121
5122- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5123  [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
5124      #pragma unused(stop)
5125      block([NSString stringWithFormat:@"%d", key], @(value));
5126  }];
5127}
5128
5129- (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
5130  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5131  if (wrapped && value) {
5132    int32_t result = [wrapped intValue];
5133    if (!_validationFunc(result)) {
5134      result = kGPBUnrecognizedEnumeratorValue;
5135    }
5136    *value = result;
5137  }
5138  return (wrapped != NULL);
5139}
5140
5141- (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
5142  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5143  if (wrapped && rawValue) {
5144    *rawValue = [wrapped intValue];
5145  }
5146  return (wrapped != NULL);
5147}
5148
5149- (void)enumerateKeysAndValuesUsingBlock:
5150    (void (^)(int32_t key, int32_t value, BOOL *stop))block {
5151  GPBEnumValidationFunc func = _validationFunc;
5152  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5153                                                   NSNumber *aValue,
5154                                                   BOOL *stop) {
5155      int32_t unwrapped = [aValue intValue];
5156      if (!func(unwrapped)) {
5157        unwrapped = kGPBUnrecognizedEnumeratorValue;
5158      }
5159      block([aKey intValue], unwrapped, stop);
5160  }];
5161}
5162
5163- (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
5164  if (otherDictionary) {
5165    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5166    if (_autocreator) {
5167      GPBAutocreatedDictionaryModified(_autocreator, self);
5168    }
5169  }
5170}
5171
5172- (void)setRawValue:(int32_t)value forKey:(int32_t)key {
5173  [_dictionary setObject:@(value) forKey:@(key)];
5174  if (_autocreator) {
5175    GPBAutocreatedDictionaryModified(_autocreator, self);
5176  }
5177}
5178
5179- (void)removeValueForKey:(int32_t)aKey {
5180  [_dictionary removeObjectForKey:@(aKey)];
5181}
5182
5183- (void)removeAll {
5184  [_dictionary removeAllObjects];
5185}
5186
5187- (void)setValue:(int32_t)value forKey:(int32_t)key {
5188  if (!_validationFunc(value)) {
5189    [NSException raise:NSInvalidArgumentException
5190                format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
5191                       value];
5192  }
5193
5194  [_dictionary setObject:@(value) forKey:@(key)];
5195  if (_autocreator) {
5196    GPBAutocreatedDictionaryModified(_autocreator, self);
5197  }
5198}
5199
5200@end
5201
5202#pragma mark - Int32 -> Object
5203
5204@implementation GPBInt32ObjectDictionary {
5205 @package
5206  NSMutableDictionary *_dictionary;
5207}
5208
5209+ (instancetype)dictionary {
5210  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
5211}
5212
5213+ (instancetype)dictionaryWithObject:(id)object
5214                              forKey:(int32_t)key {
5215  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
5216  // on to get the type correct.
5217  return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
5218                                                           forKeys:&key
5219                                                             count:1] autorelease];
5220}
5221
5222+ (instancetype)dictionaryWithObjects:(const id [])objects
5223                              forKeys:(const int32_t [])keys
5224                                count:(NSUInteger)count {
5225  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
5226  // on to get the type correct.
5227  return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
5228                                                          forKeys:keys
5229                                                            count:count] autorelease];
5230}
5231
5232+ (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
5233  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5234  // on to get the type correct.
5235  return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5236}
5237
5238+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5239  return [[[self alloc] initWithCapacity:numItems] autorelease];
5240}
5241
5242- (instancetype)init {
5243  return [self initWithObjects:NULL forKeys:NULL count:0];
5244}
5245
5246- (instancetype)initWithObjects:(const id [])objects
5247                        forKeys:(const int32_t [])keys
5248                          count:(NSUInteger)count {
5249  self = [super init];
5250  if (self) {
5251    _dictionary = [[NSMutableDictionary alloc] init];
5252    if (count && objects && keys) {
5253      for (NSUInteger i = 0; i < count; ++i) {
5254        if (!objects[i]) {
5255          [NSException raise:NSInvalidArgumentException
5256                      format:@"Attempting to add nil object to a Dictionary"];
5257        }
5258        [_dictionary setObject:objects[i] forKey:@(keys[i])];
5259      }
5260    }
5261  }
5262  return self;
5263}
5264
5265- (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
5266  self = [self initWithObjects:NULL forKeys:NULL count:0];
5267  if (self) {
5268    if (dictionary) {
5269      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5270    }
5271  }
5272  return self;
5273}
5274
5275- (instancetype)initWithCapacity:(NSUInteger)numItems {
5276  #pragma unused(numItems)
5277  return [self initWithObjects:NULL forKeys:NULL count:0];
5278}
5279
5280- (void)dealloc {
5281  NSAssert(!_autocreator,
5282           @"%@: Autocreator must be cleared before release, autocreator: %@",
5283           [self class], _autocreator);
5284  [_dictionary release];
5285  [super dealloc];
5286}
5287
5288- (instancetype)copyWithZone:(NSZone *)zone {
5289  return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
5290}
5291
5292- (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
5293  if (self == other) {
5294    return YES;
5295  }
5296  if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
5297    return NO;
5298  }
5299  return [_dictionary isEqual:other->_dictionary];
5300}
5301
5302- (NSUInteger)hash {
5303  return _dictionary.count;
5304}
5305
5306- (NSString *)description {
5307  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5308}
5309
5310- (NSUInteger)count {
5311  return _dictionary.count;
5312}
5313
5314- (void)enumerateKeysAndObjectsUsingBlock:
5315    (void (^)(int32_t key, id object, BOOL *stop))block {
5316  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5317                                                   id aObject,
5318                                                   BOOL *stop) {
5319      block([aKey intValue], aObject, stop);
5320  }];
5321}
5322
5323- (BOOL)isInitialized {
5324  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
5325    if (!msg.initialized) {
5326      return NO;
5327    }
5328  }
5329  return YES;
5330}
5331
5332- (instancetype)deepCopyWithZone:(NSZone *)zone {
5333  GPBInt32ObjectDictionary *newDict =
5334      [[GPBInt32ObjectDictionary alloc] init];
5335  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
5336                                                   GPBMessage *msg,
5337                                                   BOOL *stop) {
5338    #pragma unused(stop)
5339    GPBMessage *copiedMsg = [msg copyWithZone:zone];
5340    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
5341    [copiedMsg release];
5342  }];
5343  return newDict;
5344}
5345
5346- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5347  NSUInteger count = _dictionary.count;
5348  if (count == 0) {
5349    return 0;
5350  }
5351
5352  GPBDataType valueDataType = GPBGetFieldDataType(field);
5353  GPBDataType keyDataType = field.mapKeyDataType;
5354  __block size_t result = 0;
5355  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5356                                                   id aObject,
5357                                                   BOOL *stop) {
5358    #pragma unused(stop)
5359    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5360    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
5361    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5362  }];
5363  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5364  result += tagSize * count;
5365  return result;
5366}
5367
5368- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5369                         asField:(GPBFieldDescriptor *)field {
5370  GPBDataType valueDataType = GPBGetFieldDataType(field);
5371  GPBDataType keyDataType = field.mapKeyDataType;
5372  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5373  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5374                                                   id aObject,
5375                                                   BOOL *stop) {
5376    #pragma unused(stop)
5377    // Write the tag.
5378    [outputStream writeInt32NoTag:tag];
5379    // Write the size of the message.
5380    size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
5381    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
5382    [outputStream writeInt32NoTag:(int32_t)msgSize];
5383    // Write the fields.
5384    WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
5385    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
5386  }];
5387}
5388
5389- (void)setGPBGenericValue:(GPBGenericValue *)value
5390     forGPBGenericValueKey:(GPBGenericValue *)key {
5391  [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
5392}
5393
5394- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5395  [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
5396      #pragma unused(stop)
5397      block([NSString stringWithFormat:@"%d", key], object);
5398  }];
5399}
5400
5401- (id)objectForKey:(int32_t)key {
5402  id result = [_dictionary objectForKey:@(key)];
5403  return result;
5404}
5405
5406- (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
5407  if (otherDictionary) {
5408    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5409    if (_autocreator) {
5410      GPBAutocreatedDictionaryModified(_autocreator, self);
5411    }
5412  }
5413}
5414
5415- (void)setObject:(id)object forKey:(int32_t)key {
5416  if (!object) {
5417    [NSException raise:NSInvalidArgumentException
5418                format:@"Attempting to add nil object to a Dictionary"];
5419  }
5420  [_dictionary setObject:object forKey:@(key)];
5421  if (_autocreator) {
5422    GPBAutocreatedDictionaryModified(_autocreator, self);
5423  }
5424}
5425
5426- (void)removeObjectForKey:(int32_t)aKey {
5427  [_dictionary removeObjectForKey:@(aKey)];
5428}
5429
5430- (void)removeAll {
5431  [_dictionary removeAllObjects];
5432}
5433
5434@end
5435
5436//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
5437// This block of code is generated, do not edit it directly.
5438
5439#pragma mark - UInt64 -> UInt32
5440
5441@implementation GPBUInt64UInt32Dictionary {
5442 @package
5443  NSMutableDictionary *_dictionary;
5444}
5445
5446+ (instancetype)dictionary {
5447  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
5448}
5449
5450+ (instancetype)dictionaryWithValue:(uint32_t)value
5451                             forKey:(uint64_t)key {
5452  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
5453  // on to get the type correct.
5454  return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
5455                                                           forKeys:&key
5456                                                             count:1] autorelease];
5457}
5458
5459+ (instancetype)dictionaryWithValues:(const uint32_t [])values
5460                             forKeys:(const uint64_t [])keys
5461                               count:(NSUInteger)count {
5462  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
5463  // on to get the type correct.
5464  return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
5465                                                           forKeys:keys
5466                                                             count:count] autorelease];
5467}
5468
5469+ (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
5470  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5471  // on to get the type correct.
5472  return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5473}
5474
5475+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5476  return [[[self alloc] initWithCapacity:numItems] autorelease];
5477}
5478
5479- (instancetype)init {
5480  return [self initWithValues:NULL forKeys:NULL count:0];
5481}
5482
5483- (instancetype)initWithValues:(const uint32_t [])values
5484                       forKeys:(const uint64_t [])keys
5485                         count:(NSUInteger)count {
5486  self = [super init];
5487  if (self) {
5488    _dictionary = [[NSMutableDictionary alloc] init];
5489    if (count && values && keys) {
5490      for (NSUInteger i = 0; i < count; ++i) {
5491        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
5492      }
5493    }
5494  }
5495  return self;
5496}
5497
5498- (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
5499  self = [self initWithValues:NULL forKeys:NULL count:0];
5500  if (self) {
5501    if (dictionary) {
5502      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5503    }
5504  }
5505  return self;
5506}
5507
5508- (instancetype)initWithCapacity:(NSUInteger)numItems {
5509  #pragma unused(numItems)
5510  return [self initWithValues:NULL forKeys:NULL count:0];
5511}
5512
5513- (void)dealloc {
5514  NSAssert(!_autocreator,
5515           @"%@: Autocreator must be cleared before release, autocreator: %@",
5516           [self class], _autocreator);
5517  [_dictionary release];
5518  [super dealloc];
5519}
5520
5521- (instancetype)copyWithZone:(NSZone *)zone {
5522  return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
5523}
5524
5525- (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
5526  if (self == other) {
5527    return YES;
5528  }
5529  if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
5530    return NO;
5531  }
5532  return [_dictionary isEqual:other->_dictionary];
5533}
5534
5535- (NSUInteger)hash {
5536  return _dictionary.count;
5537}
5538
5539- (NSString *)description {
5540  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5541}
5542
5543- (NSUInteger)count {
5544  return _dictionary.count;
5545}
5546
5547- (void)enumerateKeysAndValuesUsingBlock:
5548    (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
5549  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5550                                                   NSNumber *aValue,
5551                                                   BOOL *stop) {
5552      block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
5553  }];
5554}
5555
5556- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5557  NSUInteger count = _dictionary.count;
5558  if (count == 0) {
5559    return 0;
5560  }
5561
5562  GPBDataType valueDataType = GPBGetFieldDataType(field);
5563  GPBDataType keyDataType = field.mapKeyDataType;
5564  __block size_t result = 0;
5565  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5566                                                   NSNumber *aValue,
5567                                                   BOOL *stop) {
5568    #pragma unused(stop)
5569    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5570    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
5571    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5572  }];
5573  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5574  result += tagSize * count;
5575  return result;
5576}
5577
5578- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5579                         asField:(GPBFieldDescriptor *)field {
5580  GPBDataType valueDataType = GPBGetFieldDataType(field);
5581  GPBDataType keyDataType = field.mapKeyDataType;
5582  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5583  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5584                                                   NSNumber *aValue,
5585                                                   BOOL *stop) {
5586    #pragma unused(stop)
5587    // Write the tag.
5588    [outputStream writeInt32NoTag:tag];
5589    // Write the size of the message.
5590    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5591    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
5592    [outputStream writeInt32NoTag:(int32_t)msgSize];
5593    // Write the fields.
5594    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5595    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
5596  }];
5597}
5598
5599- (void)setGPBGenericValue:(GPBGenericValue *)value
5600     forGPBGenericValueKey:(GPBGenericValue *)key {
5601  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
5602}
5603
5604- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5605  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
5606      #pragma unused(stop)
5607      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
5608  }];
5609}
5610
5611- (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
5612  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5613  if (wrapped && value) {
5614    *value = [wrapped unsignedIntValue];
5615  }
5616  return (wrapped != NULL);
5617}
5618
5619- (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
5620  if (otherDictionary) {
5621    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5622    if (_autocreator) {
5623      GPBAutocreatedDictionaryModified(_autocreator, self);
5624    }
5625  }
5626}
5627
5628- (void)setValue:(uint32_t)value forKey:(uint64_t)key {
5629  [_dictionary setObject:@(value) forKey:@(key)];
5630  if (_autocreator) {
5631    GPBAutocreatedDictionaryModified(_autocreator, self);
5632  }
5633}
5634
5635- (void)removeValueForKey:(uint64_t)aKey {
5636  [_dictionary removeObjectForKey:@(aKey)];
5637}
5638
5639- (void)removeAll {
5640  [_dictionary removeAllObjects];
5641}
5642
5643@end
5644
5645#pragma mark - UInt64 -> Int32
5646
5647@implementation GPBUInt64Int32Dictionary {
5648 @package
5649  NSMutableDictionary *_dictionary;
5650}
5651
5652+ (instancetype)dictionary {
5653  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
5654}
5655
5656+ (instancetype)dictionaryWithValue:(int32_t)value
5657                             forKey:(uint64_t)key {
5658  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
5659  // on to get the type correct.
5660  return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
5661                                                          forKeys:&key
5662                                                            count:1] autorelease];
5663}
5664
5665+ (instancetype)dictionaryWithValues:(const int32_t [])values
5666                             forKeys:(const uint64_t [])keys
5667                               count:(NSUInteger)count {
5668  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
5669  // on to get the type correct.
5670  return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
5671                                                          forKeys:keys
5672                                                            count:count] autorelease];
5673}
5674
5675+ (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
5676  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5677  // on to get the type correct.
5678  return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5679}
5680
5681+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5682  return [[[self alloc] initWithCapacity:numItems] autorelease];
5683}
5684
5685- (instancetype)init {
5686  return [self initWithValues:NULL forKeys:NULL count:0];
5687}
5688
5689- (instancetype)initWithValues:(const int32_t [])values
5690                       forKeys:(const uint64_t [])keys
5691                         count:(NSUInteger)count {
5692  self = [super init];
5693  if (self) {
5694    _dictionary = [[NSMutableDictionary alloc] init];
5695    if (count && values && keys) {
5696      for (NSUInteger i = 0; i < count; ++i) {
5697        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
5698      }
5699    }
5700  }
5701  return self;
5702}
5703
5704- (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
5705  self = [self initWithValues:NULL forKeys:NULL count:0];
5706  if (self) {
5707    if (dictionary) {
5708      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5709    }
5710  }
5711  return self;
5712}
5713
5714- (instancetype)initWithCapacity:(NSUInteger)numItems {
5715  #pragma unused(numItems)
5716  return [self initWithValues:NULL forKeys:NULL count:0];
5717}
5718
5719- (void)dealloc {
5720  NSAssert(!_autocreator,
5721           @"%@: Autocreator must be cleared before release, autocreator: %@",
5722           [self class], _autocreator);
5723  [_dictionary release];
5724  [super dealloc];
5725}
5726
5727- (instancetype)copyWithZone:(NSZone *)zone {
5728  return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
5729}
5730
5731- (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
5732  if (self == other) {
5733    return YES;
5734  }
5735  if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
5736    return NO;
5737  }
5738  return [_dictionary isEqual:other->_dictionary];
5739}
5740
5741- (NSUInteger)hash {
5742  return _dictionary.count;
5743}
5744
5745- (NSString *)description {
5746  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5747}
5748
5749- (NSUInteger)count {
5750  return _dictionary.count;
5751}
5752
5753- (void)enumerateKeysAndValuesUsingBlock:
5754    (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
5755  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5756                                                   NSNumber *aValue,
5757                                                   BOOL *stop) {
5758      block([aKey unsignedLongLongValue], [aValue intValue], stop);
5759  }];
5760}
5761
5762- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5763  NSUInteger count = _dictionary.count;
5764  if (count == 0) {
5765    return 0;
5766  }
5767
5768  GPBDataType valueDataType = GPBGetFieldDataType(field);
5769  GPBDataType keyDataType = field.mapKeyDataType;
5770  __block size_t result = 0;
5771  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5772                                                   NSNumber *aValue,
5773                                                   BOOL *stop) {
5774    #pragma unused(stop)
5775    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5776    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5777    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5778  }];
5779  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5780  result += tagSize * count;
5781  return result;
5782}
5783
5784- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5785                         asField:(GPBFieldDescriptor *)field {
5786  GPBDataType valueDataType = GPBGetFieldDataType(field);
5787  GPBDataType keyDataType = field.mapKeyDataType;
5788  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5789  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5790                                                   NSNumber *aValue,
5791                                                   BOOL *stop) {
5792    #pragma unused(stop)
5793    // Write the tag.
5794    [outputStream writeInt32NoTag:tag];
5795    // Write the size of the message.
5796    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5797    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
5798    [outputStream writeInt32NoTag:(int32_t)msgSize];
5799    // Write the fields.
5800    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5801    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
5802  }];
5803}
5804
5805- (void)setGPBGenericValue:(GPBGenericValue *)value
5806     forGPBGenericValueKey:(GPBGenericValue *)key {
5807  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
5808}
5809
5810- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
5811  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
5812      #pragma unused(stop)
5813      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
5814  }];
5815}
5816
5817- (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
5818  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
5819  if (wrapped && value) {
5820    *value = [wrapped intValue];
5821  }
5822  return (wrapped != NULL);
5823}
5824
5825- (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
5826  if (otherDictionary) {
5827    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
5828    if (_autocreator) {
5829      GPBAutocreatedDictionaryModified(_autocreator, self);
5830    }
5831  }
5832}
5833
5834- (void)setValue:(int32_t)value forKey:(uint64_t)key {
5835  [_dictionary setObject:@(value) forKey:@(key)];
5836  if (_autocreator) {
5837    GPBAutocreatedDictionaryModified(_autocreator, self);
5838  }
5839}
5840
5841- (void)removeValueForKey:(uint64_t)aKey {
5842  [_dictionary removeObjectForKey:@(aKey)];
5843}
5844
5845- (void)removeAll {
5846  [_dictionary removeAllObjects];
5847}
5848
5849@end
5850
5851#pragma mark - UInt64 -> UInt64
5852
5853@implementation GPBUInt64UInt64Dictionary {
5854 @package
5855  NSMutableDictionary *_dictionary;
5856}
5857
5858+ (instancetype)dictionary {
5859  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
5860}
5861
5862+ (instancetype)dictionaryWithValue:(uint64_t)value
5863                             forKey:(uint64_t)key {
5864  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
5865  // on to get the type correct.
5866  return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
5867                                                           forKeys:&key
5868                                                             count:1] autorelease];
5869}
5870
5871+ (instancetype)dictionaryWithValues:(const uint64_t [])values
5872                             forKeys:(const uint64_t [])keys
5873                               count:(NSUInteger)count {
5874  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
5875  // on to get the type correct.
5876  return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
5877                                                           forKeys:keys
5878                                                             count:count] autorelease];
5879}
5880
5881+ (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
5882  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
5883  // on to get the type correct.
5884  return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
5885}
5886
5887+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
5888  return [[[self alloc] initWithCapacity:numItems] autorelease];
5889}
5890
5891- (instancetype)init {
5892  return [self initWithValues:NULL forKeys:NULL count:0];
5893}
5894
5895- (instancetype)initWithValues:(const uint64_t [])values
5896                       forKeys:(const uint64_t [])keys
5897                         count:(NSUInteger)count {
5898  self = [super init];
5899  if (self) {
5900    _dictionary = [[NSMutableDictionary alloc] init];
5901    if (count && values && keys) {
5902      for (NSUInteger i = 0; i < count; ++i) {
5903        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
5904      }
5905    }
5906  }
5907  return self;
5908}
5909
5910- (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
5911  self = [self initWithValues:NULL forKeys:NULL count:0];
5912  if (self) {
5913    if (dictionary) {
5914      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
5915    }
5916  }
5917  return self;
5918}
5919
5920- (instancetype)initWithCapacity:(NSUInteger)numItems {
5921  #pragma unused(numItems)
5922  return [self initWithValues:NULL forKeys:NULL count:0];
5923}
5924
5925- (void)dealloc {
5926  NSAssert(!_autocreator,
5927           @"%@: Autocreator must be cleared before release, autocreator: %@",
5928           [self class], _autocreator);
5929  [_dictionary release];
5930  [super dealloc];
5931}
5932
5933- (instancetype)copyWithZone:(NSZone *)zone {
5934  return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
5935}
5936
5937- (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
5938  if (self == other) {
5939    return YES;
5940  }
5941  if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
5942    return NO;
5943  }
5944  return [_dictionary isEqual:other->_dictionary];
5945}
5946
5947- (NSUInteger)hash {
5948  return _dictionary.count;
5949}
5950
5951- (NSString *)description {
5952  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
5953}
5954
5955- (NSUInteger)count {
5956  return _dictionary.count;
5957}
5958
5959- (void)enumerateKeysAndValuesUsingBlock:
5960    (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
5961  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5962                                                   NSNumber *aValue,
5963                                                   BOOL *stop) {
5964      block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
5965  }];
5966}
5967
5968- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
5969  NSUInteger count = _dictionary.count;
5970  if (count == 0) {
5971    return 0;
5972  }
5973
5974  GPBDataType valueDataType = GPBGetFieldDataType(field);
5975  GPBDataType keyDataType = field.mapKeyDataType;
5976  __block size_t result = 0;
5977  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5978                                                   NSNumber *aValue,
5979                                                   BOOL *stop) {
5980    #pragma unused(stop)
5981    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
5982    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
5983    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
5984  }];
5985  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
5986  result += tagSize * count;
5987  return result;
5988}
5989
5990- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
5991                         asField:(GPBFieldDescriptor *)field {
5992  GPBDataType valueDataType = GPBGetFieldDataType(field);
5993  GPBDataType keyDataType = field.mapKeyDataType;
5994  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
5995  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
5996                                                   NSNumber *aValue,
5997                                                   BOOL *stop) {
5998    #pragma unused(stop)
5999    // Write the tag.
6000    [outputStream writeInt32NoTag:tag];
6001    // Write the size of the message.
6002    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6003    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
6004    [outputStream writeInt32NoTag:(int32_t)msgSize];
6005    // Write the fields.
6006    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6007    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
6008  }];
6009}
6010
6011- (void)setGPBGenericValue:(GPBGenericValue *)value
6012     forGPBGenericValueKey:(GPBGenericValue *)key {
6013  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
6014}
6015
6016- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6017  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
6018      #pragma unused(stop)
6019      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
6020  }];
6021}
6022
6023- (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
6024  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6025  if (wrapped && value) {
6026    *value = [wrapped unsignedLongLongValue];
6027  }
6028  return (wrapped != NULL);
6029}
6030
6031- (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
6032  if (otherDictionary) {
6033    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6034    if (_autocreator) {
6035      GPBAutocreatedDictionaryModified(_autocreator, self);
6036    }
6037  }
6038}
6039
6040- (void)setValue:(uint64_t)value forKey:(uint64_t)key {
6041  [_dictionary setObject:@(value) forKey:@(key)];
6042  if (_autocreator) {
6043    GPBAutocreatedDictionaryModified(_autocreator, self);
6044  }
6045}
6046
6047- (void)removeValueForKey:(uint64_t)aKey {
6048  [_dictionary removeObjectForKey:@(aKey)];
6049}
6050
6051- (void)removeAll {
6052  [_dictionary removeAllObjects];
6053}
6054
6055@end
6056
6057#pragma mark - UInt64 -> Int64
6058
6059@implementation GPBUInt64Int64Dictionary {
6060 @package
6061  NSMutableDictionary *_dictionary;
6062}
6063
6064+ (instancetype)dictionary {
6065  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
6066}
6067
6068+ (instancetype)dictionaryWithValue:(int64_t)value
6069                             forKey:(uint64_t)key {
6070  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6071  // on to get the type correct.
6072  return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
6073                                                          forKeys:&key
6074                                                            count:1] autorelease];
6075}
6076
6077+ (instancetype)dictionaryWithValues:(const int64_t [])values
6078                             forKeys:(const uint64_t [])keys
6079                               count:(NSUInteger)count {
6080  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6081  // on to get the type correct.
6082  return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
6083                                                          forKeys:keys
6084                                                            count:count] autorelease];
6085}
6086
6087+ (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
6088  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6089  // on to get the type correct.
6090  return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6091}
6092
6093+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6094  return [[[self alloc] initWithCapacity:numItems] autorelease];
6095}
6096
6097- (instancetype)init {
6098  return [self initWithValues:NULL forKeys:NULL count:0];
6099}
6100
6101- (instancetype)initWithValues:(const int64_t [])values
6102                       forKeys:(const uint64_t [])keys
6103                         count:(NSUInteger)count {
6104  self = [super init];
6105  if (self) {
6106    _dictionary = [[NSMutableDictionary alloc] init];
6107    if (count && values && keys) {
6108      for (NSUInteger i = 0; i < count; ++i) {
6109        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6110      }
6111    }
6112  }
6113  return self;
6114}
6115
6116- (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
6117  self = [self initWithValues:NULL forKeys:NULL count:0];
6118  if (self) {
6119    if (dictionary) {
6120      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6121    }
6122  }
6123  return self;
6124}
6125
6126- (instancetype)initWithCapacity:(NSUInteger)numItems {
6127  #pragma unused(numItems)
6128  return [self initWithValues:NULL forKeys:NULL count:0];
6129}
6130
6131- (void)dealloc {
6132  NSAssert(!_autocreator,
6133           @"%@: Autocreator must be cleared before release, autocreator: %@",
6134           [self class], _autocreator);
6135  [_dictionary release];
6136  [super dealloc];
6137}
6138
6139- (instancetype)copyWithZone:(NSZone *)zone {
6140  return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
6141}
6142
6143- (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
6144  if (self == other) {
6145    return YES;
6146  }
6147  if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
6148    return NO;
6149  }
6150  return [_dictionary isEqual:other->_dictionary];
6151}
6152
6153- (NSUInteger)hash {
6154  return _dictionary.count;
6155}
6156
6157- (NSString *)description {
6158  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6159}
6160
6161- (NSUInteger)count {
6162  return _dictionary.count;
6163}
6164
6165- (void)enumerateKeysAndValuesUsingBlock:
6166    (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
6167  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6168                                                   NSNumber *aValue,
6169                                                   BOOL *stop) {
6170      block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
6171  }];
6172}
6173
6174- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6175  NSUInteger count = _dictionary.count;
6176  if (count == 0) {
6177    return 0;
6178  }
6179
6180  GPBDataType valueDataType = GPBGetFieldDataType(field);
6181  GPBDataType keyDataType = field.mapKeyDataType;
6182  __block size_t result = 0;
6183  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6184                                                   NSNumber *aValue,
6185                                                   BOOL *stop) {
6186    #pragma unused(stop)
6187    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6188    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
6189    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6190  }];
6191  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6192  result += tagSize * count;
6193  return result;
6194}
6195
6196- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6197                         asField:(GPBFieldDescriptor *)field {
6198  GPBDataType valueDataType = GPBGetFieldDataType(field);
6199  GPBDataType keyDataType = field.mapKeyDataType;
6200  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6201  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6202                                                   NSNumber *aValue,
6203                                                   BOOL *stop) {
6204    #pragma unused(stop)
6205    // Write the tag.
6206    [outputStream writeInt32NoTag:tag];
6207    // Write the size of the message.
6208    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6209    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
6210    [outputStream writeInt32NoTag:(int32_t)msgSize];
6211    // Write the fields.
6212    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6213    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
6214  }];
6215}
6216
6217- (void)setGPBGenericValue:(GPBGenericValue *)value
6218     forGPBGenericValueKey:(GPBGenericValue *)key {
6219  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
6220}
6221
6222- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6223  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
6224      #pragma unused(stop)
6225      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
6226  }];
6227}
6228
6229- (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
6230  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6231  if (wrapped && value) {
6232    *value = [wrapped longLongValue];
6233  }
6234  return (wrapped != NULL);
6235}
6236
6237- (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
6238  if (otherDictionary) {
6239    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6240    if (_autocreator) {
6241      GPBAutocreatedDictionaryModified(_autocreator, self);
6242    }
6243  }
6244}
6245
6246- (void)setValue:(int64_t)value forKey:(uint64_t)key {
6247  [_dictionary setObject:@(value) forKey:@(key)];
6248  if (_autocreator) {
6249    GPBAutocreatedDictionaryModified(_autocreator, self);
6250  }
6251}
6252
6253- (void)removeValueForKey:(uint64_t)aKey {
6254  [_dictionary removeObjectForKey:@(aKey)];
6255}
6256
6257- (void)removeAll {
6258  [_dictionary removeAllObjects];
6259}
6260
6261@end
6262
6263#pragma mark - UInt64 -> Bool
6264
6265@implementation GPBUInt64BoolDictionary {
6266 @package
6267  NSMutableDictionary *_dictionary;
6268}
6269
6270+ (instancetype)dictionary {
6271  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
6272}
6273
6274+ (instancetype)dictionaryWithValue:(BOOL)value
6275                             forKey:(uint64_t)key {
6276  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6277  // on to get the type correct.
6278  return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
6279                                                         forKeys:&key
6280                                                           count:1] autorelease];
6281}
6282
6283+ (instancetype)dictionaryWithValues:(const BOOL [])values
6284                             forKeys:(const uint64_t [])keys
6285                               count:(NSUInteger)count {
6286  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6287  // on to get the type correct.
6288  return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
6289                                                         forKeys:keys
6290                                                           count:count] autorelease];
6291}
6292
6293+ (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
6294  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6295  // on to get the type correct.
6296  return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6297}
6298
6299+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6300  return [[[self alloc] initWithCapacity:numItems] autorelease];
6301}
6302
6303- (instancetype)init {
6304  return [self initWithValues:NULL forKeys:NULL count:0];
6305}
6306
6307- (instancetype)initWithValues:(const BOOL [])values
6308                       forKeys:(const uint64_t [])keys
6309                         count:(NSUInteger)count {
6310  self = [super init];
6311  if (self) {
6312    _dictionary = [[NSMutableDictionary alloc] init];
6313    if (count && values && keys) {
6314      for (NSUInteger i = 0; i < count; ++i) {
6315        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6316      }
6317    }
6318  }
6319  return self;
6320}
6321
6322- (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
6323  self = [self initWithValues:NULL forKeys:NULL count:0];
6324  if (self) {
6325    if (dictionary) {
6326      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6327    }
6328  }
6329  return self;
6330}
6331
6332- (instancetype)initWithCapacity:(NSUInteger)numItems {
6333  #pragma unused(numItems)
6334  return [self initWithValues:NULL forKeys:NULL count:0];
6335}
6336
6337- (void)dealloc {
6338  NSAssert(!_autocreator,
6339           @"%@: Autocreator must be cleared before release, autocreator: %@",
6340           [self class], _autocreator);
6341  [_dictionary release];
6342  [super dealloc];
6343}
6344
6345- (instancetype)copyWithZone:(NSZone *)zone {
6346  return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
6347}
6348
6349- (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
6350  if (self == other) {
6351    return YES;
6352  }
6353  if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
6354    return NO;
6355  }
6356  return [_dictionary isEqual:other->_dictionary];
6357}
6358
6359- (NSUInteger)hash {
6360  return _dictionary.count;
6361}
6362
6363- (NSString *)description {
6364  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6365}
6366
6367- (NSUInteger)count {
6368  return _dictionary.count;
6369}
6370
6371- (void)enumerateKeysAndValuesUsingBlock:
6372    (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
6373  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6374                                                   NSNumber *aValue,
6375                                                   BOOL *stop) {
6376      block([aKey unsignedLongLongValue], [aValue boolValue], stop);
6377  }];
6378}
6379
6380- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6381  NSUInteger count = _dictionary.count;
6382  if (count == 0) {
6383    return 0;
6384  }
6385
6386  GPBDataType valueDataType = GPBGetFieldDataType(field);
6387  GPBDataType keyDataType = field.mapKeyDataType;
6388  __block size_t result = 0;
6389  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6390                                                   NSNumber *aValue,
6391                                                   BOOL *stop) {
6392    #pragma unused(stop)
6393    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6394    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
6395    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6396  }];
6397  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6398  result += tagSize * count;
6399  return result;
6400}
6401
6402- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6403                         asField:(GPBFieldDescriptor *)field {
6404  GPBDataType valueDataType = GPBGetFieldDataType(field);
6405  GPBDataType keyDataType = field.mapKeyDataType;
6406  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6407  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6408                                                   NSNumber *aValue,
6409                                                   BOOL *stop) {
6410    #pragma unused(stop)
6411    // Write the tag.
6412    [outputStream writeInt32NoTag:tag];
6413    // Write the size of the message.
6414    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6415    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
6416    [outputStream writeInt32NoTag:(int32_t)msgSize];
6417    // Write the fields.
6418    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6419    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
6420  }];
6421}
6422
6423- (void)setGPBGenericValue:(GPBGenericValue *)value
6424     forGPBGenericValueKey:(GPBGenericValue *)key {
6425  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
6426}
6427
6428- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6429  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
6430      #pragma unused(stop)
6431      block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
6432  }];
6433}
6434
6435- (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
6436  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6437  if (wrapped && value) {
6438    *value = [wrapped boolValue];
6439  }
6440  return (wrapped != NULL);
6441}
6442
6443- (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
6444  if (otherDictionary) {
6445    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6446    if (_autocreator) {
6447      GPBAutocreatedDictionaryModified(_autocreator, self);
6448    }
6449  }
6450}
6451
6452- (void)setValue:(BOOL)value forKey:(uint64_t)key {
6453  [_dictionary setObject:@(value) forKey:@(key)];
6454  if (_autocreator) {
6455    GPBAutocreatedDictionaryModified(_autocreator, self);
6456  }
6457}
6458
6459- (void)removeValueForKey:(uint64_t)aKey {
6460  [_dictionary removeObjectForKey:@(aKey)];
6461}
6462
6463- (void)removeAll {
6464  [_dictionary removeAllObjects];
6465}
6466
6467@end
6468
6469#pragma mark - UInt64 -> Float
6470
6471@implementation GPBUInt64FloatDictionary {
6472 @package
6473  NSMutableDictionary *_dictionary;
6474}
6475
6476+ (instancetype)dictionary {
6477  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
6478}
6479
6480+ (instancetype)dictionaryWithValue:(float)value
6481                             forKey:(uint64_t)key {
6482  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6483  // on to get the type correct.
6484  return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
6485                                                          forKeys:&key
6486                                                            count:1] autorelease];
6487}
6488
6489+ (instancetype)dictionaryWithValues:(const float [])values
6490                             forKeys:(const uint64_t [])keys
6491                               count:(NSUInteger)count {
6492  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6493  // on to get the type correct.
6494  return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
6495                                                          forKeys:keys
6496                                                            count:count] autorelease];
6497}
6498
6499+ (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
6500  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6501  // on to get the type correct.
6502  return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6503}
6504
6505+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6506  return [[[self alloc] initWithCapacity:numItems] autorelease];
6507}
6508
6509- (instancetype)init {
6510  return [self initWithValues:NULL forKeys:NULL count:0];
6511}
6512
6513- (instancetype)initWithValues:(const float [])values
6514                       forKeys:(const uint64_t [])keys
6515                         count:(NSUInteger)count {
6516  self = [super init];
6517  if (self) {
6518    _dictionary = [[NSMutableDictionary alloc] init];
6519    if (count && values && keys) {
6520      for (NSUInteger i = 0; i < count; ++i) {
6521        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6522      }
6523    }
6524  }
6525  return self;
6526}
6527
6528- (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
6529  self = [self initWithValues:NULL forKeys:NULL count:0];
6530  if (self) {
6531    if (dictionary) {
6532      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6533    }
6534  }
6535  return self;
6536}
6537
6538- (instancetype)initWithCapacity:(NSUInteger)numItems {
6539  #pragma unused(numItems)
6540  return [self initWithValues:NULL forKeys:NULL count:0];
6541}
6542
6543- (void)dealloc {
6544  NSAssert(!_autocreator,
6545           @"%@: Autocreator must be cleared before release, autocreator: %@",
6546           [self class], _autocreator);
6547  [_dictionary release];
6548  [super dealloc];
6549}
6550
6551- (instancetype)copyWithZone:(NSZone *)zone {
6552  return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
6553}
6554
6555- (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
6556  if (self == other) {
6557    return YES;
6558  }
6559  if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
6560    return NO;
6561  }
6562  return [_dictionary isEqual:other->_dictionary];
6563}
6564
6565- (NSUInteger)hash {
6566  return _dictionary.count;
6567}
6568
6569- (NSString *)description {
6570  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6571}
6572
6573- (NSUInteger)count {
6574  return _dictionary.count;
6575}
6576
6577- (void)enumerateKeysAndValuesUsingBlock:
6578    (void (^)(uint64_t key, float value, BOOL *stop))block {
6579  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6580                                                   NSNumber *aValue,
6581                                                   BOOL *stop) {
6582      block([aKey unsignedLongLongValue], [aValue floatValue], stop);
6583  }];
6584}
6585
6586- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6587  NSUInteger count = _dictionary.count;
6588  if (count == 0) {
6589    return 0;
6590  }
6591
6592  GPBDataType valueDataType = GPBGetFieldDataType(field);
6593  GPBDataType keyDataType = field.mapKeyDataType;
6594  __block size_t result = 0;
6595  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6596                                                   NSNumber *aValue,
6597                                                   BOOL *stop) {
6598    #pragma unused(stop)
6599    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6600    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
6601    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6602  }];
6603  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6604  result += tagSize * count;
6605  return result;
6606}
6607
6608- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6609                         asField:(GPBFieldDescriptor *)field {
6610  GPBDataType valueDataType = GPBGetFieldDataType(field);
6611  GPBDataType keyDataType = field.mapKeyDataType;
6612  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6613  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6614                                                   NSNumber *aValue,
6615                                                   BOOL *stop) {
6616    #pragma unused(stop)
6617    // Write the tag.
6618    [outputStream writeInt32NoTag:tag];
6619    // Write the size of the message.
6620    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6621    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
6622    [outputStream writeInt32NoTag:(int32_t)msgSize];
6623    // Write the fields.
6624    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6625    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
6626  }];
6627}
6628
6629- (void)setGPBGenericValue:(GPBGenericValue *)value
6630     forGPBGenericValueKey:(GPBGenericValue *)key {
6631  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
6632}
6633
6634- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6635  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
6636      #pragma unused(stop)
6637      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
6638  }];
6639}
6640
6641- (BOOL)valueForKey:(uint64_t)key value:(float *)value {
6642  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6643  if (wrapped && value) {
6644    *value = [wrapped floatValue];
6645  }
6646  return (wrapped != NULL);
6647}
6648
6649- (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
6650  if (otherDictionary) {
6651    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6652    if (_autocreator) {
6653      GPBAutocreatedDictionaryModified(_autocreator, self);
6654    }
6655  }
6656}
6657
6658- (void)setValue:(float)value forKey:(uint64_t)key {
6659  [_dictionary setObject:@(value) forKey:@(key)];
6660  if (_autocreator) {
6661    GPBAutocreatedDictionaryModified(_autocreator, self);
6662  }
6663}
6664
6665- (void)removeValueForKey:(uint64_t)aKey {
6666  [_dictionary removeObjectForKey:@(aKey)];
6667}
6668
6669- (void)removeAll {
6670  [_dictionary removeAllObjects];
6671}
6672
6673@end
6674
6675#pragma mark - UInt64 -> Double
6676
6677@implementation GPBUInt64DoubleDictionary {
6678 @package
6679  NSMutableDictionary *_dictionary;
6680}
6681
6682+ (instancetype)dictionary {
6683  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
6684}
6685
6686+ (instancetype)dictionaryWithValue:(double)value
6687                             forKey:(uint64_t)key {
6688  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6689  // on to get the type correct.
6690  return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
6691                                                           forKeys:&key
6692                                                             count:1] autorelease];
6693}
6694
6695+ (instancetype)dictionaryWithValues:(const double [])values
6696                             forKeys:(const uint64_t [])keys
6697                               count:(NSUInteger)count {
6698  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6699  // on to get the type correct.
6700  return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
6701                                                           forKeys:keys
6702                                                             count:count] autorelease];
6703}
6704
6705+ (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
6706  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
6707  // on to get the type correct.
6708  return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6709}
6710
6711+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
6712  return [[[self alloc] initWithCapacity:numItems] autorelease];
6713}
6714
6715- (instancetype)init {
6716  return [self initWithValues:NULL forKeys:NULL count:0];
6717}
6718
6719- (instancetype)initWithValues:(const double [])values
6720                       forKeys:(const uint64_t [])keys
6721                         count:(NSUInteger)count {
6722  self = [super init];
6723  if (self) {
6724    _dictionary = [[NSMutableDictionary alloc] init];
6725    if (count && values && keys) {
6726      for (NSUInteger i = 0; i < count; ++i) {
6727        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
6728      }
6729    }
6730  }
6731  return self;
6732}
6733
6734- (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
6735  self = [self initWithValues:NULL forKeys:NULL count:0];
6736  if (self) {
6737    if (dictionary) {
6738      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6739    }
6740  }
6741  return self;
6742}
6743
6744- (instancetype)initWithCapacity:(NSUInteger)numItems {
6745  #pragma unused(numItems)
6746  return [self initWithValues:NULL forKeys:NULL count:0];
6747}
6748
6749- (void)dealloc {
6750  NSAssert(!_autocreator,
6751           @"%@: Autocreator must be cleared before release, autocreator: %@",
6752           [self class], _autocreator);
6753  [_dictionary release];
6754  [super dealloc];
6755}
6756
6757- (instancetype)copyWithZone:(NSZone *)zone {
6758  return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
6759}
6760
6761- (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
6762  if (self == other) {
6763    return YES;
6764  }
6765  if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
6766    return NO;
6767  }
6768  return [_dictionary isEqual:other->_dictionary];
6769}
6770
6771- (NSUInteger)hash {
6772  return _dictionary.count;
6773}
6774
6775- (NSString *)description {
6776  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
6777}
6778
6779- (NSUInteger)count {
6780  return _dictionary.count;
6781}
6782
6783- (void)enumerateKeysAndValuesUsingBlock:
6784    (void (^)(uint64_t key, double value, BOOL *stop))block {
6785  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6786                                                   NSNumber *aValue,
6787                                                   BOOL *stop) {
6788      block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
6789  }];
6790}
6791
6792- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
6793  NSUInteger count = _dictionary.count;
6794  if (count == 0) {
6795    return 0;
6796  }
6797
6798  GPBDataType valueDataType = GPBGetFieldDataType(field);
6799  GPBDataType keyDataType = field.mapKeyDataType;
6800  __block size_t result = 0;
6801  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6802                                                   NSNumber *aValue,
6803                                                   BOOL *stop) {
6804    #pragma unused(stop)
6805    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6806    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
6807    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
6808  }];
6809  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
6810  result += tagSize * count;
6811  return result;
6812}
6813
6814- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
6815                         asField:(GPBFieldDescriptor *)field {
6816  GPBDataType valueDataType = GPBGetFieldDataType(field);
6817  GPBDataType keyDataType = field.mapKeyDataType;
6818  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
6819  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
6820                                                   NSNumber *aValue,
6821                                                   BOOL *stop) {
6822    #pragma unused(stop)
6823    // Write the tag.
6824    [outputStream writeInt32NoTag:tag];
6825    // Write the size of the message.
6826    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6827    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
6828    [outputStream writeInt32NoTag:(int32_t)msgSize];
6829    // Write the fields.
6830    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
6831    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
6832  }];
6833}
6834
6835- (void)setGPBGenericValue:(GPBGenericValue *)value
6836     forGPBGenericValueKey:(GPBGenericValue *)key {
6837  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
6838}
6839
6840- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
6841  [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
6842      #pragma unused(stop)
6843      block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
6844  }];
6845}
6846
6847- (BOOL)valueForKey:(uint64_t)key value:(double *)value {
6848  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
6849  if (wrapped && value) {
6850    *value = [wrapped doubleValue];
6851  }
6852  return (wrapped != NULL);
6853}
6854
6855- (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
6856  if (otherDictionary) {
6857    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
6858    if (_autocreator) {
6859      GPBAutocreatedDictionaryModified(_autocreator, self);
6860    }
6861  }
6862}
6863
6864- (void)setValue:(double)value forKey:(uint64_t)key {
6865  [_dictionary setObject:@(value) forKey:@(key)];
6866  if (_autocreator) {
6867    GPBAutocreatedDictionaryModified(_autocreator, self);
6868  }
6869}
6870
6871- (void)removeValueForKey:(uint64_t)aKey {
6872  [_dictionary removeObjectForKey:@(aKey)];
6873}
6874
6875- (void)removeAll {
6876  [_dictionary removeAllObjects];
6877}
6878
6879@end
6880
6881#pragma mark - UInt64 -> Enum
6882
6883@implementation GPBUInt64EnumDictionary {
6884 @package
6885  NSMutableDictionary *_dictionary;
6886  GPBEnumValidationFunc _validationFunc;
6887}
6888
6889@synthesize validationFunc = _validationFunc;
6890
6891+ (instancetype)dictionary {
6892  return [[[self alloc] initWithValidationFunction:NULL
6893                                         rawValues:NULL
6894                                           forKeys:NULL
6895                                             count:0] autorelease];
6896}
6897
6898+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
6899  return [[[self alloc] initWithValidationFunction:func
6900                                         rawValues:NULL
6901                                           forKeys:NULL
6902                                             count:0] autorelease];
6903}
6904
6905+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
6906                                        rawValue:(int32_t)rawValue
6907                                          forKey:(uint64_t)key {
6908  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6909  // on to get the type correct.
6910  return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
6911                                                                   rawValues:&rawValue
6912                                                                     forKeys:&key
6913                                                                       count:1] autorelease];
6914}
6915
6916+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
6917                                       rawValues:(const int32_t [])rawValues
6918                                         forKeys:(const uint64_t [])keys
6919                                           count:(NSUInteger)count {
6920  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6921  // on to get the type correct.
6922  return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
6923                                                                   rawValues:rawValues
6924                                                                     forKeys:keys
6925                                                                       count:count] autorelease];
6926}
6927
6928+ (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
6929  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
6930  // on to get the type correct.
6931  return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
6932}
6933
6934+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
6935                                        capacity:(NSUInteger)numItems {
6936  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
6937}
6938
6939- (instancetype)init {
6940  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
6941}
6942
6943- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
6944  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
6945}
6946
6947- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
6948                                 rawValues:(const int32_t [])rawValues
6949                                   forKeys:(const uint64_t [])keys
6950                                     count:(NSUInteger)count {
6951  self = [super init];
6952  if (self) {
6953    _dictionary = [[NSMutableDictionary alloc] init];
6954    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
6955    if (count && rawValues && keys) {
6956      for (NSUInteger i = 0; i < count; ++i) {
6957        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
6958      }
6959    }
6960  }
6961  return self;
6962}
6963
6964- (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
6965  self = [self initWithValidationFunction:dictionary.validationFunc
6966                                rawValues:NULL
6967                                  forKeys:NULL
6968                                    count:0];
6969  if (self) {
6970    if (dictionary) {
6971      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
6972    }
6973  }
6974  return self;
6975}
6976
6977- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
6978                                  capacity:(NSUInteger)numItems {
6979  #pragma unused(numItems)
6980  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
6981}
6982
6983- (void)dealloc {
6984  NSAssert(!_autocreator,
6985           @"%@: Autocreator must be cleared before release, autocreator: %@",
6986           [self class], _autocreator);
6987  [_dictionary release];
6988  [super dealloc];
6989}
6990
6991- (instancetype)copyWithZone:(NSZone *)zone {
6992  return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
6993}
6994
6995- (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
6996  if (self == other) {
6997    return YES;
6998  }
6999  if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
7000    return NO;
7001  }
7002  return [_dictionary isEqual:other->_dictionary];
7003}
7004
7005- (NSUInteger)hash {
7006  return _dictionary.count;
7007}
7008
7009- (NSString *)description {
7010  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7011}
7012
7013- (NSUInteger)count {
7014  return _dictionary.count;
7015}
7016
7017- (void)enumerateKeysAndRawValuesUsingBlock:
7018    (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
7019  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7020                                                   NSNumber *aValue,
7021                                                   BOOL *stop) {
7022      block([aKey unsignedLongLongValue], [aValue intValue], stop);
7023  }];
7024}
7025
7026- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7027  NSUInteger count = _dictionary.count;
7028  if (count == 0) {
7029    return 0;
7030  }
7031
7032  GPBDataType valueDataType = GPBGetFieldDataType(field);
7033  GPBDataType keyDataType = field.mapKeyDataType;
7034  __block size_t result = 0;
7035  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7036                                                   NSNumber *aValue,
7037                                                   BOOL *stop) {
7038    #pragma unused(stop)
7039    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7040    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7041    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7042  }];
7043  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7044  result += tagSize * count;
7045  return result;
7046}
7047
7048- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7049                         asField:(GPBFieldDescriptor *)field {
7050  GPBDataType valueDataType = GPBGetFieldDataType(field);
7051  GPBDataType keyDataType = field.mapKeyDataType;
7052  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7053  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7054                                                   NSNumber *aValue,
7055                                                   BOOL *stop) {
7056    #pragma unused(stop)
7057    // Write the tag.
7058    [outputStream writeInt32NoTag:tag];
7059    // Write the size of the message.
7060    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7061    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7062    [outputStream writeInt32NoTag:(int32_t)msgSize];
7063    // Write the fields.
7064    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7065    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
7066  }];
7067}
7068
7069- (NSData *)serializedDataForUnknownValue:(int32_t)value
7070                                   forKey:(GPBGenericValue *)key
7071                              keyDataType:(GPBDataType)keyDataType {
7072  size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
7073  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
7074  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
7075  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
7076  WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
7077  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
7078  [outputStream release];
7079  return data;
7080}
7081- (void)setGPBGenericValue:(GPBGenericValue *)value
7082     forGPBGenericValueKey:(GPBGenericValue *)key {
7083  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
7084}
7085
7086- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7087  [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
7088      #pragma unused(stop)
7089      block([NSString stringWithFormat:@"%llu", key], @(value));
7090  }];
7091}
7092
7093- (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
7094  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7095  if (wrapped && value) {
7096    int32_t result = [wrapped intValue];
7097    if (!_validationFunc(result)) {
7098      result = kGPBUnrecognizedEnumeratorValue;
7099    }
7100    *value = result;
7101  }
7102  return (wrapped != NULL);
7103}
7104
7105- (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
7106  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7107  if (wrapped && rawValue) {
7108    *rawValue = [wrapped intValue];
7109  }
7110  return (wrapped != NULL);
7111}
7112
7113- (void)enumerateKeysAndValuesUsingBlock:
7114    (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
7115  GPBEnumValidationFunc func = _validationFunc;
7116  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7117                                                   NSNumber *aValue,
7118                                                   BOOL *stop) {
7119      int32_t unwrapped = [aValue intValue];
7120      if (!func(unwrapped)) {
7121        unwrapped = kGPBUnrecognizedEnumeratorValue;
7122      }
7123      block([aKey unsignedLongLongValue], unwrapped, stop);
7124  }];
7125}
7126
7127- (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
7128  if (otherDictionary) {
7129    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7130    if (_autocreator) {
7131      GPBAutocreatedDictionaryModified(_autocreator, self);
7132    }
7133  }
7134}
7135
7136- (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
7137  [_dictionary setObject:@(value) forKey:@(key)];
7138  if (_autocreator) {
7139    GPBAutocreatedDictionaryModified(_autocreator, self);
7140  }
7141}
7142
7143- (void)removeValueForKey:(uint64_t)aKey {
7144  [_dictionary removeObjectForKey:@(aKey)];
7145}
7146
7147- (void)removeAll {
7148  [_dictionary removeAllObjects];
7149}
7150
7151- (void)setValue:(int32_t)value forKey:(uint64_t)key {
7152  if (!_validationFunc(value)) {
7153    [NSException raise:NSInvalidArgumentException
7154                format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
7155                       value];
7156  }
7157
7158  [_dictionary setObject:@(value) forKey:@(key)];
7159  if (_autocreator) {
7160    GPBAutocreatedDictionaryModified(_autocreator, self);
7161  }
7162}
7163
7164@end
7165
7166#pragma mark - UInt64 -> Object
7167
7168@implementation GPBUInt64ObjectDictionary {
7169 @package
7170  NSMutableDictionary *_dictionary;
7171}
7172
7173+ (instancetype)dictionary {
7174  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
7175}
7176
7177+ (instancetype)dictionaryWithObject:(id)object
7178                              forKey:(uint64_t)key {
7179  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
7180  // on to get the type correct.
7181  return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
7182                                                            forKeys:&key
7183                                                              count:1] autorelease];
7184}
7185
7186+ (instancetype)dictionaryWithObjects:(const id [])objects
7187                              forKeys:(const uint64_t [])keys
7188                                count:(NSUInteger)count {
7189  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
7190  // on to get the type correct.
7191  return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
7192                                                           forKeys:keys
7193                                                             count:count] autorelease];
7194}
7195
7196+ (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
7197  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7198  // on to get the type correct.
7199  return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7200}
7201
7202+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7203  return [[[self alloc] initWithCapacity:numItems] autorelease];
7204}
7205
7206- (instancetype)init {
7207  return [self initWithObjects:NULL forKeys:NULL count:0];
7208}
7209
7210- (instancetype)initWithObjects:(const id [])objects
7211                        forKeys:(const uint64_t [])keys
7212                          count:(NSUInteger)count {
7213  self = [super init];
7214  if (self) {
7215    _dictionary = [[NSMutableDictionary alloc] init];
7216    if (count && objects && keys) {
7217      for (NSUInteger i = 0; i < count; ++i) {
7218        if (!objects[i]) {
7219          [NSException raise:NSInvalidArgumentException
7220                      format:@"Attempting to add nil object to a Dictionary"];
7221        }
7222        [_dictionary setObject:objects[i] forKey:@(keys[i])];
7223      }
7224    }
7225  }
7226  return self;
7227}
7228
7229- (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
7230  self = [self initWithObjects:NULL forKeys:NULL count:0];
7231  if (self) {
7232    if (dictionary) {
7233      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7234    }
7235  }
7236  return self;
7237}
7238
7239- (instancetype)initWithCapacity:(NSUInteger)numItems {
7240  #pragma unused(numItems)
7241  return [self initWithObjects:NULL forKeys:NULL count:0];
7242}
7243
7244- (void)dealloc {
7245  NSAssert(!_autocreator,
7246           @"%@: Autocreator must be cleared before release, autocreator: %@",
7247           [self class], _autocreator);
7248  [_dictionary release];
7249  [super dealloc];
7250}
7251
7252- (instancetype)copyWithZone:(NSZone *)zone {
7253  return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
7254}
7255
7256- (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
7257  if (self == other) {
7258    return YES;
7259  }
7260  if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
7261    return NO;
7262  }
7263  return [_dictionary isEqual:other->_dictionary];
7264}
7265
7266- (NSUInteger)hash {
7267  return _dictionary.count;
7268}
7269
7270- (NSString *)description {
7271  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7272}
7273
7274- (NSUInteger)count {
7275  return _dictionary.count;
7276}
7277
7278- (void)enumerateKeysAndObjectsUsingBlock:
7279    (void (^)(uint64_t key, id object, BOOL *stop))block {
7280  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7281                                                   id aObject,
7282                                                   BOOL *stop) {
7283      block([aKey unsignedLongLongValue], aObject, stop);
7284  }];
7285}
7286
7287- (BOOL)isInitialized {
7288  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
7289    if (!msg.initialized) {
7290      return NO;
7291    }
7292  }
7293  return YES;
7294}
7295
7296- (instancetype)deepCopyWithZone:(NSZone *)zone {
7297  GPBUInt64ObjectDictionary *newDict =
7298      [[GPBUInt64ObjectDictionary alloc] init];
7299  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
7300                                                   GPBMessage *msg,
7301                                                   BOOL *stop) {
7302    #pragma unused(stop)
7303    GPBMessage *copiedMsg = [msg copyWithZone:zone];
7304    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
7305    [copiedMsg release];
7306  }];
7307  return newDict;
7308}
7309
7310- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7311  NSUInteger count = _dictionary.count;
7312  if (count == 0) {
7313    return 0;
7314  }
7315
7316  GPBDataType valueDataType = GPBGetFieldDataType(field);
7317  GPBDataType keyDataType = field.mapKeyDataType;
7318  __block size_t result = 0;
7319  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7320                                                   id aObject,
7321                                                   BOOL *stop) {
7322    #pragma unused(stop)
7323    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7324    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
7325    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7326  }];
7327  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7328  result += tagSize * count;
7329  return result;
7330}
7331
7332- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7333                         asField:(GPBFieldDescriptor *)field {
7334  GPBDataType valueDataType = GPBGetFieldDataType(field);
7335  GPBDataType keyDataType = field.mapKeyDataType;
7336  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7337  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7338                                                   id aObject,
7339                                                   BOOL *stop) {
7340    #pragma unused(stop)
7341    // Write the tag.
7342    [outputStream writeInt32NoTag:tag];
7343    // Write the size of the message.
7344    size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7345    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
7346    [outputStream writeInt32NoTag:(int32_t)msgSize];
7347    // Write the fields.
7348    WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
7349    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
7350  }];
7351}
7352
7353- (void)setGPBGenericValue:(GPBGenericValue *)value
7354     forGPBGenericValueKey:(GPBGenericValue *)key {
7355  [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
7356}
7357
7358- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7359  [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
7360      #pragma unused(stop)
7361      block([NSString stringWithFormat:@"%llu", key], object);
7362  }];
7363}
7364
7365- (id)objectForKey:(uint64_t)key {
7366  id result = [_dictionary objectForKey:@(key)];
7367  return result;
7368}
7369
7370- (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
7371  if (otherDictionary) {
7372    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7373    if (_autocreator) {
7374      GPBAutocreatedDictionaryModified(_autocreator, self);
7375    }
7376  }
7377}
7378
7379- (void)setObject:(id)object forKey:(uint64_t)key {
7380  if (!object) {
7381    [NSException raise:NSInvalidArgumentException
7382                format:@"Attempting to add nil object to a Dictionary"];
7383  }
7384  [_dictionary setObject:object forKey:@(key)];
7385  if (_autocreator) {
7386    GPBAutocreatedDictionaryModified(_autocreator, self);
7387  }
7388}
7389
7390- (void)removeObjectForKey:(uint64_t)aKey {
7391  [_dictionary removeObjectForKey:@(aKey)];
7392}
7393
7394- (void)removeAll {
7395  [_dictionary removeAllObjects];
7396}
7397
7398@end
7399
7400//%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
7401// This block of code is generated, do not edit it directly.
7402
7403#pragma mark - Int64 -> UInt32
7404
7405@implementation GPBInt64UInt32Dictionary {
7406 @package
7407  NSMutableDictionary *_dictionary;
7408}
7409
7410+ (instancetype)dictionary {
7411  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
7412}
7413
7414+ (instancetype)dictionaryWithValue:(uint32_t)value
7415                             forKey:(int64_t)key {
7416  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
7417  // on to get the type correct.
7418  return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
7419                                                          forKeys:&key
7420                                                            count:1] autorelease];
7421}
7422
7423+ (instancetype)dictionaryWithValues:(const uint32_t [])values
7424                             forKeys:(const int64_t [])keys
7425                               count:(NSUInteger)count {
7426  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
7427  // on to get the type correct.
7428  return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
7429                                                          forKeys:keys
7430                                                            count:count] autorelease];
7431}
7432
7433+ (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
7434  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7435  // on to get the type correct.
7436  return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7437}
7438
7439+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7440  return [[[self alloc] initWithCapacity:numItems] autorelease];
7441}
7442
7443- (instancetype)init {
7444  return [self initWithValues:NULL forKeys:NULL count:0];
7445}
7446
7447- (instancetype)initWithValues:(const uint32_t [])values
7448                       forKeys:(const int64_t [])keys
7449                         count:(NSUInteger)count {
7450  self = [super init];
7451  if (self) {
7452    _dictionary = [[NSMutableDictionary alloc] init];
7453    if (count && values && keys) {
7454      for (NSUInteger i = 0; i < count; ++i) {
7455        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
7456      }
7457    }
7458  }
7459  return self;
7460}
7461
7462- (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
7463  self = [self initWithValues:NULL forKeys:NULL count:0];
7464  if (self) {
7465    if (dictionary) {
7466      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7467    }
7468  }
7469  return self;
7470}
7471
7472- (instancetype)initWithCapacity:(NSUInteger)numItems {
7473  #pragma unused(numItems)
7474  return [self initWithValues:NULL forKeys:NULL count:0];
7475}
7476
7477- (void)dealloc {
7478  NSAssert(!_autocreator,
7479           @"%@: Autocreator must be cleared before release, autocreator: %@",
7480           [self class], _autocreator);
7481  [_dictionary release];
7482  [super dealloc];
7483}
7484
7485- (instancetype)copyWithZone:(NSZone *)zone {
7486  return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
7487}
7488
7489- (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
7490  if (self == other) {
7491    return YES;
7492  }
7493  if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
7494    return NO;
7495  }
7496  return [_dictionary isEqual:other->_dictionary];
7497}
7498
7499- (NSUInteger)hash {
7500  return _dictionary.count;
7501}
7502
7503- (NSString *)description {
7504  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7505}
7506
7507- (NSUInteger)count {
7508  return _dictionary.count;
7509}
7510
7511- (void)enumerateKeysAndValuesUsingBlock:
7512    (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
7513  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7514                                                   NSNumber *aValue,
7515                                                   BOOL *stop) {
7516      block([aKey longLongValue], [aValue unsignedIntValue], stop);
7517  }];
7518}
7519
7520- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7521  NSUInteger count = _dictionary.count;
7522  if (count == 0) {
7523    return 0;
7524  }
7525
7526  GPBDataType valueDataType = GPBGetFieldDataType(field);
7527  GPBDataType keyDataType = field.mapKeyDataType;
7528  __block size_t result = 0;
7529  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7530                                                   NSNumber *aValue,
7531                                                   BOOL *stop) {
7532    #pragma unused(stop)
7533    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7534    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
7535    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7536  }];
7537  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7538  result += tagSize * count;
7539  return result;
7540}
7541
7542- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7543                         asField:(GPBFieldDescriptor *)field {
7544  GPBDataType valueDataType = GPBGetFieldDataType(field);
7545  GPBDataType keyDataType = field.mapKeyDataType;
7546  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7547  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7548                                                   NSNumber *aValue,
7549                                                   BOOL *stop) {
7550    #pragma unused(stop)
7551    // Write the tag.
7552    [outputStream writeInt32NoTag:tag];
7553    // Write the size of the message.
7554    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7555    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
7556    [outputStream writeInt32NoTag:(int32_t)msgSize];
7557    // Write the fields.
7558    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7559    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
7560  }];
7561}
7562
7563- (void)setGPBGenericValue:(GPBGenericValue *)value
7564     forGPBGenericValueKey:(GPBGenericValue *)key {
7565  [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
7566}
7567
7568- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7569  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
7570      #pragma unused(stop)
7571      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
7572  }];
7573}
7574
7575- (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
7576  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7577  if (wrapped && value) {
7578    *value = [wrapped unsignedIntValue];
7579  }
7580  return (wrapped != NULL);
7581}
7582
7583- (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
7584  if (otherDictionary) {
7585    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7586    if (_autocreator) {
7587      GPBAutocreatedDictionaryModified(_autocreator, self);
7588    }
7589  }
7590}
7591
7592- (void)setValue:(uint32_t)value forKey:(int64_t)key {
7593  [_dictionary setObject:@(value) forKey:@(key)];
7594  if (_autocreator) {
7595    GPBAutocreatedDictionaryModified(_autocreator, self);
7596  }
7597}
7598
7599- (void)removeValueForKey:(int64_t)aKey {
7600  [_dictionary removeObjectForKey:@(aKey)];
7601}
7602
7603- (void)removeAll {
7604  [_dictionary removeAllObjects];
7605}
7606
7607@end
7608
7609#pragma mark - Int64 -> Int32
7610
7611@implementation GPBInt64Int32Dictionary {
7612 @package
7613  NSMutableDictionary *_dictionary;
7614}
7615
7616+ (instancetype)dictionary {
7617  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
7618}
7619
7620+ (instancetype)dictionaryWithValue:(int32_t)value
7621                             forKey:(int64_t)key {
7622  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
7623  // on to get the type correct.
7624  return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
7625                                                         forKeys:&key
7626                                                           count:1] autorelease];
7627}
7628
7629+ (instancetype)dictionaryWithValues:(const int32_t [])values
7630                             forKeys:(const int64_t [])keys
7631                               count:(NSUInteger)count {
7632  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
7633  // on to get the type correct.
7634  return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
7635                                                         forKeys:keys
7636                                                           count:count] autorelease];
7637}
7638
7639+ (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
7640  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7641  // on to get the type correct.
7642  return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7643}
7644
7645+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7646  return [[[self alloc] initWithCapacity:numItems] autorelease];
7647}
7648
7649- (instancetype)init {
7650  return [self initWithValues:NULL forKeys:NULL count:0];
7651}
7652
7653- (instancetype)initWithValues:(const int32_t [])values
7654                       forKeys:(const int64_t [])keys
7655                         count:(NSUInteger)count {
7656  self = [super init];
7657  if (self) {
7658    _dictionary = [[NSMutableDictionary alloc] init];
7659    if (count && values && keys) {
7660      for (NSUInteger i = 0; i < count; ++i) {
7661        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
7662      }
7663    }
7664  }
7665  return self;
7666}
7667
7668- (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
7669  self = [self initWithValues:NULL forKeys:NULL count:0];
7670  if (self) {
7671    if (dictionary) {
7672      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7673    }
7674  }
7675  return self;
7676}
7677
7678- (instancetype)initWithCapacity:(NSUInteger)numItems {
7679  #pragma unused(numItems)
7680  return [self initWithValues:NULL forKeys:NULL count:0];
7681}
7682
7683- (void)dealloc {
7684  NSAssert(!_autocreator,
7685           @"%@: Autocreator must be cleared before release, autocreator: %@",
7686           [self class], _autocreator);
7687  [_dictionary release];
7688  [super dealloc];
7689}
7690
7691- (instancetype)copyWithZone:(NSZone *)zone {
7692  return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
7693}
7694
7695- (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
7696  if (self == other) {
7697    return YES;
7698  }
7699  if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
7700    return NO;
7701  }
7702  return [_dictionary isEqual:other->_dictionary];
7703}
7704
7705- (NSUInteger)hash {
7706  return _dictionary.count;
7707}
7708
7709- (NSString *)description {
7710  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7711}
7712
7713- (NSUInteger)count {
7714  return _dictionary.count;
7715}
7716
7717- (void)enumerateKeysAndValuesUsingBlock:
7718    (void (^)(int64_t key, int32_t value, BOOL *stop))block {
7719  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7720                                                   NSNumber *aValue,
7721                                                   BOOL *stop) {
7722      block([aKey longLongValue], [aValue intValue], stop);
7723  }];
7724}
7725
7726- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7727  NSUInteger count = _dictionary.count;
7728  if (count == 0) {
7729    return 0;
7730  }
7731
7732  GPBDataType valueDataType = GPBGetFieldDataType(field);
7733  GPBDataType keyDataType = field.mapKeyDataType;
7734  __block size_t result = 0;
7735  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7736                                                   NSNumber *aValue,
7737                                                   BOOL *stop) {
7738    #pragma unused(stop)
7739    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7740    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7741    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7742  }];
7743  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7744  result += tagSize * count;
7745  return result;
7746}
7747
7748- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7749                         asField:(GPBFieldDescriptor *)field {
7750  GPBDataType valueDataType = GPBGetFieldDataType(field);
7751  GPBDataType keyDataType = field.mapKeyDataType;
7752  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7753  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7754                                                   NSNumber *aValue,
7755                                                   BOOL *stop) {
7756    #pragma unused(stop)
7757    // Write the tag.
7758    [outputStream writeInt32NoTag:tag];
7759    // Write the size of the message.
7760    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7761    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
7762    [outputStream writeInt32NoTag:(int32_t)msgSize];
7763    // Write the fields.
7764    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7765    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
7766  }];
7767}
7768
7769- (void)setGPBGenericValue:(GPBGenericValue *)value
7770     forGPBGenericValueKey:(GPBGenericValue *)key {
7771  [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
7772}
7773
7774- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7775  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
7776      #pragma unused(stop)
7777      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
7778  }];
7779}
7780
7781- (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
7782  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7783  if (wrapped && value) {
7784    *value = [wrapped intValue];
7785  }
7786  return (wrapped != NULL);
7787}
7788
7789- (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
7790  if (otherDictionary) {
7791    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7792    if (_autocreator) {
7793      GPBAutocreatedDictionaryModified(_autocreator, self);
7794    }
7795  }
7796}
7797
7798- (void)setValue:(int32_t)value forKey:(int64_t)key {
7799  [_dictionary setObject:@(value) forKey:@(key)];
7800  if (_autocreator) {
7801    GPBAutocreatedDictionaryModified(_autocreator, self);
7802  }
7803}
7804
7805- (void)removeValueForKey:(int64_t)aKey {
7806  [_dictionary removeObjectForKey:@(aKey)];
7807}
7808
7809- (void)removeAll {
7810  [_dictionary removeAllObjects];
7811}
7812
7813@end
7814
7815#pragma mark - Int64 -> UInt64
7816
7817@implementation GPBInt64UInt64Dictionary {
7818 @package
7819  NSMutableDictionary *_dictionary;
7820}
7821
7822+ (instancetype)dictionary {
7823  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
7824}
7825
7826+ (instancetype)dictionaryWithValue:(uint64_t)value
7827                             forKey:(int64_t)key {
7828  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
7829  // on to get the type correct.
7830  return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
7831                                                          forKeys:&key
7832                                                            count:1] autorelease];
7833}
7834
7835+ (instancetype)dictionaryWithValues:(const uint64_t [])values
7836                             forKeys:(const int64_t [])keys
7837                               count:(NSUInteger)count {
7838  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
7839  // on to get the type correct.
7840  return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
7841                                                          forKeys:keys
7842                                                            count:count] autorelease];
7843}
7844
7845+ (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
7846  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
7847  // on to get the type correct.
7848  return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
7849}
7850
7851+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
7852  return [[[self alloc] initWithCapacity:numItems] autorelease];
7853}
7854
7855- (instancetype)init {
7856  return [self initWithValues:NULL forKeys:NULL count:0];
7857}
7858
7859- (instancetype)initWithValues:(const uint64_t [])values
7860                       forKeys:(const int64_t [])keys
7861                         count:(NSUInteger)count {
7862  self = [super init];
7863  if (self) {
7864    _dictionary = [[NSMutableDictionary alloc] init];
7865    if (count && values && keys) {
7866      for (NSUInteger i = 0; i < count; ++i) {
7867        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
7868      }
7869    }
7870  }
7871  return self;
7872}
7873
7874- (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
7875  self = [self initWithValues:NULL forKeys:NULL count:0];
7876  if (self) {
7877    if (dictionary) {
7878      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
7879    }
7880  }
7881  return self;
7882}
7883
7884- (instancetype)initWithCapacity:(NSUInteger)numItems {
7885  #pragma unused(numItems)
7886  return [self initWithValues:NULL forKeys:NULL count:0];
7887}
7888
7889- (void)dealloc {
7890  NSAssert(!_autocreator,
7891           @"%@: Autocreator must be cleared before release, autocreator: %@",
7892           [self class], _autocreator);
7893  [_dictionary release];
7894  [super dealloc];
7895}
7896
7897- (instancetype)copyWithZone:(NSZone *)zone {
7898  return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
7899}
7900
7901- (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
7902  if (self == other) {
7903    return YES;
7904  }
7905  if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
7906    return NO;
7907  }
7908  return [_dictionary isEqual:other->_dictionary];
7909}
7910
7911- (NSUInteger)hash {
7912  return _dictionary.count;
7913}
7914
7915- (NSString *)description {
7916  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
7917}
7918
7919- (NSUInteger)count {
7920  return _dictionary.count;
7921}
7922
7923- (void)enumerateKeysAndValuesUsingBlock:
7924    (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
7925  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7926                                                   NSNumber *aValue,
7927                                                   BOOL *stop) {
7928      block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
7929  }];
7930}
7931
7932- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
7933  NSUInteger count = _dictionary.count;
7934  if (count == 0) {
7935    return 0;
7936  }
7937
7938  GPBDataType valueDataType = GPBGetFieldDataType(field);
7939  GPBDataType keyDataType = field.mapKeyDataType;
7940  __block size_t result = 0;
7941  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7942                                                   NSNumber *aValue,
7943                                                   BOOL *stop) {
7944    #pragma unused(stop)
7945    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7946    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
7947    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
7948  }];
7949  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
7950  result += tagSize * count;
7951  return result;
7952}
7953
7954- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
7955                         asField:(GPBFieldDescriptor *)field {
7956  GPBDataType valueDataType = GPBGetFieldDataType(field);
7957  GPBDataType keyDataType = field.mapKeyDataType;
7958  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
7959  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
7960                                                   NSNumber *aValue,
7961                                                   BOOL *stop) {
7962    #pragma unused(stop)
7963    // Write the tag.
7964    [outputStream writeInt32NoTag:tag];
7965    // Write the size of the message.
7966    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7967    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
7968    [outputStream writeInt32NoTag:(int32_t)msgSize];
7969    // Write the fields.
7970    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
7971    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
7972  }];
7973}
7974
7975- (void)setGPBGenericValue:(GPBGenericValue *)value
7976     forGPBGenericValueKey:(GPBGenericValue *)key {
7977  [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
7978}
7979
7980- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
7981  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
7982      #pragma unused(stop)
7983      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
7984  }];
7985}
7986
7987- (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
7988  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
7989  if (wrapped && value) {
7990    *value = [wrapped unsignedLongLongValue];
7991  }
7992  return (wrapped != NULL);
7993}
7994
7995- (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
7996  if (otherDictionary) {
7997    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
7998    if (_autocreator) {
7999      GPBAutocreatedDictionaryModified(_autocreator, self);
8000    }
8001  }
8002}
8003
8004- (void)setValue:(uint64_t)value forKey:(int64_t)key {
8005  [_dictionary setObject:@(value) forKey:@(key)];
8006  if (_autocreator) {
8007    GPBAutocreatedDictionaryModified(_autocreator, self);
8008  }
8009}
8010
8011- (void)removeValueForKey:(int64_t)aKey {
8012  [_dictionary removeObjectForKey:@(aKey)];
8013}
8014
8015- (void)removeAll {
8016  [_dictionary removeAllObjects];
8017}
8018
8019@end
8020
8021#pragma mark - Int64 -> Int64
8022
8023@implementation GPBInt64Int64Dictionary {
8024 @package
8025  NSMutableDictionary *_dictionary;
8026}
8027
8028+ (instancetype)dictionary {
8029  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
8030}
8031
8032+ (instancetype)dictionaryWithValue:(int64_t)value
8033                             forKey:(int64_t)key {
8034  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8035  // on to get the type correct.
8036  return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
8037                                                         forKeys:&key
8038                                                           count:1] autorelease];
8039}
8040
8041+ (instancetype)dictionaryWithValues:(const int64_t [])values
8042                             forKeys:(const int64_t [])keys
8043                               count:(NSUInteger)count {
8044  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8045  // on to get the type correct.
8046  return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
8047                                                         forKeys:keys
8048                                                           count:count] autorelease];
8049}
8050
8051+ (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
8052  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8053  // on to get the type correct.
8054  return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8055}
8056
8057+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8058  return [[[self alloc] initWithCapacity:numItems] autorelease];
8059}
8060
8061- (instancetype)init {
8062  return [self initWithValues:NULL forKeys:NULL count:0];
8063}
8064
8065- (instancetype)initWithValues:(const int64_t [])values
8066                       forKeys:(const int64_t [])keys
8067                         count:(NSUInteger)count {
8068  self = [super init];
8069  if (self) {
8070    _dictionary = [[NSMutableDictionary alloc] init];
8071    if (count && values && keys) {
8072      for (NSUInteger i = 0; i < count; ++i) {
8073        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8074      }
8075    }
8076  }
8077  return self;
8078}
8079
8080- (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
8081  self = [self initWithValues:NULL forKeys:NULL count:0];
8082  if (self) {
8083    if (dictionary) {
8084      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8085    }
8086  }
8087  return self;
8088}
8089
8090- (instancetype)initWithCapacity:(NSUInteger)numItems {
8091  #pragma unused(numItems)
8092  return [self initWithValues:NULL forKeys:NULL count:0];
8093}
8094
8095- (void)dealloc {
8096  NSAssert(!_autocreator,
8097           @"%@: Autocreator must be cleared before release, autocreator: %@",
8098           [self class], _autocreator);
8099  [_dictionary release];
8100  [super dealloc];
8101}
8102
8103- (instancetype)copyWithZone:(NSZone *)zone {
8104  return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
8105}
8106
8107- (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
8108  if (self == other) {
8109    return YES;
8110  }
8111  if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
8112    return NO;
8113  }
8114  return [_dictionary isEqual:other->_dictionary];
8115}
8116
8117- (NSUInteger)hash {
8118  return _dictionary.count;
8119}
8120
8121- (NSString *)description {
8122  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8123}
8124
8125- (NSUInteger)count {
8126  return _dictionary.count;
8127}
8128
8129- (void)enumerateKeysAndValuesUsingBlock:
8130    (void (^)(int64_t key, int64_t value, BOOL *stop))block {
8131  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8132                                                   NSNumber *aValue,
8133                                                   BOOL *stop) {
8134      block([aKey longLongValue], [aValue longLongValue], stop);
8135  }];
8136}
8137
8138- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8139  NSUInteger count = _dictionary.count;
8140  if (count == 0) {
8141    return 0;
8142  }
8143
8144  GPBDataType valueDataType = GPBGetFieldDataType(field);
8145  GPBDataType keyDataType = field.mapKeyDataType;
8146  __block size_t result = 0;
8147  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8148                                                   NSNumber *aValue,
8149                                                   BOOL *stop) {
8150    #pragma unused(stop)
8151    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8152    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
8153    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8154  }];
8155  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8156  result += tagSize * count;
8157  return result;
8158}
8159
8160- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8161                         asField:(GPBFieldDescriptor *)field {
8162  GPBDataType valueDataType = GPBGetFieldDataType(field);
8163  GPBDataType keyDataType = field.mapKeyDataType;
8164  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8165  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8166                                                   NSNumber *aValue,
8167                                                   BOOL *stop) {
8168    #pragma unused(stop)
8169    // Write the tag.
8170    [outputStream writeInt32NoTag:tag];
8171    // Write the size of the message.
8172    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8173    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
8174    [outputStream writeInt32NoTag:(int32_t)msgSize];
8175    // Write the fields.
8176    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8177    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
8178  }];
8179}
8180
8181- (void)setGPBGenericValue:(GPBGenericValue *)value
8182     forGPBGenericValueKey:(GPBGenericValue *)key {
8183  [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
8184}
8185
8186- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8187  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
8188      #pragma unused(stop)
8189      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
8190  }];
8191}
8192
8193- (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
8194  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8195  if (wrapped && value) {
8196    *value = [wrapped longLongValue];
8197  }
8198  return (wrapped != NULL);
8199}
8200
8201- (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
8202  if (otherDictionary) {
8203    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8204    if (_autocreator) {
8205      GPBAutocreatedDictionaryModified(_autocreator, self);
8206    }
8207  }
8208}
8209
8210- (void)setValue:(int64_t)value forKey:(int64_t)key {
8211  [_dictionary setObject:@(value) forKey:@(key)];
8212  if (_autocreator) {
8213    GPBAutocreatedDictionaryModified(_autocreator, self);
8214  }
8215}
8216
8217- (void)removeValueForKey:(int64_t)aKey {
8218  [_dictionary removeObjectForKey:@(aKey)];
8219}
8220
8221- (void)removeAll {
8222  [_dictionary removeAllObjects];
8223}
8224
8225@end
8226
8227#pragma mark - Int64 -> Bool
8228
8229@implementation GPBInt64BoolDictionary {
8230 @package
8231  NSMutableDictionary *_dictionary;
8232}
8233
8234+ (instancetype)dictionary {
8235  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
8236}
8237
8238+ (instancetype)dictionaryWithValue:(BOOL)value
8239                             forKey:(int64_t)key {
8240  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8241  // on to get the type correct.
8242  return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
8243                                                        forKeys:&key
8244                                                          count:1] autorelease];
8245}
8246
8247+ (instancetype)dictionaryWithValues:(const BOOL [])values
8248                             forKeys:(const int64_t [])keys
8249                               count:(NSUInteger)count {
8250  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8251  // on to get the type correct.
8252  return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
8253                                                        forKeys:keys
8254                                                          count:count] autorelease];
8255}
8256
8257+ (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
8258  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8259  // on to get the type correct.
8260  return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8261}
8262
8263+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8264  return [[[self alloc] initWithCapacity:numItems] autorelease];
8265}
8266
8267- (instancetype)init {
8268  return [self initWithValues:NULL forKeys:NULL count:0];
8269}
8270
8271- (instancetype)initWithValues:(const BOOL [])values
8272                       forKeys:(const int64_t [])keys
8273                         count:(NSUInteger)count {
8274  self = [super init];
8275  if (self) {
8276    _dictionary = [[NSMutableDictionary alloc] init];
8277    if (count && values && keys) {
8278      for (NSUInteger i = 0; i < count; ++i) {
8279        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8280      }
8281    }
8282  }
8283  return self;
8284}
8285
8286- (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
8287  self = [self initWithValues:NULL forKeys:NULL count:0];
8288  if (self) {
8289    if (dictionary) {
8290      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8291    }
8292  }
8293  return self;
8294}
8295
8296- (instancetype)initWithCapacity:(NSUInteger)numItems {
8297  #pragma unused(numItems)
8298  return [self initWithValues:NULL forKeys:NULL count:0];
8299}
8300
8301- (void)dealloc {
8302  NSAssert(!_autocreator,
8303           @"%@: Autocreator must be cleared before release, autocreator: %@",
8304           [self class], _autocreator);
8305  [_dictionary release];
8306  [super dealloc];
8307}
8308
8309- (instancetype)copyWithZone:(NSZone *)zone {
8310  return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
8311}
8312
8313- (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
8314  if (self == other) {
8315    return YES;
8316  }
8317  if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
8318    return NO;
8319  }
8320  return [_dictionary isEqual:other->_dictionary];
8321}
8322
8323- (NSUInteger)hash {
8324  return _dictionary.count;
8325}
8326
8327- (NSString *)description {
8328  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8329}
8330
8331- (NSUInteger)count {
8332  return _dictionary.count;
8333}
8334
8335- (void)enumerateKeysAndValuesUsingBlock:
8336    (void (^)(int64_t key, BOOL value, BOOL *stop))block {
8337  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8338                                                   NSNumber *aValue,
8339                                                   BOOL *stop) {
8340      block([aKey longLongValue], [aValue boolValue], stop);
8341  }];
8342}
8343
8344- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8345  NSUInteger count = _dictionary.count;
8346  if (count == 0) {
8347    return 0;
8348  }
8349
8350  GPBDataType valueDataType = GPBGetFieldDataType(field);
8351  GPBDataType keyDataType = field.mapKeyDataType;
8352  __block size_t result = 0;
8353  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8354                                                   NSNumber *aValue,
8355                                                   BOOL *stop) {
8356    #pragma unused(stop)
8357    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8358    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
8359    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8360  }];
8361  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8362  result += tagSize * count;
8363  return result;
8364}
8365
8366- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8367                         asField:(GPBFieldDescriptor *)field {
8368  GPBDataType valueDataType = GPBGetFieldDataType(field);
8369  GPBDataType keyDataType = field.mapKeyDataType;
8370  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8371  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8372                                                   NSNumber *aValue,
8373                                                   BOOL *stop) {
8374    #pragma unused(stop)
8375    // Write the tag.
8376    [outputStream writeInt32NoTag:tag];
8377    // Write the size of the message.
8378    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8379    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
8380    [outputStream writeInt32NoTag:(int32_t)msgSize];
8381    // Write the fields.
8382    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8383    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
8384  }];
8385}
8386
8387- (void)setGPBGenericValue:(GPBGenericValue *)value
8388     forGPBGenericValueKey:(GPBGenericValue *)key {
8389  [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
8390}
8391
8392- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8393  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
8394      #pragma unused(stop)
8395      block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
8396  }];
8397}
8398
8399- (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
8400  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8401  if (wrapped && value) {
8402    *value = [wrapped boolValue];
8403  }
8404  return (wrapped != NULL);
8405}
8406
8407- (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
8408  if (otherDictionary) {
8409    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8410    if (_autocreator) {
8411      GPBAutocreatedDictionaryModified(_autocreator, self);
8412    }
8413  }
8414}
8415
8416- (void)setValue:(BOOL)value forKey:(int64_t)key {
8417  [_dictionary setObject:@(value) forKey:@(key)];
8418  if (_autocreator) {
8419    GPBAutocreatedDictionaryModified(_autocreator, self);
8420  }
8421}
8422
8423- (void)removeValueForKey:(int64_t)aKey {
8424  [_dictionary removeObjectForKey:@(aKey)];
8425}
8426
8427- (void)removeAll {
8428  [_dictionary removeAllObjects];
8429}
8430
8431@end
8432
8433#pragma mark - Int64 -> Float
8434
8435@implementation GPBInt64FloatDictionary {
8436 @package
8437  NSMutableDictionary *_dictionary;
8438}
8439
8440+ (instancetype)dictionary {
8441  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
8442}
8443
8444+ (instancetype)dictionaryWithValue:(float)value
8445                             forKey:(int64_t)key {
8446  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8447  // on to get the type correct.
8448  return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
8449                                                         forKeys:&key
8450                                                           count:1] autorelease];
8451}
8452
8453+ (instancetype)dictionaryWithValues:(const float [])values
8454                             forKeys:(const int64_t [])keys
8455                               count:(NSUInteger)count {
8456  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8457  // on to get the type correct.
8458  return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
8459                                                         forKeys:keys
8460                                                           count:count] autorelease];
8461}
8462
8463+ (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
8464  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8465  // on to get the type correct.
8466  return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8467}
8468
8469+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8470  return [[[self alloc] initWithCapacity:numItems] autorelease];
8471}
8472
8473- (instancetype)init {
8474  return [self initWithValues:NULL forKeys:NULL count:0];
8475}
8476
8477- (instancetype)initWithValues:(const float [])values
8478                       forKeys:(const int64_t [])keys
8479                         count:(NSUInteger)count {
8480  self = [super init];
8481  if (self) {
8482    _dictionary = [[NSMutableDictionary alloc] init];
8483    if (count && values && keys) {
8484      for (NSUInteger i = 0; i < count; ++i) {
8485        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8486      }
8487    }
8488  }
8489  return self;
8490}
8491
8492- (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
8493  self = [self initWithValues:NULL forKeys:NULL count:0];
8494  if (self) {
8495    if (dictionary) {
8496      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8497    }
8498  }
8499  return self;
8500}
8501
8502- (instancetype)initWithCapacity:(NSUInteger)numItems {
8503  #pragma unused(numItems)
8504  return [self initWithValues:NULL forKeys:NULL count:0];
8505}
8506
8507- (void)dealloc {
8508  NSAssert(!_autocreator,
8509           @"%@: Autocreator must be cleared before release, autocreator: %@",
8510           [self class], _autocreator);
8511  [_dictionary release];
8512  [super dealloc];
8513}
8514
8515- (instancetype)copyWithZone:(NSZone *)zone {
8516  return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
8517}
8518
8519- (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
8520  if (self == other) {
8521    return YES;
8522  }
8523  if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
8524    return NO;
8525  }
8526  return [_dictionary isEqual:other->_dictionary];
8527}
8528
8529- (NSUInteger)hash {
8530  return _dictionary.count;
8531}
8532
8533- (NSString *)description {
8534  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8535}
8536
8537- (NSUInteger)count {
8538  return _dictionary.count;
8539}
8540
8541- (void)enumerateKeysAndValuesUsingBlock:
8542    (void (^)(int64_t key, float value, BOOL *stop))block {
8543  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8544                                                   NSNumber *aValue,
8545                                                   BOOL *stop) {
8546      block([aKey longLongValue], [aValue floatValue], stop);
8547  }];
8548}
8549
8550- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8551  NSUInteger count = _dictionary.count;
8552  if (count == 0) {
8553    return 0;
8554  }
8555
8556  GPBDataType valueDataType = GPBGetFieldDataType(field);
8557  GPBDataType keyDataType = field.mapKeyDataType;
8558  __block size_t result = 0;
8559  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8560                                                   NSNumber *aValue,
8561                                                   BOOL *stop) {
8562    #pragma unused(stop)
8563    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8564    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
8565    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8566  }];
8567  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8568  result += tagSize * count;
8569  return result;
8570}
8571
8572- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8573                         asField:(GPBFieldDescriptor *)field {
8574  GPBDataType valueDataType = GPBGetFieldDataType(field);
8575  GPBDataType keyDataType = field.mapKeyDataType;
8576  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8577  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8578                                                   NSNumber *aValue,
8579                                                   BOOL *stop) {
8580    #pragma unused(stop)
8581    // Write the tag.
8582    [outputStream writeInt32NoTag:tag];
8583    // Write the size of the message.
8584    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8585    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
8586    [outputStream writeInt32NoTag:(int32_t)msgSize];
8587    // Write the fields.
8588    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8589    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
8590  }];
8591}
8592
8593- (void)setGPBGenericValue:(GPBGenericValue *)value
8594     forGPBGenericValueKey:(GPBGenericValue *)key {
8595  [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
8596}
8597
8598- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8599  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
8600      #pragma unused(stop)
8601      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
8602  }];
8603}
8604
8605- (BOOL)valueForKey:(int64_t)key value:(float *)value {
8606  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8607  if (wrapped && value) {
8608    *value = [wrapped floatValue];
8609  }
8610  return (wrapped != NULL);
8611}
8612
8613- (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
8614  if (otherDictionary) {
8615    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8616    if (_autocreator) {
8617      GPBAutocreatedDictionaryModified(_autocreator, self);
8618    }
8619  }
8620}
8621
8622- (void)setValue:(float)value forKey:(int64_t)key {
8623  [_dictionary setObject:@(value) forKey:@(key)];
8624  if (_autocreator) {
8625    GPBAutocreatedDictionaryModified(_autocreator, self);
8626  }
8627}
8628
8629- (void)removeValueForKey:(int64_t)aKey {
8630  [_dictionary removeObjectForKey:@(aKey)];
8631}
8632
8633- (void)removeAll {
8634  [_dictionary removeAllObjects];
8635}
8636
8637@end
8638
8639#pragma mark - Int64 -> Double
8640
8641@implementation GPBInt64DoubleDictionary {
8642 @package
8643  NSMutableDictionary *_dictionary;
8644}
8645
8646+ (instancetype)dictionary {
8647  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
8648}
8649
8650+ (instancetype)dictionaryWithValue:(double)value
8651                             forKey:(int64_t)key {
8652  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8653  // on to get the type correct.
8654  return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
8655                                                          forKeys:&key
8656                                                            count:1] autorelease];
8657}
8658
8659+ (instancetype)dictionaryWithValues:(const double [])values
8660                             forKeys:(const int64_t [])keys
8661                               count:(NSUInteger)count {
8662  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8663  // on to get the type correct.
8664  return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
8665                                                          forKeys:keys
8666                                                            count:count] autorelease];
8667}
8668
8669+ (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
8670  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
8671  // on to get the type correct.
8672  return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8673}
8674
8675+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
8676  return [[[self alloc] initWithCapacity:numItems] autorelease];
8677}
8678
8679- (instancetype)init {
8680  return [self initWithValues:NULL forKeys:NULL count:0];
8681}
8682
8683- (instancetype)initWithValues:(const double [])values
8684                       forKeys:(const int64_t [])keys
8685                         count:(NSUInteger)count {
8686  self = [super init];
8687  if (self) {
8688    _dictionary = [[NSMutableDictionary alloc] init];
8689    if (count && values && keys) {
8690      for (NSUInteger i = 0; i < count; ++i) {
8691        [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
8692      }
8693    }
8694  }
8695  return self;
8696}
8697
8698- (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
8699  self = [self initWithValues:NULL forKeys:NULL count:0];
8700  if (self) {
8701    if (dictionary) {
8702      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8703    }
8704  }
8705  return self;
8706}
8707
8708- (instancetype)initWithCapacity:(NSUInteger)numItems {
8709  #pragma unused(numItems)
8710  return [self initWithValues:NULL forKeys:NULL count:0];
8711}
8712
8713- (void)dealloc {
8714  NSAssert(!_autocreator,
8715           @"%@: Autocreator must be cleared before release, autocreator: %@",
8716           [self class], _autocreator);
8717  [_dictionary release];
8718  [super dealloc];
8719}
8720
8721- (instancetype)copyWithZone:(NSZone *)zone {
8722  return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
8723}
8724
8725- (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
8726  if (self == other) {
8727    return YES;
8728  }
8729  if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
8730    return NO;
8731  }
8732  return [_dictionary isEqual:other->_dictionary];
8733}
8734
8735- (NSUInteger)hash {
8736  return _dictionary.count;
8737}
8738
8739- (NSString *)description {
8740  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8741}
8742
8743- (NSUInteger)count {
8744  return _dictionary.count;
8745}
8746
8747- (void)enumerateKeysAndValuesUsingBlock:
8748    (void (^)(int64_t key, double value, BOOL *stop))block {
8749  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8750                                                   NSNumber *aValue,
8751                                                   BOOL *stop) {
8752      block([aKey longLongValue], [aValue doubleValue], stop);
8753  }];
8754}
8755
8756- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8757  NSUInteger count = _dictionary.count;
8758  if (count == 0) {
8759    return 0;
8760  }
8761
8762  GPBDataType valueDataType = GPBGetFieldDataType(field);
8763  GPBDataType keyDataType = field.mapKeyDataType;
8764  __block size_t result = 0;
8765  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8766                                                   NSNumber *aValue,
8767                                                   BOOL *stop) {
8768    #pragma unused(stop)
8769    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8770    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
8771    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
8772  }];
8773  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
8774  result += tagSize * count;
8775  return result;
8776}
8777
8778- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
8779                         asField:(GPBFieldDescriptor *)field {
8780  GPBDataType valueDataType = GPBGetFieldDataType(field);
8781  GPBDataType keyDataType = field.mapKeyDataType;
8782  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
8783  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8784                                                   NSNumber *aValue,
8785                                                   BOOL *stop) {
8786    #pragma unused(stop)
8787    // Write the tag.
8788    [outputStream writeInt32NoTag:tag];
8789    // Write the size of the message.
8790    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8791    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
8792    [outputStream writeInt32NoTag:(int32_t)msgSize];
8793    // Write the fields.
8794    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
8795    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
8796  }];
8797}
8798
8799- (void)setGPBGenericValue:(GPBGenericValue *)value
8800     forGPBGenericValueKey:(GPBGenericValue *)key {
8801  [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
8802}
8803
8804- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
8805  [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
8806      #pragma unused(stop)
8807      block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
8808  }];
8809}
8810
8811- (BOOL)valueForKey:(int64_t)key value:(double *)value {
8812  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
8813  if (wrapped && value) {
8814    *value = [wrapped doubleValue];
8815  }
8816  return (wrapped != NULL);
8817}
8818
8819- (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
8820  if (otherDictionary) {
8821    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
8822    if (_autocreator) {
8823      GPBAutocreatedDictionaryModified(_autocreator, self);
8824    }
8825  }
8826}
8827
8828- (void)setValue:(double)value forKey:(int64_t)key {
8829  [_dictionary setObject:@(value) forKey:@(key)];
8830  if (_autocreator) {
8831    GPBAutocreatedDictionaryModified(_autocreator, self);
8832  }
8833}
8834
8835- (void)removeValueForKey:(int64_t)aKey {
8836  [_dictionary removeObjectForKey:@(aKey)];
8837}
8838
8839- (void)removeAll {
8840  [_dictionary removeAllObjects];
8841}
8842
8843@end
8844
8845#pragma mark - Int64 -> Enum
8846
8847@implementation GPBInt64EnumDictionary {
8848 @package
8849  NSMutableDictionary *_dictionary;
8850  GPBEnumValidationFunc _validationFunc;
8851}
8852
8853@synthesize validationFunc = _validationFunc;
8854
8855+ (instancetype)dictionary {
8856  return [[[self alloc] initWithValidationFunction:NULL
8857                                         rawValues:NULL
8858                                           forKeys:NULL
8859                                             count:0] autorelease];
8860}
8861
8862+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
8863  return [[[self alloc] initWithValidationFunction:func
8864                                         rawValues:NULL
8865                                           forKeys:NULL
8866                                             count:0] autorelease];
8867}
8868
8869+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
8870                                        rawValue:(int32_t)rawValue
8871                                          forKey:(int64_t)key {
8872  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8873  // on to get the type correct.
8874  return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
8875                                                                  rawValues:&rawValue
8876                                                                    forKeys:&key
8877                                                                      count:1] autorelease];
8878}
8879
8880+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
8881                                       rawValues:(const int32_t [])rawValues
8882                                         forKeys:(const int64_t [])keys
8883                                           count:(NSUInteger)count {
8884  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8885  // on to get the type correct.
8886  return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
8887                                                                  rawValues:rawValues
8888                                                                    forKeys:keys
8889                                                                      count:count] autorelease];
8890}
8891
8892+ (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
8893  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
8894  // on to get the type correct.
8895  return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
8896}
8897
8898+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
8899                                        capacity:(NSUInteger)numItems {
8900  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
8901}
8902
8903- (instancetype)init {
8904  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
8905}
8906
8907- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
8908  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
8909}
8910
8911- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
8912                                 rawValues:(const int32_t [])rawValues
8913                                   forKeys:(const int64_t [])keys
8914                                     count:(NSUInteger)count {
8915  self = [super init];
8916  if (self) {
8917    _dictionary = [[NSMutableDictionary alloc] init];
8918    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
8919    if (count && rawValues && keys) {
8920      for (NSUInteger i = 0; i < count; ++i) {
8921        [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
8922      }
8923    }
8924  }
8925  return self;
8926}
8927
8928- (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
8929  self = [self initWithValidationFunction:dictionary.validationFunc
8930                                rawValues:NULL
8931                                  forKeys:NULL
8932                                    count:0];
8933  if (self) {
8934    if (dictionary) {
8935      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
8936    }
8937  }
8938  return self;
8939}
8940
8941- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
8942                                  capacity:(NSUInteger)numItems {
8943  #pragma unused(numItems)
8944  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
8945}
8946
8947- (void)dealloc {
8948  NSAssert(!_autocreator,
8949           @"%@: Autocreator must be cleared before release, autocreator: %@",
8950           [self class], _autocreator);
8951  [_dictionary release];
8952  [super dealloc];
8953}
8954
8955- (instancetype)copyWithZone:(NSZone *)zone {
8956  return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
8957}
8958
8959- (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
8960  if (self == other) {
8961    return YES;
8962  }
8963  if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
8964    return NO;
8965  }
8966  return [_dictionary isEqual:other->_dictionary];
8967}
8968
8969- (NSUInteger)hash {
8970  return _dictionary.count;
8971}
8972
8973- (NSString *)description {
8974  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
8975}
8976
8977- (NSUInteger)count {
8978  return _dictionary.count;
8979}
8980
8981- (void)enumerateKeysAndRawValuesUsingBlock:
8982    (void (^)(int64_t key, int32_t value, BOOL *stop))block {
8983  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
8984                                                   NSNumber *aValue,
8985                                                   BOOL *stop) {
8986      block([aKey longLongValue], [aValue intValue], stop);
8987  }];
8988}
8989
8990- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
8991  NSUInteger count = _dictionary.count;
8992  if (count == 0) {
8993    return 0;
8994  }
8995
8996  GPBDataType valueDataType = GPBGetFieldDataType(field);
8997  GPBDataType keyDataType = field.mapKeyDataType;
8998  __block size_t result = 0;
8999  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9000                                                   NSNumber *aValue,
9001                                                   BOOL *stop) {
9002    #pragma unused(stop)
9003    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9004    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9005    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9006  }];
9007  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9008  result += tagSize * count;
9009  return result;
9010}
9011
9012- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9013                         asField:(GPBFieldDescriptor *)field {
9014  GPBDataType valueDataType = GPBGetFieldDataType(field);
9015  GPBDataType keyDataType = field.mapKeyDataType;
9016  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9017  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9018                                                   NSNumber *aValue,
9019                                                   BOOL *stop) {
9020    #pragma unused(stop)
9021    // Write the tag.
9022    [outputStream writeInt32NoTag:tag];
9023    // Write the size of the message.
9024    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9025    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9026    [outputStream writeInt32NoTag:(int32_t)msgSize];
9027    // Write the fields.
9028    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9029    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
9030  }];
9031}
9032
9033- (NSData *)serializedDataForUnknownValue:(int32_t)value
9034                                   forKey:(GPBGenericValue *)key
9035                              keyDataType:(GPBDataType)keyDataType {
9036  size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
9037  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
9038  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
9039  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
9040  WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
9041  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
9042  [outputStream release];
9043  return data;
9044}
9045- (void)setGPBGenericValue:(GPBGenericValue *)value
9046     forGPBGenericValueKey:(GPBGenericValue *)key {
9047  [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
9048}
9049
9050- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9051  [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
9052      #pragma unused(stop)
9053      block([NSString stringWithFormat:@"%lld", key], @(value));
9054  }];
9055}
9056
9057- (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
9058  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
9059  if (wrapped && value) {
9060    int32_t result = [wrapped intValue];
9061    if (!_validationFunc(result)) {
9062      result = kGPBUnrecognizedEnumeratorValue;
9063    }
9064    *value = result;
9065  }
9066  return (wrapped != NULL);
9067}
9068
9069- (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
9070  NSNumber *wrapped = [_dictionary objectForKey:@(key)];
9071  if (wrapped && rawValue) {
9072    *rawValue = [wrapped intValue];
9073  }
9074  return (wrapped != NULL);
9075}
9076
9077- (void)enumerateKeysAndValuesUsingBlock:
9078    (void (^)(int64_t key, int32_t value, BOOL *stop))block {
9079  GPBEnumValidationFunc func = _validationFunc;
9080  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9081                                                   NSNumber *aValue,
9082                                                   BOOL *stop) {
9083      int32_t unwrapped = [aValue intValue];
9084      if (!func(unwrapped)) {
9085        unwrapped = kGPBUnrecognizedEnumeratorValue;
9086      }
9087      block([aKey longLongValue], unwrapped, stop);
9088  }];
9089}
9090
9091- (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
9092  if (otherDictionary) {
9093    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9094    if (_autocreator) {
9095      GPBAutocreatedDictionaryModified(_autocreator, self);
9096    }
9097  }
9098}
9099
9100- (void)setRawValue:(int32_t)value forKey:(int64_t)key {
9101  [_dictionary setObject:@(value) forKey:@(key)];
9102  if (_autocreator) {
9103    GPBAutocreatedDictionaryModified(_autocreator, self);
9104  }
9105}
9106
9107- (void)removeValueForKey:(int64_t)aKey {
9108  [_dictionary removeObjectForKey:@(aKey)];
9109}
9110
9111- (void)removeAll {
9112  [_dictionary removeAllObjects];
9113}
9114
9115- (void)setValue:(int32_t)value forKey:(int64_t)key {
9116  if (!_validationFunc(value)) {
9117    [NSException raise:NSInvalidArgumentException
9118                format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
9119                       value];
9120  }
9121
9122  [_dictionary setObject:@(value) forKey:@(key)];
9123  if (_autocreator) {
9124    GPBAutocreatedDictionaryModified(_autocreator, self);
9125  }
9126}
9127
9128@end
9129
9130#pragma mark - Int64 -> Object
9131
9132@implementation GPBInt64ObjectDictionary {
9133 @package
9134  NSMutableDictionary *_dictionary;
9135}
9136
9137+ (instancetype)dictionary {
9138  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
9139}
9140
9141+ (instancetype)dictionaryWithObject:(id)object
9142                              forKey:(int64_t)key {
9143  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
9144  // on to get the type correct.
9145  return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
9146                                                           forKeys:&key
9147                                                             count:1] autorelease];
9148}
9149
9150+ (instancetype)dictionaryWithObjects:(const id [])objects
9151                              forKeys:(const int64_t [])keys
9152                                count:(NSUInteger)count {
9153  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
9154  // on to get the type correct.
9155  return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
9156                                                          forKeys:keys
9157                                                            count:count] autorelease];
9158}
9159
9160+ (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
9161  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9162  // on to get the type correct.
9163  return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9164}
9165
9166+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9167  return [[[self alloc] initWithCapacity:numItems] autorelease];
9168}
9169
9170- (instancetype)init {
9171  return [self initWithObjects:NULL forKeys:NULL count:0];
9172}
9173
9174- (instancetype)initWithObjects:(const id [])objects
9175                        forKeys:(const int64_t [])keys
9176                          count:(NSUInteger)count {
9177  self = [super init];
9178  if (self) {
9179    _dictionary = [[NSMutableDictionary alloc] init];
9180    if (count && objects && keys) {
9181      for (NSUInteger i = 0; i < count; ++i) {
9182        if (!objects[i]) {
9183          [NSException raise:NSInvalidArgumentException
9184                      format:@"Attempting to add nil object to a Dictionary"];
9185        }
9186        [_dictionary setObject:objects[i] forKey:@(keys[i])];
9187      }
9188    }
9189  }
9190  return self;
9191}
9192
9193- (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
9194  self = [self initWithObjects:NULL forKeys:NULL count:0];
9195  if (self) {
9196    if (dictionary) {
9197      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9198    }
9199  }
9200  return self;
9201}
9202
9203- (instancetype)initWithCapacity:(NSUInteger)numItems {
9204  #pragma unused(numItems)
9205  return [self initWithObjects:NULL forKeys:NULL count:0];
9206}
9207
9208- (void)dealloc {
9209  NSAssert(!_autocreator,
9210           @"%@: Autocreator must be cleared before release, autocreator: %@",
9211           [self class], _autocreator);
9212  [_dictionary release];
9213  [super dealloc];
9214}
9215
9216- (instancetype)copyWithZone:(NSZone *)zone {
9217  return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
9218}
9219
9220- (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
9221  if (self == other) {
9222    return YES;
9223  }
9224  if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
9225    return NO;
9226  }
9227  return [_dictionary isEqual:other->_dictionary];
9228}
9229
9230- (NSUInteger)hash {
9231  return _dictionary.count;
9232}
9233
9234- (NSString *)description {
9235  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9236}
9237
9238- (NSUInteger)count {
9239  return _dictionary.count;
9240}
9241
9242- (void)enumerateKeysAndObjectsUsingBlock:
9243    (void (^)(int64_t key, id object, BOOL *stop))block {
9244  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9245                                                   id aObject,
9246                                                   BOOL *stop) {
9247      block([aKey longLongValue], aObject, stop);
9248  }];
9249}
9250
9251- (BOOL)isInitialized {
9252  for (GPBMessage *msg in [_dictionary objectEnumerator]) {
9253    if (!msg.initialized) {
9254      return NO;
9255    }
9256  }
9257  return YES;
9258}
9259
9260- (instancetype)deepCopyWithZone:(NSZone *)zone {
9261  GPBInt64ObjectDictionary *newDict =
9262      [[GPBInt64ObjectDictionary alloc] init];
9263  [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
9264                                                   GPBMessage *msg,
9265                                                   BOOL *stop) {
9266    #pragma unused(stop)
9267    GPBMessage *copiedMsg = [msg copyWithZone:zone];
9268    [newDict->_dictionary setObject:copiedMsg forKey:aKey];
9269    [copiedMsg release];
9270  }];
9271  return newDict;
9272}
9273
9274- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9275  NSUInteger count = _dictionary.count;
9276  if (count == 0) {
9277    return 0;
9278  }
9279
9280  GPBDataType valueDataType = GPBGetFieldDataType(field);
9281  GPBDataType keyDataType = field.mapKeyDataType;
9282  __block size_t result = 0;
9283  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9284                                                   id aObject,
9285                                                   BOOL *stop) {
9286    #pragma unused(stop)
9287    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9288    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
9289    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9290  }];
9291  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9292  result += tagSize * count;
9293  return result;
9294}
9295
9296- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9297                         asField:(GPBFieldDescriptor *)field {
9298  GPBDataType valueDataType = GPBGetFieldDataType(field);
9299  GPBDataType keyDataType = field.mapKeyDataType;
9300  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9301  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
9302                                                   id aObject,
9303                                                   BOOL *stop) {
9304    #pragma unused(stop)
9305    // Write the tag.
9306    [outputStream writeInt32NoTag:tag];
9307    // Write the size of the message.
9308    size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9309    msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
9310    [outputStream writeInt32NoTag:(int32_t)msgSize];
9311    // Write the fields.
9312    WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
9313    WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
9314  }];
9315}
9316
9317- (void)setGPBGenericValue:(GPBGenericValue *)value
9318     forGPBGenericValueKey:(GPBGenericValue *)key {
9319  [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
9320}
9321
9322- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9323  [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
9324      #pragma unused(stop)
9325      block([NSString stringWithFormat:@"%lld", key], object);
9326  }];
9327}
9328
9329- (id)objectForKey:(int64_t)key {
9330  id result = [_dictionary objectForKey:@(key)];
9331  return result;
9332}
9333
9334- (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
9335  if (otherDictionary) {
9336    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9337    if (_autocreator) {
9338      GPBAutocreatedDictionaryModified(_autocreator, self);
9339    }
9340  }
9341}
9342
9343- (void)setObject:(id)object forKey:(int64_t)key {
9344  if (!object) {
9345    [NSException raise:NSInvalidArgumentException
9346                format:@"Attempting to add nil object to a Dictionary"];
9347  }
9348  [_dictionary setObject:object forKey:@(key)];
9349  if (_autocreator) {
9350    GPBAutocreatedDictionaryModified(_autocreator, self);
9351  }
9352}
9353
9354- (void)removeObjectForKey:(int64_t)aKey {
9355  [_dictionary removeObjectForKey:@(aKey)];
9356}
9357
9358- (void)removeAll {
9359  [_dictionary removeAllObjects];
9360}
9361
9362@end
9363
9364//%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
9365// This block of code is generated, do not edit it directly.
9366
9367#pragma mark - String -> UInt32
9368
9369@implementation GPBStringUInt32Dictionary {
9370 @package
9371  NSMutableDictionary *_dictionary;
9372}
9373
9374+ (instancetype)dictionary {
9375  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
9376}
9377
9378+ (instancetype)dictionaryWithValue:(uint32_t)value
9379                             forKey:(NSString *)key {
9380  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
9381  // on to get the type correct.
9382  return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
9383                                                           forKeys:&key
9384                                                             count:1] autorelease];
9385}
9386
9387+ (instancetype)dictionaryWithValues:(const uint32_t [])values
9388                             forKeys:(const NSString * [])keys
9389                               count:(NSUInteger)count {
9390  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
9391  // on to get the type correct.
9392  return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
9393                                                           forKeys:keys
9394                                                             count:count] autorelease];
9395}
9396
9397+ (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
9398  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9399  // on to get the type correct.
9400  return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9401}
9402
9403+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9404  return [[[self alloc] initWithCapacity:numItems] autorelease];
9405}
9406
9407- (instancetype)init {
9408  return [self initWithValues:NULL forKeys:NULL count:0];
9409}
9410
9411- (instancetype)initWithValues:(const uint32_t [])values
9412                       forKeys:(const NSString * [])keys
9413                         count:(NSUInteger)count {
9414  self = [super init];
9415  if (self) {
9416    _dictionary = [[NSMutableDictionary alloc] init];
9417    if (count && values && keys) {
9418      for (NSUInteger i = 0; i < count; ++i) {
9419        if (!keys[i]) {
9420          [NSException raise:NSInvalidArgumentException
9421                      format:@"Attempting to add nil key to a Dictionary"];
9422        }
9423        [_dictionary setObject:@(values[i]) forKey:keys[i]];
9424      }
9425    }
9426  }
9427  return self;
9428}
9429
9430- (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
9431  self = [self initWithValues:NULL forKeys:NULL count:0];
9432  if (self) {
9433    if (dictionary) {
9434      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9435    }
9436  }
9437  return self;
9438}
9439
9440- (instancetype)initWithCapacity:(NSUInteger)numItems {
9441  #pragma unused(numItems)
9442  return [self initWithValues:NULL forKeys:NULL count:0];
9443}
9444
9445- (void)dealloc {
9446  NSAssert(!_autocreator,
9447           @"%@: Autocreator must be cleared before release, autocreator: %@",
9448           [self class], _autocreator);
9449  [_dictionary release];
9450  [super dealloc];
9451}
9452
9453- (instancetype)copyWithZone:(NSZone *)zone {
9454  return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
9455}
9456
9457- (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
9458  if (self == other) {
9459    return YES;
9460  }
9461  if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
9462    return NO;
9463  }
9464  return [_dictionary isEqual:other->_dictionary];
9465}
9466
9467- (NSUInteger)hash {
9468  return _dictionary.count;
9469}
9470
9471- (NSString *)description {
9472  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9473}
9474
9475- (NSUInteger)count {
9476  return _dictionary.count;
9477}
9478
9479- (void)enumerateKeysAndValuesUsingBlock:
9480    (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
9481  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9482                                                   NSNumber *aValue,
9483                                                   BOOL *stop) {
9484      block(aKey, [aValue unsignedIntValue], stop);
9485  }];
9486}
9487
9488- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9489  NSUInteger count = _dictionary.count;
9490  if (count == 0) {
9491    return 0;
9492  }
9493
9494  GPBDataType valueDataType = GPBGetFieldDataType(field);
9495  GPBDataType keyDataType = field.mapKeyDataType;
9496  __block size_t result = 0;
9497  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9498                                                   NSNumber *aValue,
9499                                                   BOOL *stop) {
9500    #pragma unused(stop)
9501    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9502    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
9503    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9504  }];
9505  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9506  result += tagSize * count;
9507  return result;
9508}
9509
9510- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9511                         asField:(GPBFieldDescriptor *)field {
9512  GPBDataType valueDataType = GPBGetFieldDataType(field);
9513  GPBDataType keyDataType = field.mapKeyDataType;
9514  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9515  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9516                                                   NSNumber *aValue,
9517                                                   BOOL *stop) {
9518    #pragma unused(stop)
9519    // Write the tag.
9520    [outputStream writeInt32NoTag:tag];
9521    // Write the size of the message.
9522    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9523    msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
9524    [outputStream writeInt32NoTag:(int32_t)msgSize];
9525    // Write the fields.
9526    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
9527    WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
9528  }];
9529}
9530
9531- (void)setGPBGenericValue:(GPBGenericValue *)value
9532     forGPBGenericValueKey:(GPBGenericValue *)key {
9533  [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
9534}
9535
9536- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9537  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
9538      #pragma unused(stop)
9539      block(key, [NSString stringWithFormat:@"%u", value]);
9540  }];
9541}
9542
9543- (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
9544  NSNumber *wrapped = [_dictionary objectForKey:key];
9545  if (wrapped && value) {
9546    *value = [wrapped unsignedIntValue];
9547  }
9548  return (wrapped != NULL);
9549}
9550
9551- (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
9552  if (otherDictionary) {
9553    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9554    if (_autocreator) {
9555      GPBAutocreatedDictionaryModified(_autocreator, self);
9556    }
9557  }
9558}
9559
9560- (void)setValue:(uint32_t)value forKey:(NSString *)key {
9561  if (!key) {
9562    [NSException raise:NSInvalidArgumentException
9563                format:@"Attempting to add nil key to a Dictionary"];
9564  }
9565  [_dictionary setObject:@(value) forKey:key];
9566  if (_autocreator) {
9567    GPBAutocreatedDictionaryModified(_autocreator, self);
9568  }
9569}
9570
9571- (void)removeValueForKey:(NSString *)aKey {
9572  [_dictionary removeObjectForKey:aKey];
9573}
9574
9575- (void)removeAll {
9576  [_dictionary removeAllObjects];
9577}
9578
9579@end
9580
9581#pragma mark - String -> Int32
9582
9583@implementation GPBStringInt32Dictionary {
9584 @package
9585  NSMutableDictionary *_dictionary;
9586}
9587
9588+ (instancetype)dictionary {
9589  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
9590}
9591
9592+ (instancetype)dictionaryWithValue:(int32_t)value
9593                             forKey:(NSString *)key {
9594  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
9595  // on to get the type correct.
9596  return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
9597                                                          forKeys:&key
9598                                                            count:1] autorelease];
9599}
9600
9601+ (instancetype)dictionaryWithValues:(const int32_t [])values
9602                             forKeys:(const NSString * [])keys
9603                               count:(NSUInteger)count {
9604  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
9605  // on to get the type correct.
9606  return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
9607                                                          forKeys:keys
9608                                                            count:count] autorelease];
9609}
9610
9611+ (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
9612  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9613  // on to get the type correct.
9614  return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9615}
9616
9617+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9618  return [[[self alloc] initWithCapacity:numItems] autorelease];
9619}
9620
9621- (instancetype)init {
9622  return [self initWithValues:NULL forKeys:NULL count:0];
9623}
9624
9625- (instancetype)initWithValues:(const int32_t [])values
9626                       forKeys:(const NSString * [])keys
9627                         count:(NSUInteger)count {
9628  self = [super init];
9629  if (self) {
9630    _dictionary = [[NSMutableDictionary alloc] init];
9631    if (count && values && keys) {
9632      for (NSUInteger i = 0; i < count; ++i) {
9633        if (!keys[i]) {
9634          [NSException raise:NSInvalidArgumentException
9635                      format:@"Attempting to add nil key to a Dictionary"];
9636        }
9637        [_dictionary setObject:@(values[i]) forKey:keys[i]];
9638      }
9639    }
9640  }
9641  return self;
9642}
9643
9644- (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
9645  self = [self initWithValues:NULL forKeys:NULL count:0];
9646  if (self) {
9647    if (dictionary) {
9648      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9649    }
9650  }
9651  return self;
9652}
9653
9654- (instancetype)initWithCapacity:(NSUInteger)numItems {
9655  #pragma unused(numItems)
9656  return [self initWithValues:NULL forKeys:NULL count:0];
9657}
9658
9659- (void)dealloc {
9660  NSAssert(!_autocreator,
9661           @"%@: Autocreator must be cleared before release, autocreator: %@",
9662           [self class], _autocreator);
9663  [_dictionary release];
9664  [super dealloc];
9665}
9666
9667- (instancetype)copyWithZone:(NSZone *)zone {
9668  return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
9669}
9670
9671- (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
9672  if (self == other) {
9673    return YES;
9674  }
9675  if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
9676    return NO;
9677  }
9678  return [_dictionary isEqual:other->_dictionary];
9679}
9680
9681- (NSUInteger)hash {
9682  return _dictionary.count;
9683}
9684
9685- (NSString *)description {
9686  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9687}
9688
9689- (NSUInteger)count {
9690  return _dictionary.count;
9691}
9692
9693- (void)enumerateKeysAndValuesUsingBlock:
9694    (void (^)(NSString *key, int32_t value, BOOL *stop))block {
9695  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9696                                                   NSNumber *aValue,
9697                                                   BOOL *stop) {
9698      block(aKey, [aValue intValue], stop);
9699  }];
9700}
9701
9702- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9703  NSUInteger count = _dictionary.count;
9704  if (count == 0) {
9705    return 0;
9706  }
9707
9708  GPBDataType valueDataType = GPBGetFieldDataType(field);
9709  GPBDataType keyDataType = field.mapKeyDataType;
9710  __block size_t result = 0;
9711  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9712                                                   NSNumber *aValue,
9713                                                   BOOL *stop) {
9714    #pragma unused(stop)
9715    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9716    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9717    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9718  }];
9719  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9720  result += tagSize * count;
9721  return result;
9722}
9723
9724- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9725                         asField:(GPBFieldDescriptor *)field {
9726  GPBDataType valueDataType = GPBGetFieldDataType(field);
9727  GPBDataType keyDataType = field.mapKeyDataType;
9728  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9729  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9730                                                   NSNumber *aValue,
9731                                                   BOOL *stop) {
9732    #pragma unused(stop)
9733    // Write the tag.
9734    [outputStream writeInt32NoTag:tag];
9735    // Write the size of the message.
9736    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9737    msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
9738    [outputStream writeInt32NoTag:(int32_t)msgSize];
9739    // Write the fields.
9740    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
9741    WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
9742  }];
9743}
9744
9745- (void)setGPBGenericValue:(GPBGenericValue *)value
9746     forGPBGenericValueKey:(GPBGenericValue *)key {
9747  [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
9748}
9749
9750- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9751  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
9752      #pragma unused(stop)
9753      block(key, [NSString stringWithFormat:@"%d", value]);
9754  }];
9755}
9756
9757- (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
9758  NSNumber *wrapped = [_dictionary objectForKey:key];
9759  if (wrapped && value) {
9760    *value = [wrapped intValue];
9761  }
9762  return (wrapped != NULL);
9763}
9764
9765- (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
9766  if (otherDictionary) {
9767    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9768    if (_autocreator) {
9769      GPBAutocreatedDictionaryModified(_autocreator, self);
9770    }
9771  }
9772}
9773
9774- (void)setValue:(int32_t)value forKey:(NSString *)key {
9775  if (!key) {
9776    [NSException raise:NSInvalidArgumentException
9777                format:@"Attempting to add nil key to a Dictionary"];
9778  }
9779  [_dictionary setObject:@(value) forKey:key];
9780  if (_autocreator) {
9781    GPBAutocreatedDictionaryModified(_autocreator, self);
9782  }
9783}
9784
9785- (void)removeValueForKey:(NSString *)aKey {
9786  [_dictionary removeObjectForKey:aKey];
9787}
9788
9789- (void)removeAll {
9790  [_dictionary removeAllObjects];
9791}
9792
9793@end
9794
9795#pragma mark - String -> UInt64
9796
9797@implementation GPBStringUInt64Dictionary {
9798 @package
9799  NSMutableDictionary *_dictionary;
9800}
9801
9802+ (instancetype)dictionary {
9803  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
9804}
9805
9806+ (instancetype)dictionaryWithValue:(uint64_t)value
9807                             forKey:(NSString *)key {
9808  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
9809  // on to get the type correct.
9810  return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
9811                                                           forKeys:&key
9812                                                             count:1] autorelease];
9813}
9814
9815+ (instancetype)dictionaryWithValues:(const uint64_t [])values
9816                             forKeys:(const NSString * [])keys
9817                               count:(NSUInteger)count {
9818  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
9819  // on to get the type correct.
9820  return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
9821                                                           forKeys:keys
9822                                                             count:count] autorelease];
9823}
9824
9825+ (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
9826  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
9827  // on to get the type correct.
9828  return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
9829}
9830
9831+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
9832  return [[[self alloc] initWithCapacity:numItems] autorelease];
9833}
9834
9835- (instancetype)init {
9836  return [self initWithValues:NULL forKeys:NULL count:0];
9837}
9838
9839- (instancetype)initWithValues:(const uint64_t [])values
9840                       forKeys:(const NSString * [])keys
9841                         count:(NSUInteger)count {
9842  self = [super init];
9843  if (self) {
9844    _dictionary = [[NSMutableDictionary alloc] init];
9845    if (count && values && keys) {
9846      for (NSUInteger i = 0; i < count; ++i) {
9847        if (!keys[i]) {
9848          [NSException raise:NSInvalidArgumentException
9849                      format:@"Attempting to add nil key to a Dictionary"];
9850        }
9851        [_dictionary setObject:@(values[i]) forKey:keys[i]];
9852      }
9853    }
9854  }
9855  return self;
9856}
9857
9858- (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
9859  self = [self initWithValues:NULL forKeys:NULL count:0];
9860  if (self) {
9861    if (dictionary) {
9862      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
9863    }
9864  }
9865  return self;
9866}
9867
9868- (instancetype)initWithCapacity:(NSUInteger)numItems {
9869  #pragma unused(numItems)
9870  return [self initWithValues:NULL forKeys:NULL count:0];
9871}
9872
9873- (void)dealloc {
9874  NSAssert(!_autocreator,
9875           @"%@: Autocreator must be cleared before release, autocreator: %@",
9876           [self class], _autocreator);
9877  [_dictionary release];
9878  [super dealloc];
9879}
9880
9881- (instancetype)copyWithZone:(NSZone *)zone {
9882  return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
9883}
9884
9885- (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
9886  if (self == other) {
9887    return YES;
9888  }
9889  if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
9890    return NO;
9891  }
9892  return [_dictionary isEqual:other->_dictionary];
9893}
9894
9895- (NSUInteger)hash {
9896  return _dictionary.count;
9897}
9898
9899- (NSString *)description {
9900  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
9901}
9902
9903- (NSUInteger)count {
9904  return _dictionary.count;
9905}
9906
9907- (void)enumerateKeysAndValuesUsingBlock:
9908    (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
9909  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9910                                                   NSNumber *aValue,
9911                                                   BOOL *stop) {
9912      block(aKey, [aValue unsignedLongLongValue], stop);
9913  }];
9914}
9915
9916- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
9917  NSUInteger count = _dictionary.count;
9918  if (count == 0) {
9919    return 0;
9920  }
9921
9922  GPBDataType valueDataType = GPBGetFieldDataType(field);
9923  GPBDataType keyDataType = field.mapKeyDataType;
9924  __block size_t result = 0;
9925  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9926                                                   NSNumber *aValue,
9927                                                   BOOL *stop) {
9928    #pragma unused(stop)
9929    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9930    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
9931    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
9932  }];
9933  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
9934  result += tagSize * count;
9935  return result;
9936}
9937
9938- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
9939                         asField:(GPBFieldDescriptor *)field {
9940  GPBDataType valueDataType = GPBGetFieldDataType(field);
9941  GPBDataType keyDataType = field.mapKeyDataType;
9942  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
9943  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
9944                                                   NSNumber *aValue,
9945                                                   BOOL *stop) {
9946    #pragma unused(stop)
9947    // Write the tag.
9948    [outputStream writeInt32NoTag:tag];
9949    // Write the size of the message.
9950    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
9951    msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
9952    [outputStream writeInt32NoTag:(int32_t)msgSize];
9953    // Write the fields.
9954    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
9955    WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
9956  }];
9957}
9958
9959- (void)setGPBGenericValue:(GPBGenericValue *)value
9960     forGPBGenericValueKey:(GPBGenericValue *)key {
9961  [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
9962}
9963
9964- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
9965  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
9966      #pragma unused(stop)
9967      block(key, [NSString stringWithFormat:@"%llu", value]);
9968  }];
9969}
9970
9971- (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
9972  NSNumber *wrapped = [_dictionary objectForKey:key];
9973  if (wrapped && value) {
9974    *value = [wrapped unsignedLongLongValue];
9975  }
9976  return (wrapped != NULL);
9977}
9978
9979- (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
9980  if (otherDictionary) {
9981    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
9982    if (_autocreator) {
9983      GPBAutocreatedDictionaryModified(_autocreator, self);
9984    }
9985  }
9986}
9987
9988- (void)setValue:(uint64_t)value forKey:(NSString *)key {
9989  if (!key) {
9990    [NSException raise:NSInvalidArgumentException
9991                format:@"Attempting to add nil key to a Dictionary"];
9992  }
9993  [_dictionary setObject:@(value) forKey:key];
9994  if (_autocreator) {
9995    GPBAutocreatedDictionaryModified(_autocreator, self);
9996  }
9997}
9998
9999- (void)removeValueForKey:(NSString *)aKey {
10000  [_dictionary removeObjectForKey:aKey];
10001}
10002
10003- (void)removeAll {
10004  [_dictionary removeAllObjects];
10005}
10006
10007@end
10008
10009#pragma mark - String -> Int64
10010
10011@implementation GPBStringInt64Dictionary {
10012 @package
10013  NSMutableDictionary *_dictionary;
10014}
10015
10016+ (instancetype)dictionary {
10017  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
10018}
10019
10020+ (instancetype)dictionaryWithValue:(int64_t)value
10021                             forKey:(NSString *)key {
10022  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10023  // on to get the type correct.
10024  return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
10025                                                          forKeys:&key
10026                                                            count:1] autorelease];
10027}
10028
10029+ (instancetype)dictionaryWithValues:(const int64_t [])values
10030                             forKeys:(const NSString * [])keys
10031                               count:(NSUInteger)count {
10032  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10033  // on to get the type correct.
10034  return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
10035                                                          forKeys:keys
10036                                                            count:count] autorelease];
10037}
10038
10039+ (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
10040  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10041  // on to get the type correct.
10042  return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10043}
10044
10045+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10046  return [[[self alloc] initWithCapacity:numItems] autorelease];
10047}
10048
10049- (instancetype)init {
10050  return [self initWithValues:NULL forKeys:NULL count:0];
10051}
10052
10053- (instancetype)initWithValues:(const int64_t [])values
10054                       forKeys:(const NSString * [])keys
10055                         count:(NSUInteger)count {
10056  self = [super init];
10057  if (self) {
10058    _dictionary = [[NSMutableDictionary alloc] init];
10059    if (count && values && keys) {
10060      for (NSUInteger i = 0; i < count; ++i) {
10061        if (!keys[i]) {
10062          [NSException raise:NSInvalidArgumentException
10063                      format:@"Attempting to add nil key to a Dictionary"];
10064        }
10065        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10066      }
10067    }
10068  }
10069  return self;
10070}
10071
10072- (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
10073  self = [self initWithValues:NULL forKeys:NULL count:0];
10074  if (self) {
10075    if (dictionary) {
10076      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10077    }
10078  }
10079  return self;
10080}
10081
10082- (instancetype)initWithCapacity:(NSUInteger)numItems {
10083  #pragma unused(numItems)
10084  return [self initWithValues:NULL forKeys:NULL count:0];
10085}
10086
10087- (void)dealloc {
10088  NSAssert(!_autocreator,
10089           @"%@: Autocreator must be cleared before release, autocreator: %@",
10090           [self class], _autocreator);
10091  [_dictionary release];
10092  [super dealloc];
10093}
10094
10095- (instancetype)copyWithZone:(NSZone *)zone {
10096  return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
10097}
10098
10099- (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
10100  if (self == other) {
10101    return YES;
10102  }
10103  if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
10104    return NO;
10105  }
10106  return [_dictionary isEqual:other->_dictionary];
10107}
10108
10109- (NSUInteger)hash {
10110  return _dictionary.count;
10111}
10112
10113- (NSString *)description {
10114  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10115}
10116
10117- (NSUInteger)count {
10118  return _dictionary.count;
10119}
10120
10121- (void)enumerateKeysAndValuesUsingBlock:
10122    (void (^)(NSString *key, int64_t value, BOOL *stop))block {
10123  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10124                                                   NSNumber *aValue,
10125                                                   BOOL *stop) {
10126      block(aKey, [aValue longLongValue], stop);
10127  }];
10128}
10129
10130- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10131  NSUInteger count = _dictionary.count;
10132  if (count == 0) {
10133    return 0;
10134  }
10135
10136  GPBDataType valueDataType = GPBGetFieldDataType(field);
10137  GPBDataType keyDataType = field.mapKeyDataType;
10138  __block size_t result = 0;
10139  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10140                                                   NSNumber *aValue,
10141                                                   BOOL *stop) {
10142    #pragma unused(stop)
10143    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10144    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
10145    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10146  }];
10147  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10148  result += tagSize * count;
10149  return result;
10150}
10151
10152- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10153                         asField:(GPBFieldDescriptor *)field {
10154  GPBDataType valueDataType = GPBGetFieldDataType(field);
10155  GPBDataType keyDataType = field.mapKeyDataType;
10156  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10157  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10158                                                   NSNumber *aValue,
10159                                                   BOOL *stop) {
10160    #pragma unused(stop)
10161    // Write the tag.
10162    [outputStream writeInt32NoTag:tag];
10163    // Write the size of the message.
10164    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10165    msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
10166    [outputStream writeInt32NoTag:(int32_t)msgSize];
10167    // Write the fields.
10168    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10169    WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
10170  }];
10171}
10172
10173- (void)setGPBGenericValue:(GPBGenericValue *)value
10174     forGPBGenericValueKey:(GPBGenericValue *)key {
10175  [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
10176}
10177
10178- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10179  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
10180      #pragma unused(stop)
10181      block(key, [NSString stringWithFormat:@"%lld", value]);
10182  }];
10183}
10184
10185- (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
10186  NSNumber *wrapped = [_dictionary objectForKey:key];
10187  if (wrapped && value) {
10188    *value = [wrapped longLongValue];
10189  }
10190  return (wrapped != NULL);
10191}
10192
10193- (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
10194  if (otherDictionary) {
10195    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10196    if (_autocreator) {
10197      GPBAutocreatedDictionaryModified(_autocreator, self);
10198    }
10199  }
10200}
10201
10202- (void)setValue:(int64_t)value forKey:(NSString *)key {
10203  if (!key) {
10204    [NSException raise:NSInvalidArgumentException
10205                format:@"Attempting to add nil key to a Dictionary"];
10206  }
10207  [_dictionary setObject:@(value) forKey:key];
10208  if (_autocreator) {
10209    GPBAutocreatedDictionaryModified(_autocreator, self);
10210  }
10211}
10212
10213- (void)removeValueForKey:(NSString *)aKey {
10214  [_dictionary removeObjectForKey:aKey];
10215}
10216
10217- (void)removeAll {
10218  [_dictionary removeAllObjects];
10219}
10220
10221@end
10222
10223#pragma mark - String -> Bool
10224
10225@implementation GPBStringBoolDictionary {
10226 @package
10227  NSMutableDictionary *_dictionary;
10228}
10229
10230+ (instancetype)dictionary {
10231  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
10232}
10233
10234+ (instancetype)dictionaryWithValue:(BOOL)value
10235                             forKey:(NSString *)key {
10236  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10237  // on to get the type correct.
10238  return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
10239                                                         forKeys:&key
10240                                                           count:1] autorelease];
10241}
10242
10243+ (instancetype)dictionaryWithValues:(const BOOL [])values
10244                             forKeys:(const NSString * [])keys
10245                               count:(NSUInteger)count {
10246  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10247  // on to get the type correct.
10248  return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
10249                                                         forKeys:keys
10250                                                           count:count] autorelease];
10251}
10252
10253+ (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
10254  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10255  // on to get the type correct.
10256  return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10257}
10258
10259+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10260  return [[[self alloc] initWithCapacity:numItems] autorelease];
10261}
10262
10263- (instancetype)init {
10264  return [self initWithValues:NULL forKeys:NULL count:0];
10265}
10266
10267- (instancetype)initWithValues:(const BOOL [])values
10268                       forKeys:(const NSString * [])keys
10269                         count:(NSUInteger)count {
10270  self = [super init];
10271  if (self) {
10272    _dictionary = [[NSMutableDictionary alloc] init];
10273    if (count && values && keys) {
10274      for (NSUInteger i = 0; i < count; ++i) {
10275        if (!keys[i]) {
10276          [NSException raise:NSInvalidArgumentException
10277                      format:@"Attempting to add nil key to a Dictionary"];
10278        }
10279        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10280      }
10281    }
10282  }
10283  return self;
10284}
10285
10286- (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
10287  self = [self initWithValues:NULL forKeys:NULL count:0];
10288  if (self) {
10289    if (dictionary) {
10290      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10291    }
10292  }
10293  return self;
10294}
10295
10296- (instancetype)initWithCapacity:(NSUInteger)numItems {
10297  #pragma unused(numItems)
10298  return [self initWithValues:NULL forKeys:NULL count:0];
10299}
10300
10301- (void)dealloc {
10302  NSAssert(!_autocreator,
10303           @"%@: Autocreator must be cleared before release, autocreator: %@",
10304           [self class], _autocreator);
10305  [_dictionary release];
10306  [super dealloc];
10307}
10308
10309- (instancetype)copyWithZone:(NSZone *)zone {
10310  return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
10311}
10312
10313- (BOOL)isEqual:(GPBStringBoolDictionary *)other {
10314  if (self == other) {
10315    return YES;
10316  }
10317  if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
10318    return NO;
10319  }
10320  return [_dictionary isEqual:other->_dictionary];
10321}
10322
10323- (NSUInteger)hash {
10324  return _dictionary.count;
10325}
10326
10327- (NSString *)description {
10328  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10329}
10330
10331- (NSUInteger)count {
10332  return _dictionary.count;
10333}
10334
10335- (void)enumerateKeysAndValuesUsingBlock:
10336    (void (^)(NSString *key, BOOL value, BOOL *stop))block {
10337  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10338                                                   NSNumber *aValue,
10339                                                   BOOL *stop) {
10340      block(aKey, [aValue boolValue], stop);
10341  }];
10342}
10343
10344- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10345  NSUInteger count = _dictionary.count;
10346  if (count == 0) {
10347    return 0;
10348  }
10349
10350  GPBDataType valueDataType = GPBGetFieldDataType(field);
10351  GPBDataType keyDataType = field.mapKeyDataType;
10352  __block size_t result = 0;
10353  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10354                                                   NSNumber *aValue,
10355                                                   BOOL *stop) {
10356    #pragma unused(stop)
10357    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10358    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
10359    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10360  }];
10361  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10362  result += tagSize * count;
10363  return result;
10364}
10365
10366- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10367                         asField:(GPBFieldDescriptor *)field {
10368  GPBDataType valueDataType = GPBGetFieldDataType(field);
10369  GPBDataType keyDataType = field.mapKeyDataType;
10370  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10371  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10372                                                   NSNumber *aValue,
10373                                                   BOOL *stop) {
10374    #pragma unused(stop)
10375    // Write the tag.
10376    [outputStream writeInt32NoTag:tag];
10377    // Write the size of the message.
10378    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10379    msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
10380    [outputStream writeInt32NoTag:(int32_t)msgSize];
10381    // Write the fields.
10382    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10383    WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
10384  }];
10385}
10386
10387- (void)setGPBGenericValue:(GPBGenericValue *)value
10388     forGPBGenericValueKey:(GPBGenericValue *)key {
10389  [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
10390}
10391
10392- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10393  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
10394      #pragma unused(stop)
10395      block(key, (value ? @"true" : @"false"));
10396  }];
10397}
10398
10399- (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
10400  NSNumber *wrapped = [_dictionary objectForKey:key];
10401  if (wrapped && value) {
10402    *value = [wrapped boolValue];
10403  }
10404  return (wrapped != NULL);
10405}
10406
10407- (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
10408  if (otherDictionary) {
10409    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10410    if (_autocreator) {
10411      GPBAutocreatedDictionaryModified(_autocreator, self);
10412    }
10413  }
10414}
10415
10416- (void)setValue:(BOOL)value forKey:(NSString *)key {
10417  if (!key) {
10418    [NSException raise:NSInvalidArgumentException
10419                format:@"Attempting to add nil key to a Dictionary"];
10420  }
10421  [_dictionary setObject:@(value) forKey:key];
10422  if (_autocreator) {
10423    GPBAutocreatedDictionaryModified(_autocreator, self);
10424  }
10425}
10426
10427- (void)removeValueForKey:(NSString *)aKey {
10428  [_dictionary removeObjectForKey:aKey];
10429}
10430
10431- (void)removeAll {
10432  [_dictionary removeAllObjects];
10433}
10434
10435@end
10436
10437#pragma mark - String -> Float
10438
10439@implementation GPBStringFloatDictionary {
10440 @package
10441  NSMutableDictionary *_dictionary;
10442}
10443
10444+ (instancetype)dictionary {
10445  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
10446}
10447
10448+ (instancetype)dictionaryWithValue:(float)value
10449                             forKey:(NSString *)key {
10450  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10451  // on to get the type correct.
10452  return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
10453                                                          forKeys:&key
10454                                                            count:1] autorelease];
10455}
10456
10457+ (instancetype)dictionaryWithValues:(const float [])values
10458                             forKeys:(const NSString * [])keys
10459                               count:(NSUInteger)count {
10460  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10461  // on to get the type correct.
10462  return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
10463                                                          forKeys:keys
10464                                                            count:count] autorelease];
10465}
10466
10467+ (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
10468  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10469  // on to get the type correct.
10470  return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10471}
10472
10473+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10474  return [[[self alloc] initWithCapacity:numItems] autorelease];
10475}
10476
10477- (instancetype)init {
10478  return [self initWithValues:NULL forKeys:NULL count:0];
10479}
10480
10481- (instancetype)initWithValues:(const float [])values
10482                       forKeys:(const NSString * [])keys
10483                         count:(NSUInteger)count {
10484  self = [super init];
10485  if (self) {
10486    _dictionary = [[NSMutableDictionary alloc] init];
10487    if (count && values && keys) {
10488      for (NSUInteger i = 0; i < count; ++i) {
10489        if (!keys[i]) {
10490          [NSException raise:NSInvalidArgumentException
10491                      format:@"Attempting to add nil key to a Dictionary"];
10492        }
10493        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10494      }
10495    }
10496  }
10497  return self;
10498}
10499
10500- (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
10501  self = [self initWithValues:NULL forKeys:NULL count:0];
10502  if (self) {
10503    if (dictionary) {
10504      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10505    }
10506  }
10507  return self;
10508}
10509
10510- (instancetype)initWithCapacity:(NSUInteger)numItems {
10511  #pragma unused(numItems)
10512  return [self initWithValues:NULL forKeys:NULL count:0];
10513}
10514
10515- (void)dealloc {
10516  NSAssert(!_autocreator,
10517           @"%@: Autocreator must be cleared before release, autocreator: %@",
10518           [self class], _autocreator);
10519  [_dictionary release];
10520  [super dealloc];
10521}
10522
10523- (instancetype)copyWithZone:(NSZone *)zone {
10524  return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
10525}
10526
10527- (BOOL)isEqual:(GPBStringFloatDictionary *)other {
10528  if (self == other) {
10529    return YES;
10530  }
10531  if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
10532    return NO;
10533  }
10534  return [_dictionary isEqual:other->_dictionary];
10535}
10536
10537- (NSUInteger)hash {
10538  return _dictionary.count;
10539}
10540
10541- (NSString *)description {
10542  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10543}
10544
10545- (NSUInteger)count {
10546  return _dictionary.count;
10547}
10548
10549- (void)enumerateKeysAndValuesUsingBlock:
10550    (void (^)(NSString *key, float value, BOOL *stop))block {
10551  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10552                                                   NSNumber *aValue,
10553                                                   BOOL *stop) {
10554      block(aKey, [aValue floatValue], stop);
10555  }];
10556}
10557
10558- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10559  NSUInteger count = _dictionary.count;
10560  if (count == 0) {
10561    return 0;
10562  }
10563
10564  GPBDataType valueDataType = GPBGetFieldDataType(field);
10565  GPBDataType keyDataType = field.mapKeyDataType;
10566  __block size_t result = 0;
10567  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10568                                                   NSNumber *aValue,
10569                                                   BOOL *stop) {
10570    #pragma unused(stop)
10571    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10572    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
10573    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10574  }];
10575  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10576  result += tagSize * count;
10577  return result;
10578}
10579
10580- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10581                         asField:(GPBFieldDescriptor *)field {
10582  GPBDataType valueDataType = GPBGetFieldDataType(field);
10583  GPBDataType keyDataType = field.mapKeyDataType;
10584  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10585  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10586                                                   NSNumber *aValue,
10587                                                   BOOL *stop) {
10588    #pragma unused(stop)
10589    // Write the tag.
10590    [outputStream writeInt32NoTag:tag];
10591    // Write the size of the message.
10592    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10593    msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
10594    [outputStream writeInt32NoTag:(int32_t)msgSize];
10595    // Write the fields.
10596    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10597    WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
10598  }];
10599}
10600
10601- (void)setGPBGenericValue:(GPBGenericValue *)value
10602     forGPBGenericValueKey:(GPBGenericValue *)key {
10603  [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
10604}
10605
10606- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10607  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
10608      #pragma unused(stop)
10609      block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
10610  }];
10611}
10612
10613- (BOOL)valueForKey:(NSString *)key value:(float *)value {
10614  NSNumber *wrapped = [_dictionary objectForKey:key];
10615  if (wrapped && value) {
10616    *value = [wrapped floatValue];
10617  }
10618  return (wrapped != NULL);
10619}
10620
10621- (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
10622  if (otherDictionary) {
10623    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10624    if (_autocreator) {
10625      GPBAutocreatedDictionaryModified(_autocreator, self);
10626    }
10627  }
10628}
10629
10630- (void)setValue:(float)value forKey:(NSString *)key {
10631  if (!key) {
10632    [NSException raise:NSInvalidArgumentException
10633                format:@"Attempting to add nil key to a Dictionary"];
10634  }
10635  [_dictionary setObject:@(value) forKey:key];
10636  if (_autocreator) {
10637    GPBAutocreatedDictionaryModified(_autocreator, self);
10638  }
10639}
10640
10641- (void)removeValueForKey:(NSString *)aKey {
10642  [_dictionary removeObjectForKey:aKey];
10643}
10644
10645- (void)removeAll {
10646  [_dictionary removeAllObjects];
10647}
10648
10649@end
10650
10651#pragma mark - String -> Double
10652
10653@implementation GPBStringDoubleDictionary {
10654 @package
10655  NSMutableDictionary *_dictionary;
10656}
10657
10658+ (instancetype)dictionary {
10659  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
10660}
10661
10662+ (instancetype)dictionaryWithValue:(double)value
10663                             forKey:(NSString *)key {
10664  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10665  // on to get the type correct.
10666  return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
10667                                                           forKeys:&key
10668                                                             count:1] autorelease];
10669}
10670
10671+ (instancetype)dictionaryWithValues:(const double [])values
10672                             forKeys:(const NSString * [])keys
10673                               count:(NSUInteger)count {
10674  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10675  // on to get the type correct.
10676  return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
10677                                                           forKeys:keys
10678                                                             count:count] autorelease];
10679}
10680
10681+ (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
10682  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
10683  // on to get the type correct.
10684  return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10685}
10686
10687+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
10688  return [[[self alloc] initWithCapacity:numItems] autorelease];
10689}
10690
10691- (instancetype)init {
10692  return [self initWithValues:NULL forKeys:NULL count:0];
10693}
10694
10695- (instancetype)initWithValues:(const double [])values
10696                       forKeys:(const NSString * [])keys
10697                         count:(NSUInteger)count {
10698  self = [super init];
10699  if (self) {
10700    _dictionary = [[NSMutableDictionary alloc] init];
10701    if (count && values && keys) {
10702      for (NSUInteger i = 0; i < count; ++i) {
10703        if (!keys[i]) {
10704          [NSException raise:NSInvalidArgumentException
10705                      format:@"Attempting to add nil key to a Dictionary"];
10706        }
10707        [_dictionary setObject:@(values[i]) forKey:keys[i]];
10708      }
10709    }
10710  }
10711  return self;
10712}
10713
10714- (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
10715  self = [self initWithValues:NULL forKeys:NULL count:0];
10716  if (self) {
10717    if (dictionary) {
10718      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10719    }
10720  }
10721  return self;
10722}
10723
10724- (instancetype)initWithCapacity:(NSUInteger)numItems {
10725  #pragma unused(numItems)
10726  return [self initWithValues:NULL forKeys:NULL count:0];
10727}
10728
10729- (void)dealloc {
10730  NSAssert(!_autocreator,
10731           @"%@: Autocreator must be cleared before release, autocreator: %@",
10732           [self class], _autocreator);
10733  [_dictionary release];
10734  [super dealloc];
10735}
10736
10737- (instancetype)copyWithZone:(NSZone *)zone {
10738  return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
10739}
10740
10741- (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
10742  if (self == other) {
10743    return YES;
10744  }
10745  if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
10746    return NO;
10747  }
10748  return [_dictionary isEqual:other->_dictionary];
10749}
10750
10751- (NSUInteger)hash {
10752  return _dictionary.count;
10753}
10754
10755- (NSString *)description {
10756  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10757}
10758
10759- (NSUInteger)count {
10760  return _dictionary.count;
10761}
10762
10763- (void)enumerateKeysAndValuesUsingBlock:
10764    (void (^)(NSString *key, double value, BOOL *stop))block {
10765  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10766                                                   NSNumber *aValue,
10767                                                   BOOL *stop) {
10768      block(aKey, [aValue doubleValue], stop);
10769  }];
10770}
10771
10772- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
10773  NSUInteger count = _dictionary.count;
10774  if (count == 0) {
10775    return 0;
10776  }
10777
10778  GPBDataType valueDataType = GPBGetFieldDataType(field);
10779  GPBDataType keyDataType = field.mapKeyDataType;
10780  __block size_t result = 0;
10781  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10782                                                   NSNumber *aValue,
10783                                                   BOOL *stop) {
10784    #pragma unused(stop)
10785    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10786    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
10787    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
10788  }];
10789  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
10790  result += tagSize * count;
10791  return result;
10792}
10793
10794- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
10795                         asField:(GPBFieldDescriptor *)field {
10796  GPBDataType valueDataType = GPBGetFieldDataType(field);
10797  GPBDataType keyDataType = field.mapKeyDataType;
10798  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
10799  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
10800                                                   NSNumber *aValue,
10801                                                   BOOL *stop) {
10802    #pragma unused(stop)
10803    // Write the tag.
10804    [outputStream writeInt32NoTag:tag];
10805    // Write the size of the message.
10806    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
10807    msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
10808    [outputStream writeInt32NoTag:(int32_t)msgSize];
10809    // Write the fields.
10810    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
10811    WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
10812  }];
10813}
10814
10815- (void)setGPBGenericValue:(GPBGenericValue *)value
10816     forGPBGenericValueKey:(GPBGenericValue *)key {
10817  [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
10818}
10819
10820- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
10821  [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
10822      #pragma unused(stop)
10823      block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
10824  }];
10825}
10826
10827- (BOOL)valueForKey:(NSString *)key value:(double *)value {
10828  NSNumber *wrapped = [_dictionary objectForKey:key];
10829  if (wrapped && value) {
10830    *value = [wrapped doubleValue];
10831  }
10832  return (wrapped != NULL);
10833}
10834
10835- (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
10836  if (otherDictionary) {
10837    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
10838    if (_autocreator) {
10839      GPBAutocreatedDictionaryModified(_autocreator, self);
10840    }
10841  }
10842}
10843
10844- (void)setValue:(double)value forKey:(NSString *)key {
10845  if (!key) {
10846    [NSException raise:NSInvalidArgumentException
10847                format:@"Attempting to add nil key to a Dictionary"];
10848  }
10849  [_dictionary setObject:@(value) forKey:key];
10850  if (_autocreator) {
10851    GPBAutocreatedDictionaryModified(_autocreator, self);
10852  }
10853}
10854
10855- (void)removeValueForKey:(NSString *)aKey {
10856  [_dictionary removeObjectForKey:aKey];
10857}
10858
10859- (void)removeAll {
10860  [_dictionary removeAllObjects];
10861}
10862
10863@end
10864
10865#pragma mark - String -> Enum
10866
10867@implementation GPBStringEnumDictionary {
10868 @package
10869  NSMutableDictionary *_dictionary;
10870  GPBEnumValidationFunc _validationFunc;
10871}
10872
10873@synthesize validationFunc = _validationFunc;
10874
10875+ (instancetype)dictionary {
10876  return [[[self alloc] initWithValidationFunction:NULL
10877                                         rawValues:NULL
10878                                           forKeys:NULL
10879                                             count:0] autorelease];
10880}
10881
10882+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
10883  return [[[self alloc] initWithValidationFunction:func
10884                                         rawValues:NULL
10885                                           forKeys:NULL
10886                                             count:0] autorelease];
10887}
10888
10889+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
10890                                        rawValue:(int32_t)rawValue
10891                                          forKey:(NSString *)key {
10892  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10893  // on to get the type correct.
10894  return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
10895                                                                   rawValues:&rawValue
10896                                                                     forKeys:&key
10897                                                                       count:1] autorelease];
10898}
10899
10900+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
10901                                       rawValues:(const int32_t [])rawValues
10902                                         forKeys:(const NSString * [])keys
10903                                           count:(NSUInteger)count {
10904  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10905  // on to get the type correct.
10906  return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
10907                                                                   rawValues:rawValues
10908                                                                     forKeys:keys
10909                                                                       count:count] autorelease];
10910}
10911
10912+ (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
10913  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
10914  // on to get the type correct.
10915  return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
10916}
10917
10918+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
10919                                        capacity:(NSUInteger)numItems {
10920  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
10921}
10922
10923- (instancetype)init {
10924  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
10925}
10926
10927- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
10928  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
10929}
10930
10931- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
10932                                 rawValues:(const int32_t [])rawValues
10933                                   forKeys:(const NSString * [])keys
10934                                     count:(NSUInteger)count {
10935  self = [super init];
10936  if (self) {
10937    _dictionary = [[NSMutableDictionary alloc] init];
10938    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
10939    if (count && rawValues && keys) {
10940      for (NSUInteger i = 0; i < count; ++i) {
10941        if (!keys[i]) {
10942          [NSException raise:NSInvalidArgumentException
10943                      format:@"Attempting to add nil key to a Dictionary"];
10944        }
10945        [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
10946      }
10947    }
10948  }
10949  return self;
10950}
10951
10952- (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
10953  self = [self initWithValidationFunction:dictionary.validationFunc
10954                                rawValues:NULL
10955                                  forKeys:NULL
10956                                    count:0];
10957  if (self) {
10958    if (dictionary) {
10959      [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
10960    }
10961  }
10962  return self;
10963}
10964
10965- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
10966                                  capacity:(NSUInteger)numItems {
10967  #pragma unused(numItems)
10968  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
10969}
10970
10971- (void)dealloc {
10972  NSAssert(!_autocreator,
10973           @"%@: Autocreator must be cleared before release, autocreator: %@",
10974           [self class], _autocreator);
10975  [_dictionary release];
10976  [super dealloc];
10977}
10978
10979- (instancetype)copyWithZone:(NSZone *)zone {
10980  return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
10981}
10982
10983- (BOOL)isEqual:(GPBStringEnumDictionary *)other {
10984  if (self == other) {
10985    return YES;
10986  }
10987  if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
10988    return NO;
10989  }
10990  return [_dictionary isEqual:other->_dictionary];
10991}
10992
10993- (NSUInteger)hash {
10994  return _dictionary.count;
10995}
10996
10997- (NSString *)description {
10998  return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
10999}
11000
11001- (NSUInteger)count {
11002  return _dictionary.count;
11003}
11004
11005- (void)enumerateKeysAndRawValuesUsingBlock:
11006    (void (^)(NSString *key, int32_t value, BOOL *stop))block {
11007  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11008                                                   NSNumber *aValue,
11009                                                   BOOL *stop) {
11010      block(aKey, [aValue intValue], stop);
11011  }];
11012}
11013
11014- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11015  NSUInteger count = _dictionary.count;
11016  if (count == 0) {
11017    return 0;
11018  }
11019
11020  GPBDataType valueDataType = GPBGetFieldDataType(field);
11021  GPBDataType keyDataType = field.mapKeyDataType;
11022  __block size_t result = 0;
11023  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11024                                                   NSNumber *aValue,
11025                                                   BOOL *stop) {
11026    #pragma unused(stop)
11027    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
11028    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
11029    result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11030  }];
11031  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11032  result += tagSize * count;
11033  return result;
11034}
11035
11036- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11037                         asField:(GPBFieldDescriptor *)field {
11038  GPBDataType valueDataType = GPBGetFieldDataType(field);
11039  GPBDataType keyDataType = field.mapKeyDataType;
11040  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11041  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11042                                                   NSNumber *aValue,
11043                                                   BOOL *stop) {
11044    #pragma unused(stop)
11045    // Write the tag.
11046    [outputStream writeInt32NoTag:tag];
11047    // Write the size of the message.
11048    size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
11049    msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
11050    [outputStream writeInt32NoTag:(int32_t)msgSize];
11051    // Write the fields.
11052    WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
11053    WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
11054  }];
11055}
11056
11057- (NSData *)serializedDataForUnknownValue:(int32_t)value
11058                                   forKey:(GPBGenericValue *)key
11059                              keyDataType:(GPBDataType)keyDataType {
11060  size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
11061  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
11062  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
11063  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
11064  WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
11065  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
11066  [outputStream release];
11067  return data;
11068}
11069- (void)setGPBGenericValue:(GPBGenericValue *)value
11070     forGPBGenericValueKey:(GPBGenericValue *)key {
11071  [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
11072}
11073
11074- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11075  [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
11076      #pragma unused(stop)
11077      block(key, @(value));
11078  }];
11079}
11080
11081- (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
11082  NSNumber *wrapped = [_dictionary objectForKey:key];
11083  if (wrapped && value) {
11084    int32_t result = [wrapped intValue];
11085    if (!_validationFunc(result)) {
11086      result = kGPBUnrecognizedEnumeratorValue;
11087    }
11088    *value = result;
11089  }
11090  return (wrapped != NULL);
11091}
11092
11093- (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
11094  NSNumber *wrapped = [_dictionary objectForKey:key];
11095  if (wrapped && rawValue) {
11096    *rawValue = [wrapped intValue];
11097  }
11098  return (wrapped != NULL);
11099}
11100
11101- (void)enumerateKeysAndValuesUsingBlock:
11102    (void (^)(NSString *key, int32_t value, BOOL *stop))block {
11103  GPBEnumValidationFunc func = _validationFunc;
11104  [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
11105                                                   NSNumber *aValue,
11106                                                   BOOL *stop) {
11107      int32_t unwrapped = [aValue intValue];
11108      if (!func(unwrapped)) {
11109        unwrapped = kGPBUnrecognizedEnumeratorValue;
11110      }
11111      block(aKey, unwrapped, stop);
11112  }];
11113}
11114
11115- (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
11116  if (otherDictionary) {
11117    [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
11118    if (_autocreator) {
11119      GPBAutocreatedDictionaryModified(_autocreator, self);
11120    }
11121  }
11122}
11123
11124- (void)setRawValue:(int32_t)value forKey:(NSString *)key {
11125  if (!key) {
11126    [NSException raise:NSInvalidArgumentException
11127                format:@"Attempting to add nil key to a Dictionary"];
11128  }
11129  [_dictionary setObject:@(value) forKey:key];
11130  if (_autocreator) {
11131    GPBAutocreatedDictionaryModified(_autocreator, self);
11132  }
11133}
11134
11135- (void)removeValueForKey:(NSString *)aKey {
11136  [_dictionary removeObjectForKey:aKey];
11137}
11138
11139- (void)removeAll {
11140  [_dictionary removeAllObjects];
11141}
11142
11143- (void)setValue:(int32_t)value forKey:(NSString *)key {
11144  if (!key) {
11145    [NSException raise:NSInvalidArgumentException
11146                format:@"Attempting to add nil key to a Dictionary"];
11147  }
11148  if (!_validationFunc(value)) {
11149    [NSException raise:NSInvalidArgumentException
11150                format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
11151                       value];
11152  }
11153
11154  [_dictionary setObject:@(value) forKey:key];
11155  if (_autocreator) {
11156    GPBAutocreatedDictionaryModified(_autocreator, self);
11157  }
11158}
11159
11160@end
11161
11162//%PDDM-EXPAND-END (5 expansions)
11163
11164
11165//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
11166// This block of code is generated, do not edit it directly.
11167
11168#pragma mark - Bool -> UInt32
11169
11170@implementation GPBBoolUInt32Dictionary {
11171 @package
11172  uint32_t _values[2];
11173  BOOL _valueSet[2];
11174}
11175
11176+ (instancetype)dictionary {
11177  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
11178}
11179
11180+ (instancetype)dictionaryWithValue:(uint32_t)value
11181                             forKey:(BOOL)key {
11182  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11183  // on to get the type correct.
11184  return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
11185                                                         forKeys:&key
11186                                                           count:1] autorelease];
11187}
11188
11189+ (instancetype)dictionaryWithValues:(const uint32_t [])values
11190                             forKeys:(const BOOL [])keys
11191                               count:(NSUInteger)count {
11192  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11193  // on to get the type correct.
11194  return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
11195                                                         forKeys:keys
11196                                                           count:count] autorelease];
11197}
11198
11199+ (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
11200  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11201  // on to get the type correct.
11202  return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11203}
11204
11205+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11206  return [[[self alloc] initWithCapacity:numItems] autorelease];
11207}
11208
11209- (instancetype)init {
11210  return [self initWithValues:NULL forKeys:NULL count:0];
11211}
11212
11213- (instancetype)initWithValues:(const uint32_t [])values
11214                       forKeys:(const BOOL [])keys
11215                         count:(NSUInteger)count {
11216  self = [super init];
11217  if (self) {
11218    for (NSUInteger i = 0; i < count; ++i) {
11219      int idx = keys[i] ? 1 : 0;
11220      _values[idx] = values[i];
11221      _valueSet[idx] = YES;
11222    }
11223  }
11224  return self;
11225}
11226
11227- (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
11228  self = [self initWithValues:NULL forKeys:NULL count:0];
11229  if (self) {
11230    if (dictionary) {
11231      for (int i = 0; i < 2; ++i) {
11232        if (dictionary->_valueSet[i]) {
11233          _values[i] = dictionary->_values[i];
11234          _valueSet[i] = YES;
11235        }
11236      }
11237    }
11238  }
11239  return self;
11240}
11241
11242- (instancetype)initWithCapacity:(NSUInteger)numItems {
11243  #pragma unused(numItems)
11244  return [self initWithValues:NULL forKeys:NULL count:0];
11245}
11246
11247#if !defined(NS_BLOCK_ASSERTIONS)
11248- (void)dealloc {
11249  NSAssert(!_autocreator,
11250           @"%@: Autocreator must be cleared before release, autocreator: %@",
11251           [self class], _autocreator);
11252  [super dealloc];
11253}
11254#endif  // !defined(NS_BLOCK_ASSERTIONS)
11255
11256- (instancetype)copyWithZone:(NSZone *)zone {
11257  return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
11258}
11259
11260- (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
11261  if (self == other) {
11262    return YES;
11263  }
11264  if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
11265    return NO;
11266  }
11267  if ((_valueSet[0] != other->_valueSet[0]) ||
11268      (_valueSet[1] != other->_valueSet[1])) {
11269    return NO;
11270  }
11271  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
11272      (_valueSet[1] && (_values[1] != other->_values[1]))) {
11273    return NO;
11274  }
11275  return YES;
11276}
11277
11278- (NSUInteger)hash {
11279  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11280}
11281
11282- (NSString *)description {
11283  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
11284  if (_valueSet[0]) {
11285    [result appendFormat:@"NO: %u", _values[0]];
11286  }
11287  if (_valueSet[1]) {
11288    [result appendFormat:@"YES: %u", _values[1]];
11289  }
11290  [result appendString:@" }"];
11291  return result;
11292}
11293
11294- (NSUInteger)count {
11295  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11296}
11297
11298- (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
11299  int idx = (key ? 1 : 0);
11300  if (_valueSet[idx]) {
11301    if (value) {
11302      *value = _values[idx];
11303    }
11304    return YES;
11305  }
11306  return NO;
11307}
11308
11309- (void)setGPBGenericValue:(GPBGenericValue *)value
11310     forGPBGenericValueKey:(GPBGenericValue *)key {
11311  int idx = (key->valueBool ? 1 : 0);
11312  _values[idx] = value->valueUInt32;
11313  _valueSet[idx] = YES;
11314}
11315
11316- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11317  if (_valueSet[0]) {
11318    block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
11319  }
11320  if (_valueSet[1]) {
11321    block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
11322  }
11323}
11324
11325- (void)enumerateKeysAndValuesUsingBlock:
11326    (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
11327  BOOL stop = NO;
11328  if (_valueSet[0]) {
11329    block(NO, _values[0], &stop);
11330  }
11331  if (!stop && _valueSet[1]) {
11332    block(YES, _values[1], &stop);
11333  }
11334}
11335
11336- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11337  GPBDataType valueDataType = GPBGetFieldDataType(field);
11338  NSUInteger count = 0;
11339  size_t result = 0;
11340  for (int i = 0; i < 2; ++i) {
11341    if (_valueSet[i]) {
11342      ++count;
11343      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11344      msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11345      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11346    }
11347  }
11348  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11349  result += tagSize * count;
11350  return result;
11351}
11352
11353- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11354                         asField:(GPBFieldDescriptor *)field {
11355  GPBDataType valueDataType = GPBGetFieldDataType(field);
11356  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11357  for (int i = 0; i < 2; ++i) {
11358    if (_valueSet[i]) {
11359      // Write the tag.
11360      [outputStream writeInt32NoTag:tag];
11361      // Write the size of the message.
11362      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11363      msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11364      [outputStream writeInt32NoTag:(int32_t)msgSize];
11365      // Write the fields.
11366      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11367      WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
11368    }
11369  }
11370}
11371
11372- (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
11373  if (otherDictionary) {
11374    for (int i = 0; i < 2; ++i) {
11375      if (otherDictionary->_valueSet[i]) {
11376        _valueSet[i] = YES;
11377        _values[i] = otherDictionary->_values[i];
11378      }
11379    }
11380    if (_autocreator) {
11381      GPBAutocreatedDictionaryModified(_autocreator, self);
11382    }
11383  }
11384}
11385
11386- (void)setValue:(uint32_t)value forKey:(BOOL)key {
11387  int idx = (key ? 1 : 0);
11388  _values[idx] = value;
11389  _valueSet[idx] = YES;
11390  if (_autocreator) {
11391    GPBAutocreatedDictionaryModified(_autocreator, self);
11392  }
11393}
11394
11395- (void)removeValueForKey:(BOOL)aKey {
11396  _valueSet[aKey ? 1 : 0] = NO;
11397}
11398
11399- (void)removeAll {
11400  _valueSet[0] = NO;
11401  _valueSet[1] = NO;
11402}
11403
11404@end
11405
11406//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
11407// This block of code is generated, do not edit it directly.
11408
11409#pragma mark - Bool -> Int32
11410
11411@implementation GPBBoolInt32Dictionary {
11412 @package
11413  int32_t _values[2];
11414  BOOL _valueSet[2];
11415}
11416
11417+ (instancetype)dictionary {
11418  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
11419}
11420
11421+ (instancetype)dictionaryWithValue:(int32_t)value
11422                             forKey:(BOOL)key {
11423  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11424  // on to get the type correct.
11425  return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
11426                                                        forKeys:&key
11427                                                          count:1] autorelease];
11428}
11429
11430+ (instancetype)dictionaryWithValues:(const int32_t [])values
11431                             forKeys:(const BOOL [])keys
11432                               count:(NSUInteger)count {
11433  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11434  // on to get the type correct.
11435  return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
11436                                                        forKeys:keys
11437                                                          count:count] autorelease];
11438}
11439
11440+ (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
11441  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11442  // on to get the type correct.
11443  return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11444}
11445
11446+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11447  return [[[self alloc] initWithCapacity:numItems] autorelease];
11448}
11449
11450- (instancetype)init {
11451  return [self initWithValues:NULL forKeys:NULL count:0];
11452}
11453
11454- (instancetype)initWithValues:(const int32_t [])values
11455                       forKeys:(const BOOL [])keys
11456                         count:(NSUInteger)count {
11457  self = [super init];
11458  if (self) {
11459    for (NSUInteger i = 0; i < count; ++i) {
11460      int idx = keys[i] ? 1 : 0;
11461      _values[idx] = values[i];
11462      _valueSet[idx] = YES;
11463    }
11464  }
11465  return self;
11466}
11467
11468- (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
11469  self = [self initWithValues:NULL forKeys:NULL count:0];
11470  if (self) {
11471    if (dictionary) {
11472      for (int i = 0; i < 2; ++i) {
11473        if (dictionary->_valueSet[i]) {
11474          _values[i] = dictionary->_values[i];
11475          _valueSet[i] = YES;
11476        }
11477      }
11478    }
11479  }
11480  return self;
11481}
11482
11483- (instancetype)initWithCapacity:(NSUInteger)numItems {
11484  #pragma unused(numItems)
11485  return [self initWithValues:NULL forKeys:NULL count:0];
11486}
11487
11488#if !defined(NS_BLOCK_ASSERTIONS)
11489- (void)dealloc {
11490  NSAssert(!_autocreator,
11491           @"%@: Autocreator must be cleared before release, autocreator: %@",
11492           [self class], _autocreator);
11493  [super dealloc];
11494}
11495#endif  // !defined(NS_BLOCK_ASSERTIONS)
11496
11497- (instancetype)copyWithZone:(NSZone *)zone {
11498  return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
11499}
11500
11501- (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
11502  if (self == other) {
11503    return YES;
11504  }
11505  if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
11506    return NO;
11507  }
11508  if ((_valueSet[0] != other->_valueSet[0]) ||
11509      (_valueSet[1] != other->_valueSet[1])) {
11510    return NO;
11511  }
11512  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
11513      (_valueSet[1] && (_values[1] != other->_values[1]))) {
11514    return NO;
11515  }
11516  return YES;
11517}
11518
11519- (NSUInteger)hash {
11520  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11521}
11522
11523- (NSString *)description {
11524  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
11525  if (_valueSet[0]) {
11526    [result appendFormat:@"NO: %d", _values[0]];
11527  }
11528  if (_valueSet[1]) {
11529    [result appendFormat:@"YES: %d", _values[1]];
11530  }
11531  [result appendString:@" }"];
11532  return result;
11533}
11534
11535- (NSUInteger)count {
11536  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11537}
11538
11539- (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
11540  int idx = (key ? 1 : 0);
11541  if (_valueSet[idx]) {
11542    if (value) {
11543      *value = _values[idx];
11544    }
11545    return YES;
11546  }
11547  return NO;
11548}
11549
11550- (void)setGPBGenericValue:(GPBGenericValue *)value
11551     forGPBGenericValueKey:(GPBGenericValue *)key {
11552  int idx = (key->valueBool ? 1 : 0);
11553  _values[idx] = value->valueInt32;
11554  _valueSet[idx] = YES;
11555}
11556
11557- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11558  if (_valueSet[0]) {
11559    block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
11560  }
11561  if (_valueSet[1]) {
11562    block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
11563  }
11564}
11565
11566- (void)enumerateKeysAndValuesUsingBlock:
11567    (void (^)(BOOL key, int32_t value, BOOL *stop))block {
11568  BOOL stop = NO;
11569  if (_valueSet[0]) {
11570    block(NO, _values[0], &stop);
11571  }
11572  if (!stop && _valueSet[1]) {
11573    block(YES, _values[1], &stop);
11574  }
11575}
11576
11577- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11578  GPBDataType valueDataType = GPBGetFieldDataType(field);
11579  NSUInteger count = 0;
11580  size_t result = 0;
11581  for (int i = 0; i < 2; ++i) {
11582    if (_valueSet[i]) {
11583      ++count;
11584      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11585      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11586      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11587    }
11588  }
11589  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11590  result += tagSize * count;
11591  return result;
11592}
11593
11594- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11595                         asField:(GPBFieldDescriptor *)field {
11596  GPBDataType valueDataType = GPBGetFieldDataType(field);
11597  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11598  for (int i = 0; i < 2; ++i) {
11599    if (_valueSet[i]) {
11600      // Write the tag.
11601      [outputStream writeInt32NoTag:tag];
11602      // Write the size of the message.
11603      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11604      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11605      [outputStream writeInt32NoTag:(int32_t)msgSize];
11606      // Write the fields.
11607      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11608      WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
11609    }
11610  }
11611}
11612
11613- (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
11614  if (otherDictionary) {
11615    for (int i = 0; i < 2; ++i) {
11616      if (otherDictionary->_valueSet[i]) {
11617        _valueSet[i] = YES;
11618        _values[i] = otherDictionary->_values[i];
11619      }
11620    }
11621    if (_autocreator) {
11622      GPBAutocreatedDictionaryModified(_autocreator, self);
11623    }
11624  }
11625}
11626
11627- (void)setValue:(int32_t)value forKey:(BOOL)key {
11628  int idx = (key ? 1 : 0);
11629  _values[idx] = value;
11630  _valueSet[idx] = YES;
11631  if (_autocreator) {
11632    GPBAutocreatedDictionaryModified(_autocreator, self);
11633  }
11634}
11635
11636- (void)removeValueForKey:(BOOL)aKey {
11637  _valueSet[aKey ? 1 : 0] = NO;
11638}
11639
11640- (void)removeAll {
11641  _valueSet[0] = NO;
11642  _valueSet[1] = NO;
11643}
11644
11645@end
11646
11647//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
11648// This block of code is generated, do not edit it directly.
11649
11650#pragma mark - Bool -> UInt64
11651
11652@implementation GPBBoolUInt64Dictionary {
11653 @package
11654  uint64_t _values[2];
11655  BOOL _valueSet[2];
11656}
11657
11658+ (instancetype)dictionary {
11659  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
11660}
11661
11662+ (instancetype)dictionaryWithValue:(uint64_t)value
11663                             forKey:(BOOL)key {
11664  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11665  // on to get the type correct.
11666  return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
11667                                                         forKeys:&key
11668                                                           count:1] autorelease];
11669}
11670
11671+ (instancetype)dictionaryWithValues:(const uint64_t [])values
11672                             forKeys:(const BOOL [])keys
11673                               count:(NSUInteger)count {
11674  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11675  // on to get the type correct.
11676  return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
11677                                                         forKeys:keys
11678                                                           count:count] autorelease];
11679}
11680
11681+ (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
11682  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11683  // on to get the type correct.
11684  return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11685}
11686
11687+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11688  return [[[self alloc] initWithCapacity:numItems] autorelease];
11689}
11690
11691- (instancetype)init {
11692  return [self initWithValues:NULL forKeys:NULL count:0];
11693}
11694
11695- (instancetype)initWithValues:(const uint64_t [])values
11696                       forKeys:(const BOOL [])keys
11697                         count:(NSUInteger)count {
11698  self = [super init];
11699  if (self) {
11700    for (NSUInteger i = 0; i < count; ++i) {
11701      int idx = keys[i] ? 1 : 0;
11702      _values[idx] = values[i];
11703      _valueSet[idx] = YES;
11704    }
11705  }
11706  return self;
11707}
11708
11709- (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
11710  self = [self initWithValues:NULL forKeys:NULL count:0];
11711  if (self) {
11712    if (dictionary) {
11713      for (int i = 0; i < 2; ++i) {
11714        if (dictionary->_valueSet[i]) {
11715          _values[i] = dictionary->_values[i];
11716          _valueSet[i] = YES;
11717        }
11718      }
11719    }
11720  }
11721  return self;
11722}
11723
11724- (instancetype)initWithCapacity:(NSUInteger)numItems {
11725  #pragma unused(numItems)
11726  return [self initWithValues:NULL forKeys:NULL count:0];
11727}
11728
11729#if !defined(NS_BLOCK_ASSERTIONS)
11730- (void)dealloc {
11731  NSAssert(!_autocreator,
11732           @"%@: Autocreator must be cleared before release, autocreator: %@",
11733           [self class], _autocreator);
11734  [super dealloc];
11735}
11736#endif  // !defined(NS_BLOCK_ASSERTIONS)
11737
11738- (instancetype)copyWithZone:(NSZone *)zone {
11739  return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
11740}
11741
11742- (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
11743  if (self == other) {
11744    return YES;
11745  }
11746  if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
11747    return NO;
11748  }
11749  if ((_valueSet[0] != other->_valueSet[0]) ||
11750      (_valueSet[1] != other->_valueSet[1])) {
11751    return NO;
11752  }
11753  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
11754      (_valueSet[1] && (_values[1] != other->_values[1]))) {
11755    return NO;
11756  }
11757  return YES;
11758}
11759
11760- (NSUInteger)hash {
11761  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11762}
11763
11764- (NSString *)description {
11765  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
11766  if (_valueSet[0]) {
11767    [result appendFormat:@"NO: %llu", _values[0]];
11768  }
11769  if (_valueSet[1]) {
11770    [result appendFormat:@"YES: %llu", _values[1]];
11771  }
11772  [result appendString:@" }"];
11773  return result;
11774}
11775
11776- (NSUInteger)count {
11777  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
11778}
11779
11780- (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
11781  int idx = (key ? 1 : 0);
11782  if (_valueSet[idx]) {
11783    if (value) {
11784      *value = _values[idx];
11785    }
11786    return YES;
11787  }
11788  return NO;
11789}
11790
11791- (void)setGPBGenericValue:(GPBGenericValue *)value
11792     forGPBGenericValueKey:(GPBGenericValue *)key {
11793  int idx = (key->valueBool ? 1 : 0);
11794  _values[idx] = value->valueUInt64;
11795  _valueSet[idx] = YES;
11796}
11797
11798- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
11799  if (_valueSet[0]) {
11800    block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
11801  }
11802  if (_valueSet[1]) {
11803    block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
11804  }
11805}
11806
11807- (void)enumerateKeysAndValuesUsingBlock:
11808    (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
11809  BOOL stop = NO;
11810  if (_valueSet[0]) {
11811    block(NO, _values[0], &stop);
11812  }
11813  if (!stop && _valueSet[1]) {
11814    block(YES, _values[1], &stop);
11815  }
11816}
11817
11818- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
11819  GPBDataType valueDataType = GPBGetFieldDataType(field);
11820  NSUInteger count = 0;
11821  size_t result = 0;
11822  for (int i = 0; i < 2; ++i) {
11823    if (_valueSet[i]) {
11824      ++count;
11825      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11826      msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11827      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
11828    }
11829  }
11830  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
11831  result += tagSize * count;
11832  return result;
11833}
11834
11835- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
11836                         asField:(GPBFieldDescriptor *)field {
11837  GPBDataType valueDataType = GPBGetFieldDataType(field);
11838  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
11839  for (int i = 0; i < 2; ++i) {
11840    if (_valueSet[i]) {
11841      // Write the tag.
11842      [outputStream writeInt32NoTag:tag];
11843      // Write the size of the message.
11844      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11845      msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
11846      [outputStream writeInt32NoTag:(int32_t)msgSize];
11847      // Write the fields.
11848      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
11849      WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
11850    }
11851  }
11852}
11853
11854- (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
11855  if (otherDictionary) {
11856    for (int i = 0; i < 2; ++i) {
11857      if (otherDictionary->_valueSet[i]) {
11858        _valueSet[i] = YES;
11859        _values[i] = otherDictionary->_values[i];
11860      }
11861    }
11862    if (_autocreator) {
11863      GPBAutocreatedDictionaryModified(_autocreator, self);
11864    }
11865  }
11866}
11867
11868- (void)setValue:(uint64_t)value forKey:(BOOL)key {
11869  int idx = (key ? 1 : 0);
11870  _values[idx] = value;
11871  _valueSet[idx] = YES;
11872  if (_autocreator) {
11873    GPBAutocreatedDictionaryModified(_autocreator, self);
11874  }
11875}
11876
11877- (void)removeValueForKey:(BOOL)aKey {
11878  _valueSet[aKey ? 1 : 0] = NO;
11879}
11880
11881- (void)removeAll {
11882  _valueSet[0] = NO;
11883  _valueSet[1] = NO;
11884}
11885
11886@end
11887
11888//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
11889// This block of code is generated, do not edit it directly.
11890
11891#pragma mark - Bool -> Int64
11892
11893@implementation GPBBoolInt64Dictionary {
11894 @package
11895  int64_t _values[2];
11896  BOOL _valueSet[2];
11897}
11898
11899+ (instancetype)dictionary {
11900  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
11901}
11902
11903+ (instancetype)dictionaryWithValue:(int64_t)value
11904                             forKey:(BOOL)key {
11905  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11906  // on to get the type correct.
11907  return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
11908                                                        forKeys:&key
11909                                                          count:1] autorelease];
11910}
11911
11912+ (instancetype)dictionaryWithValues:(const int64_t [])values
11913                             forKeys:(const BOOL [])keys
11914                               count:(NSUInteger)count {
11915  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
11916  // on to get the type correct.
11917  return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
11918                                                        forKeys:keys
11919                                                          count:count] autorelease];
11920}
11921
11922+ (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
11923  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
11924  // on to get the type correct.
11925  return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
11926}
11927
11928+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
11929  return [[[self alloc] initWithCapacity:numItems] autorelease];
11930}
11931
11932- (instancetype)init {
11933  return [self initWithValues:NULL forKeys:NULL count:0];
11934}
11935
11936- (instancetype)initWithValues:(const int64_t [])values
11937                       forKeys:(const BOOL [])keys
11938                         count:(NSUInteger)count {
11939  self = [super init];
11940  if (self) {
11941    for (NSUInteger i = 0; i < count; ++i) {
11942      int idx = keys[i] ? 1 : 0;
11943      _values[idx] = values[i];
11944      _valueSet[idx] = YES;
11945    }
11946  }
11947  return self;
11948}
11949
11950- (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
11951  self = [self initWithValues:NULL forKeys:NULL count:0];
11952  if (self) {
11953    if (dictionary) {
11954      for (int i = 0; i < 2; ++i) {
11955        if (dictionary->_valueSet[i]) {
11956          _values[i] = dictionary->_values[i];
11957          _valueSet[i] = YES;
11958        }
11959      }
11960    }
11961  }
11962  return self;
11963}
11964
11965- (instancetype)initWithCapacity:(NSUInteger)numItems {
11966  #pragma unused(numItems)
11967  return [self initWithValues:NULL forKeys:NULL count:0];
11968}
11969
11970#if !defined(NS_BLOCK_ASSERTIONS)
11971- (void)dealloc {
11972  NSAssert(!_autocreator,
11973           @"%@: Autocreator must be cleared before release, autocreator: %@",
11974           [self class], _autocreator);
11975  [super dealloc];
11976}
11977#endif  // !defined(NS_BLOCK_ASSERTIONS)
11978
11979- (instancetype)copyWithZone:(NSZone *)zone {
11980  return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
11981}
11982
11983- (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
11984  if (self == other) {
11985    return YES;
11986  }
11987  if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
11988    return NO;
11989  }
11990  if ((_valueSet[0] != other->_valueSet[0]) ||
11991      (_valueSet[1] != other->_valueSet[1])) {
11992    return NO;
11993  }
11994  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
11995      (_valueSet[1] && (_values[1] != other->_values[1]))) {
11996    return NO;
11997  }
11998  return YES;
11999}
12000
12001- (NSUInteger)hash {
12002  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12003}
12004
12005- (NSString *)description {
12006  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12007  if (_valueSet[0]) {
12008    [result appendFormat:@"NO: %lld", _values[0]];
12009  }
12010  if (_valueSet[1]) {
12011    [result appendFormat:@"YES: %lld", _values[1]];
12012  }
12013  [result appendString:@" }"];
12014  return result;
12015}
12016
12017- (NSUInteger)count {
12018  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12019}
12020
12021- (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
12022  int idx = (key ? 1 : 0);
12023  if (_valueSet[idx]) {
12024    if (value) {
12025      *value = _values[idx];
12026    }
12027    return YES;
12028  }
12029  return NO;
12030}
12031
12032- (void)setGPBGenericValue:(GPBGenericValue *)value
12033     forGPBGenericValueKey:(GPBGenericValue *)key {
12034  int idx = (key->valueBool ? 1 : 0);
12035  _values[idx] = value->valueInt64;
12036  _valueSet[idx] = YES;
12037}
12038
12039- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12040  if (_valueSet[0]) {
12041    block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
12042  }
12043  if (_valueSet[1]) {
12044    block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
12045  }
12046}
12047
12048- (void)enumerateKeysAndValuesUsingBlock:
12049    (void (^)(BOOL key, int64_t value, BOOL *stop))block {
12050  BOOL stop = NO;
12051  if (_valueSet[0]) {
12052    block(NO, _values[0], &stop);
12053  }
12054  if (!stop && _valueSet[1]) {
12055    block(YES, _values[1], &stop);
12056  }
12057}
12058
12059- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12060  GPBDataType valueDataType = GPBGetFieldDataType(field);
12061  NSUInteger count = 0;
12062  size_t result = 0;
12063  for (int i = 0; i < 2; ++i) {
12064    if (_valueSet[i]) {
12065      ++count;
12066      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12067      msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12068      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12069    }
12070  }
12071  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12072  result += tagSize * count;
12073  return result;
12074}
12075
12076- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12077                         asField:(GPBFieldDescriptor *)field {
12078  GPBDataType valueDataType = GPBGetFieldDataType(field);
12079  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12080  for (int i = 0; i < 2; ++i) {
12081    if (_valueSet[i]) {
12082      // Write the tag.
12083      [outputStream writeInt32NoTag:tag];
12084      // Write the size of the message.
12085      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12086      msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12087      [outputStream writeInt32NoTag:(int32_t)msgSize];
12088      // Write the fields.
12089      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12090      WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12091    }
12092  }
12093}
12094
12095- (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
12096  if (otherDictionary) {
12097    for (int i = 0; i < 2; ++i) {
12098      if (otherDictionary->_valueSet[i]) {
12099        _valueSet[i] = YES;
12100        _values[i] = otherDictionary->_values[i];
12101      }
12102    }
12103    if (_autocreator) {
12104      GPBAutocreatedDictionaryModified(_autocreator, self);
12105    }
12106  }
12107}
12108
12109- (void)setValue:(int64_t)value forKey:(BOOL)key {
12110  int idx = (key ? 1 : 0);
12111  _values[idx] = value;
12112  _valueSet[idx] = YES;
12113  if (_autocreator) {
12114    GPBAutocreatedDictionaryModified(_autocreator, self);
12115  }
12116}
12117
12118- (void)removeValueForKey:(BOOL)aKey {
12119  _valueSet[aKey ? 1 : 0] = NO;
12120}
12121
12122- (void)removeAll {
12123  _valueSet[0] = NO;
12124  _valueSet[1] = NO;
12125}
12126
12127@end
12128
12129//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
12130// This block of code is generated, do not edit it directly.
12131
12132#pragma mark - Bool -> Bool
12133
12134@implementation GPBBoolBoolDictionary {
12135 @package
12136  BOOL _values[2];
12137  BOOL _valueSet[2];
12138}
12139
12140+ (instancetype)dictionary {
12141  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
12142}
12143
12144+ (instancetype)dictionaryWithValue:(BOOL)value
12145                             forKey:(BOOL)key {
12146  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
12147  // on to get the type correct.
12148  return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
12149                                                       forKeys:&key
12150                                                         count:1] autorelease];
12151}
12152
12153+ (instancetype)dictionaryWithValues:(const BOOL [])values
12154                             forKeys:(const BOOL [])keys
12155                               count:(NSUInteger)count {
12156  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
12157  // on to get the type correct.
12158  return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
12159                                                       forKeys:keys
12160                                                         count:count] autorelease];
12161}
12162
12163+ (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
12164  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12165  // on to get the type correct.
12166  return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12167}
12168
12169+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12170  return [[[self alloc] initWithCapacity:numItems] autorelease];
12171}
12172
12173- (instancetype)init {
12174  return [self initWithValues:NULL forKeys:NULL count:0];
12175}
12176
12177- (instancetype)initWithValues:(const BOOL [])values
12178                       forKeys:(const BOOL [])keys
12179                         count:(NSUInteger)count {
12180  self = [super init];
12181  if (self) {
12182    for (NSUInteger i = 0; i < count; ++i) {
12183      int idx = keys[i] ? 1 : 0;
12184      _values[idx] = values[i];
12185      _valueSet[idx] = YES;
12186    }
12187  }
12188  return self;
12189}
12190
12191- (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
12192  self = [self initWithValues:NULL forKeys:NULL count:0];
12193  if (self) {
12194    if (dictionary) {
12195      for (int i = 0; i < 2; ++i) {
12196        if (dictionary->_valueSet[i]) {
12197          _values[i] = dictionary->_values[i];
12198          _valueSet[i] = YES;
12199        }
12200      }
12201    }
12202  }
12203  return self;
12204}
12205
12206- (instancetype)initWithCapacity:(NSUInteger)numItems {
12207  #pragma unused(numItems)
12208  return [self initWithValues:NULL forKeys:NULL count:0];
12209}
12210
12211#if !defined(NS_BLOCK_ASSERTIONS)
12212- (void)dealloc {
12213  NSAssert(!_autocreator,
12214           @"%@: Autocreator must be cleared before release, autocreator: %@",
12215           [self class], _autocreator);
12216  [super dealloc];
12217}
12218#endif  // !defined(NS_BLOCK_ASSERTIONS)
12219
12220- (instancetype)copyWithZone:(NSZone *)zone {
12221  return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
12222}
12223
12224- (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
12225  if (self == other) {
12226    return YES;
12227  }
12228  if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
12229    return NO;
12230  }
12231  if ((_valueSet[0] != other->_valueSet[0]) ||
12232      (_valueSet[1] != other->_valueSet[1])) {
12233    return NO;
12234  }
12235  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
12236      (_valueSet[1] && (_values[1] != other->_values[1]))) {
12237    return NO;
12238  }
12239  return YES;
12240}
12241
12242- (NSUInteger)hash {
12243  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12244}
12245
12246- (NSString *)description {
12247  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12248  if (_valueSet[0]) {
12249    [result appendFormat:@"NO: %d", _values[0]];
12250  }
12251  if (_valueSet[1]) {
12252    [result appendFormat:@"YES: %d", _values[1]];
12253  }
12254  [result appendString:@" }"];
12255  return result;
12256}
12257
12258- (NSUInteger)count {
12259  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12260}
12261
12262- (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
12263  int idx = (key ? 1 : 0);
12264  if (_valueSet[idx]) {
12265    if (value) {
12266      *value = _values[idx];
12267    }
12268    return YES;
12269  }
12270  return NO;
12271}
12272
12273- (void)setGPBGenericValue:(GPBGenericValue *)value
12274     forGPBGenericValueKey:(GPBGenericValue *)key {
12275  int idx = (key->valueBool ? 1 : 0);
12276  _values[idx] = value->valueBool;
12277  _valueSet[idx] = YES;
12278}
12279
12280- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12281  if (_valueSet[0]) {
12282    block(@"false", (_values[0] ? @"true" : @"false"));
12283  }
12284  if (_valueSet[1]) {
12285    block(@"true", (_values[1] ? @"true" : @"false"));
12286  }
12287}
12288
12289- (void)enumerateKeysAndValuesUsingBlock:
12290    (void (^)(BOOL key, BOOL value, BOOL *stop))block {
12291  BOOL stop = NO;
12292  if (_valueSet[0]) {
12293    block(NO, _values[0], &stop);
12294  }
12295  if (!stop && _valueSet[1]) {
12296    block(YES, _values[1], &stop);
12297  }
12298}
12299
12300- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12301  GPBDataType valueDataType = GPBGetFieldDataType(field);
12302  NSUInteger count = 0;
12303  size_t result = 0;
12304  for (int i = 0; i < 2; ++i) {
12305    if (_valueSet[i]) {
12306      ++count;
12307      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12308      msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12309      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12310    }
12311  }
12312  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12313  result += tagSize * count;
12314  return result;
12315}
12316
12317- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12318                         asField:(GPBFieldDescriptor *)field {
12319  GPBDataType valueDataType = GPBGetFieldDataType(field);
12320  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12321  for (int i = 0; i < 2; ++i) {
12322    if (_valueSet[i]) {
12323      // Write the tag.
12324      [outputStream writeInt32NoTag:tag];
12325      // Write the size of the message.
12326      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12327      msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12328      [outputStream writeInt32NoTag:(int32_t)msgSize];
12329      // Write the fields.
12330      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12331      WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12332    }
12333  }
12334}
12335
12336- (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
12337  if (otherDictionary) {
12338    for (int i = 0; i < 2; ++i) {
12339      if (otherDictionary->_valueSet[i]) {
12340        _valueSet[i] = YES;
12341        _values[i] = otherDictionary->_values[i];
12342      }
12343    }
12344    if (_autocreator) {
12345      GPBAutocreatedDictionaryModified(_autocreator, self);
12346    }
12347  }
12348}
12349
12350- (void)setValue:(BOOL)value forKey:(BOOL)key {
12351  int idx = (key ? 1 : 0);
12352  _values[idx] = value;
12353  _valueSet[idx] = YES;
12354  if (_autocreator) {
12355    GPBAutocreatedDictionaryModified(_autocreator, self);
12356  }
12357}
12358
12359- (void)removeValueForKey:(BOOL)aKey {
12360  _valueSet[aKey ? 1 : 0] = NO;
12361}
12362
12363- (void)removeAll {
12364  _valueSet[0] = NO;
12365  _valueSet[1] = NO;
12366}
12367
12368@end
12369
12370//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
12371// This block of code is generated, do not edit it directly.
12372
12373#pragma mark - Bool -> Float
12374
12375@implementation GPBBoolFloatDictionary {
12376 @package
12377  float _values[2];
12378  BOOL _valueSet[2];
12379}
12380
12381+ (instancetype)dictionary {
12382  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
12383}
12384
12385+ (instancetype)dictionaryWithValue:(float)value
12386                             forKey:(BOOL)key {
12387  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
12388  // on to get the type correct.
12389  return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
12390                                                        forKeys:&key
12391                                                          count:1] autorelease];
12392}
12393
12394+ (instancetype)dictionaryWithValues:(const float [])values
12395                             forKeys:(const BOOL [])keys
12396                               count:(NSUInteger)count {
12397  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
12398  // on to get the type correct.
12399  return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
12400                                                        forKeys:keys
12401                                                          count:count] autorelease];
12402}
12403
12404+ (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
12405  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12406  // on to get the type correct.
12407  return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12408}
12409
12410+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12411  return [[[self alloc] initWithCapacity:numItems] autorelease];
12412}
12413
12414- (instancetype)init {
12415  return [self initWithValues:NULL forKeys:NULL count:0];
12416}
12417
12418- (instancetype)initWithValues:(const float [])values
12419                       forKeys:(const BOOL [])keys
12420                         count:(NSUInteger)count {
12421  self = [super init];
12422  if (self) {
12423    for (NSUInteger i = 0; i < count; ++i) {
12424      int idx = keys[i] ? 1 : 0;
12425      _values[idx] = values[i];
12426      _valueSet[idx] = YES;
12427    }
12428  }
12429  return self;
12430}
12431
12432- (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
12433  self = [self initWithValues:NULL forKeys:NULL count:0];
12434  if (self) {
12435    if (dictionary) {
12436      for (int i = 0; i < 2; ++i) {
12437        if (dictionary->_valueSet[i]) {
12438          _values[i] = dictionary->_values[i];
12439          _valueSet[i] = YES;
12440        }
12441      }
12442    }
12443  }
12444  return self;
12445}
12446
12447- (instancetype)initWithCapacity:(NSUInteger)numItems {
12448  #pragma unused(numItems)
12449  return [self initWithValues:NULL forKeys:NULL count:0];
12450}
12451
12452#if !defined(NS_BLOCK_ASSERTIONS)
12453- (void)dealloc {
12454  NSAssert(!_autocreator,
12455           @"%@: Autocreator must be cleared before release, autocreator: %@",
12456           [self class], _autocreator);
12457  [super dealloc];
12458}
12459#endif  // !defined(NS_BLOCK_ASSERTIONS)
12460
12461- (instancetype)copyWithZone:(NSZone *)zone {
12462  return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
12463}
12464
12465- (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
12466  if (self == other) {
12467    return YES;
12468  }
12469  if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
12470    return NO;
12471  }
12472  if ((_valueSet[0] != other->_valueSet[0]) ||
12473      (_valueSet[1] != other->_valueSet[1])) {
12474    return NO;
12475  }
12476  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
12477      (_valueSet[1] && (_values[1] != other->_values[1]))) {
12478    return NO;
12479  }
12480  return YES;
12481}
12482
12483- (NSUInteger)hash {
12484  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12485}
12486
12487- (NSString *)description {
12488  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12489  if (_valueSet[0]) {
12490    [result appendFormat:@"NO: %f", _values[0]];
12491  }
12492  if (_valueSet[1]) {
12493    [result appendFormat:@"YES: %f", _values[1]];
12494  }
12495  [result appendString:@" }"];
12496  return result;
12497}
12498
12499- (NSUInteger)count {
12500  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12501}
12502
12503- (BOOL)valueForKey:(BOOL)key value:(float *)value {
12504  int idx = (key ? 1 : 0);
12505  if (_valueSet[idx]) {
12506    if (value) {
12507      *value = _values[idx];
12508    }
12509    return YES;
12510  }
12511  return NO;
12512}
12513
12514- (void)setGPBGenericValue:(GPBGenericValue *)value
12515     forGPBGenericValueKey:(GPBGenericValue *)key {
12516  int idx = (key->valueBool ? 1 : 0);
12517  _values[idx] = value->valueFloat;
12518  _valueSet[idx] = YES;
12519}
12520
12521- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12522  if (_valueSet[0]) {
12523    block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
12524  }
12525  if (_valueSet[1]) {
12526    block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
12527  }
12528}
12529
12530- (void)enumerateKeysAndValuesUsingBlock:
12531    (void (^)(BOOL key, float value, BOOL *stop))block {
12532  BOOL stop = NO;
12533  if (_valueSet[0]) {
12534    block(NO, _values[0], &stop);
12535  }
12536  if (!stop && _valueSet[1]) {
12537    block(YES, _values[1], &stop);
12538  }
12539}
12540
12541- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12542  GPBDataType valueDataType = GPBGetFieldDataType(field);
12543  NSUInteger count = 0;
12544  size_t result = 0;
12545  for (int i = 0; i < 2; ++i) {
12546    if (_valueSet[i]) {
12547      ++count;
12548      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12549      msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12550      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12551    }
12552  }
12553  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12554  result += tagSize * count;
12555  return result;
12556}
12557
12558- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12559                         asField:(GPBFieldDescriptor *)field {
12560  GPBDataType valueDataType = GPBGetFieldDataType(field);
12561  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12562  for (int i = 0; i < 2; ++i) {
12563    if (_valueSet[i]) {
12564      // Write the tag.
12565      [outputStream writeInt32NoTag:tag];
12566      // Write the size of the message.
12567      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12568      msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12569      [outputStream writeInt32NoTag:(int32_t)msgSize];
12570      // Write the fields.
12571      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12572      WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12573    }
12574  }
12575}
12576
12577- (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
12578  if (otherDictionary) {
12579    for (int i = 0; i < 2; ++i) {
12580      if (otherDictionary->_valueSet[i]) {
12581        _valueSet[i] = YES;
12582        _values[i] = otherDictionary->_values[i];
12583      }
12584    }
12585    if (_autocreator) {
12586      GPBAutocreatedDictionaryModified(_autocreator, self);
12587    }
12588  }
12589}
12590
12591- (void)setValue:(float)value forKey:(BOOL)key {
12592  int idx = (key ? 1 : 0);
12593  _values[idx] = value;
12594  _valueSet[idx] = YES;
12595  if (_autocreator) {
12596    GPBAutocreatedDictionaryModified(_autocreator, self);
12597  }
12598}
12599
12600- (void)removeValueForKey:(BOOL)aKey {
12601  _valueSet[aKey ? 1 : 0] = NO;
12602}
12603
12604- (void)removeAll {
12605  _valueSet[0] = NO;
12606  _valueSet[1] = NO;
12607}
12608
12609@end
12610
12611//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
12612// This block of code is generated, do not edit it directly.
12613
12614#pragma mark - Bool -> Double
12615
12616@implementation GPBBoolDoubleDictionary {
12617 @package
12618  double _values[2];
12619  BOOL _valueSet[2];
12620}
12621
12622+ (instancetype)dictionary {
12623  return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
12624}
12625
12626+ (instancetype)dictionaryWithValue:(double)value
12627                             forKey:(BOOL)key {
12628  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
12629  // on to get the type correct.
12630  return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
12631                                                         forKeys:&key
12632                                                           count:1] autorelease];
12633}
12634
12635+ (instancetype)dictionaryWithValues:(const double [])values
12636                             forKeys:(const BOOL [])keys
12637                               count:(NSUInteger)count {
12638  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
12639  // on to get the type correct.
12640  return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
12641                                                         forKeys:keys
12642                                                           count:count] autorelease];
12643}
12644
12645+ (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
12646  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12647  // on to get the type correct.
12648  return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12649}
12650
12651+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12652  return [[[self alloc] initWithCapacity:numItems] autorelease];
12653}
12654
12655- (instancetype)init {
12656  return [self initWithValues:NULL forKeys:NULL count:0];
12657}
12658
12659- (instancetype)initWithValues:(const double [])values
12660                       forKeys:(const BOOL [])keys
12661                         count:(NSUInteger)count {
12662  self = [super init];
12663  if (self) {
12664    for (NSUInteger i = 0; i < count; ++i) {
12665      int idx = keys[i] ? 1 : 0;
12666      _values[idx] = values[i];
12667      _valueSet[idx] = YES;
12668    }
12669  }
12670  return self;
12671}
12672
12673- (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
12674  self = [self initWithValues:NULL forKeys:NULL count:0];
12675  if (self) {
12676    if (dictionary) {
12677      for (int i = 0; i < 2; ++i) {
12678        if (dictionary->_valueSet[i]) {
12679          _values[i] = dictionary->_values[i];
12680          _valueSet[i] = YES;
12681        }
12682      }
12683    }
12684  }
12685  return self;
12686}
12687
12688- (instancetype)initWithCapacity:(NSUInteger)numItems {
12689  #pragma unused(numItems)
12690  return [self initWithValues:NULL forKeys:NULL count:0];
12691}
12692
12693#if !defined(NS_BLOCK_ASSERTIONS)
12694- (void)dealloc {
12695  NSAssert(!_autocreator,
12696           @"%@: Autocreator must be cleared before release, autocreator: %@",
12697           [self class], _autocreator);
12698  [super dealloc];
12699}
12700#endif  // !defined(NS_BLOCK_ASSERTIONS)
12701
12702- (instancetype)copyWithZone:(NSZone *)zone {
12703  return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
12704}
12705
12706- (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
12707  if (self == other) {
12708    return YES;
12709  }
12710  if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
12711    return NO;
12712  }
12713  if ((_valueSet[0] != other->_valueSet[0]) ||
12714      (_valueSet[1] != other->_valueSet[1])) {
12715    return NO;
12716  }
12717  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
12718      (_valueSet[1] && (_values[1] != other->_values[1]))) {
12719    return NO;
12720  }
12721  return YES;
12722}
12723
12724- (NSUInteger)hash {
12725  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12726}
12727
12728- (NSString *)description {
12729  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12730  if (_valueSet[0]) {
12731    [result appendFormat:@"NO: %lf", _values[0]];
12732  }
12733  if (_valueSet[1]) {
12734    [result appendFormat:@"YES: %lf", _values[1]];
12735  }
12736  [result appendString:@" }"];
12737  return result;
12738}
12739
12740- (NSUInteger)count {
12741  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
12742}
12743
12744- (BOOL)valueForKey:(BOOL)key value:(double *)value {
12745  int idx = (key ? 1 : 0);
12746  if (_valueSet[idx]) {
12747    if (value) {
12748      *value = _values[idx];
12749    }
12750    return YES;
12751  }
12752  return NO;
12753}
12754
12755- (void)setGPBGenericValue:(GPBGenericValue *)value
12756     forGPBGenericValueKey:(GPBGenericValue *)key {
12757  int idx = (key->valueBool ? 1 : 0);
12758  _values[idx] = value->valueDouble;
12759  _valueSet[idx] = YES;
12760}
12761
12762- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12763  if (_valueSet[0]) {
12764    block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
12765  }
12766  if (_valueSet[1]) {
12767    block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
12768  }
12769}
12770
12771- (void)enumerateKeysAndValuesUsingBlock:
12772    (void (^)(BOOL key, double value, BOOL *stop))block {
12773  BOOL stop = NO;
12774  if (_valueSet[0]) {
12775    block(NO, _values[0], &stop);
12776  }
12777  if (!stop && _valueSet[1]) {
12778    block(YES, _values[1], &stop);
12779  }
12780}
12781
12782- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
12783  GPBDataType valueDataType = GPBGetFieldDataType(field);
12784  NSUInteger count = 0;
12785  size_t result = 0;
12786  for (int i = 0; i < 2; ++i) {
12787    if (_valueSet[i]) {
12788      ++count;
12789      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12790      msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12791      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
12792    }
12793  }
12794  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
12795  result += tagSize * count;
12796  return result;
12797}
12798
12799- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
12800                         asField:(GPBFieldDescriptor *)field {
12801  GPBDataType valueDataType = GPBGetFieldDataType(field);
12802  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
12803  for (int i = 0; i < 2; ++i) {
12804    if (_valueSet[i]) {
12805      // Write the tag.
12806      [outputStream writeInt32NoTag:tag];
12807      // Write the size of the message.
12808      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12809      msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
12810      [outputStream writeInt32NoTag:(int32_t)msgSize];
12811      // Write the fields.
12812      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
12813      WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
12814    }
12815  }
12816}
12817
12818- (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
12819  if (otherDictionary) {
12820    for (int i = 0; i < 2; ++i) {
12821      if (otherDictionary->_valueSet[i]) {
12822        _valueSet[i] = YES;
12823        _values[i] = otherDictionary->_values[i];
12824      }
12825    }
12826    if (_autocreator) {
12827      GPBAutocreatedDictionaryModified(_autocreator, self);
12828    }
12829  }
12830}
12831
12832- (void)setValue:(double)value forKey:(BOOL)key {
12833  int idx = (key ? 1 : 0);
12834  _values[idx] = value;
12835  _valueSet[idx] = YES;
12836  if (_autocreator) {
12837    GPBAutocreatedDictionaryModified(_autocreator, self);
12838  }
12839}
12840
12841- (void)removeValueForKey:(BOOL)aKey {
12842  _valueSet[aKey ? 1 : 0] = NO;
12843}
12844
12845- (void)removeAll {
12846  _valueSet[0] = NO;
12847  _valueSet[1] = NO;
12848}
12849
12850@end
12851
12852//%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
12853// This block of code is generated, do not edit it directly.
12854
12855#pragma mark - Bool -> Object
12856
12857@implementation GPBBoolObjectDictionary {
12858 @package
12859  id _values[2];
12860}
12861
12862+ (instancetype)dictionary {
12863  return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
12864}
12865
12866+ (instancetype)dictionaryWithObject:(id)object
12867                              forKey:(BOOL)key {
12868  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
12869  // on to get the type correct.
12870  return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
12871                                                          forKeys:&key
12872                                                            count:1] autorelease];
12873}
12874
12875+ (instancetype)dictionaryWithObjects:(const id [])objects
12876                              forKeys:(const BOOL [])keys
12877                                count:(NSUInteger)count {
12878  // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
12879  // on to get the type correct.
12880  return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
12881                                                          forKeys:keys
12882                                                            count:count] autorelease];
12883}
12884
12885+ (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
12886  // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
12887  // on to get the type correct.
12888  return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
12889}
12890
12891+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
12892  return [[[self alloc] initWithCapacity:numItems] autorelease];
12893}
12894
12895- (instancetype)init {
12896  return [self initWithObjects:NULL forKeys:NULL count:0];
12897}
12898
12899- (instancetype)initWithObjects:(const id [])objects
12900                        forKeys:(const BOOL [])keys
12901                          count:(NSUInteger)count {
12902  self = [super init];
12903  if (self) {
12904    for (NSUInteger i = 0; i < count; ++i) {
12905      if (!objects[i]) {
12906        [NSException raise:NSInvalidArgumentException
12907                    format:@"Attempting to add nil object to a Dictionary"];
12908      }
12909      int idx = keys[i] ? 1 : 0;
12910      [_values[idx] release];
12911      _values[idx] = (id)[objects[i] retain];
12912    }
12913  }
12914  return self;
12915}
12916
12917- (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
12918  self = [self initWithObjects:NULL forKeys:NULL count:0];
12919  if (self) {
12920    if (dictionary) {
12921      _values[0] = [dictionary->_values[0] retain];
12922      _values[1] = [dictionary->_values[1] retain];
12923    }
12924  }
12925  return self;
12926}
12927
12928- (instancetype)initWithCapacity:(NSUInteger)numItems {
12929  #pragma unused(numItems)
12930  return [self initWithObjects:NULL forKeys:NULL count:0];
12931}
12932
12933- (void)dealloc {
12934  NSAssert(!_autocreator,
12935           @"%@: Autocreator must be cleared before release, autocreator: %@",
12936           [self class], _autocreator);
12937  [_values[0] release];
12938  [_values[1] release];
12939  [super dealloc];
12940}
12941
12942- (instancetype)copyWithZone:(NSZone *)zone {
12943  return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
12944}
12945
12946- (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
12947  if (self == other) {
12948    return YES;
12949  }
12950  if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
12951    return NO;
12952  }
12953  if (((_values[0] != nil) != (other->_values[0] != nil)) ||
12954      ((_values[1] != nil) != (other->_values[1] != nil))) {
12955    return NO;
12956  }
12957  if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
12958      ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
12959    return NO;
12960  }
12961  return YES;
12962}
12963
12964- (NSUInteger)hash {
12965  return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
12966}
12967
12968- (NSString *)description {
12969  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
12970  if ((_values[0] != nil)) {
12971    [result appendFormat:@"NO: %@", _values[0]];
12972  }
12973  if ((_values[1] != nil)) {
12974    [result appendFormat:@"YES: %@", _values[1]];
12975  }
12976  [result appendString:@" }"];
12977  return result;
12978}
12979
12980- (NSUInteger)count {
12981  return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
12982}
12983
12984- (id)objectForKey:(BOOL)key {
12985  return _values[key ? 1 : 0];
12986}
12987
12988- (void)setGPBGenericValue:(GPBGenericValue *)value
12989     forGPBGenericValueKey:(GPBGenericValue *)key {
12990  int idx = (key->valueBool ? 1 : 0);
12991  [_values[idx] release];
12992  _values[idx] = [value->valueString retain];
12993}
12994
12995- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
12996  if (_values[0] != nil) {
12997    block(@"false", _values[0]);
12998  }
12999  if ((_values[1] != nil)) {
13000    block(@"true", _values[1]);
13001  }
13002}
13003
13004- (void)enumerateKeysAndObjectsUsingBlock:
13005    (void (^)(BOOL key, id object, BOOL *stop))block {
13006  BOOL stop = NO;
13007  if (_values[0] != nil) {
13008    block(NO, _values[0], &stop);
13009  }
13010  if (!stop && (_values[1] != nil)) {
13011    block(YES, _values[1], &stop);
13012  }
13013}
13014
13015- (BOOL)isInitialized {
13016  if (_values[0] && ![_values[0] isInitialized]) {
13017    return NO;
13018  }
13019  if (_values[1] && ![_values[1] isInitialized]) {
13020    return NO;
13021  }
13022  return YES;
13023}
13024
13025- (instancetype)deepCopyWithZone:(NSZone *)zone {
13026  GPBBoolObjectDictionary *newDict =
13027      [[GPBBoolObjectDictionary alloc] init];
13028  for (int i = 0; i < 2; ++i) {
13029    if (_values[i] != nil) {
13030      newDict->_values[i] = [_values[i] copyWithZone:zone];
13031    }
13032  }
13033  return newDict;
13034}
13035
13036- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
13037  GPBDataType valueDataType = GPBGetFieldDataType(field);
13038  NSUInteger count = 0;
13039  size_t result = 0;
13040  for (int i = 0; i < 2; ++i) {
13041    if (_values[i] != nil) {
13042      ++count;
13043      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13044      msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13045      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
13046    }
13047  }
13048  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
13049  result += tagSize * count;
13050  return result;
13051}
13052
13053- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
13054                         asField:(GPBFieldDescriptor *)field {
13055  GPBDataType valueDataType = GPBGetFieldDataType(field);
13056  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
13057  for (int i = 0; i < 2; ++i) {
13058    if (_values[i] != nil) {
13059      // Write the tag.
13060      [outputStream writeInt32NoTag:tag];
13061      // Write the size of the message.
13062      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13063      msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13064      [outputStream writeInt32NoTag:(int32_t)msgSize];
13065      // Write the fields.
13066      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13067      WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
13068    }
13069  }
13070}
13071
13072- (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
13073  if (otherDictionary) {
13074    for (int i = 0; i < 2; ++i) {
13075      if (otherDictionary->_values[i] != nil) {
13076        [_values[i] release];
13077        _values[i] = [otherDictionary->_values[i] retain];
13078      }
13079    }
13080    if (_autocreator) {
13081      GPBAutocreatedDictionaryModified(_autocreator, self);
13082    }
13083  }
13084}
13085
13086- (void)setObject:(id)object forKey:(BOOL)key {
13087  if (!object) {
13088    [NSException raise:NSInvalidArgumentException
13089                format:@"Attempting to add nil object to a Dictionary"];
13090  }
13091  int idx = (key ? 1 : 0);
13092  [_values[idx] release];
13093  _values[idx] = [object retain];
13094  if (_autocreator) {
13095    GPBAutocreatedDictionaryModified(_autocreator, self);
13096  }
13097}
13098
13099- (void)removeObjectForKey:(BOOL)aKey {
13100  int idx = (aKey ? 1 : 0);
13101  [_values[idx] release];
13102  _values[idx] = nil;
13103}
13104
13105- (void)removeAll {
13106  for (int i = 0; i < 2; ++i) {
13107    [_values[i] release];
13108    _values[i] = nil;
13109  }
13110}
13111
13112@end
13113
13114//%PDDM-EXPAND-END (8 expansions)
13115
13116#pragma mark - Bool -> Enum
13117
13118@implementation GPBBoolEnumDictionary {
13119 @package
13120  GPBEnumValidationFunc _validationFunc;
13121  int32_t _values[2];
13122  BOOL _valueSet[2];
13123}
13124
13125@synthesize validationFunc = _validationFunc;
13126
13127+ (instancetype)dictionary {
13128  return [[[self alloc] initWithValidationFunction:NULL
13129                                         rawValues:NULL
13130                                           forKeys:NULL
13131                                             count:0] autorelease];
13132}
13133
13134+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
13135  return [[[self alloc] initWithValidationFunction:func
13136                                         rawValues:NULL
13137                                           forKeys:NULL
13138                                             count:0] autorelease];
13139}
13140
13141+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
13142                                       rawValue:(int32_t)rawValue
13143                                          forKey:(BOOL)key {
13144  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
13145  // on to get the type correct.
13146  return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
13147                                                                 rawValues:&rawValue
13148                                                                   forKeys:&key
13149                                                                     count:1] autorelease];
13150}
13151
13152+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
13153                                       rawValues:(const int32_t [])values
13154                                         forKeys:(const BOOL [])keys
13155                                           count:(NSUInteger)count {
13156  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
13157  // on to get the type correct.
13158  return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
13159                                                                 rawValues:values
13160                                                                   forKeys:keys
13161                                                                     count:count] autorelease];
13162}
13163
13164+ (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
13165  // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
13166  // on to get the type correct.
13167  return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
13168}
13169
13170+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
13171                                        capacity:(NSUInteger)numItems {
13172  return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
13173}
13174
13175- (instancetype)init {
13176  return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
13177}
13178
13179- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
13180  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
13181}
13182
13183- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
13184                                rawValues:(const int32_t [])rawValues
13185                                   forKeys:(const BOOL [])keys
13186                                     count:(NSUInteger)count {
13187  self = [super init];
13188  if (self) {
13189    _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
13190    for (NSUInteger i = 0; i < count; ++i) {
13191      int idx = keys[i] ? 1 : 0;
13192      _values[idx] = rawValues[i];
13193      _valueSet[idx] = YES;
13194    }
13195  }
13196  return self;
13197}
13198
13199- (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
13200  self = [self initWithValidationFunction:dictionary.validationFunc
13201                                rawValues:NULL
13202                                  forKeys:NULL
13203                                    count:0];
13204  if (self) {
13205    if (dictionary) {
13206      for (int i = 0; i < 2; ++i) {
13207        if (dictionary->_valueSet[i]) {
13208          _values[i] = dictionary->_values[i];
13209          _valueSet[i] = YES;
13210        }
13211      }
13212    }
13213  }
13214  return self;
13215}
13216
13217- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
13218                                  capacity:(NSUInteger)numItems {
13219#pragma unused(numItems)
13220  return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
13221}
13222
13223#if !defined(NS_BLOCK_ASSERTIONS)
13224- (void)dealloc {
13225  NSAssert(!_autocreator,
13226           @"%@: Autocreator must be cleared before release, autocreator: %@",
13227           [self class], _autocreator);
13228  [super dealloc];
13229}
13230#endif  // !defined(NS_BLOCK_ASSERTIONS)
13231
13232- (instancetype)copyWithZone:(NSZone *)zone {
13233  return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
13234}
13235
13236- (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
13237  if (self == other) {
13238    return YES;
13239  }
13240  if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
13241    return NO;
13242  }
13243  if ((_valueSet[0] != other->_valueSet[0]) ||
13244      (_valueSet[1] != other->_valueSet[1])) {
13245    return NO;
13246  }
13247  if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
13248      (_valueSet[1] && (_values[1] != other->_values[1]))) {
13249    return NO;
13250  }
13251  return YES;
13252}
13253
13254- (NSUInteger)hash {
13255  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
13256}
13257
13258- (NSString *)description {
13259  NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
13260  if (_valueSet[0]) {
13261    [result appendFormat:@"NO: %d", _values[0]];
13262  }
13263  if (_valueSet[1]) {
13264    [result appendFormat:@"YES: %d", _values[1]];
13265  }
13266  [result appendString:@" }"];
13267  return result;
13268}
13269
13270- (NSUInteger)count {
13271  return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
13272}
13273
13274- (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
13275  int idx = (key ? 1 : 0);
13276  if (_valueSet[idx]) {
13277    if (value) {
13278      int32_t result = _values[idx];
13279      if (!_validationFunc(result)) {
13280        result = kGPBUnrecognizedEnumeratorValue;
13281      }
13282      *value = result;
13283    }
13284    return YES;
13285  }
13286  return NO;
13287}
13288
13289- (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
13290  int idx = (key ? 1 : 0);
13291  if (_valueSet[idx]) {
13292    if (rawValue) {
13293      *rawValue = _values[idx];
13294    }
13295    return YES;
13296  }
13297  return NO;
13298}
13299
13300- (void)enumerateKeysAndValuesUsingBlock:
13301    (void (^)(BOOL key, int32_t value, BOOL *stop))block {
13302  BOOL stop = NO;
13303  if (_valueSet[0]) {
13304    block(NO, _values[0], &stop);
13305  }
13306  if (!stop && _valueSet[1]) {
13307    block(YES, _values[1], &stop);
13308  }
13309}
13310
13311- (void)enumerateKeysAndRawValuesUsingBlock:
13312    (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
13313  BOOL stop = NO;
13314  GPBEnumValidationFunc func = _validationFunc;
13315  int32_t validatedValue;
13316  if (_valueSet[0]) {
13317    validatedValue = _values[0];
13318    if (!func(validatedValue)) {
13319      validatedValue = kGPBUnrecognizedEnumeratorValue;
13320    }
13321    block(NO, validatedValue, &stop);
13322  }
13323  if (!stop && _valueSet[1]) {
13324    validatedValue = _values[1];
13325    if (!func(validatedValue)) {
13326      validatedValue = kGPBUnrecognizedEnumeratorValue;
13327    }
13328    block(YES, validatedValue, &stop);
13329  }
13330}
13331
13332//%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
13333// This block of code is generated, do not edit it directly.
13334
13335- (NSData *)serializedDataForUnknownValue:(int32_t)value
13336                                   forKey:(GPBGenericValue *)key
13337                              keyDataType:(GPBDataType)keyDataType {
13338  size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
13339  msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
13340  NSMutableData *data = [NSMutableData dataWithLength:msgSize];
13341  GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
13342  WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
13343  WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
13344  [outputStream release];
13345  return data;
13346}
13347
13348//%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
13349
13350- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
13351  GPBDataType valueDataType = GPBGetFieldDataType(field);
13352  NSUInteger count = 0;
13353  size_t result = 0;
13354  for (int i = 0; i < 2; ++i) {
13355    if (_valueSet[i]) {
13356      ++count;
13357      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13358      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13359      result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
13360    }
13361  }
13362  size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
13363  result += tagSize * count;
13364  return result;
13365}
13366
13367- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
13368                         asField:(GPBFieldDescriptor *)field {
13369  GPBDataType valueDataType = GPBGetFieldDataType(field);
13370  uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
13371  for (int i = 0; i < 2; ++i) {
13372    if (_valueSet[i]) {
13373      // Write the tag.
13374      [outputStream writeInt32NoTag:tag];
13375      // Write the size of the message.
13376      size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13377      msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
13378      [outputStream writeInt32NoTag:(int32_t)msgSize];
13379      // Write the fields.
13380      WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
13381      WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
13382    }
13383  }
13384}
13385
13386- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
13387  if (_valueSet[0]) {
13388    block(@"false", @(_values[0]));
13389  }
13390  if (_valueSet[1]) {
13391    block(@"true", @(_values[1]));
13392  }
13393}
13394
13395- (void)setGPBGenericValue:(GPBGenericValue *)value
13396     forGPBGenericValueKey:(GPBGenericValue *)key {
13397  int idx = (key->valueBool ? 1 : 0);
13398  _values[idx] = value->valueInt32;
13399  _valueSet[idx] = YES;
13400}
13401
13402- (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
13403  if (otherDictionary) {
13404    for (int i = 0; i < 2; ++i) {
13405      if (otherDictionary->_valueSet[i]) {
13406        _valueSet[i] = YES;
13407        _values[i] = otherDictionary->_values[i];
13408      }
13409    }
13410    if (_autocreator) {
13411      GPBAutocreatedDictionaryModified(_autocreator, self);
13412    }
13413  }
13414}
13415
13416- (void)setValue:(int32_t)value forKey:(BOOL)key {
13417  if (!_validationFunc(value)) {
13418    [NSException raise:NSInvalidArgumentException
13419                format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
13420     value];
13421  }
13422  int idx = (key ? 1 : 0);
13423  _values[idx] = value;
13424  _valueSet[idx] = YES;
13425  if (_autocreator) {
13426    GPBAutocreatedDictionaryModified(_autocreator, self);
13427  }
13428}
13429
13430- (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
13431  int idx = (key ? 1 : 0);
13432  _values[idx] = rawValue;
13433  _valueSet[idx] = YES;
13434  if (_autocreator) {
13435    GPBAutocreatedDictionaryModified(_autocreator, self);
13436  }
13437}
13438
13439- (void)removeValueForKey:(BOOL)aKey {
13440  _valueSet[aKey ? 1 : 0] = NO;
13441}
13442
13443- (void)removeAll {
13444  _valueSet[0] = NO;
13445  _valueSet[1] = NO;
13446}
13447
13448@end
13449
13450#pragma mark - NSDictionary Subclass
13451
13452@implementation GPBAutocreatedDictionary {
13453  NSMutableDictionary *_dictionary;
13454}
13455
13456- (void)dealloc {
13457  NSAssert(!_autocreator,
13458           @"%@: Autocreator must be cleared before release, autocreator: %@",
13459           [self class], _autocreator);
13460  [_dictionary release];
13461  [super dealloc];
13462}
13463
13464#pragma mark Required NSDictionary overrides
13465
13466- (instancetype)initWithObjects:(const id [])objects
13467                        forKeys:(const id<NSCopying> [])keys
13468                          count:(NSUInteger)count {
13469  self = [super init];
13470  if (self) {
13471    _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
13472                                                       forKeys:keys
13473                                                         count:count];
13474  }
13475  return self;
13476}
13477
13478- (NSUInteger)count {
13479  return [_dictionary count];
13480}
13481
13482- (id)objectForKey:(id)aKey {
13483  return [_dictionary objectForKey:aKey];
13484}
13485
13486- (NSEnumerator *)keyEnumerator {
13487  if (_dictionary == nil) {
13488    _dictionary = [[NSMutableDictionary alloc] init];
13489  }
13490  return [_dictionary keyEnumerator];
13491}
13492
13493#pragma mark Required NSMutableDictionary overrides
13494
13495// Only need to call GPBAutocreatedDictionaryModified() when adding things
13496// since we only autocreate empty dictionaries.
13497
13498- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
13499  if (_dictionary == nil) {
13500    _dictionary = [[NSMutableDictionary alloc] init];
13501  }
13502  [_dictionary setObject:anObject forKey:aKey];
13503  if (_autocreator) {
13504    GPBAutocreatedDictionaryModified(_autocreator, self);
13505  }
13506}
13507
13508- (void)removeObjectForKey:(id)aKey {
13509  [_dictionary removeObjectForKey:aKey];
13510}
13511
13512#pragma mark Extra things hooked
13513
13514- (id)copyWithZone:(NSZone *)zone {
13515  if (_dictionary == nil) {
13516    _dictionary = [[NSMutableDictionary alloc] init];
13517  }
13518  return [_dictionary copyWithZone:zone];
13519}
13520
13521- (id)mutableCopyWithZone:(NSZone *)zone {
13522  if (_dictionary == nil) {
13523    _dictionary = [[NSMutableDictionary alloc] init];
13524  }
13525  return [_dictionary mutableCopyWithZone:zone];
13526}
13527
13528- (id)objectForKeyedSubscript:(id)key {
13529  return [_dictionary objectForKeyedSubscript:key];
13530}
13531
13532- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
13533  if (_dictionary == nil) {
13534    _dictionary = [[NSMutableDictionary alloc] init];
13535  }
13536  [_dictionary setObject:obj forKeyedSubscript:key];
13537  if (_autocreator) {
13538    GPBAutocreatedDictionaryModified(_autocreator, self);
13539  }
13540}
13541
13542- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
13543                                                    id obj,
13544                                                    BOOL *stop))block {
13545  [_dictionary enumerateKeysAndObjectsUsingBlock:block];
13546}
13547
13548- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
13549                                usingBlock:(void (^)(id key,
13550                                                     id obj,
13551                                                     BOOL *stop))block {
13552  [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
13553}
13554
13555@end
13556