java泛型:C#泛型秘诀(5.3)

    Nested Types#region Nested Types
  Enumerator K, V#region Enumerator <K, V>
  [Serializable, StructLayout(LayoutKind.Sequential)]
  private struct Enumerator<K, V> : IEnumerator<KeyValuePair<K, V>>, IDisposable,
    IDictionaryEnumerator, IEnumerator
  {
    private ReversibleSortedList<K, V> _ReversibleSortedList;
    private K key;
    private V value;
    private index;
    private version;
    ernal Enumerator(ReversibleSortedList<K, V> ReversibleSortedList)
    {
      this._ReversibleSortedList = ReversibleSortedList;
      this.index = 0;
      this.version = this._ReversibleSortedList.version;
      this.key = default(K);
      this.value = default(V);
    }
    public void Dispose
    {
      this.index = 0;
      this.key = default(K);
      this.value = default(V);
    }
    object IDictionaryEnumerator.Key
    {
      get
      {
         ((this.index 0) ||
          (this.index (this._ReversibleSortedList.Count + 1)))
        {
          throw InvalidOperationException(
              "Enumeration operation cannot occur.");
        }
         this.key;
      }
    }
    public bool MoveNext
    {
       (this.version != this._ReversibleSortedList.version)
      {
        throw InvalidOperationException(
            "Enumeration failed version check");
      }
       (this.index < this._ReversibleSortedList.Count)
      {
        this.key = this._ReversibleSortedList.keys[this.index];
        this.value = this._ReversibleSortedList.values[this.index];
        this.index;
         true;
      }
      this.index = this._ReversibleSortedList.Count + 1;
      this.key = default(K);
      this.value = default(V);
       false;
    }
    DictionaryEntry IDictionaryEnumerator.Entry
    {
      get
      {
         ((this.index 0) ||
          (this.index (this._ReversibleSortedList.Count + 1)))
        {
          throw InvalidOperationException(
              "Enumeration operation cannot happen.");
        }
         DictionaryEntry(this.key, this.value);
      }
    }
    public KeyValuePair<K, V> Current
    {
      get
      {
         KeyValuePair<K, V>(this.key, this.value);
      }
    }
    object IEnumerator.Current
    {
      get
      {
         ((this.index 0) ||
          (this.index (this._ReversibleSortedList.Count + 1)))
        {
          throw InvalidOperationException(
              "Enumeration operation cannot occur");
        }
         DictionaryEntry(this.key, this.value);
      }
    }
    object IDictionaryEnumerator.Value
    {
      get
      {
         ((this.index 0) ||
          (this.index (this._ReversibleSortedList.Count + 1)))
        {
          throw InvalidOperationException(
              "Enumeration operation cannot occur");
        }
         this.value;
      }
    }
    void IEnumerator.Re
    {
       (this.version != this._ReversibleSortedList.version)
      {
        throw InvalidOperationException(
            "Enumeration version check failed");
      }
      this.index = 0;
      this.key = default(K);
      this.value = default(V);
    }
  }
  #endregion // Enumerator <K, V>
  KeyListK,V#region KeyList<K,V>
  [Serializable]
  private sealed KeyList<K, V> : IList<K>, ICollection<K>,
                        IEnumerable<K>, ICollection, IEnumerable
  {
    // Methods
    ernal KeyList(ReversibleSortedList<K, V> dictionary)
    {
      this._dict = dictionary;
    }
    public void Add(K key)
    {
      throw NotSupportedException("Add is unsupported");
    }
    public void Clear
    {
      throw NotSupportedException("Clear is unsupported");
    }
    public bool Contains(K key)
    {
       this._dict.ContainsKey(key);
    }
    public void CopyTo(K .gif' />, .gif' />Index)
    {
      Array.Copy(this._dict.keys, 0, .gif' />, .gif' />Index, this._dict.Count);
    }
    public IEnumerator<K> GetEnumerator
    {
      
         ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
                           this._dict);
    }
    public IndexOf(K key)
    {
       (key null)
      {
        throw ArgumentNullException("key");
      }
       num1 = Array.BinarySearch<K>(this._dict.keys, 0,
                          this._dict.Count, key,
                          this._dict._sortDirectionComparer);
       (num1 >= 0)
      {
         num1;
      }
       -1;
    }
    public void Insert( index, K value)
    {
      throw NotSupportedException("Insert is unsupported");
    }
    public bool Remove(K key)
    {
      //throw NotSupportedException("Remove is unsupported");
       false;
    }
    public void RemoveAt( index)
    {
      throw NotSupportedException("RemoveAt is unsupported");
    }
    void ICollection.CopyTo(Array .gif' />, .gif' />Index)
    {
       ((.gif' /> != null) && (.gif' />.Rank != 1))
      {
        throw ArgumentException(
           "MultiDimensional .gif' />s are not unsupported");
      }
      try
      {
        Array.Copy(this._dict.keys, 0, .gif' />, .gif' />Index,
              this._dict.Count);
      }
      catch (ArrayTypeMismatchException atme)
      {
        throw ArgumentException("InvalidArrayType", atme);
      }
    }
    IEnumerator IEnumerable.GetEnumerator
    {
      
          ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
                                      this._dict);
    }
    // Properties
    public Count
    {
      get
      {
         this._dict._size;
      }
    }
    public bool IsReadOnly
    {
      get
      {
         true;
      }
    }
    public K this[ index]
    {
      get
      {
         this._dict.GetKey(index);
      }
      
      {
        throw NotSupportedException("Set is an unsupported operation");
      }
    }
    bool ICollection.IsSynchronized
    {
      get
      {
         false;
      }
    }
    object ICollection.SyncRoot
    {
      get
      {
         this._dict;
      }
    }
    // Fields
    private ReversibleSortedList<K, V> _dict;
  }
  #endregion // KeyList<K,V>
  ReversibleSortedListKeyEnumerator definition#region ReversibleSortedListKeyEnumerator definition
  [Serializable]
  private sealed ReversibleSortedListKeyEnumerator : IEnumerator<TKey>,
                               IDisposable,
                               IEnumerator
  {
    // Methods
    ernal ReversibleSortedListKeyEnumerator(
        ReversibleSortedList<TKey, TValue> ReversibleSortedList)
    {
      this._ReversibleSortedList = ReversibleSortedList;
      this.version = ReversibleSortedList.version;
    }
    public void Dispose
    {
      this.index = 0;
      this.currentKey = default(TKey);
    }
    public bool MoveNext
    {
       (this.version != this._ReversibleSortedList.version)
      {
        throw InvalidOperationException(
          "Enumeration failed version check");
      }
       (this.index < this._ReversibleSortedList.Count)
      {
        this.currentKey = this._ReversibleSortedList.keys[this.index];
        this.index;
         true;
      }
      this.index = this._ReversibleSortedList.Count + 1;
      this.currentKey = default(TKey);
       false;
    }
    void IEnumerator.Re
    {
       (this.version != this._ReversibleSortedList.version)
      {
        throw InvalidOperationException(
          "Enumeration failed version check");
      }
      this.index = 0;
      this.currentKey = default(TKey);
    }
    // Properties
    public TKey Current
    {
      get
      {
         this.currentKey;
      }
    }
    object IEnumerator.Current
    {
      get
      {
         ((this.index 0) || (this.index
            (this._ReversibleSortedList.Count + 1)))
        {
          throw InvalidOperationException(
              "Enumeration operation could not occur");
        }
         this.currentKey;
      }
    }
    // Fields
    private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
    private TKey currentKey;
    private index;
    private version;
  }
  #endregion //ReversibleSortedListKeyEnumerator definition
  ReversibleSortedListValueEnumerator definition#region ReversibleSortedListValueEnumerator definition
  [Serializable]
  private sealed ReversibleSortedListValueEnumerator : IEnumerator<TValue>,
                                IDisposable,
                                IEnumerator
  {
    // Methods
    ernal ReversibleSortedListValueEnumerator(
             ReversibleSortedList<TKey, TValue> ReversibleSortedList)
    {
      this._ReversibleSortedList = ReversibleSortedList;
      this.version = ReversibleSortedList.version;
    }
    public void Dispose
    {
      this.index = 0;
      this.currentValue = default(TValue);
    }
    public bool MoveNext
    {
       (this.version != this._ReversibleSortedList.version)
      {
        throw InvalidOperationException(
          "Enumeration failed version check");
      }
       (this.index < this._ReversibleSortedList.Count)
      {
        this.currentValue = this._ReversibleSortedList.values[this.index];
        this.index;
         true;
      }
      this.index = this._ReversibleSortedList.Count + 1;
      this.currentValue = default(TValue);
       false;
    }
    void IEnumerator.Re
    {
       (this.version != this._ReversibleSortedList.version)
      {
        throw InvalidOperationException(
          "Enumeration failed version check");
      }
      this.index = 0;
      this.currentValue = default(TValue);
    }
    // Properties
    public TValue Current
    {
      get
      {
         this.currentValue;
      }
    }
    object IEnumerator.Current
    {
      get
      {
         ((this.index 0) || (this.index
            (this._ReversibleSortedList.Count + 1)))
        {
          throw InvalidOperationException(
              "Enumeration operation could not occur");
        }
         this.currentValue;
      }
    }
    // Fields
    private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
    private TValue currentValue;
    private index;
    private version;
  }
  #endregion //ReversibleSortedListValueEnumerator
  ValueList K, V definition#region ValueList <K, V> definition
  [Serializable]
  private sealed ValueList<K, V> : IList<V>, ICollection<V>,
                         IEnumerable<V>, ICollection, IEnumerable
  {
    // Methods
    ernal ValueList(ReversibleSortedList<K, V> dictionary)
    {
      this._dict = dictionary;
    }
    public void Add(V key)
    {
      throw NotSupportedException("Add is not supported");
    }
    public void Clear
    {
      throw NotSupportedException("Clear is not supported");
    }
    public bool Contains(V value)
    {
       this._dict.ContainsValue(value);
    }
    public void CopyTo(V .gif' />, .gif' />Index)
    {
      Array.Copy(this._dict.values, 0, .gif' />, .gif' />Index, this._dict.Count);
    }
    public IEnumerator<V> GetEnumerator
    {
      
          ReversibleSortedList<K, V>.ReversibleSortedListValueEnumerator(
                                    this._dict);
    }
    public IndexOf(V value)
    {
       Array.IndexOf<V>(this._dict.values, value, 0, this._dict.Count);
    }
    public void Insert( index, V value)
    {
      throw NotSupportedException("Insert is not supported");
    }
    public bool Remove(V value)
    {
      //throw NotSupportedException("Remove is not supported");
       false;
    }
    public void RemoveAt( index)
    {
      throw NotSupportedException("RemoveAt is not supported");
    }
    void ICollection.CopyTo(Array .gif' />, .gif' />Index)
    {
       ((.gif' /> != null) && (.gif' />.Rank != 1))
      {
        throw ArgumentException(
          "MultiDimensional .gif' />s not supported");
      }
      try
      {
        Array.Copy(this._dict.values, 0, .gif' />, .gif' />Index,
              this._dict.Count);
      }
      catch (ArrayTypeMismatchException atme)
      {
        throw ArgumentException("Invalid .gif' /> type", atme);
      }
    }
    IEnumerator IEnumerable.GetEnumerator
    {
      
          ReversibleSortedList<K, V>.ReversibleSortedListValueEnumerator(
                                   this._dict);
    }
    // Properties
    public Count
    {
      get
      {
         this._dict._size;
      }
    }
    public bool IsReadOnly
    {
      get
      {
         true;
      }
    }
    public V this[ index]
    {
      get
      {
         this._dict.GetByIndex(index);
      }
      
      {
        throw NotSupportedException("Set by indexer is not supported");
      }
    }
    bool ICollection.IsSynchronized
    {
      get
      {
         false;
      }
    }
    object ICollection.SyncRoot
    {
      get
      {
         this._dict;
      }
    }
    // Fields
    private ReversibleSortedList<K, V> _dict;
  }
  #endregion // ValueList <TKey, TValue> definition
  #endregion // Nested types
}


  在SortedList混合使用了和列表语法这使得以任方式访问数据变得非常容易ReversibleSortedList<T>中数据也可以使用键/值对或索引来访问和SortedList它不允许重复键另外不管值是引用类型还是值类型都可以为null但键不行ReversibleSortedList<T>默认容量是16这和SortedList是里面项可以使用foreach循环进行迭代它返回KeyValuePair但这是只读迭代语法禁止在读取列表时进行元素更新或删除否则就是无效迭代器



Tags:  泛型编程与stl 泛型编程 .net泛型 java泛型

延伸阅读

最新评论

发表评论