ListDictionary.cs 8.0 KB

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