MapField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Linq;
  36. namespace Google.Protobuf.Collections
  37. {
  38. /// <summary>
  39. /// Representation of a map field in a Protocol Buffer message.
  40. /// </summary>
  41. /// <remarks>
  42. /// This implementation preserves insertion order for simplicity of testing
  43. /// code using maps fields. Overwriting an existing entry does not change the
  44. /// position of that entry within the map. Equality is not order-sensitive.
  45. /// For string keys, the equality comparison is provided by <see cref="StringComparer.Ordinal"/>.
  46. /// </remarks>
  47. /// <typeparam name="TKey">Key type in the map. Must be a type supported by Protocol Buffer map keys.</typeparam>
  48. /// <typeparam name="TValue">Value type in the map. Must be a type supported by Protocol Buffers.</typeparam>
  49. public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IFreezable, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
  50. {
  51. // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.)
  52. private readonly bool allowNullValues;
  53. private bool frozen;
  54. private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map =
  55. new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>();
  56. private readonly LinkedList<KeyValuePair<TKey, TValue>> list = new LinkedList<KeyValuePair<TKey, TValue>>();
  57. /// <summary>
  58. /// Constructs a new map field, defaulting the value nullability to only allow null values for message types
  59. /// and non-nullable value types.
  60. /// </summary>
  61. public MapField() : this(typeof(IMessage).IsAssignableFrom(typeof(TValue)) || Nullable.GetUnderlyingType(typeof(TValue)) != null)
  62. {
  63. }
  64. /// <summary>
  65. /// Constructs a new map field, overriding the choice of whether null values are permitted in the map.
  66. /// This is used by wrapper types, where maps with string and bytes wrappers as the value types
  67. /// support null values.
  68. /// </summary>
  69. /// <param name="allowNullValues">Whether null values are permitted in the map or not.</param>
  70. public MapField(bool allowNullValues)
  71. {
  72. if (allowNullValues && typeof(TValue).IsValueType && Nullable.GetUnderlyingType(typeof(TValue)) == null)
  73. {
  74. throw new ArgumentException("allowNullValues", "Non-nullable value types do not support null values");
  75. }
  76. this.allowNullValues = allowNullValues;
  77. }
  78. public MapField<TKey, TValue> Clone()
  79. {
  80. var clone = new MapField<TKey, TValue>(allowNullValues);
  81. // Keys are never cloneable. Values might be.
  82. if (typeof(IDeepCloneable<TValue>).IsAssignableFrom(typeof(TValue)))
  83. {
  84. foreach (var pair in list)
  85. {
  86. clone.Add(pair.Key, pair.Value == null ? pair.Value : ((IDeepCloneable<TValue>)pair.Value).Clone());
  87. }
  88. }
  89. else
  90. {
  91. // Nothing is cloneable, so we don't need to worry.
  92. clone.Add(this);
  93. }
  94. return clone;
  95. }
  96. public void Add(TKey key, TValue value)
  97. {
  98. // Validation of arguments happens in ContainsKey and the indexer
  99. if (ContainsKey(key))
  100. {
  101. throw new ArgumentException("Key already exists in map", "key");
  102. }
  103. this[key] = value;
  104. }
  105. public bool ContainsKey(TKey key)
  106. {
  107. ThrowHelper.ThrowIfNull(key, "key");
  108. return map.ContainsKey(key);
  109. }
  110. public bool Remove(TKey key)
  111. {
  112. this.CheckMutable();
  113. ThrowHelper.ThrowIfNull(key, "key");
  114. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  115. if (map.TryGetValue(key, out node))
  116. {
  117. map.Remove(key);
  118. node.List.Remove(node);
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. public bool TryGetValue(TKey key, out TValue value)
  127. {
  128. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  129. if (map.TryGetValue(key, out node))
  130. {
  131. value = node.Value.Value;
  132. return true;
  133. }
  134. else
  135. {
  136. value = default(TValue);
  137. return false;
  138. }
  139. }
  140. public TValue this[TKey key]
  141. {
  142. get
  143. {
  144. ThrowHelper.ThrowIfNull(key, "key");
  145. TValue value;
  146. if (TryGetValue(key, out value))
  147. {
  148. return value;
  149. }
  150. throw new KeyNotFoundException();
  151. }
  152. set
  153. {
  154. ThrowHelper.ThrowIfNull(key, "key");
  155. // value == null check here is redundant, but avoids boxing.
  156. if (value == null && !allowNullValues)
  157. {
  158. ThrowHelper.ThrowIfNull(value, "value");
  159. }
  160. this.CheckMutable();
  161. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  162. var pair = new KeyValuePair<TKey, TValue>(key, value);
  163. if (map.TryGetValue(key, out node))
  164. {
  165. node.Value = pair;
  166. }
  167. else
  168. {
  169. node = list.AddLast(pair);
  170. map[key] = node;
  171. }
  172. }
  173. }
  174. // TODO: Make these views?
  175. public ICollection<TKey> Keys { get { return list.Select(t => t.Key).ToList(); } }
  176. public ICollection<TValue> Values { get { return list.Select(t => t.Value).ToList(); } }
  177. public void Add(IDictionary<TKey, TValue> entries)
  178. {
  179. ThrowHelper.ThrowIfNull(entries, "entries");
  180. foreach (var pair in entries)
  181. {
  182. Add(pair.Key, pair.Value);
  183. }
  184. }
  185. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  186. {
  187. return list.GetEnumerator();
  188. }
  189. IEnumerator IEnumerable.GetEnumerator()
  190. {
  191. return GetEnumerator();
  192. }
  193. void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
  194. {
  195. Add(item.Key, item.Value);
  196. }
  197. public void Clear()
  198. {
  199. this.CheckMutable();
  200. list.Clear();
  201. map.Clear();
  202. }
  203. bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
  204. {
  205. TValue value;
  206. return TryGetValue(item.Key, out value)
  207. && EqualityComparer<TValue>.Default.Equals(item.Value, value);
  208. }
  209. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  210. {
  211. list.CopyTo(array, arrayIndex);
  212. }
  213. bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
  214. {
  215. this.CheckMutable();
  216. if (item.Key == null)
  217. {
  218. throw new ArgumentException("Key is null", "item");
  219. }
  220. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  221. if (map.TryGetValue(item.Key, out node) &&
  222. EqualityComparer<TValue>.Default.Equals(item.Value, node.Value.Value))
  223. {
  224. map.Remove(item.Key);
  225. node.List.Remove(node);
  226. return true;
  227. }
  228. else
  229. {
  230. return false;
  231. }
  232. }
  233. /// <summary>
  234. /// Returns whether or not this map allows values to be null.
  235. /// </summary>
  236. public bool AllowsNullValues { get { return allowNullValues; } }
  237. public int Count { get { return list.Count; } }
  238. public bool IsReadOnly { get { return frozen; } }
  239. public void Freeze()
  240. {
  241. if (IsFrozen)
  242. {
  243. return;
  244. }
  245. frozen = true;
  246. // Only values can be frozen, as all the key types are simple.
  247. // Everything can be done in-place, as we're just freezing objects.
  248. if (typeof(IFreezable).IsAssignableFrom(typeof(TValue)))
  249. {
  250. for (var node = list.First; node != null; node = node.Next)
  251. {
  252. var pair = node.Value;
  253. IFreezable freezableValue = pair.Value as IFreezable;
  254. if (freezableValue != null)
  255. {
  256. freezableValue.Freeze();
  257. }
  258. }
  259. }
  260. }
  261. public bool IsFrozen { get { return frozen; } }
  262. public override bool Equals(object other)
  263. {
  264. return Equals(other as MapField<TKey, TValue>);
  265. }
  266. public override int GetHashCode()
  267. {
  268. var valueComparer = EqualityComparer<TValue>.Default;
  269. int hash = 0;
  270. foreach (var pair in list)
  271. {
  272. hash ^= pair.Key.GetHashCode() * 31 + valueComparer.GetHashCode(pair.Value);
  273. }
  274. return hash;
  275. }
  276. public bool Equals(MapField<TKey, TValue> other)
  277. {
  278. if (other == null)
  279. {
  280. return false;
  281. }
  282. if (other == this)
  283. {
  284. return true;
  285. }
  286. if (other.Count != this.Count)
  287. {
  288. return false;
  289. }
  290. var valueComparer = EqualityComparer<TValue>.Default;
  291. foreach (var pair in this)
  292. {
  293. TValue value;
  294. if (!other.TryGetValue(pair.Key, out value))
  295. {
  296. return false;
  297. }
  298. if (!valueComparer.Equals(value, pair.Value))
  299. {
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. /// <summary>
  306. /// Adds entries to the map from the given stream.
  307. /// </summary>
  308. /// <remarks>
  309. /// It is assumed that the stream is initially positioned after the tag specified by the codec.
  310. /// This method will continue reading entries from the stream until the end is reached, or
  311. /// a different tag is encountered.
  312. /// </remarks>
  313. /// <param name="input">Stream to read from</param>
  314. /// <param name="codec">Codec describing how the key/value pairs are encoded</param>
  315. public void AddEntriesFrom(CodedInputStream input, Codec codec)
  316. {
  317. var adapter = new Codec.MessageAdapter(codec);
  318. do
  319. {
  320. adapter.Reset();
  321. input.ReadMessage(adapter);
  322. this[adapter.Key] = adapter.Value;
  323. } while (input.MaybeConsumeTag(codec.MapTag));
  324. }
  325. public void WriteTo(CodedOutputStream output, Codec codec)
  326. {
  327. var message = new Codec.MessageAdapter(codec);
  328. foreach (var entry in list)
  329. {
  330. message.Key = entry.Key;
  331. message.Value = entry.Value;
  332. output.WriteTag(codec.MapTag);
  333. output.WriteMessage(message);
  334. }
  335. }
  336. public int CalculateSize(Codec codec)
  337. {
  338. if (Count == 0)
  339. {
  340. return 0;
  341. }
  342. var message = new Codec.MessageAdapter(codec);
  343. int size = 0;
  344. foreach (var entry in list)
  345. {
  346. message.Key = entry.Key;
  347. message.Value = entry.Value;
  348. size += CodedOutputStream.ComputeRawVarint32Size(codec.MapTag);
  349. size += CodedOutputStream.ComputeMessageSize(message);
  350. }
  351. return size;
  352. }
  353. #region IDictionary explicit interface implementation
  354. void IDictionary.Add(object key, object value)
  355. {
  356. Add((TKey)key, (TValue)value);
  357. }
  358. bool IDictionary.Contains(object key)
  359. {
  360. if (!(key is TKey))
  361. {
  362. return false;
  363. }
  364. return ContainsKey((TKey)key);
  365. }
  366. IDictionaryEnumerator IDictionary.GetEnumerator()
  367. {
  368. return new DictionaryEnumerator(GetEnumerator());
  369. }
  370. void IDictionary.Remove(object key)
  371. {
  372. ThrowHelper.ThrowIfNull(key, "key");
  373. this.CheckMutable();
  374. if (!(key is TKey))
  375. {
  376. return;
  377. }
  378. Remove((TKey)key);
  379. }
  380. void ICollection.CopyTo(Array array, int index)
  381. {
  382. // This is ugly and slow as heck, but with any luck it will never be used anyway.
  383. ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList();
  384. temp.CopyTo(array, index);
  385. }
  386. bool IDictionary.IsFixedSize { get { return IsFrozen; } }
  387. ICollection IDictionary.Keys { get { return (ICollection)Keys; } }
  388. ICollection IDictionary.Values { get { return (ICollection)Values; } }
  389. bool ICollection.IsSynchronized { get { return false; } }
  390. object ICollection.SyncRoot { get { return this; } }
  391. object IDictionary.this[object key]
  392. {
  393. get
  394. {
  395. ThrowHelper.ThrowIfNull(key, "key");
  396. if (!(key is TKey))
  397. {
  398. return null;
  399. }
  400. TValue value;
  401. TryGetValue((TKey)key, out value);
  402. return value;
  403. }
  404. set
  405. {
  406. if (frozen)
  407. {
  408. throw new NotSupportedException("Dictionary is frozen");
  409. }
  410. this[(TKey)key] = (TValue)value;
  411. }
  412. }
  413. #endregion
  414. private class DictionaryEnumerator : IDictionaryEnumerator
  415. {
  416. private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
  417. internal DictionaryEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> enumerator)
  418. {
  419. this.enumerator = enumerator;
  420. }
  421. public bool MoveNext()
  422. {
  423. return enumerator.MoveNext();
  424. }
  425. public void Reset()
  426. {
  427. enumerator.Reset();
  428. }
  429. public object Current { get { return Entry; } }
  430. public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } }
  431. public object Key { get { return enumerator.Current.Key; } }
  432. public object Value { get { return enumerator.Current.Value; } }
  433. }
  434. /// <summary>
  435. /// A codec for a specific map field. This contains all the information required to encoded and
  436. /// decode the nested messages.
  437. /// </summary>
  438. public sealed class Codec
  439. {
  440. private readonly FieldCodec<TKey> keyCodec;
  441. private readonly FieldCodec<TValue> valueCodec;
  442. private readonly uint mapTag;
  443. public Codec(FieldCodec<TKey> keyCodec, FieldCodec<TValue> valueCodec, uint mapTag)
  444. {
  445. this.keyCodec = keyCodec;
  446. this.valueCodec = valueCodec;
  447. this.mapTag = mapTag;
  448. }
  449. /// <summary>
  450. /// The tag used in the enclosing message to indicate map entries.
  451. /// </summary>
  452. internal uint MapTag { get { return mapTag; } }
  453. /// <summary>
  454. /// A mutable message class, used for parsing and serializing. This
  455. /// delegates the work to a codec, but implements the <see cref="IMessage"/> interface
  456. /// for interop with <see cref="CodedInputStream"/> and <see cref="CodedOutputStream"/>.
  457. /// This is nested inside Codec as it's tightly coupled to the associated codec,
  458. /// and it's simpler if it has direct access to all its fields.
  459. /// </summary>
  460. internal class MessageAdapter : IMessage
  461. {
  462. private readonly Codec codec;
  463. internal TKey Key { get; set; }
  464. internal TValue Value { get; set; }
  465. internal MessageAdapter(Codec codec)
  466. {
  467. this.codec = codec;
  468. }
  469. internal void Reset()
  470. {
  471. Key = codec.keyCodec.DefaultValue;
  472. Value = codec.valueCodec.DefaultValue;
  473. }
  474. public void MergeFrom(CodedInputStream input)
  475. {
  476. uint tag;
  477. while (input.ReadTag(out tag))
  478. {
  479. if (tag == 0)
  480. {
  481. throw InvalidProtocolBufferException.InvalidTag();
  482. }
  483. if (tag == codec.keyCodec.Tag)
  484. {
  485. Key = codec.keyCodec.Read(input);
  486. }
  487. else if (tag == codec.valueCodec.Tag)
  488. {
  489. Value = codec.valueCodec.Read(input);
  490. }
  491. else if (WireFormat.IsEndGroupTag(tag))
  492. {
  493. // TODO(jonskeet): Do we need this? (Given that we don't support groups...)
  494. return;
  495. }
  496. }
  497. }
  498. public void WriteTo(CodedOutputStream output)
  499. {
  500. codec.keyCodec.WriteTagAndValue(output, Key);
  501. codec.valueCodec.WriteTagAndValue(output, Value);
  502. }
  503. public int CalculateSize()
  504. {
  505. return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value);
  506. }
  507. }
  508. }
  509. }
  510. }