ListDictionary.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. namespace System.Collections.Specialized
  2. {
  3. [Serializable]
  4. public class ListDictionary : IDictionary, ICollection, IEnumerable
  5. {
  6. private int count;
  7. private int modCount;
  8. private ListEntry root;
  9. private IComparer comparer;
  10. public ListDictionary()
  11. {
  12. count = 0;
  13. modCount = 0;
  14. comparer = null;
  15. root = null;
  16. }
  17. public ListDictionary(IComparer comparer) : this()
  18. {
  19. this.comparer = comparer;
  20. }
  21. private bool AreEqual(object obj1, object obj2)
  22. {
  23. if (comparer != null) {
  24. if (comparer.Compare(obj1, obj2) == 0) {
  25. return true;
  26. }
  27. } else {
  28. if (obj1.Equals(obj2)) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. private ListEntry FindEntry(object key)
  35. {
  36. if (key == null) {
  37. throw new ArgumentNullException("Attempted lookup for a null key.");
  38. }
  39. if (root == null) {
  40. return null;
  41. } else {
  42. ListEntry entry = root;
  43. while (entry != null) {
  44. if (AreEqual(key, entry.key)) {
  45. return entry;
  46. }
  47. entry = entry.next;
  48. }
  49. }
  50. return null;
  51. }
  52. private void AddImpl(object key, object value)
  53. {
  54. if (key == null) {
  55. throw new ArgumentNullException("Attempted add with a null key.");
  56. }
  57. if (root == null) {
  58. root = new ListEntry();
  59. root.key = key;
  60. root.value = value;
  61. } else {
  62. ListEntry entry = root;
  63. while (entry != null) {
  64. if (AreEqual(key, entry.key)) {
  65. throw new ArgumentException("Duplicate key in add.");
  66. }
  67. if (entry.next == null) {
  68. break;
  69. }
  70. entry = entry.next;
  71. }
  72. entry.next = new ListEntry();
  73. entry.next.key = key;
  74. entry.next.value = value;
  75. }
  76. count++;
  77. modCount++;
  78. }
  79. // IEnumerable Interface
  80. IEnumerator IEnumerable.GetEnumerator()
  81. {
  82. return new ListEntryEnumerator(this);
  83. }
  84. // ICollection Interface
  85. public int Count {
  86. get {
  87. return count;
  88. }
  89. }
  90. public bool IsSynchronized {
  91. get {
  92. return false;
  93. }
  94. }
  95. public object SyncRoot {
  96. get {
  97. return this;
  98. }
  99. }
  100. public void CopyTo(Array array, int index)
  101. {
  102. int i = index;
  103. foreach ( DictionaryEntry entry in this )
  104. array.SetValue( entry, i++ );
  105. }
  106. // IDictionary Interface
  107. public bool IsFixedSize
  108. {
  109. get {
  110. return false;
  111. }
  112. }
  113. public bool IsReadOnly
  114. {
  115. get {
  116. return false;
  117. }
  118. }
  119. // Indexer
  120. public object this[object key]
  121. {
  122. get {
  123. ListEntry entry = FindEntry(key);
  124. return entry == null ? entry : entry.value;
  125. }
  126. set {
  127. ListEntry entry = FindEntry(key);
  128. if (entry != null)
  129. entry.value = value;
  130. else
  131. AddImpl(key, value);
  132. }
  133. }
  134. public ICollection Keys
  135. {
  136. get {
  137. return new ListEntryCollection(this, true);
  138. }
  139. }
  140. public ICollection Values
  141. {
  142. get {
  143. return new ListEntryCollection(this, false);
  144. }
  145. }
  146. public void Add(object key, object value)
  147. {
  148. AddImpl(key, value);
  149. }
  150. public void Clear()
  151. {
  152. root = null;
  153. count = 0;
  154. modCount++;
  155. }
  156. public bool Contains(object key)
  157. {
  158. return FindEntry(key) != null ? true : false;
  159. }
  160. public IDictionaryEnumerator GetEnumerator()
  161. {
  162. return new ListEntryEnumerator(this);
  163. }
  164. public void Remove(object key)
  165. {
  166. ListEntry entry = root;
  167. for (ListEntry prev = null; entry != null; prev = entry, entry = entry.next) {
  168. if (AreEqual(key, entry.key)) {
  169. if (prev != null) {
  170. prev.next = entry.next;
  171. } else {
  172. root = entry.next;
  173. }
  174. entry.value = null;
  175. count--;
  176. modCount++;
  177. }
  178. }
  179. }
  180. private class ListEntry
  181. {
  182. public object key = null;
  183. public object value = null;
  184. public ListEntry next = null;
  185. }
  186. private class ListEntryEnumerator : IEnumerator, IDictionaryEnumerator
  187. {
  188. private ListDictionary dict;
  189. private bool isAtStart;
  190. private ListEntry current;
  191. private int version;
  192. public ListEntryEnumerator(ListDictionary dict)
  193. {
  194. this.dict = dict;
  195. version = dict.modCount;
  196. Reset();
  197. }
  198. private void FailFast()
  199. {
  200. if (version != dict.modCount) {
  201. throw new InvalidOperationException(
  202. "The ListDictionary's contents changed after this enumerator was instantiated.");
  203. }
  204. }
  205. public bool MoveNext()
  206. {
  207. FailFast();
  208. if (isAtStart) {
  209. current = dict.root;
  210. isAtStart = false;
  211. } else {
  212. current = current.next;
  213. }
  214. return current != null ? true : false;
  215. }
  216. public void Reset()
  217. {
  218. FailFast();
  219. isAtStart = true;
  220. current = null;
  221. }
  222. public object Current
  223. {
  224. get {
  225. FailFast();
  226. if (isAtStart || current == null) {
  227. throw new InvalidOperationException(
  228. "Enumerator is positioned before the collection's first element or after the last element.");
  229. }
  230. return new DictionaryEntry(current.key, current.value);
  231. }
  232. }
  233. // IDictionaryEnumerator
  234. public DictionaryEntry Entry
  235. {
  236. get {
  237. FailFast();
  238. return (DictionaryEntry) Current;
  239. }
  240. }
  241. public object Key
  242. {
  243. get {
  244. FailFast();
  245. if (isAtStart || current == null) {
  246. throw new InvalidOperationException(
  247. "Enumerator is positioned before the collection's first element or after the last element.");
  248. }
  249. return current.key;
  250. }
  251. }
  252. public object Value
  253. {
  254. get {
  255. FailFast();
  256. if (isAtStart || current == null) {
  257. throw new InvalidOperationException(
  258. "Enumerator is positioned before the collection's first element or after the last element.");
  259. }
  260. return current.value;
  261. }
  262. }
  263. }
  264. private class ListEntryCollection : ICollection
  265. {
  266. private ListDictionary dict;
  267. private bool isKeyList;
  268. public ListEntryCollection(ListDictionary dict, bool isKeyList)
  269. {
  270. this.dict = dict;
  271. this.isKeyList = isKeyList;
  272. }
  273. // ICollection Interface
  274. public int Count {
  275. get {
  276. return dict.Count;
  277. }
  278. }
  279. public bool IsSynchronized
  280. {
  281. get {
  282. return false;
  283. }
  284. }
  285. public object SyncRoot
  286. {
  287. get {
  288. return dict.SyncRoot;
  289. }
  290. }
  291. public void CopyTo(Array array, int index)
  292. {
  293. int i = index;
  294. foreach ( object obj in this )
  295. array.SetValue( obj, i++ );
  296. }
  297. // IEnumerable Interface
  298. public IEnumerator GetEnumerator()
  299. {
  300. return new ListEntryCollectionEnumerator(dict, isKeyList);
  301. }
  302. private class ListEntryCollectionEnumerator : IEnumerator
  303. {
  304. private ListDictionary dict;
  305. private bool isKeyList;
  306. private bool isAtStart;
  307. private int version;
  308. private ListEntry current;
  309. public ListEntryCollectionEnumerator(ListDictionary dict, bool isKeyList)
  310. {
  311. this.dict = dict;
  312. this.isKeyList = isKeyList;
  313. isAtStart = true;
  314. version = dict.modCount;
  315. }
  316. private void FailFast()
  317. {
  318. if (version != dict.modCount) {
  319. throw new InvalidOperationException(
  320. "The Collection's contents changed after this " +
  321. "enumerator was instantiated.");
  322. }
  323. }
  324. public object Current
  325. {
  326. get {
  327. FailFast();
  328. if (isAtStart || current == null) {
  329. throw new InvalidOperationException(
  330. "Enumerator is positioned before the collection's " +
  331. "first element or after the last element.");
  332. }
  333. return isKeyList ? current.key : current.value;
  334. }
  335. }
  336. public bool MoveNext()
  337. {
  338. FailFast();
  339. if (isAtStart) {
  340. current = dict.root;
  341. isAtStart = false;
  342. } else {
  343. current = current.next;
  344. }
  345. return current != null ? true : false;
  346. }
  347. public void Reset()
  348. {
  349. FailFast();
  350. isAtStart = true;
  351. current = null;
  352. }
  353. }
  354. }
  355. }
  356. }