LinkedDictionary.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections
  6. {
  7. public class LinkedDictionary
  8. : IDictionary
  9. {
  10. internal readonly IDictionary hash = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  11. internal readonly IList keys = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  12. public LinkedDictionary()
  13. {
  14. }
  15. public virtual void Add(object k, object v)
  16. {
  17. hash.Add(k, v);
  18. keys.Add(k);
  19. }
  20. public virtual void Clear()
  21. {
  22. hash.Clear();
  23. keys.Clear();
  24. }
  25. public virtual bool Contains(object k)
  26. {
  27. return hash.Contains(k);
  28. }
  29. public virtual void CopyTo(Array array, int index)
  30. {
  31. foreach (object k in keys)
  32. {
  33. array.SetValue(hash[k], index++);
  34. }
  35. }
  36. public virtual int Count
  37. {
  38. get { return hash.Count; }
  39. }
  40. IEnumerator IEnumerable.GetEnumerator()
  41. {
  42. return GetEnumerator();
  43. }
  44. public virtual IDictionaryEnumerator GetEnumerator()
  45. {
  46. return new LinkedDictionaryEnumerator(this);
  47. }
  48. public virtual void Remove(object k)
  49. {
  50. hash.Remove(k);
  51. keys.Remove(k);
  52. }
  53. public virtual bool IsFixedSize
  54. {
  55. get { return false; }
  56. }
  57. public virtual bool IsReadOnly
  58. {
  59. get { return false; }
  60. }
  61. public virtual bool IsSynchronized
  62. {
  63. get { return false; }
  64. }
  65. public virtual object SyncRoot
  66. {
  67. get { return false; }
  68. }
  69. public virtual ICollection Keys
  70. {
  71. get { return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(keys); }
  72. }
  73. public virtual ICollection Values
  74. {
  75. // NB: Order has to be the same as for Keys property
  76. get
  77. {
  78. IList values = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(keys.Count);
  79. foreach (object k in keys)
  80. {
  81. values.Add(hash[k]);
  82. }
  83. return values;
  84. }
  85. }
  86. public virtual object this[object k]
  87. {
  88. get
  89. {
  90. return hash[k];
  91. }
  92. set
  93. {
  94. if (!hash.Contains(k))
  95. keys.Add(k);
  96. hash[k] = value;
  97. }
  98. }
  99. }
  100. internal class LinkedDictionaryEnumerator : IDictionaryEnumerator
  101. {
  102. private readonly LinkedDictionary parent;
  103. private int pos = -1;
  104. internal LinkedDictionaryEnumerator(LinkedDictionary parent)
  105. {
  106. this.parent = parent;
  107. }
  108. public virtual object Current
  109. {
  110. get { return Entry; }
  111. }
  112. public virtual DictionaryEntry Entry
  113. {
  114. get
  115. {
  116. object k = CurrentKey;
  117. return new DictionaryEntry(k, parent.hash[k]);
  118. }
  119. }
  120. public virtual object Key
  121. {
  122. get
  123. {
  124. return CurrentKey;
  125. }
  126. }
  127. public virtual bool MoveNext()
  128. {
  129. if (pos >= parent.keys.Count)
  130. return false;
  131. return ++pos < parent.keys.Count;
  132. }
  133. public virtual void Reset()
  134. {
  135. this.pos = -1;
  136. }
  137. public virtual object Value
  138. {
  139. get
  140. {
  141. return parent.hash[CurrentKey];
  142. }
  143. }
  144. private object CurrentKey
  145. {
  146. get
  147. {
  148. if (pos < 0 || pos >= parent.keys.Count)
  149. throw new InvalidOperationException();
  150. return parent.keys[pos];
  151. }
  152. }
  153. }
  154. }
  155. #pragma warning restore
  156. #endif