9 using System.Collections;
10 using System.Collections.Generic;
11 using System.Diagnostics;
13 namespace SiliconStudio.Core.Collections
25 private SortedDictionary<TKey, List<TValue>> _SortedDictionary;
27 private bool _IsModified;
36 _SortedDictionary =
new SortedDictionary<TKey, List<TValue>>();
51 _SortedDictionary =
new SortedDictionary<TKey, List<TValue>>(comparer);
68 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
70 foreach (KeyValuePair<TKey, TValue> pair
in dictionary)
71 this.Add(pair.Key, pair.Value);
94 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
96 foreach (KeyValuePair<TKey, TValue> pair
in dictionary)
97 this.Add(pair.Key, pair.Value);
117 public int Count {
get {
return _Count; } }
135 public ValueCollection Values {
get {
return new ValueCollection(
this); } }
152 public TValue
this[TKey key]
156 if (key == null)
throw new ArgumentNullException(
"key");
159 if (!_SortedDictionary.TryGetValue(key, out values))
160 return (TValue)TryToGetOnNotFound(key);
161 TValue value =
default(TValue);
168 if (key == null)
throw new ArgumentNullException(
"key");
184 public void Add(TKey key, TValue value)
186 if (key == null)
throw new ArgumentNullException(
"key");
190 if (!_SortedDictionary.TryGetValue(key, out values))
192 values =
new List<TValue>();
193 _SortedDictionary[key] = values;
206 _SortedDictionary.Clear();
225 return _SortedDictionary.ContainsKey(key);
242 bool contains =
false;
243 foreach (TValue val
in Values)
245 if (val.Equals(value))
278 public void CopyTo(KeyValuePair<TKey, TValue>[] array,
int index)
281 throw new ArgumentNullException(
"array");
283 throw new ArgumentOutOfRangeException(
"index");
284 else if (index >= array.Length || _Count > array.Length - index)
285 throw new ArgumentException(
"index");
287 int copyIndex = index;
288 foreach (KeyValuePair<TKey, TValue> pair
in this)
289 array[copyIndex++] =
new KeyValuePair<TKey, TValue>(pair.Key, pair.Value);
302 return new Enumerator(
this);
316 public bool Remove(KeyValuePair<TKey, TValue> item)
318 if (item.Key == null ||
320 throw new ArgumentException();
322 bool removed =
false;
324 if (_SortedDictionary.TryGetValue(item.Key, out values) &&
325 values.Remove(item.Value))
328 if (values.Count == 0)
329 _SortedDictionary.Remove(item.Key);
349 if (key == null)
throw new ArgumentNullException();
351 bool removed =
false;
353 if (_SortedDictionary.TryGetValue(key, out values) &&
354 _SortedDictionary.Remove(key))
356 _Count -= values.Count;
357 _IsModified = removed =
true;
382 if (key == null)
throw new ArgumentNullException();
384 value =
default(TValue);
387 if (_SortedDictionary.TryGetValue(key, out values))
414 if (key == null)
throw new ArgumentNullException();
418 List<TValue> valuesList;
419 if (_SortedDictionary.TryGetValue(key, out valuesList))
427 #region IDictionary members
436 get {
return Values; }
439 bool IDictionary.IsFixedSize {
get {
return false; } }
440 bool IDictionary.IsReadOnly {
get {
return false; } }
442 ICollection IDictionary.Values {
get {
return new ValueCollection(
this); } }
443 object IDictionary.this[
object key]
447 if (key == null)
throw new ArgumentNullException(
"key");
450 var keyT = (TKey)
Convert.ChangeType(key, typeof(TKey));
451 if (!_SortedDictionary.TryGetValue(keyT, out values))
452 return TryToGetOnNotFound(keyT);
457 if (key == null)
throw new ArgumentNullException(
"key");
459 Add((TKey)
Convert.ChangeType(key, typeof(TKey)), (TValue)
Convert.ChangeType(value, typeof(TValue)));
462 void IDictionary.Add(
object key,
object value)
464 if (key == null)
throw new ArgumentNullException(
"key");
466 Add((TKey)
Convert.ChangeType(key, typeof(TKey)), (TValue)
Convert.ChangeType(value, typeof(TValue)));
469 bool IDictionary.Contains(
object key)
471 if (key == null)
throw new ArgumentNullException(
"key");
473 return ContainsKey((TKey)
Convert.ChangeType(key, typeof(TKey)));
476 IDictionaryEnumerator IDictionary.GetEnumerator()
479 return new Enumerator(
this);
482 void IDictionary.Remove(
object key)
484 if (key == null)
throw new ArgumentNullException(
"key");
486 this.Remove((TKey)
Convert.ChangeType(key, typeof(TKey)));
491 #region ICollection members
494 bool ICollection.IsSynchronized {
get {
return false; } }
495 object ICollection.SyncRoot {
get {
return this; } }
496 void ICollection.CopyTo(
Array array,
int index)
499 throw new ArgumentNullException(
"array");
501 throw new ArgumentOutOfRangeException(
"index");
502 else if (index >= array.Length || _Count > array.Length - index)
503 throw new ArgumentException(
"index");
505 int copyIndex = index;
506 foreach (KeyValuePair<TKey, TValue> pair
in this)
507 array.SetValue(pair, copyIndex++);
514 if (_SortedDictionary.TryGetValue(item.Key, out values) &&
515 values.Contains(item.Value))
517 if (values.Remove(item.Value))
520 _IsModified = done =
true;
521 if (values.Count == 0)
522 _SortedDictionary.Remove(item.Key);
531 return _SortedDictionary.TryGetValue(item.Key, out values) &&
532 values.Contains(item.Value);
537 Add(item.Key, item.Value);
542 #region IEnumerable members
546 return GetEnumerator();
549 IEnumerator IEnumerable.GetEnumerator()
551 return GetEnumerator();
560 [DebuggerDisplay(
"Count = {Count}")]
579 if (dictionary == null)
throw new ArgumentNullException();
581 _Dictionary = dictionary;
592 public int Count {
get {
return _Dictionary.Count; } }
619 public void CopyTo(TValue[] array,
int index)
622 throw new ArgumentNullException(
"array");
624 throw new ArgumentOutOfRangeException(
"index");
625 else if (index >= array.Length || Count > array.Length - index)
626 throw new ArgumentException(
"index");
628 int copyIndex = index;
629 foreach (TValue value
in this)
630 array[copyIndex++] = value;
642 _Dictionary._IsModified =
false;
643 return new Enumerator(_Dictionary);
646 #region ICollection member
658 throw new InvalidOperationException();
663 throw new InvalidOperationException();
668 throw new InvalidOperationException();
671 void ICollection.CopyTo(
Array array,
int index)
674 throw new ArgumentNullException(
"array");
676 throw new ArgumentOutOfRangeException(
"index");
677 else if (index >= array.Length || Count > array.Length - index)
678 throw new ArgumentException(
"index");
680 int copyIndex = index;
681 foreach (TValue value
in this)
682 array.SetValue(value, copyIndex++);
687 return _Dictionary.ContainsValue(item);
698 bool ICollection.IsSynchronized {
get {
return false; } }
706 object ICollection.SyncRoot {
get {
return this; } }
710 #region IEnumerator members
714 return GetEnumerator();
717 IEnumerator IEnumerable.GetEnumerator()
719 return GetEnumerator();
727 public struct Enumerator : IEnumerator<TValue>, IDisposable, IEnumerator
734 _Dictionary = dictionary;
745 public TValue Current {
get {
return _Enumerator.Current.Value; } }
766 return _Enumerator.MoveNext();
776 object IEnumerator.Current
778 get {
return Current; }
788 void IEnumerator.Reset()
790 (_Enumerator as IEnumerator).Reset();
797 throw new KeyNotFoundException();
803 public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, IDictionaryEnumerator, IEnumerator
805 private readonly KeyValuePair<TKey, TValue> DefaultCurrent;
807 private IEnumerator<KeyValuePair<TKey, List<TValue>>> _Enumerator1;
808 private IEnumerator<TValue> _Enumerator2;
809 private KeyValuePair<TKey, TValue> _Current;
816 if (dictionary == null)
817 throw new ArgumentNullException(
"dictionary");
819 _Dictionary = dictionary;
820 _Enumerator1 = dictionary._SortedDictionary.GetEnumerator();
822 DefaultCurrent =
new KeyValuePair<TKey, TValue>(
default(TKey),
default(TValue));
823 _Current = DefaultCurrent;
835 public KeyValuePair<TKey, TValue> Current
854 DictionaryEntry IDictionaryEnumerator.Entry
858 return new DictionaryEntry(Current.Key, Current.Value);
872 object IDictionaryEnumerator.Key
890 object IDictionaryEnumerator.Value
894 return Current.Value;
925 if (_Dictionary._IsModified)
926 throw new InvalidOperationException(
"The collection was modified after the enumerator was created.");
928 if (!_Valid)
return false;
929 TKey key =
default(TKey);
930 TValue value =
default(TValue);
931 if (_Enumerator2 == null)
933 if (_Enumerator1.MoveNext())
935 if (_Enumerator1.Current.Value != null)
936 _Enumerator2 = _Enumerator1.Current.Value.GetEnumerator();
942 if (!_Valid)
return false;
944 key = _Enumerator1.Current.Key;
945 if (_Enumerator2 != null)
947 if (_Enumerator2.MoveNext())
948 value = _Enumerator2.Current;
955 _Current =
new KeyValuePair<TKey, TValue>(key, value);
966 object IEnumerator.Current
968 get {
return Current; }
978 void IEnumerator.Reset()
982 if (_Dictionary._IsModified)
983 throw new InvalidOperationException(
"The collection was modified after the enumerator was created.");
985 _Enumerator1.Reset();
MultiValueSortedDictionary(IDictionary< TKey, TValue > dictionary, IComparer< TKey > comparer)
Initializes a new instance of the KoderHack.MultiValueSortedDictionary class that contai...
MultiValueSortedDictionary(IDictionary< TKey, TValue > dictionary)
Initializes a new instance of the KoderHack.MultiValueSortedDictionary class that contai...
void Dispose()
Releases all resources used by the KoderHack.MultiValueSortedDictionary.ValueCollection.Enumerator.
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
void Clear()
Removes all elements from the KoderHack.MultiValueSortedDictionary.
MultiValueSortedDictionary()
Initializes a new instance of the KoderHack.MultiValueSortedDictionary class that is emp...
Enumerator GetEnumerator()
Returns an enumerator that iterates through the KoderHack.MultiValueSortedDictionary.
bool Remove(TKey key)
Removes the elements with the specified key from the KoderHack.MultiValueSortedDictionary.
void CopyTo(TValue[] array, int index)
Copies the KoderHack.MultiValueSortedDictionary.ValueCollection elements to an existing ...
SiliconStudio.Paradox.Input.Keys Keys
bool MoveNext()
Advances the enumerator to the next element of the KoderHack.MultiValueSortedDictionary.
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
Copies the elements of the KoderHack.MultiValueSortedDictionary to the specified array o...
bool ContainsKey(TKey key)
Determines whether the KoderHack.MultiValueSortedDictionary contains an element with the...
bool TryGetValue(TKey key, out IEnumerable< TValue > values)
Gets the value associated with the specified key.
void Add(TKey key, TValue value)
Adds an element with the specified key and value into the KoderHack.MultiValueSortedDictionary.
bool MoveNext()
Advances the enumerator to the next element of the KoderHack.MultiValueSortedDictionary.ValueCollection.
Enumerator GetEnumerator()
Returns an enumerator that iterates through the KoderHack.MultiValueSortedDictionary.ValueCollection.
bool ContainsValue(TValue value)
Determines whether the KoderHack.MultiValueSortedDictionary contains an element with the...
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
virtual object TryToGetOnNotFound(TKey name)
MultiValueSortedDictionary(IComparer< TKey > comparer)
Initializes a new instance of the KoderHack.MultiValueSortedDictionary class that is emp...
void Dispose()
Releases all resources used by the KoderHack.MultiValueSortedDictionary.Enumerator.
ValueCollection(MultiValueSortedDictionary< TKey, TValue > dictionary)
Initializes a new instance of the KoderHack.MultiValueSortedDictionary.ValueCollection class that reflects the values in the specified KoderHack.MultiValueSortedDictionary.
Enumerator(MultiValueSortedDictionary< TKey, TValue > dictionary)
Represents a priority queue, where keys are sorted and each key might have mlutiple values...
bool Remove(KeyValuePair< TKey, TValue > item)
Removes the element with the specified key from the KoderHack.MultiValueSortedDictionary.