Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2623


Ignore:
Timestamp:
01/13/10 03:32:32 (15 years ago)
Author:
swagner
Message:

Worked on HeuristicLab.Collections (#819)

  • added ObservableSet
  • restructured interfaces again
Location:
trunk/sources/HeuristicLab.Collections/3.3
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Collections/3.3/HeuristicLab.Collections-3.3.csproj

    r2618 r2623  
    4949  </ItemGroup>
    5050  <ItemGroup>
     51    <Compile Include="ReadOnlyObservableSet.cs" />
     52    <Compile Include="IObservableSet.cs" />
     53    <Compile Include="ObservableSet.cs">
     54      <SubType>Code</SubType>
     55    </Compile>
    5156    <Compile Include="ReadOnlyObservableKeyedCollection.cs">
    5257      <SubType>Code</SubType>
  • trunk/sources/HeuristicLab.Collections/3.3/IObservableDictionary.cs

    r2620 r2623  
    2727
    2828namespace HeuristicLab.Collections {
    29   public interface IObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyPropertyChanged {
    30     event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
    31     event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
     29  public interface IObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IObservableCollection<KeyValuePair<TKey, TValue>> {
    3230    event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
    33     event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
    3431  }
    3532}
  • trunk/sources/HeuristicLab.Collections/3.3/IObservableKeyedCollection.cs

    r2620 r2623  
    2727
    2828namespace HeuristicLab.Collections {
    29   public interface IObservableKeyedCollection<TKey, TItem> : ICollection<TItem>, INotifyPropertyChanged {
     29  public interface IObservableKeyedCollection<TKey, TItem> : IObservableCollection<TItem> {
    3030    TItem this[TKey key] { get; }
    3131
     
    3333    bool TryGetValue(TKey key, out TItem item);
    3434
    35     event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
    36     event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
     35    bool Remove(TKey key);
     36
    3737    event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
    38     event CollectionItemsChangedEventHandler<TItem> CollectionReset;
    3938  }
    4039}
  • trunk/sources/HeuristicLab.Collections/3.3/IObservableList.cs

    r2620 r2623  
    2727
    2828namespace HeuristicLab.Collections {
    29   public interface IObservableList<T> : IList<T>, INotifyPropertyChanged {
    30     event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
    31     event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
     29  public interface IObservableList<T> : IList<T>, IObservableCollection<T> {
     30    new event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
     31    new event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
    3232    event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
    3333    event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
    34     event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
     34    new event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
    3535  }
    3636}
  • trunk/sources/HeuristicLab.Collections/3.3/IndexedItem.cs

    r2572 r2623  
    4444    public override string ToString() {
    4545      StringBuilder sb = new StringBuilder();
    46       sb.Append("[").Append(index).Append(", ").Append(value != null ? value.ToString() : "null").Append("]");
     46      sb.Append("[").Append(index).Append(", ");
     47      if (value != null) sb.Append(value.ToString());
     48      sb.Append("]");
    4749      return sb.ToString();
    4850    }
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableDictionary.cs

    r2620 r2623  
    173173
    174174    #region Enumeration
    175     public Dictionary<TKey, TValue>.Enumerator GetEnumerator() {
     175    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
    176176      return dict.GetEnumerator();
    177177    }
    178     IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() {
    179       return ((IEnumerable<KeyValuePair<TKey, TValue>>)dict).GetEnumerator();
    180     }
    181178    IEnumerator IEnumerable.GetEnumerator() {
    182       return ((IEnumerable)dict).GetEnumerator();
     179      return dict.GetEnumerator();
    183180    }
    184181    #endregion
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableList.cs

    r2620 r2623  
    7777      list = new List<T>(collection);
    7878      OnItemsAdded(GetIndexedItems());
     79      OnItemsAdded(collection);
    7980    }
    8081    #endregion
     
    163164      OnPropertyChanged("Count");
    164165      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(list.Count - 1, item) });
     166      OnItemsAdded(new T[] { item });
    165167    }
    166168    public void AddRange(IEnumerable<T> collection) {
     
    179181        OnPropertyChanged("Count");
    180182        OnItemsAdded(items);
     183        OnItemsAdded(collection);
    181184      }
    182185    }
     
    190193      OnPropertyChanged("Count");
    191194      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
     195      OnItemsAdded(new T[] { item });
    192196    }
    193197    public void InsertRange(int index, IEnumerable<T> collection) {
     
    205209        OnPropertyChanged("Count");
    206210        OnItemsAdded(items);
     211        OnItemsAdded(collection);
    207212      }
    208213    }
     
    215220        OnPropertyChanged("Count");
    216221        OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
     222        OnItemsRemoved(new T[] { item });
    217223        return true;
    218224      }
     
    221227    public int RemoveAll(Predicate<T> match) {
    222228      if (match == null) throw new ArgumentNullException();
    223       List<IndexedItem<T>> items = new List<IndexedItem<T>>();
     229      List<IndexedItem<T>> indexedItems = new List<IndexedItem<T>>();
     230      List<T> items = new List<T>();
    224231      for (int i = 0; i < list.Count; i++) {
    225         if (match(list[i]))
    226           items.Add(new IndexedItem<T>(i, list[i]));
     232        if (match(list[i])) {
     233          indexedItems.Add(new IndexedItem<T>(i, list[i]));
     234          items.Add(list[i]);
     235        }
    227236      }
    228237      int result = 0;
    229       if (items.Count > 0) {
     238      if (indexedItems.Count > 0) {
    230239        result = list.RemoveAll(match);
    231240        OnPropertyChanged("Item[]");
    232241        OnPropertyChanged("Count");
     242        OnItemsRemoved(indexedItems);
    233243        OnItemsRemoved(items);
    234244      }
     
    241251      OnPropertyChanged("Count");
    242252      OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
     253      OnItemsRemoved(new T[] { item });
    243254    }
    244255    public void RemoveRange(int index, int count) {
    245256      if (count > 0) {
    246         IndexedItem<T>[] items = GetIndexedItems(index, count);
     257        IndexedItem<T>[] indexedItems = GetIndexedItems(index, count);
     258        T[] items = new T[count];
     259        list.CopyTo(index, items, 0, count);
    247260        list.RemoveRange(index, count);
    248261        OnPropertyChanged("Item[]");
    249262        OnPropertyChanged("Count");
     263        OnItemsRemoved(indexedItems);
    250264        OnItemsRemoved(items);
    251265      }
     
    254268    public void Clear() {
    255269      if (list.Count > 0) {
    256         IndexedItem<T>[] items = GetIndexedItems();
     270        IndexedItem<T>[] indexedItems = GetIndexedItems();
     271        T[] items = list.ToArray();
    257272        list.Clear();
    258273        OnPropertyChanged("Item[]");
    259274        OnPropertyChanged("Count");
    260         OnCollectionReset(new IndexedItem<T>[0], items);
     275        OnCollectionReset(new IndexedItem<T>[0], indexedItems);
     276        OnCollectionReset(new T[0], items);
    261277      }
    262278    }
     
    320336      return list.ToArray();
    321337    }
    322     void ICollection<T>.CopyTo(T[] array, int arrayIndex) {
     338    public void CopyTo(T[] array) {
     339      list.CopyTo(array);
     340    }
     341    public void CopyTo(T[] array, int arrayIndex) {
    323342      list.CopyTo(array, arrayIndex);
     343    }
     344    public void CopyTo(int index, T[] array, int arrayIndex, int count) {
     345      list.CopyTo(index, array, arrayIndex, count);
    324346    }
    325347    public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) {
     
    338360
    339361    #region Enumeration
    340     public List<T>.Enumerator GetEnumerator() {
     362    public IEnumerator<T> GetEnumerator() {
    341363      return list.GetEnumerator();
    342364    }
    343     IEnumerator<T> IEnumerable<T>.GetEnumerator() {
    344       return ((IEnumerable<T>)list).GetEnumerator();
    345     }
    346365    IEnumerator IEnumerable.GetEnumerator() {
    347       return ((IEnumerable)list).GetEnumerator();
     366      return list.GetEnumerator();
    348367    }
    349368    #endregion
     
    367386
    368387    [field: NonSerialized]
     388    private event CollectionItemsChangedEventHandler<T> itemsAdded;
     389    event CollectionItemsChangedEventHandler<T> IObservableCollection<T>.ItemsAdded {
     390      add { itemsAdded += value; }
     391      remove { itemsAdded -= value; }
     392    }
     393    private void OnItemsAdded(IEnumerable<T> items) {
     394      if (itemsAdded != null)
     395        itemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
     396    }
     397
     398    [field: NonSerialized]
    369399    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
    370400    protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
     
    374404
    375405    [field: NonSerialized]
     406    private event CollectionItemsChangedEventHandler<T> itemsRemoved;
     407    event CollectionItemsChangedEventHandler<T> IObservableCollection<T>.ItemsRemoved {
     408      add { itemsRemoved += value; }
     409      remove { itemsRemoved -= value; }
     410    }
     411    private void OnItemsRemoved(IEnumerable<T> items) {
     412      if (itemsRemoved != null)
     413        itemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
     414    }
     415
     416    [field: NonSerialized]
    376417    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
    377418    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
     
    392433      if (CollectionReset != null)
    393434        CollectionReset(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
     435    }
     436
     437    [field: NonSerialized]
     438    private event CollectionItemsChangedEventHandler<T> collectionReset;
     439    event CollectionItemsChangedEventHandler<T> IObservableCollection<T>.CollectionReset {
     440      add { collectionReset += value; }
     441      remove { collectionReset -= value; }
     442    }
     443    private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
     444      if (collectionReset != null)
     445        collectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
    394446    }
    395447
  • trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedCollection.cs

    r2620 r2623  
    8080    }
    8181
     82    bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
     83      throw new NotSupportedException();
     84    }
    8285    bool ICollection<TItem>.Remove(TItem item) {
    8386      throw new NotSupportedException();
  • trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableList.cs

    r2620 r2623  
    5757      this.list = list;
    5858      list.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsAdded);
     59      ((IObservableCollection<T>)list).ItemsAdded += new CollectionItemsChangedEventHandler<T>(list_ItemsAdded);
    5960      list.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsRemoved);
     61      ((IObservableCollection<T>)list).ItemsRemoved += new CollectionItemsChangedEventHandler<T>(list_ItemsRemoved);
    6062      list.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsReplaced);
    6163      list.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsMoved);
    6264      list.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_CollectionReset);
     65      ((IObservableCollection<T>)list).CollectionReset += new CollectionItemsChangedEventHandler<T>(list_CollectionReset);
    6366      list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
    6467    }
     
    97100
    98101    #region Conversion
    99     void ICollection<T>.CopyTo(T[] array, int arrayIndex) {
     102    public void CopyTo(T[] array, int arrayIndex) {
    100103      list.CopyTo(array, arrayIndex);
    101104    }
     
    120123
    121124    [field: NonSerialized]
     125    private event CollectionItemsChangedEventHandler<T> itemsAdded;
     126    event CollectionItemsChangedEventHandler<T> IObservableCollection<T>.ItemsAdded {
     127      add { itemsAdded += value; }
     128      remove { itemsAdded -= value; }
     129    }
     130    private void OnItemsAdded(IEnumerable<T> items) {
     131      if (itemsAdded != null)
     132        itemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
     133    }
     134
     135    [field: NonSerialized]
    122136    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
    123137    protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
     
    127141
    128142    [field: NonSerialized]
     143    private event CollectionItemsChangedEventHandler<T> itemsRemoved;
     144    event CollectionItemsChangedEventHandler<T> IObservableCollection<T>.ItemsRemoved {
     145      add { itemsRemoved += value; }
     146      remove { itemsRemoved -= value; }
     147    }
     148    private void OnItemsRemoved(IEnumerable<T> items) {
     149      if (itemsRemoved != null)
     150        itemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
     151    }
     152
     153    [field: NonSerialized]
    129154    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
    130155    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
     
    148173
    149174    [field: NonSerialized]
     175    private event CollectionItemsChangedEventHandler<T> collectionReset;
     176    event CollectionItemsChangedEventHandler<T> IObservableCollection<T>.CollectionReset {
     177      add { collectionReset += value; }
     178      remove { collectionReset -= value; }
     179    }
     180    private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
     181      if (collectionReset != null)
     182        collectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
     183    }
     184
     185    [field: NonSerialized]
    150186    public event PropertyChangedEventHandler PropertyChanged;
    151187    protected virtual void OnPropertyChanged(string propertyName) {
     
    157193      OnItemsAdded(e.Items);
    158194    }
     195    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
     196      OnItemsAdded(e.Items);
     197    }
    159198    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    160199      OnItemsRemoved(e.Items);
    161200    }
     201    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
     202      OnItemsRemoved(e.Items);
     203    }
    162204    private void list_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    163205      OnItemsReplaced(e.Items, e.OldItems);
     
    167209    }
    168210    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     211      OnCollectionReset(e.Items, e.OldItems);
     212    }
     213    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
    169214      OnCollectionReset(e.Items, e.OldItems);
    170215    }
Note: See TracChangeset for help on using the changeset viewer.