Common
A library of common classes.
HashMap.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.Serialization;
4
6{
10 [DataContract(Name = "HashMap", Namespace = Constants.DataContractNamespace)]
11 public class HashMap<TKey, TValue> : HashMapBase<TKey, TValue>
12 {
16 [DataMember(Order = 1, Name = "Map")]
18
22 public HashMap()
23 {
24
25 }
26
31 public HashMap(int capacity)
32 {
33 Dictionary = new Dictionary<TKey, TValue>(capacity);
34 }
35
41 {
42 Dictionary = new Dictionary<TKey, TValue>(dict.Dictionary);
43 }
44
49 public static implicit operator HashMap<TKey, TValue>(Dictionary<TKey, TValue> dict)
50 {
51 return dict == null ? null : new HashMap<TKey, TValue> { Dictionary = dict };
52 }
53
58 {
59 if (Dictionary.Count == 0)
60 return Array.Empty<KeyValue<TKey, TValue>>();
61
62 int i = 0;
63 var array = new KeyValue<TKey, TValue>[Dictionary.Count];
64
65 foreach (var pair in Dictionary)
66 {
67 array[i++] = new KeyValue<TKey, TValue>(pair.Key, pair.Value);
68 }
69
70 return array;
71 }
72
78 {
79 Dictionary = new Dictionary<TKey, TValue>(array.Length);
80
81 foreach (var pair in array)
82 {
83 Dictionary.Add(pair.Key, pair.Value);
84 }
85 }
86 }
87}
The base class for dictionaries with custom data contract serialization.
Definition: HashMapBase.cs:13
Dictionary< TKey, TValue > Dictionary
The underlying dictionary.
Definition: HashMapBase.cs:17
A dictionary that is data contract serializable.
Definition: HashMap.cs:12
HashMap(HashMap< TKey, TValue > dict)
Initializes a copy of the specified dictionary.
Definition: HashMap.cs:40
void SetDictionary(KeyValue< TKey, TValue >[] array)
Sets the dictionary from an array of key value pairs.
Definition: HashMap.cs:77
HashMap(int capacity)
Initializes a new dictionary with the specified capacity.
Definition: HashMap.cs:31
KeyValue< TKey, TValue >[] KeyValueArray
An array of key value pairs.
Definition: HashMap.cs:17
HashMap()
Initializes a new empty dictionary.
Definition: HashMap.cs:22
KeyValue< TKey, TValue >[] GetKeyValueArray()
Returns a new array of key value pairs for the dictionary.
Definition: HashMap.cs:57
A serializable key value pair.
Definition: KeyValue.cs:10