MapField.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.Reflection;
  33. using System;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Linq;
  37. using Google.Protobuf.Compatibility;
  38. namespace Google.Protobuf.Collections
  39. {
  40. /// <summary>
  41. /// Representation of a map field in a Protocol Buffer message.
  42. /// </summary>
  43. /// <typeparam name="TKey">Key type in the map. Must be a type supported by Protocol Buffer map keys.</typeparam>
  44. /// <typeparam name="TValue">Value type in the map. Must be a type supported by Protocol Buffers.</typeparam>
  45. /// <remarks>
  46. /// This implementation preserves insertion order for simplicity of testing
  47. /// code using maps fields. Overwriting an existing entry does not change the
  48. /// position of that entry within the map. Equality is not order-sensitive.
  49. /// For string keys, the equality comparison is provided by <see cref="StringComparer.Ordinal" />.
  50. /// </remarks>
  51. public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
  52. {
  53. // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.)
  54. private readonly bool allowNullValues;
  55. private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map =
  56. new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>();
  57. private readonly LinkedList<KeyValuePair<TKey, TValue>> list = new LinkedList<KeyValuePair<TKey, TValue>>();
  58. /// <summary>
  59. /// Constructs a new map field, defaulting the value nullability to only allow null values for message types
  60. /// and non-nullable value types.
  61. /// </summary>
  62. public MapField() : this(typeof(IMessage).IsAssignableFrom(typeof(TValue)) || Nullable.GetUnderlyingType(typeof(TValue)) != null)
  63. {
  64. }
  65. /// <summary>
  66. /// Constructs a new map field, overriding the choice of whether null values are permitted in the map.
  67. /// This is used by wrapper types, where maps with string and bytes wrappers as the value types
  68. /// support null values.
  69. /// </summary>
  70. /// <param name="allowNullValues">Whether null values are permitted in the map or not.</param>
  71. public MapField(bool allowNullValues)
  72. {
  73. if (allowNullValues && typeof(TValue).IsValueType() && Nullable.GetUnderlyingType(typeof(TValue)) == null)
  74. {
  75. throw new ArgumentException("allowNullValues", "Non-nullable value types do not support null values");
  76. }
  77. this.allowNullValues = allowNullValues;
  78. }
  79. /// <summary>
  80. /// Creates a deep clone of this object.
  81. /// </summary>
  82. /// <returns>
  83. /// A deep clone of this object.
  84. /// </returns>
  85. public MapField<TKey, TValue> Clone()
  86. {
  87. var clone = new MapField<TKey, TValue>(allowNullValues);
  88. // Keys are never cloneable. Values might be.
  89. if (typeof(IDeepCloneable<TValue>).IsAssignableFrom(typeof(TValue)))
  90. {
  91. foreach (var pair in list)
  92. {
  93. clone.Add(pair.Key, pair.Value == null ? pair.Value : ((IDeepCloneable<TValue>)pair.Value).Clone());
  94. }
  95. }
  96. else
  97. {
  98. // Nothing is cloneable, so we don't need to worry.
  99. clone.Add(this);
  100. }
  101. return clone;
  102. }
  103. /// <summary>
  104. /// Adds the specified key/value pair to the map.
  105. /// </summary>
  106. /// <remarks>
  107. /// This operation fails if the key already exists in the map. To replace an existing entry, use the indexer.
  108. /// </remarks>
  109. /// <param name="key">The key to add</param>
  110. /// <param name="value">The value to add.</param>
  111. /// <exception cref="System.ArgumentException">The given key already exists in map.</exception>
  112. public void Add(TKey key, TValue value)
  113. {
  114. // Validation of arguments happens in ContainsKey and the indexer
  115. if (ContainsKey(key))
  116. {
  117. throw new ArgumentException("Key already exists in map", "key");
  118. }
  119. this[key] = value;
  120. }
  121. /// <summary>
  122. /// Determines whether the specified key is present in the map.
  123. /// </summary>
  124. /// <param name="key">The key to check.</param>
  125. /// <returns><c>true</c> if the map contains the given key; <c>false</c> otherwise.</returns>
  126. public bool ContainsKey(TKey key)
  127. {
  128. Preconditions.CheckNotNullUnconstrained(key, "key");
  129. return map.ContainsKey(key);
  130. }
  131. /// <summary>
  132. /// Removes the entry identified by the given key from the map.
  133. /// </summary>
  134. /// <param name="key">The key indicating the entry to remove from the map.</param>
  135. /// <returns><c>true</c> if the map contained the given key before the entry was removed; <c>false</c> otherwise.</returns>
  136. public bool Remove(TKey key)
  137. {
  138. Preconditions.CheckNotNullUnconstrained(key, "key");
  139. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  140. if (map.TryGetValue(key, out node))
  141. {
  142. map.Remove(key);
  143. node.List.Remove(node);
  144. return true;
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. }
  151. /// <summary>
  152. /// Gets the value associated with the specified key.
  153. /// </summary>
  154. /// <param name="key">The key whose value to get.</param>
  155. /// <param name="value">When this method returns, the value associated with the specified key, if the key is found;
  156. /// otherwise, the default value for the type of the <paramref name="value"/> parameter.
  157. /// This parameter is passed uninitialized.</param>
  158. /// <returns><c>true</c> if the map contains an element with the specified key; otherwise, <c>false</c>.</returns>
  159. public bool TryGetValue(TKey key, out TValue value)
  160. {
  161. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  162. if (map.TryGetValue(key, out node))
  163. {
  164. value = node.Value.Value;
  165. return true;
  166. }
  167. else
  168. {
  169. value = default(TValue);
  170. return false;
  171. }
  172. }
  173. /// <summary>
  174. /// Gets or sets the value associated with the specified key.
  175. /// </summary>
  176. /// <param name="key">The key of the value to get or set.</param>
  177. /// <exception cref="KeyNotFoundException">The property is retrieved and key does not exist in the collection.</exception>
  178. /// <returns>The value associated with the specified key. If the specified key is not found,
  179. /// a get operation throws a <see cref="KeyNotFoundException"/>, and a set operation creates a new element with the specified key.</returns>
  180. public TValue this[TKey key]
  181. {
  182. get
  183. {
  184. Preconditions.CheckNotNullUnconstrained(key, "key");
  185. TValue value;
  186. if (TryGetValue(key, out value))
  187. {
  188. return value;
  189. }
  190. throw new KeyNotFoundException();
  191. }
  192. set
  193. {
  194. Preconditions.CheckNotNullUnconstrained(key, "key");
  195. // value == null check here is redundant, but avoids boxing.
  196. if (value == null && !allowNullValues)
  197. {
  198. Preconditions.CheckNotNullUnconstrained(value, "value");
  199. }
  200. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  201. var pair = new KeyValuePair<TKey, TValue>(key, value);
  202. if (map.TryGetValue(key, out node))
  203. {
  204. node.Value = pair;
  205. }
  206. else
  207. {
  208. node = list.AddLast(pair);
  209. map[key] = node;
  210. }
  211. }
  212. }
  213. // TODO: Make these views?
  214. /// <summary>
  215. /// Gets a collection containing the keys in the map.
  216. /// </summary>
  217. public ICollection<TKey> Keys { get { return list.Select(t => t.Key).ToList(); } }
  218. /// <summary>
  219. /// Gets a collection containing the values in the map.
  220. /// </summary>
  221. public ICollection<TValue> Values { get { return list.Select(t => t.Value).ToList(); } }
  222. /// <summary>
  223. /// Adds the specified entries to the map.
  224. /// </summary>
  225. /// <param name="entries">The entries to add to the map.</param>
  226. public void Add(IDictionary<TKey, TValue> entries)
  227. {
  228. Preconditions.CheckNotNull(entries, "entries");
  229. foreach (var pair in entries)
  230. {
  231. Add(pair.Key, pair.Value);
  232. }
  233. }
  234. /// <summary>
  235. /// Returns an enumerator that iterates through the collection.
  236. /// </summary>
  237. /// <returns>
  238. /// An enumerator that can be used to iterate through the collection.
  239. /// </returns>
  240. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  241. {
  242. return list.GetEnumerator();
  243. }
  244. /// <summary>
  245. /// Returns an enumerator that iterates through a collection.
  246. /// </summary>
  247. /// <returns>
  248. /// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
  249. /// </returns>
  250. IEnumerator IEnumerable.GetEnumerator()
  251. {
  252. return GetEnumerator();
  253. }
  254. /// <summary>
  255. /// Adds the specified item to the map.
  256. /// </summary>
  257. /// <param name="item">The item to add to the map.</param>
  258. void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
  259. {
  260. Add(item.Key, item.Value);
  261. }
  262. /// <summary>
  263. /// Removes all items from the map.
  264. /// </summary>
  265. public void Clear()
  266. {
  267. list.Clear();
  268. map.Clear();
  269. }
  270. /// <summary>
  271. /// Determines whether map contains an entry equivalent to the given key/value pair.
  272. /// </summary>
  273. /// <param name="item">The key/value pair to find.</param>
  274. /// <returns></returns>
  275. bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
  276. {
  277. TValue value;
  278. return TryGetValue(item.Key, out value)
  279. && EqualityComparer<TValue>.Default.Equals(item.Value, value);
  280. }
  281. /// <summary>
  282. /// Copies the key/value pairs in this map to an array.
  283. /// </summary>
  284. /// <param name="array">The array to copy the entries into.</param>
  285. /// <param name="arrayIndex">The index of the array at which to start copying values.</param>
  286. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  287. {
  288. list.CopyTo(array, arrayIndex);
  289. }
  290. /// <summary>
  291. /// Removes the specified key/value pair from the map.
  292. /// </summary>
  293. /// <remarks>Both the key and the value must be found for the entry to be removed.</remarks>
  294. /// <param name="item">The key/value pair to remove.</param>
  295. /// <returns><c>true</c> if the key/value pair was found and removed; <c>false</c> otherwise.</returns>
  296. bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
  297. {
  298. if (item.Key == null)
  299. {
  300. throw new ArgumentException("Key is null", "item");
  301. }
  302. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  303. if (map.TryGetValue(item.Key, out node) &&
  304. EqualityComparer<TValue>.Default.Equals(item.Value, node.Value.Value))
  305. {
  306. map.Remove(item.Key);
  307. node.List.Remove(node);
  308. return true;
  309. }
  310. else
  311. {
  312. return false;
  313. }
  314. }
  315. /// <summary>
  316. /// Returns whether or not this map allows values to be null.
  317. /// </summary>
  318. public bool AllowsNullValues { get { return allowNullValues; } }
  319. /// <summary>
  320. /// Gets the number of elements contained in the map.
  321. /// </summary>
  322. public int Count { get { return list.Count; } }
  323. /// <summary>
  324. /// Gets a value indicating whether the map is read-only.
  325. /// </summary>
  326. public bool IsReadOnly { get { return false; } }
  327. /// <summary>
  328. /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
  329. /// </summary>
  330. /// <param name="other">The <see cref="System.Object" /> to compare with this instance.</param>
  331. /// <returns>
  332. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  333. /// </returns>
  334. public override bool Equals(object other)
  335. {
  336. return Equals(other as MapField<TKey, TValue>);
  337. }
  338. /// <summary>
  339. /// Returns a hash code for this instance.
  340. /// </summary>
  341. /// <returns>
  342. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  343. /// </returns>
  344. public override int GetHashCode()
  345. {
  346. var valueComparer = EqualityComparer<TValue>.Default;
  347. int hash = 0;
  348. foreach (var pair in list)
  349. {
  350. hash ^= pair.Key.GetHashCode() * 31 + valueComparer.GetHashCode(pair.Value);
  351. }
  352. return hash;
  353. }
  354. /// <summary>
  355. /// Compares this map with another for equality.
  356. /// </summary>
  357. /// <remarks>
  358. /// The order of the key/value pairs in the maps is not deemed significant in this comparison.
  359. /// </remarks>
  360. /// <param name="other">The map to compare this with.</param>
  361. /// <returns><c>true</c> if <paramref name="other"/> refers to an equal map; <c>false</c> otherwise.</returns>
  362. public bool Equals(MapField<TKey, TValue> other)
  363. {
  364. if (other == null)
  365. {
  366. return false;
  367. }
  368. if (other == this)
  369. {
  370. return true;
  371. }
  372. if (other.Count != this.Count)
  373. {
  374. return false;
  375. }
  376. var valueComparer = EqualityComparer<TValue>.Default;
  377. foreach (var pair in this)
  378. {
  379. TValue value;
  380. if (!other.TryGetValue(pair.Key, out value))
  381. {
  382. return false;
  383. }
  384. if (!valueComparer.Equals(value, pair.Value))
  385. {
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. /// <summary>
  392. /// Adds entries to the map from the given stream.
  393. /// </summary>
  394. /// <remarks>
  395. /// It is assumed that the stream is initially positioned after the tag specified by the codec.
  396. /// This method will continue reading entries from the stream until the end is reached, or
  397. /// a different tag is encountered.
  398. /// </remarks>
  399. /// <param name="input">Stream to read from</param>
  400. /// <param name="codec">Codec describing how the key/value pairs are encoded</param>
  401. public void AddEntriesFrom(CodedInputStream input, Codec codec)
  402. {
  403. var adapter = new Codec.MessageAdapter(codec);
  404. do
  405. {
  406. adapter.Reset();
  407. input.ReadMessage(adapter);
  408. this[adapter.Key] = adapter.Value;
  409. } while (input.MaybeConsumeTag(codec.MapTag));
  410. }
  411. /// <summary>
  412. /// Writes the contents of this map to the given coded output stream, using the specified codec
  413. /// to encode each entry.
  414. /// </summary>
  415. /// <param name="output">The output stream to write to.</param>
  416. /// <param name="codec">The codec to use for each entry.</param>
  417. public void WriteTo(CodedOutputStream output, Codec codec)
  418. {
  419. var message = new Codec.MessageAdapter(codec);
  420. foreach (var entry in list)
  421. {
  422. message.Key = entry.Key;
  423. message.Value = entry.Value;
  424. output.WriteTag(codec.MapTag);
  425. output.WriteMessage(message);
  426. }
  427. }
  428. /// <summary>
  429. /// Calculates the size of this map based on the given entry codec.
  430. /// </summary>
  431. /// <param name="codec">The codec to use to encode each entry.</param>
  432. /// <returns></returns>
  433. public int CalculateSize(Codec codec)
  434. {
  435. if (Count == 0)
  436. {
  437. return 0;
  438. }
  439. var message = new Codec.MessageAdapter(codec);
  440. int size = 0;
  441. foreach (var entry in list)
  442. {
  443. message.Key = entry.Key;
  444. message.Value = entry.Value;
  445. size += CodedOutputStream.ComputeRawVarint32Size(codec.MapTag);
  446. size += CodedOutputStream.ComputeMessageSize(message);
  447. }
  448. return size;
  449. }
  450. #region IDictionary explicit interface implementation
  451. void IDictionary.Add(object key, object value)
  452. {
  453. Add((TKey)key, (TValue)value);
  454. }
  455. bool IDictionary.Contains(object key)
  456. {
  457. if (!(key is TKey))
  458. {
  459. return false;
  460. }
  461. return ContainsKey((TKey)key);
  462. }
  463. IDictionaryEnumerator IDictionary.GetEnumerator()
  464. {
  465. return new DictionaryEnumerator(GetEnumerator());
  466. }
  467. void IDictionary.Remove(object key)
  468. {
  469. Preconditions.CheckNotNull(key, "key");
  470. if (!(key is TKey))
  471. {
  472. return;
  473. }
  474. Remove((TKey)key);
  475. }
  476. void ICollection.CopyTo(Array array, int index)
  477. {
  478. // This is ugly and slow as heck, but with any luck it will never be used anyway.
  479. ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList();
  480. temp.CopyTo(array, index);
  481. }
  482. bool IDictionary.IsFixedSize { get { return false; } }
  483. ICollection IDictionary.Keys { get { return (ICollection)Keys; } }
  484. ICollection IDictionary.Values { get { return (ICollection)Values; } }
  485. bool ICollection.IsSynchronized { get { return false; } }
  486. object ICollection.SyncRoot { get { return this; } }
  487. object IDictionary.this[object key]
  488. {
  489. get
  490. {
  491. Preconditions.CheckNotNull(key, "key");
  492. if (!(key is TKey))
  493. {
  494. return null;
  495. }
  496. TValue value;
  497. TryGetValue((TKey)key, out value);
  498. return value;
  499. }
  500. set
  501. {
  502. this[(TKey)key] = (TValue)value;
  503. }
  504. }
  505. #endregion
  506. private class DictionaryEnumerator : IDictionaryEnumerator
  507. {
  508. private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
  509. internal DictionaryEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> enumerator)
  510. {
  511. this.enumerator = enumerator;
  512. }
  513. public bool MoveNext()
  514. {
  515. return enumerator.MoveNext();
  516. }
  517. public void Reset()
  518. {
  519. enumerator.Reset();
  520. }
  521. public object Current { get { return Entry; } }
  522. public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } }
  523. public object Key { get { return enumerator.Current.Key; } }
  524. public object Value { get { return enumerator.Current.Value; } }
  525. }
  526. /// <summary>
  527. /// A codec for a specific map field. This contains all the information required to encode and
  528. /// decode the nested messages.
  529. /// </summary>
  530. public sealed class Codec
  531. {
  532. private readonly FieldCodec<TKey> keyCodec;
  533. private readonly FieldCodec<TValue> valueCodec;
  534. private readonly uint mapTag;
  535. /// <summary>
  536. /// Creates a new entry codec based on a separate key codec and value codec,
  537. /// and the tag to use for each map entry.
  538. /// </summary>
  539. /// <param name="keyCodec">The key codec.</param>
  540. /// <param name="valueCodec">The value codec.</param>
  541. /// <param name="mapTag">The map tag to use to introduce each map entry.</param>
  542. public Codec(FieldCodec<TKey> keyCodec, FieldCodec<TValue> valueCodec, uint mapTag)
  543. {
  544. this.keyCodec = keyCodec;
  545. this.valueCodec = valueCodec;
  546. this.mapTag = mapTag;
  547. }
  548. /// <summary>
  549. /// The tag used in the enclosing message to indicate map entries.
  550. /// </summary>
  551. internal uint MapTag { get { return mapTag; } }
  552. /// <summary>
  553. /// A mutable message class, used for parsing and serializing. This
  554. /// delegates the work to a codec, but implements the <see cref="IMessage"/> interface
  555. /// for interop with <see cref="CodedInputStream"/> and <see cref="CodedOutputStream"/>.
  556. /// This is nested inside Codec as it's tightly coupled to the associated codec,
  557. /// and it's simpler if it has direct access to all its fields.
  558. /// </summary>
  559. internal class MessageAdapter : IMessage
  560. {
  561. private readonly Codec codec;
  562. internal TKey Key { get; set; }
  563. internal TValue Value { get; set; }
  564. internal MessageAdapter(Codec codec)
  565. {
  566. this.codec = codec;
  567. }
  568. internal void Reset()
  569. {
  570. Key = codec.keyCodec.DefaultValue;
  571. Value = codec.valueCodec.DefaultValue;
  572. }
  573. public void MergeFrom(CodedInputStream input)
  574. {
  575. uint tag;
  576. while ((tag = input.ReadTag()) != 0)
  577. {
  578. if (tag == codec.keyCodec.Tag)
  579. {
  580. Key = codec.keyCodec.Read(input);
  581. }
  582. else if (tag == codec.valueCodec.Tag)
  583. {
  584. Value = codec.valueCodec.Read(input);
  585. }
  586. else
  587. {
  588. input.SkipLastField();
  589. }
  590. }
  591. }
  592. public void WriteTo(CodedOutputStream output)
  593. {
  594. codec.keyCodec.WriteTagAndValue(output, Key);
  595. codec.valueCodec.WriteTagAndValue(output, Value);
  596. }
  597. public int CalculateSize()
  598. {
  599. return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value);
  600. }
  601. MessageDescriptor IMessage.Descriptor { get { return null; } }
  602. }
  603. }
  604. }
  605. }