ListDictionary.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. {
  87. get {
  88. return count;
  89. }
  90. }
  91. public bool IsSynchronized
  92. {
  93. get {
  94. return false;
  95. }
  96. }
  97. public object SyncRoot
  98. {
  99. get {
  100. return this;
  101. }
  102. }
  103. [MonoTODO]
  104. public void CopyTo(Array array, int index)
  105. {
  106. // FIXME
  107. }
  108. // IDictionary Interface
  109. public bool IsFixedSize
  110. {
  111. get {
  112. return false;
  113. }
  114. }
  115. public bool IsReadOnly
  116. {
  117. get {
  118. return false;
  119. }
  120. }
  121. // Indexer
  122. public object this[object key]
  123. {
  124. get {
  125. ListEntry entry = FindEntry(key);
  126. return entry == null ? entry : entry.value;
  127. }
  128. set {
  129. ListEntry entry = FindEntry(key);
  130. if (entry != null) {
  131. entry.value = value;
  132. count++;
  133. modCount++;
  134. } else {
  135. AddImpl(key, value);
  136. }
  137. }
  138. }
  139. public ICollection Keys
  140. {
  141. get {
  142. return new ListEntryCollection(this, true);
  143. }
  144. }
  145. public ICollection Values
  146. {
  147. get {
  148. return new ListEntryCollection(this, false);
  149. }
  150. }
  151. public void Add(object key, object value)
  152. {
  153. AddImpl(key, value);
  154. }
  155. public void Clear()
  156. {
  157. root = null;
  158. count = 0;
  159. modCount++;
  160. }
  161. public bool Contains(object key)
  162. {
  163. return FindEntry(key) != null ? true : false;
  164. }
  165. public IDictionaryEnumerator GetEnumerator()
  166. {
  167. return new ListEntryEnumerator(this);
  168. }
  169. public void Remove(object key)
  170. {
  171. ListEntry entry = root;
  172. for (ListEntry prev = null; entry != null; prev = entry, entry = entry.next) {
  173. if (AreEqual(key, entry.key)) {
  174. if (prev != null) {
  175. prev.next = entry.next;
  176. } else {
  177. root = entry.next;
  178. }
  179. entry.value = null;
  180. count--;
  181. modCount++;
  182. }
  183. }
  184. }
  185. private class ListEntry
  186. {
  187. public object key = null;
  188. public object value = null;
  189. public ListEntry next = null;
  190. }
  191. private class ListEntryEnumerator : IEnumerator, IDictionaryEnumerator
  192. {
  193. private ListDictionary dict;
  194. private bool isAtStart;
  195. private ListEntry current;
  196. private int version;
  197. public ListEntryEnumerator(ListDictionary dict)
  198. {
  199. this.dict = dict;
  200. version = dict.modCount;
  201. Reset();
  202. }
  203. private void FailFast()
  204. {
  205. if (version != dict.modCount) {
  206. throw new InvalidOperationException(
  207. "The ListDictionary's contents changed after this enumerator was instantiated.");
  208. }
  209. }
  210. public bool MoveNext()
  211. {
  212. FailFast();
  213. if (isAtStart) {
  214. current = dict.root;
  215. isAtStart = false;
  216. } else {
  217. current = current.next;
  218. }
  219. return current != null ? true : false;
  220. }
  221. public void Reset()
  222. {
  223. FailFast();
  224. isAtStart = true;
  225. current = null;
  226. }
  227. public object Current
  228. {
  229. get {
  230. FailFast();
  231. if (isAtStart || current == null) {
  232. throw new InvalidOperationException(
  233. "Enumerator is positioned before the collection's first element or after the last element.");
  234. }
  235. return new DictionaryEntry(current.key, current.value);
  236. }
  237. }
  238. // IDictionaryEnumerator
  239. public DictionaryEntry Entry
  240. {
  241. get {
  242. FailFast();
  243. return (DictionaryEntry) Current;
  244. }
  245. }
  246. public object Key
  247. {
  248. get {
  249. FailFast();
  250. if (isAtStart || current == null) {
  251. throw new InvalidOperationException(
  252. "Enumerator is positioned before the collection's first element or after the last element.");
  253. }
  254. return current.key;
  255. }
  256. }
  257. public object Value
  258. {
  259. get {
  260. FailFast();
  261. if (isAtStart || current == null) {
  262. throw new InvalidOperationException(
  263. "Enumerator is positioned before the collection's first element or after the last element.");
  264. }
  265. return current.value;
  266. }
  267. }
  268. }
  269. private class ListEntryCollection : ICollection
  270. {
  271. private ListDictionary dict;
  272. private bool isKeyList;
  273. public ListEntryCollection(ListDictionary dict, bool isKeyList)
  274. {
  275. this.dict = dict;
  276. this.isKeyList = isKeyList;
  277. }
  278. // ICollection Interface
  279. public int Count
  280. {
  281. get {
  282. return dict.Count;
  283. }
  284. }
  285. public bool IsSynchronized
  286. {
  287. get {
  288. return false;
  289. }
  290. }
  291. public object SyncRoot
  292. {
  293. get {
  294. return dict.SyncRoot;
  295. }
  296. }
  297. [MonoTODO]
  298. public void CopyTo(Array array, int index)
  299. {
  300. // FIXME
  301. }
  302. // IEnumerable Interface
  303. public IEnumerator GetEnumerator()
  304. {
  305. return new ListEntryCollectionEnumerator(dict, isKeyList);
  306. }
  307. private class ListEntryCollectionEnumerator : IEnumerator
  308. {
  309. private ListDictionary dict;
  310. private bool isKeyList;
  311. private bool isAtStart;
  312. private int version;
  313. private ListEntry current;
  314. public ListEntryCollectionEnumerator(ListDictionary dict, bool isKeyList)
  315. {
  316. this.dict = dict;
  317. this.isKeyList = isKeyList;
  318. isAtStart = true;
  319. version = dict.modCount;
  320. }
  321. private void FailFast()
  322. {
  323. if (version != dict.modCount) {
  324. throw new InvalidOperationException(
  325. "The Collection's contents changed after this " +
  326. "enumerator was instantiated.");
  327. }
  328. }
  329. public object Current
  330. {
  331. get {
  332. FailFast();
  333. if (isAtStart || current == null) {
  334. throw new InvalidOperationException(
  335. "Enumerator is positioned before the collection's " +
  336. "first element or after the last element.");
  337. }
  338. return isKeyList ? current.key : current.value;
  339. }
  340. }
  341. public bool MoveNext()
  342. {
  343. FailFast();
  344. if (isAtStart) {
  345. current = dict.root;
  346. isAtStart = false;
  347. } else {
  348. current = current.next;
  349. }
  350. return current != null ? true : false;
  351. }
  352. public void Reset()
  353. {
  354. FailFast();
  355. isAtStart = true;
  356. current = null;
  357. }
  358. }
  359. }
  360. }
  361. }