ListDictionary.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. namespace System.Collections.Specialized
  22. {
  23. [Serializable]
  24. public class ListDictionary : IDictionary, ICollection, IEnumerable
  25. {
  26. private int count;
  27. private int modCount;
  28. private ListEntry root;
  29. private IComparer comparer;
  30. public ListDictionary()
  31. {
  32. count = 0;
  33. modCount = 0;
  34. comparer = null;
  35. root = null;
  36. }
  37. public ListDictionary(IComparer comparer) : this()
  38. {
  39. this.comparer = comparer;
  40. }
  41. private bool AreEqual(object obj1, object obj2)
  42. {
  43. if (comparer != null) {
  44. if (comparer.Compare(obj1, obj2) == 0) {
  45. return true;
  46. }
  47. } else {
  48. if (obj1.Equals(obj2)) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. private ListEntry FindEntry(object key)
  55. {
  56. if (key == null) {
  57. throw new ArgumentNullException("Attempted lookup for a null key.");
  58. }
  59. if (root == null) {
  60. return null;
  61. } else {
  62. ListEntry entry = root;
  63. while (entry != null) {
  64. if (AreEqual(key, entry.key)) {
  65. return entry;
  66. }
  67. entry = entry.next;
  68. }
  69. }
  70. return null;
  71. }
  72. private void AddImpl(object key, object value)
  73. {
  74. if (key == null) {
  75. throw new ArgumentNullException("Attempted add with a null key.");
  76. }
  77. if (root == null) {
  78. root = new ListEntry();
  79. root.key = key;
  80. root.value = value;
  81. } else {
  82. ListEntry entry = root;
  83. while (entry != null) {
  84. if (AreEqual(key, entry.key)) {
  85. throw new ArgumentException("Duplicate key in add.");
  86. }
  87. if (entry.next == null) {
  88. break;
  89. }
  90. entry = entry.next;
  91. }
  92. entry.next = new ListEntry();
  93. entry.next.key = key;
  94. entry.next.value = value;
  95. }
  96. count++;
  97. modCount++;
  98. }
  99. // IEnumerable Interface
  100. IEnumerator IEnumerable.GetEnumerator()
  101. {
  102. return new ListEntryEnumerator(this);
  103. }
  104. // ICollection Interface
  105. public int Count {
  106. get {
  107. return count;
  108. }
  109. }
  110. public bool IsSynchronized {
  111. get {
  112. return false;
  113. }
  114. }
  115. public object SyncRoot {
  116. get {
  117. return this;
  118. }
  119. }
  120. public void CopyTo(Array array, int index)
  121. {
  122. if (array == null)
  123. throw new ArgumentNullException(
  124. "array",
  125. "Array cannot be null.");
  126. if (index < 0)
  127. throw new ArgumentOutOfRangeException("index", "index is less than 0");
  128. int i = index;
  129. foreach ( DictionaryEntry entry in this )
  130. array.SetValue( entry, i++ );
  131. }
  132. // IDictionary Interface
  133. public bool IsFixedSize
  134. {
  135. get {
  136. return false;
  137. }
  138. }
  139. public bool IsReadOnly
  140. {
  141. get {
  142. return false;
  143. }
  144. }
  145. // Indexer
  146. public object this[object key]
  147. {
  148. get {
  149. ListEntry entry = FindEntry(key);
  150. return entry == null ? entry : entry.value;
  151. }
  152. set {
  153. ListEntry entry = FindEntry(key);
  154. if (entry != null)
  155. entry.value = value;
  156. else
  157. AddImpl(key, value);
  158. }
  159. }
  160. public ICollection Keys
  161. {
  162. get {
  163. return new ListEntryCollection(this, true);
  164. }
  165. }
  166. public ICollection Values
  167. {
  168. get {
  169. return new ListEntryCollection(this, false);
  170. }
  171. }
  172. public void Add(object key, object value)
  173. {
  174. AddImpl(key, value);
  175. }
  176. public void Clear()
  177. {
  178. root = null;
  179. count = 0;
  180. modCount++;
  181. }
  182. public bool Contains(object key)
  183. {
  184. return FindEntry(key) != null ? true : false;
  185. }
  186. public IDictionaryEnumerator GetEnumerator()
  187. {
  188. return new ListEntryEnumerator(this);
  189. }
  190. public void Remove(object key)
  191. {
  192. if (key == null)
  193. throw new ArgumentNullException(
  194. "key",
  195. "Key cannot be null.");
  196. ListEntry entry = root;
  197. for (ListEntry prev = null; entry != null; prev = entry, entry = entry.next) {
  198. if (AreEqual(key, entry.key)) {
  199. if (prev != null) {
  200. prev.next = entry.next;
  201. } else {
  202. root = entry.next;
  203. }
  204. entry.value = null;
  205. count--;
  206. modCount++;
  207. }
  208. }
  209. }
  210. [Serializable]
  211. private class ListEntry
  212. {
  213. public object key = null;
  214. public object value = null;
  215. public ListEntry next = null;
  216. }
  217. private class ListEntryEnumerator : IEnumerator, IDictionaryEnumerator
  218. {
  219. private ListDictionary dict;
  220. private bool isAtStart;
  221. private ListEntry current;
  222. private int version;
  223. public ListEntryEnumerator(ListDictionary dict)
  224. {
  225. this.dict = dict;
  226. version = dict.modCount;
  227. Reset();
  228. }
  229. private void FailFast()
  230. {
  231. if (version != dict.modCount) {
  232. throw new InvalidOperationException(
  233. "The ListDictionary's contents changed after this enumerator was instantiated.");
  234. }
  235. }
  236. public bool MoveNext()
  237. {
  238. FailFast();
  239. if (isAtStart) {
  240. current = dict.root;
  241. isAtStart = false;
  242. } else {
  243. current = current.next;
  244. }
  245. return current != null ? true : false;
  246. }
  247. public void Reset()
  248. {
  249. FailFast();
  250. isAtStart = true;
  251. current = null;
  252. }
  253. public object Current
  254. {
  255. get {
  256. FailFast();
  257. if (isAtStart || current == null) {
  258. throw new InvalidOperationException(
  259. "Enumerator is positioned before the collection's first element or after the last element.");
  260. }
  261. return new DictionaryEntry(current.key, current.value);
  262. }
  263. }
  264. // IDictionaryEnumerator
  265. public DictionaryEntry Entry
  266. {
  267. get {
  268. FailFast();
  269. return (DictionaryEntry) Current;
  270. }
  271. }
  272. public object Key
  273. {
  274. get {
  275. FailFast();
  276. if (isAtStart || current == null) {
  277. throw new InvalidOperationException(
  278. "Enumerator is positioned before the collection's first element or after the last element.");
  279. }
  280. return current.key;
  281. }
  282. }
  283. public object Value
  284. {
  285. get {
  286. FailFast();
  287. if (isAtStart || current == null) {
  288. throw new InvalidOperationException(
  289. "Enumerator is positioned before the collection's first element or after the last element.");
  290. }
  291. return current.value;
  292. }
  293. }
  294. }
  295. private class ListEntryCollection : ICollection
  296. {
  297. private ListDictionary dict;
  298. private bool isKeyList;
  299. public ListEntryCollection(ListDictionary dict, bool isKeyList)
  300. {
  301. this.dict = dict;
  302. this.isKeyList = isKeyList;
  303. }
  304. // ICollection Interface
  305. public int Count {
  306. get {
  307. return dict.Count;
  308. }
  309. }
  310. public bool IsSynchronized
  311. {
  312. get {
  313. return false;
  314. }
  315. }
  316. public object SyncRoot
  317. {
  318. get {
  319. return dict.SyncRoot;
  320. }
  321. }
  322. public void CopyTo(Array array, int index)
  323. {
  324. int i = index;
  325. foreach ( object obj in this )
  326. array.SetValue( obj, i++ );
  327. }
  328. // IEnumerable Interface
  329. public IEnumerator GetEnumerator()
  330. {
  331. return new ListEntryCollectionEnumerator(dict, isKeyList);
  332. }
  333. private class ListEntryCollectionEnumerator : IEnumerator
  334. {
  335. private ListDictionary dict;
  336. private bool isKeyList;
  337. private bool isAtStart;
  338. private int version;
  339. private ListEntry current;
  340. public ListEntryCollectionEnumerator(ListDictionary dict, bool isKeyList)
  341. {
  342. this.dict = dict;
  343. this.isKeyList = isKeyList;
  344. isAtStart = true;
  345. version = dict.modCount;
  346. }
  347. private void FailFast()
  348. {
  349. if (version != dict.modCount) {
  350. throw new InvalidOperationException(
  351. "The Collection's contents changed after this " +
  352. "enumerator was instantiated.");
  353. }
  354. }
  355. public object Current
  356. {
  357. get {
  358. FailFast();
  359. if (isAtStart || current == null) {
  360. throw new InvalidOperationException(
  361. "Enumerator is positioned before the collection's " +
  362. "first element or after the last element.");
  363. }
  364. return isKeyList ? current.key : current.value;
  365. }
  366. }
  367. public bool MoveNext()
  368. {
  369. FailFast();
  370. if (isAtStart) {
  371. current = dict.root;
  372. isAtStart = false;
  373. } else {
  374. current = current.next;
  375. }
  376. return current != null ? true : false;
  377. }
  378. public void Reset()
  379. {
  380. FailFast();
  381. isAtStart = true;
  382. current = null;
  383. }
  384. }
  385. }
  386. }
  387. }