Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextReaderBranch/HeuristicLab.Data/ItemList_T.cs @ 122

Last change on this file since 122 was 122, checked in by gkronber, 16 years ago

bug fixes to make loading of OSGA-TSP work. Some non-working code remains

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Data {
30  public class ItemList<T> : ItemBase, IList<T> where T : IItem {
31    private List<T> list;
32
33    public ItemList() {
34      list = new List<T>();
35    }
36
37    public override IView CreateView() {
38      return new ItemListView<T>(this);
39    }
40
41    public override object Clone(IDictionary<Guid, object> clonedObjects) {
42      ItemList<T> clone = new ItemList<T>();
43      clonedObjects.Add(Guid, clone);
44      CloneElements(clone, clonedObjects);
45      return clone;
46    }
47    protected void CloneElements(ItemList<T> destination, IDictionary<Guid, object> clonedObjects) {
48      for (int i = 0; i < list.Count; i++)
49        destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
50    }
51
52    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
53      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
54      for (int i = 0; i < list.Count; i++)
55        node.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
56      return node;
57    }
58    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
59    //  base.Populate(node, restoredObjects);
60    //  for(int i = 0; i < node.ChildNodes.Count; i++)
61    //    list.Add((T)PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
62    //}
63    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
64      base.Populate(reader, restoredObjects);
65      reader.Read();
66      while(reader.IsStartElement()) {
67        list.Add((T)PersistenceManager.Restore(reader, restoredObjects));
68        reader.Skip();
69      }
70      reader.ReadEndElement();
71    }
72
73    public override string ToString() {
74      if (list.Count > 0) {
75        StringBuilder builder = new StringBuilder();
76        builder.Append(list[0].ToString());
77        for (int i = 1; i < list.Count; i++) {
78          builder.Append(";");
79          builder.Append(list[i].ToString());
80        }
81        return builder.ToString();
82      } else {
83        return "Empty List";
84      }
85    }
86
87    #region IList<T> Members
88    public int IndexOf(T item) {
89      return list.IndexOf(item);
90    }
91    public void Insert(int index, T item) {
92      list.Insert(index, item);
93      OnItemAdded(item, index);
94    }
95    public void RemoveAt(int index) {
96      IItem item = list[index];
97      list.RemoveAt(index);
98      OnItemRemoved(item, index);
99    }
100    public T this[int index] {
101      get { return list[index]; }
102      set { list[index] = value; }
103    }
104    #endregion
105
106    #region ICollection<T> Members
107    public void Add(T item) {
108      list.Add(item);
109      OnItemAdded(item, list.Count - 1);
110    }
111    public void Clear() {
112      list.Clear();
113      OnCleared();
114    }
115    public bool Contains(T item) {
116      return list.Contains(item);
117    }
118    public void CopyTo(T[] array, int arrayIndex) {
119      list.CopyTo(array, arrayIndex);
120    }
121    public int Count {
122      get { return list.Count; }
123    }
124    public bool IsReadOnly {
125      get { return false; }
126    }
127    public bool Remove(T item) {
128      int index = list.IndexOf(item);
129      if (list.Remove(item)) {
130        OnItemRemoved(item, index);
131        return true;
132      } else {
133        return false;
134      }
135    }
136    #endregion
137
138    #region IEnumerable<T> Members
139    public IEnumerator<T> GetEnumerator() {
140      return list.GetEnumerator();
141    }
142    #endregion
143
144    #region IEnumerable Members
145    IEnumerator IEnumerable.GetEnumerator() {
146      return list.GetEnumerator();
147    }
148    #endregion
149
150    #region List<T> Methods
151    public int LastIndexOf(T item) {
152      return list.LastIndexOf(item);
153    }
154
155    public int LastIndexOf(T item, int index) {
156      return list.LastIndexOf(item, index);
157    }
158
159    public int LastIndexOf(T item, int index, int count) {
160      return list.LastIndexOf(item, index, count);
161    }
162
163    public int IndexOf(T item, int index) {
164      return list.IndexOf(item, index);
165    }
166
167    public int IndexOf(T item, int index, int count) {
168      return list.IndexOf(item, index, count);
169    }
170
171    public void AddRange(IEnumerable<T> collection) {
172      foreach (T obj in collection) {
173        this.Add(obj);
174      }
175    }
176
177    public bool Exists(Predicate<T> match) {
178      return list.Exists(match);
179    }
180
181    public int BinarySearch(T item) {
182      return list.BinarySearch(item);
183    }
184
185    public int BinarySearch(T item, IComparer<T> comparer) {
186      return list.BinarySearch(item, comparer);
187    }
188
189    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
190      return list.BinarySearch(index, count, item, comparer);
191    }
192
193    public T Find(Predicate<T> match) {
194      return list.Find(match);
195    }
196
197    public List<T> FindAll(Predicate<T> match) {
198      return list.FindAll(match);
199    }
200
201    public int FindIndex(Predicate<T> match) {
202      return list.FindIndex(match);
203    }
204
205    public T FindLast(Predicate<T> match) {
206      return list.FindLast(match);
207    }
208
209    public int FindLastIndex(Predicate<T> match) {
210      return list.FindLastIndex(match);
211    }
212
213    public void Sort() {
214      list.Sort();
215    }
216
217    public void Sort(IComparer<T> comparer) {
218      list.Sort(comparer);
219    }
220
221    public void Sort(Comparison<T> comparison) {
222      list.Sort(comparison);
223    }
224
225    public void Reverse() {
226      list.Reverse();
227    }
228
229    public ItemList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) where TOutput : IItem {
230      ItemList<TOutput> targetList = new ItemList<TOutput>();
231      foreach (T item in list) {
232        targetList.Add(converter.Invoke(item));
233      }
234      return targetList;
235    }
236
237    public bool TrueForAll(Predicate<T> match) {
238      return list.TrueForAll(match);
239    }
240
241    #endregion
242
243    public event EventHandler<ItemIndexEventArgs> ItemAdded;
244    protected virtual void OnItemAdded(IItem item, int index) {
245      if (ItemAdded != null)
246        ItemAdded(this, new ItemIndexEventArgs(item, index));
247      OnChanged();
248    }
249    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
250    protected virtual void OnItemRemoved(IItem item, int index) {
251      if (ItemRemoved != null)
252        ItemRemoved(this, new ItemIndexEventArgs(item, index));
253      OnChanged();
254    }
255    public event EventHandler Cleared;
256    protected virtual void OnCleared() {
257      if (Cleared != null)
258        Cleared(this, new EventArgs());
259      OnChanged();
260    }
261  }
262}
Note: See TracBrowser for help on using the repository browser.