| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- namespace System.Collections.Specialized
- {
- public class ListDictionary : IDictionary, ICollection, IEnumerable
- {
- private int count;
- private int modCount;
- private ListEntry root;
- private IComparer comparer;
-
-
- public ListDictionary()
- {
- count = 0;
- modCount = 0;
- comparer = null;
- root = null;
- }
-
- public ListDictionary(IComparer comparer) : this()
- {
- this.comparer = comparer;
- }
-
- private bool AreEqual(object obj1, object obj2)
- {
- if (comparer != null) {
- if (comparer.Compare(obj1, obj2) == 0) {
- return true;
- }
- } else {
- if (obj1.Equals(obj2)) {
- return true;
- }
- }
-
- return false;
- }
-
- private ListEntry FindEntry(object key)
- {
- if (key == null) {
- throw new ArgumentNullException("Attempted lookup for a null key.");
- }
-
- if (root == null) {
- return null;
- } else {
- ListEntry entry = root;
-
- while (entry != null) {
- if (AreEqual(key, entry.key)) {
- return entry;
- }
-
- entry = entry.next;
- }
- }
-
- return null;
- }
- private void AddImpl(object key, object value)
- {
- if (key == null) {
- throw new ArgumentNullException("Attempted add with a null key.");
- }
-
- if (root == null) {
- root = new ListEntry();
- root.key = key;
- root.value = value;
- } else {
- ListEntry entry = root;
-
- while (entry != null) {
- if (AreEqual(key, entry.key)) {
- throw new ArgumentException("Duplicate key in add.");
- }
-
- if (entry.next == null) {
- break;
- }
-
- entry = entry.next;
- }
-
- entry.next = new ListEntry();
- entry.next.key = key;
- entry.next.value = value;
- }
-
- count++;
- modCount++;
- }
-
- // IEnumerable Interface
- IEnumerator IEnumerable.GetEnumerator()
- {
- return new ListEntryEnumerator(this);
- }
-
- // ICollection Interface
- public int Count
- {
- get {
- return count;
- }
- }
-
- public bool IsSynchronized
- {
- get {
- return false;
- }
- }
-
- public object SyncRoot
- {
- get {
- return this;
- }
- }
-
- public void CopyTo(Array array, int index)
- {
- // FIXME
- }
-
- // IDictionary Interface
- public bool IsFixedSize
- {
- get {
- return false;
- }
- }
-
- public bool IsReadOnly
- {
- get {
- return false;
- }
- }
-
- // Indexer
- public object this[object key]
- {
- get {
- ListEntry entry = FindEntry(key);
- return entry == null ? entry : entry.value;
- }
-
- set {
- ListEntry entry = FindEntry(key);
-
- if (entry != null) {
- entry.value = value;
- count++;
- modCount++;
- } else {
- AddImpl(key, value);
- }
- }
- }
-
- public ICollection Keys
- {
- get {
- return new ListEntryCollection(this, true);
- }
- }
-
- public ICollection Values
- {
- get {
- return new ListEntryCollection(this, false);
- }
- }
-
- public void Add(object key, object value)
- {
- AddImpl(key, value);
- }
-
- public void Clear()
- {
- root = null;
- count = 0;
- modCount++;
- }
-
- public bool Contains(object key)
- {
- return FindEntry(key) != null ? true : false;
- }
-
- IDictionaryEnumerator IDictionary.GetEnumerator()
- {
- return new ListEntryEnumerator(this);
- }
-
- public void Remove(object key)
- {
- ListEntry entry = root;
-
- for (ListEntry prev = null; entry != null; prev = entry, entry = entry.next) {
- if (AreEqual(key, entry.key)) {
- if (prev != null) {
- prev.next = entry.next;
- } else {
- root = entry.next;
- }
-
- entry.value = null;
- count--;
- modCount++;
- }
- }
- }
-
- private class ListEntry
- {
- public object key = null;
- public object value = null;
- public ListEntry next = null;
- }
- private class ListEntryEnumerator : IEnumerator, IDictionaryEnumerator
- {
- private ListDictionary dict;
- private bool isAtStart;
- private ListEntry current;
- private int version;
-
- public ListEntryEnumerator(ListDictionary dict)
- {
- this.dict = dict;
- version = dict.modCount;
- Reset();
- }
- private void FailFast()
- {
- if (version != dict.modCount) {
- throw new InvalidOperationException(
- "The ListDictionary's contents changed after this enumerator was instantiated.");
- }
- }
-
- public bool MoveNext()
- {
- FailFast();
-
- if (isAtStart) {
- current = dict.root;
- isAtStart = false;
- } else {
- current = current.next;
- }
-
- return current != null ? true : false;
- }
-
- public void Reset()
- {
- FailFast();
- isAtStart = true;
- current = null;
- }
-
- public object Current
- {
- get {
- FailFast();
-
- if (isAtStart || current == null) {
- throw new InvalidOperationException(
- "Enumerator is positioned before the collection's first element or after the last element.");
- }
-
- return new DictionaryEntry(current.key, current.value);
- }
- }
-
- // IDictionaryEnumerator
- public DictionaryEntry Entry
- {
- get {
- FailFast();
- return (DictionaryEntry) Current;
- }
- }
-
- public object Key
- {
- get {
- FailFast();
-
- if (isAtStart || current == null) {
- throw new InvalidOperationException(
- "Enumerator is positioned before the collection's first element or after the last element.");
- }
-
- return current.key;
- }
- }
-
- public object Value
- {
- get {
- FailFast();
-
- if (isAtStart || current == null) {
- throw new InvalidOperationException(
- "Enumerator is positioned before the collection's first element or after the last element.");
- }
-
- return current.value;
- }
- }
- }
-
- private class ListEntryCollection : ICollection
- {
- private ListDictionary dict;
- private bool isKeyList;
-
- public ListEntryCollection(ListDictionary dict, bool isKeyList)
- {
- this.dict = dict;
- this.isKeyList = isKeyList;
- }
-
- // ICollection Interface
- public int Count
- {
- get {
- return dict.Count;
- }
- }
-
- public bool IsSynchronized
- {
- get {
- return false;
- }
- }
-
- public object SyncRoot
- {
- get {
- return dict.SyncRoot;
- }
- }
-
- public void CopyTo(Array array, int index)
- {
- // FIXME
- }
-
- // IEnumerable Interface
- public IEnumerator GetEnumerator()
- {
- return new ListEntryCollectionEnumerator(dict, isKeyList);
- }
-
- private class ListEntryCollectionEnumerator : IEnumerator
- {
- private ListDictionary dict;
- private bool isKeyList;
- private bool isAtStart;
- private int version;
- private ListEntry current;
-
- public ListEntryCollectionEnumerator(ListDictionary dict, bool isKeyList)
- {
- this.dict = dict;
- this.isKeyList = isKeyList;
- isAtStart = true;
- version = dict.modCount;
- }
- private void FailFast()
- {
- if (version != dict.modCount) {
- throw new InvalidOperationException(
- "The Collection's contents changed after this " +
- "enumerator was instantiated.");
- }
- }
-
- public object Current
- {
- get {
- FailFast();
-
- if (isAtStart || current == null) {
- throw new InvalidOperationException(
- "Enumerator is positioned before the collection's " +
- "first element or after the last element.");
- }
-
- return isKeyList ? current.key : current.value;
- }
- }
-
- public bool MoveNext()
- {
- FailFast();
-
- if (isAtStart) {
- current = dict.root;
- isAtStart = false;
- } else {
- current = current.next;
- }
-
- return current != null ? true : false;
- }
-
- public void Reset()
- {
- FailFast();
- isAtStart = true;
- current = null;
- }
- }
- }
- }
- }
|