C#创建安全的字典(Dictionary)存储结构

2019-12-30 15:02:13刘景俊

 

  4.GetEnumerator():返回循环访问 Dictionary<TKey, TValue> 的枚举器。


  public Enumerator GetEnumerator() {
      return new Enumerator(this, Enumerator.KeyValuePair); 
    }
 [Serializable] 
    public struct Enumerator: IEnumerator<KeyValuePair<TKey,TValue>>,
      IDictionaryEnumerator 
    { 
      private Dictionary<TKey,TValue> dictionary;
      private int version; 
      private int index;
      private KeyValuePair<TKey,TValue> current;
      private int getEnumeratorRetType; // What should Enumerator.Current return?
      internal const int DictEntry = 1;
      internal const int KeyValuePair = 2; 
      internal Enumerator(Dictionary<TKey,TValue> dictionary, int getEnumeratorRetType) {
        this.dictionary = dictionary; 
        version = dictionary.version;
        index = 0;
        this.getEnumeratorRetType = getEnumeratorRetType;
        current = new KeyValuePair<TKey, TValue>(); 
      }
      public bool MoveNext() { 
        if (version != dictionary.version) {
          ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); 
        }
        // Use unsigned comparison since we set index to dictionary.count+1 when the enumeration ends.
        // dictionary.count+1 could be negative if dictionary.count is Int32.MaxValue 
        while ((uint)index < (uint)dictionary.count) {
          if (dictionary.entries[index].hashCode >= 0) { 
            current = new KeyValuePair<TKey, TValue>(dictionary.entries[index].key, dictionary.entries[index].value); 
            index++;
            return true; 
          }
          index++;
        }
        index = dictionary.count + 1;
        current = new KeyValuePair<TKey, TValue>(); 
        return false; 
      }
      public KeyValuePair<TKey,TValue> Current {
        get { return current; }
      }
      public void Dispose() {
      } 
      object IEnumerator.Current {
        get { 
          if( index == 0 || (index == dictionary.count + 1)) {
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
          }
          if (getEnumeratorRetType == DictEntry) {
            return new System.Collections.DictionaryEntry(current.Key, current.Value); 
          } else { 
            return new KeyValuePair<TKey, TValue>(current.Key, current.Value);
          } 
        }
      }
      void IEnumerator.Reset() { 
        if (version != dictionary.version) {
          ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); 
        } 
        index = 0; 
        current = new KeyValuePair<TKey, TValue>();
      }
      DictionaryEntry IDictionaryEnumerator.Entry { 
        get {
          if( index == 0 || (index == dictionary.count + 1)) { 
             ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); 
          }
          return new DictionaryEntry(current.Key, current.Value);
        }
      }
      object IDictionaryEnumerator.Key {
        get { 
          if( index == 0 || (index == dictionary.count + 1)) { 
             ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
          } 
          return current.Key;
        }
      } 
      object IDictionaryEnumerator.Value { 
        get { 
          if( index == 0 || (index == dictionary.count + 1)) {
             ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); 
          }
          return current.Value;
        } 
      }
    }