2 using System.Collections;
3 using System.Collections.Generic;
6 namespace Mercury.Nucleus.Collections
18 public interface IReadOnlyMultiDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
24 bool Contains(TKey key, TValue value);
33 bool ContainsKey(TKey key);
42 bool TryGetValue(TKey key, out IReadOnlyList<TValue> collection);
45 ICollection<TKey> Keys {
get; }
51 IReadOnlyList<TValue>
this[TKey key] {
get; }
64 public interface IMultiDictionary<TKey, TValue> : IReadOnlyMultiDictionary<TKey, TValue>
78 bool Add(TKey key, TValue value);
91 bool Add(KeyValuePair<TKey, TValue> tuple);
106 bool Add(TKey key, IEnumerable<TValue> values);
127 public class SetMultiDictionary<TKey, TValue> :
128 IMultiDictionary<TKey, TValue>
132 protected Dictionary<TKey, HashSet<TValue>> data
133 =
new Dictionary<TKey, HashSet<TValue>>();
137 #region [ IMultiDictionary ]
139 public bool Add(TKey key, TValue value)
142 if (data.TryGetValue(key, out set))
143 return set.Add(value);
145 data.Add(key,
new HashSet<TValue> { value });
149 public bool Add(KeyValuePair<TKey, TValue> pair)
150 {
return this.Add(pair.Key, pair.Value); }
152 public bool Add(TKey key, IEnumerable<TValue> values)
154 if (data.ContainsKey(key))
return false;
156 data.Add(key,
new HashSet<TValue>(values));
163 if (!data.TryGetValue(key, out set))
166 return set.Contains(value);
170 {
return data.ContainsKey(key); }
175 public bool TryGetValue(TKey key, out IReadOnlyList<TValue> collection)
177 if (key == null)
throw new ArgumentNullException(
"key");
179 HashSet<TValue> values = null;
180 bool r = data.TryGetValue(key, out values);
181 collection = values.ToList();
185 public ICollection<TKey> Keys {
get {
return data.Keys; } }
187 public IReadOnlyList<TValue>
this[TKey key]
188 {
get {
return data[key].ToList(); } }
192 #region [ IEnumerable ]
196 foreach(var pair
in data)
197 foreach (var value
in pair.Value)
198 yield
return new KeyValuePair<TKey, TValue>(pair.Key, value);
201 IEnumerator IEnumerable.GetEnumerator()
202 {
return GetEnumerator() as IEnumerator; }
223 public class ListMultiDictionary<TKey, TValue> :
224 IMultiDictionary<TKey, TValue>
228 private Dictionary<TKey, List<TValue>> data
229 =
new Dictionary<TKey, List<TValue>>();
243 {
foreach(var pair
in source) Add(pair.Key, pair.Value); }
252 {
return data[key][ix]; }
258 {
return data[key].Count; }
262 #region [ IMultiDictionary ]
264 public virtual bool Add(TKey key, TValue value)
266 List<TValue> values = null;
267 if (data.TryGetValue(key, out values))
270 data.Add(key,
new List<TValue> { value });
275 public virtual bool Add(KeyValuePair<TKey, TValue> pair)
276 {
return this.Add(pair.Key, pair.Value); }
278 public virtual bool Add(TKey key, IEnumerable<TValue> values)
280 if (data.ContainsKey(key))
return false;
282 data.Add(key,
new List<TValue>(values));
286 public virtual bool Contains(TKey key, TValue value)
289 if (!data.TryGetValue(key, out list))
292 return list.Contains(value);
296 {
return data.ContainsKey(key); }
301 public bool TryGetValue(TKey key, out IReadOnlyList<TValue> collection)
303 if (key == null)
throw new ArgumentNullException(
"key");
305 List<TValue> values = null;
306 bool r = data.TryGetValue(key, out values);
311 public ICollection<TKey> Keys {
get {
return data.Keys; } }
313 public IReadOnlyList<TValue>
this[TKey key]
314 {
get {
return data[key]; } }
318 #region [ IEnumerable ]
322 foreach(var pair
in data)
323 for (
int ix = 0; ix < pair.Value.Count; ++ix)
324 yield
return new KeyValuePair<TKey, TValue>(pair.Key, pair.Value[ix]);
327 IEnumerator IEnumerable.GetEnumerator()
328 {
return GetEnumerator() as IEnumerator; }
351 public class UniqueListMultiDictionary<TKey, TValue> :
352 ListMultiDictionary<TKey, TValue>, IMultiDictionary<TKey, TValue>
361 #region [ ListMultiDictionary ]
363 public override bool Add(TKey key, TValue value)
364 {
return data.Add(key, value) && base.Add(key, value); }
366 public override bool Contains(TKey key, TValue value)
367 {
return data.Contains(key, value); }
370 { data.Clear(); base.Clear(); }
bool TryGetValue(TKey key, out IReadOnlyList< TValue > collection)
Gets the value associated with the specified key.
bool ContainsKey(TKey key)
Indicates whether this collection contains the specified key.
override void Clear()
Removes the content of collection.
void Clear()
Removes the content of collection.
virtual void Clear()
Removes the content of collection.
virtual bool Add(TKey key, TValue value)
Adds the value to the dictionary. The actual behaviour and handling of duplicities is specified by an...
bool ContainsKey(TKey key)
Indicates whether this collection contains the specified key.
ListMultiDictionary(IMultiDictionary< TKey, TValue > source)
Creates a multidictionary initialized by values from another multidictionary.
virtual bool Add(TKey key, IEnumerable< TValue > values)
Adds the collection of values to the dictionary. If the key is already present, NO value is added an...
override bool Add(TKey key, TValue value)
Adds the value to the dictionary. The actual behaviour and handling of duplicities is specified by an...
bool Add(KeyValuePair< TKey, TValue > pair)
Adds the value to the dictionary. The actual behaviour and handling of duplicities is specified by an...
int GetValuesCount(TKey key)
The size of underlying list.
bool Add(TKey key, IEnumerable< TValue > values)
Adds the collection of values to the dictionary. If the key is already present, NO value is added an...
Represents a multi-valued dictionary, in other words a map between a key and a collection of values...
IEnumerator< KeyValuePair< TKey, TValue > > GetEnumerator()
An implementation of IMultiDictionary{TKey,TValue} using T:System.Collections.Generic.HashSet{T} as an underlying collection of values.
ListMultiDictionary()
Default constructor, creates an empty dictionary
virtual bool Add(KeyValuePair< TKey, TValue > pair)
Adds the value to the dictionary. The actual behaviour and handling of duplicities is specified by an...
virtual bool Contains(TKey key, TValue value)
Indicates whether this collection contains the specified key and value.
bool Add(TKey key, TValue value)
Adds the value to the dictionary. The actual behaviour and handling of duplicities is specified by an...
TValue GetValue(TKey key, int ix)
Gets the value from the underlying list.
bool Contains(TKey key, TValue value)
Indicates whether this collection contains the specified key and value.
override bool Contains(TKey key, TValue value)
Indicates whether this collection contains the specified key and value.
IEnumerator< KeyValuePair< TKey, TValue > > GetEnumerator()
bool TryGetValue(TKey key, out IReadOnlyList< TValue > collection)
Gets the value associated with the specified key.