MapField.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. /// <remarks>
  44. /// This implementation preserves insertion order for simplicity of testing
  45. /// code using maps fields. Overwriting an existing entry does not change the
  46. /// position of that entry within the map. Equality is not order-sensitive.
  47. /// For string keys, the equality comparison is provided by <see cref="StringComparer.Ordinal"/>.
  48. /// </remarks>
  49. /// <typeparam name="TKey">Key type in the map. Must be a type supported by Protocol Buffer map keys.</typeparam>
  50. /// <typeparam name="TValue">Value type in the map. Must be a type supported by Protocol Buffers.</typeparam>
  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. public MapField<TKey, TValue> Clone()
  80. {
  81. var clone = new MapField<TKey, TValue>(allowNullValues);
  82. // Keys are never cloneable. Values might be.
  83. if (typeof(IDeepCloneable<TValue>).IsAssignableFrom(typeof(TValue)))
  84. {
  85. foreach (var pair in list)
  86. {
  87. clone.Add(pair.Key, pair.Value == null ? pair.Value : ((IDeepCloneable<TValue>)pair.Value).Clone());
  88. }
  89. }
  90. else
  91. {
  92. // Nothing is cloneable, so we don't need to worry.
  93. clone.Add(this);
  94. }
  95. return clone;
  96. }
  97. public void Add(TKey key, TValue value)
  98. {
  99. // Validation of arguments happens in ContainsKey and the indexer
  100. if (ContainsKey(key))
  101. {
  102. throw new ArgumentException("Key already exists in map", "key");
  103. }
  104. this[key] = value;
  105. }
  106. public bool ContainsKey(TKey key)
  107. {
  108. Preconditions.CheckNotNullUnconstrained(key, "key");
  109. return map.ContainsKey(key);
  110. }
  111. public bool Remove(TKey key)
  112. {
  113. Preconditions.CheckNotNullUnconstrained(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. Preconditions.CheckNotNullUnconstrained(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. Preconditions.CheckNotNullUnconstrained(key, "key");
  155. // value == null check here is redundant, but avoids boxing.
  156. if (value == null && !allowNullValues)
  157. {
  158. Preconditions.CheckNotNullUnconstrained(value, "value");
  159. }
  160. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  161. var pair = new KeyValuePair<TKey, TValue>(key, value);
  162. if (map.TryGetValue(key, out node))
  163. {
  164. node.Value = pair;
  165. }
  166. else
  167. {
  168. node = list.AddLast(pair);
  169. map[key] = node;
  170. }
  171. }
  172. }
  173. // TODO: Make these views?
  174. public ICollection<TKey> Keys { get { return list.Select(t => t.Key).ToList(); } }
  175. public ICollection<TValue> Values { get { return list.Select(t => t.Value).ToList(); } }
  176. public void Add(IDictionary<TKey, TValue> entries)
  177. {
  178. Preconditions.CheckNotNull(entries, "entries");
  179. foreach (var pair in entries)
  180. {
  181. Add(pair.Key, pair.Value);
  182. }
  183. }
  184. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  185. {
  186. return list.GetEnumerator();
  187. }
  188. IEnumerator IEnumerable.GetEnumerator()
  189. {
  190. return GetEnumerator();
  191. }
  192. void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
  193. {
  194. Add(item.Key, item.Value);
  195. }
  196. public void Clear()
  197. {
  198. list.Clear();
  199. map.Clear();
  200. }
  201. bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
  202. {
  203. TValue value;
  204. return TryGetValue(item.Key, out value)
  205. && EqualityComparer<TValue>.Default.Equals(item.Value, value);
  206. }
  207. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  208. {
  209. list.CopyTo(array, arrayIndex);
  210. }
  211. bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
  212. {
  213. if (item.Key == null)
  214. {
  215. throw new ArgumentException("Key is null", "item");
  216. }
  217. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  218. if (map.TryGetValue(item.Key, out node) &&
  219. EqualityComparer<TValue>.Default.Equals(item.Value, node.Value.Value))
  220. {
  221. map.Remove(item.Key);
  222. node.List.Remove(node);
  223. return true;
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. /// <summary>
  231. /// Returns whether or not this map allows values to be null.
  232. /// </summary>
  233. public bool AllowsNullValues { get { return allowNullValues; } }
  234. public int Count { get { return list.Count; } }
  235. public bool IsReadOnly { get { return false; } }
  236. public override bool Equals(object other)
  237. {
  238. return Equals(other as MapField<TKey, TValue>);
  239. }
  240. public override int GetHashCode()
  241. {
  242. var valueComparer = EqualityComparer<TValue>.Default;
  243. int hash = 0;
  244. foreach (var pair in list)
  245. {
  246. hash ^= pair.Key.GetHashCode() * 31 + valueComparer.GetHashCode(pair.Value);
  247. }
  248. return hash;
  249. }
  250. public bool Equals(MapField<TKey, TValue> other)
  251. {
  252. if (other == null)
  253. {
  254. return false;
  255. }
  256. if (other == this)
  257. {
  258. return true;
  259. }
  260. if (other.Count != this.Count)
  261. {
  262. return false;
  263. }
  264. var valueComparer = EqualityComparer<TValue>.Default;
  265. foreach (var pair in this)
  266. {
  267. TValue value;
  268. if (!other.TryGetValue(pair.Key, out value))
  269. {
  270. return false;
  271. }
  272. if (!valueComparer.Equals(value, pair.Value))
  273. {
  274. return false;
  275. }
  276. }
  277. return true;
  278. }
  279. /// <summary>
  280. /// Adds entries to the map from the given stream.
  281. /// </summary>
  282. /// <remarks>
  283. /// It is assumed that the stream is initially positioned after the tag specified by the codec.
  284. /// This method will continue reading entries from the stream until the end is reached, or
  285. /// a different tag is encountered.
  286. /// </remarks>
  287. /// <param name="input">Stream to read from</param>
  288. /// <param name="codec">Codec describing how the key/value pairs are encoded</param>
  289. public void AddEntriesFrom(CodedInputStream input, Codec codec)
  290. {
  291. var adapter = new Codec.MessageAdapter(codec);
  292. do
  293. {
  294. adapter.Reset();
  295. input.ReadMessage(adapter);
  296. this[adapter.Key] = adapter.Value;
  297. } while (input.MaybeConsumeTag(codec.MapTag));
  298. }
  299. public void WriteTo(CodedOutputStream output, Codec codec)
  300. {
  301. var message = new Codec.MessageAdapter(codec);
  302. foreach (var entry in list)
  303. {
  304. message.Key = entry.Key;
  305. message.Value = entry.Value;
  306. output.WriteTag(codec.MapTag);
  307. output.WriteMessage(message);
  308. }
  309. }
  310. public int CalculateSize(Codec codec)
  311. {
  312. if (Count == 0)
  313. {
  314. return 0;
  315. }
  316. var message = new Codec.MessageAdapter(codec);
  317. int size = 0;
  318. foreach (var entry in list)
  319. {
  320. message.Key = entry.Key;
  321. message.Value = entry.Value;
  322. size += CodedOutputStream.ComputeRawVarint32Size(codec.MapTag);
  323. size += CodedOutputStream.ComputeMessageSize(message);
  324. }
  325. return size;
  326. }
  327. #region IDictionary explicit interface implementation
  328. void IDictionary.Add(object key, object value)
  329. {
  330. Add((TKey)key, (TValue)value);
  331. }
  332. bool IDictionary.Contains(object key)
  333. {
  334. if (!(key is TKey))
  335. {
  336. return false;
  337. }
  338. return ContainsKey((TKey)key);
  339. }
  340. IDictionaryEnumerator IDictionary.GetEnumerator()
  341. {
  342. return new DictionaryEnumerator(GetEnumerator());
  343. }
  344. void IDictionary.Remove(object key)
  345. {
  346. Preconditions.CheckNotNull(key, "key");
  347. if (!(key is TKey))
  348. {
  349. return;
  350. }
  351. Remove((TKey)key);
  352. }
  353. void ICollection.CopyTo(Array array, int index)
  354. {
  355. // This is ugly and slow as heck, but with any luck it will never be used anyway.
  356. ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList();
  357. temp.CopyTo(array, index);
  358. }
  359. bool IDictionary.IsFixedSize { get { return false; } }
  360. ICollection IDictionary.Keys { get { return (ICollection)Keys; } }
  361. ICollection IDictionary.Values { get { return (ICollection)Values; } }
  362. bool ICollection.IsSynchronized { get { return false; } }
  363. object ICollection.SyncRoot { get { return this; } }
  364. object IDictionary.this[object key]
  365. {
  366. get
  367. {
  368. Preconditions.CheckNotNull(key, "key");
  369. if (!(key is TKey))
  370. {
  371. return null;
  372. }
  373. TValue value;
  374. TryGetValue((TKey)key, out value);
  375. return value;
  376. }
  377. set
  378. {
  379. this[(TKey)key] = (TValue)value;
  380. }
  381. }
  382. #endregion
  383. private class DictionaryEnumerator : IDictionaryEnumerator
  384. {
  385. private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
  386. internal DictionaryEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> enumerator)
  387. {
  388. this.enumerator = enumerator;
  389. }
  390. public bool MoveNext()
  391. {
  392. return enumerator.MoveNext();
  393. }
  394. public void Reset()
  395. {
  396. enumerator.Reset();
  397. }
  398. public object Current { get { return Entry; } }
  399. public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } }
  400. public object Key { get { return enumerator.Current.Key; } }
  401. public object Value { get { return enumerator.Current.Value; } }
  402. }
  403. /// <summary>
  404. /// A codec for a specific map field. This contains all the information required to encoded and
  405. /// decode the nested messages.
  406. /// </summary>
  407. public sealed class Codec
  408. {
  409. private readonly FieldCodec<TKey> keyCodec;
  410. private readonly FieldCodec<TValue> valueCodec;
  411. private readonly uint mapTag;
  412. public Codec(FieldCodec<TKey> keyCodec, FieldCodec<TValue> valueCodec, uint mapTag)
  413. {
  414. this.keyCodec = keyCodec;
  415. this.valueCodec = valueCodec;
  416. this.mapTag = mapTag;
  417. }
  418. /// <summary>
  419. /// The tag used in the enclosing message to indicate map entries.
  420. /// </summary>
  421. internal uint MapTag { get { return mapTag; } }
  422. /// <summary>
  423. /// A mutable message class, used for parsing and serializing. This
  424. /// delegates the work to a codec, but implements the <see cref="IMessage"/> interface
  425. /// for interop with <see cref="CodedInputStream"/> and <see cref="CodedOutputStream"/>.
  426. /// This is nested inside Codec as it's tightly coupled to the associated codec,
  427. /// and it's simpler if it has direct access to all its fields.
  428. /// </summary>
  429. internal class MessageAdapter : IMessage
  430. {
  431. private readonly Codec codec;
  432. internal TKey Key { get; set; }
  433. internal TValue Value { get; set; }
  434. internal MessageAdapter(Codec codec)
  435. {
  436. this.codec = codec;
  437. }
  438. internal void Reset()
  439. {
  440. Key = codec.keyCodec.DefaultValue;
  441. Value = codec.valueCodec.DefaultValue;
  442. }
  443. public void MergeFrom(CodedInputStream input)
  444. {
  445. uint tag;
  446. while (input.ReadTag(out tag))
  447. {
  448. if (tag == 0)
  449. {
  450. throw InvalidProtocolBufferException.InvalidTag();
  451. }
  452. if (tag == codec.keyCodec.Tag)
  453. {
  454. Key = codec.keyCodec.Read(input);
  455. }
  456. else if (tag == codec.valueCodec.Tag)
  457. {
  458. Value = codec.valueCodec.Read(input);
  459. }
  460. else if (WireFormat.IsEndGroupTag(tag))
  461. {
  462. // TODO(jonskeet): Do we need this? (Given that we don't support groups...)
  463. return;
  464. }
  465. }
  466. }
  467. public void WriteTo(CodedOutputStream output)
  468. {
  469. codec.keyCodec.WriteTagAndValue(output, Key);
  470. codec.valueCodec.WriteTagAndValue(output, Value);
  471. }
  472. public int CalculateSize()
  473. {
  474. return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value);
  475. }
  476. MessageDescriptor IMessage.Descriptor { get { return null; } }
  477. }
  478. }
  479. }
  480. }