OrderedDictionary.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace OpenVIII
  6. {
  7. /// <summary>
  8. /// <para>List of KeyValuePairs</para>
  9. /// <para>TKey cannot be int use a List</para>
  10. /// </summary>
  11. /// <typeparam name="TKey"></typeparam>
  12. /// <typeparam name="TValue"></typeparam>
  13. public sealed class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
  14. {
  15. #region Fields
  16. private List<KeyValuePair<TKey, TValue>> _list;
  17. #endregion Fields
  18. #region Constructors
  19. public OrderedDictionary(int capacity = 0) => _list = new List<KeyValuePair<TKey, TValue>>(capacity);
  20. public OrderedDictionary(IDictionary<TKey, TValue> copy) : this(copy.Count)
  21. {
  22. foreach (KeyValuePair<TKey, TValue> pair in copy)
  23. {
  24. Add(new KeyValuePair<TKey, TValue>(pair.Key, pair.Value));
  25. }
  26. }
  27. #endregion Constructors
  28. #region Properties
  29. public bool IsFixedSize => false;
  30. public bool IsReadOnly => false;
  31. public bool IsSynchronized => false;
  32. public IReadOnlyList<TKey> Keys => _list.Select(kvp => kvp.Key).ToList();
  33. public IReadOnlyList<TValue> Values => _list.Select(kvp => kvp.Value).ToList();
  34. public int Count => _list.Count;
  35. ICollection<TKey> IDictionary<TKey, TValue>.Keys => _list.Select(kvp => kvp.Key).ToArray();
  36. ICollection<TValue> IDictionary<TKey, TValue>.Values => _list.Select(kvp => kvp.Value).ToArray();
  37. #endregion Properties
  38. #region Indexers
  39. public KeyValuePair<TKey, TValue> this[int index]
  40. {
  41. get
  42. {
  43. if (TryGetKVPByIndex(index, out KeyValuePair<TKey, TValue> kvp))
  44. return kvp;
  45. throw new KeyNotFoundException($"{this}::Index:{index} - Not Found!");
  46. }
  47. }
  48. public TValue this[TKey key]
  49. {
  50. get
  51. {
  52. if (TryGetByKey(key, out TValue value))
  53. return value;
  54. throw new KeyNotFoundException($"{this}::Key:{key} - Not Found!");
  55. }
  56. set
  57. {
  58. int index = _list.FindIndex(kvp => kvp.Key.Equals(key));
  59. if (index >= 0)
  60. _list[index] = new KeyValuePair<TKey, TValue>(key, value);
  61. else
  62. throw new KeyNotFoundException($"{this}::Key:{key} - Not Found!");
  63. }
  64. }
  65. TValue IDictionary<TKey, TValue>.this[TKey key]
  66. {
  67. get => this[key];
  68. set => this[key] = value;
  69. }
  70. #endregion Indexers
  71. #region Methods
  72. public void Add(TKey key, TValue value)
  73. {
  74. if (!TryAdd(key, value)) throw new ArgumentException($"{this}::Key:{key} - Already Exists!");
  75. }
  76. public void Add(KeyValuePair<TKey, TValue> item) => Add(item.Key, item.Value);
  77. public void Clear() => _list.Clear();
  78. public bool Contains(KeyValuePair<TKey, TValue> item) => _list.Contains(item);
  79. public Boolean ContainsIndex(Int32 index) => index < _list.Count;
  80. public Boolean ContainsKey(TKey key) => _list.Any(kvp => kvp.Key.Equals(key));
  81. public Boolean ContainsValue(TValue value) => _list.Any(kvp => kvp.Value.Equals(value));
  82. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  83. {
  84. if (Count <= array.Length + arrayIndex)
  85. {
  86. foreach (KeyValuePair<TKey, TValue> i in _list)
  87. {
  88. array[arrayIndex++] = i;
  89. }
  90. return;
  91. }
  92. throw new OverflowException($"{this}::Count:{Count} > array.Length:{array.Length} + arrayIndex:{arrayIndex}");
  93. }
  94. public IEnumerator GetEnumerator() => _list.GetEnumerator();
  95. public bool Remove(TKey key)
  96. {
  97. KeyValuePair<TKey, TValue> kvp = _list.First(_kvp => _kvp.Key.Equals(key));
  98. return _list.Remove(kvp);
  99. }
  100. public bool Remove(KeyValuePair<TKey, TValue> item) => _list.Remove(item);
  101. public bool TryAdd(TKey key, TValue value)
  102. {
  103. if (!ContainsKey(key))
  104. {
  105. _list.Add(new KeyValuePair<TKey, TValue>(key, value));
  106. return true;
  107. }
  108. return false;
  109. }
  110. public Boolean TryGetByIndex(Int32 index, out TValue value)
  111. {
  112. if (ContainsIndex(index))
  113. {
  114. value = _list[index].Value;
  115. return true;
  116. }
  117. value = default;
  118. return false;
  119. }
  120. public Boolean TryGetByKey(TKey key, out TValue value)
  121. {
  122. if (ContainsKey(key))
  123. {
  124. value = _list.First(kvp => kvp.Key.Equals(key)).Value;
  125. return true;
  126. }
  127. value = default;
  128. return false;
  129. }
  130. public Boolean TryGetIndexByKey(TKey key, out int value)
  131. {
  132. if (ContainsKey(key))
  133. {
  134. value = _list.FindIndex(kvp => kvp.Key.Equals(key));
  135. return true;
  136. }
  137. value = -1;
  138. return false;
  139. }
  140. public Boolean TryGetKeyByIndex(Int32 index, out TKey value)
  141. {
  142. if (ContainsIndex(index))
  143. {
  144. value = _list[index].Key;
  145. return true;
  146. }
  147. value = default;
  148. return false;
  149. }
  150. public Boolean TryGetKVPByIndex(Int32 index, out KeyValuePair<TKey, TValue> value)
  151. {
  152. if (ContainsIndex(index))
  153. {
  154. value = _list[index];
  155. return true;
  156. }
  157. value = default;
  158. return false;
  159. }
  160. public bool TryGetValue(TKey key, out TValue value) => TryGetByKey(key, out value);
  161. public OrderedDictionary<TKey, TValue> Clone() => new OrderedDictionary<TKey, TValue>(this);
  162. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => _list.GetEnumerator();
  163. #endregion Methods
  164. }
  165. }