Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataTable.cs @ 4047

Last change on this file since 4047 was 3826, checked in by mkommend, 15 years ago

fixed performance critical bug in DataTable (ticket #893)

File size: 8.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Data;
28using System;
29using System.Collections.Generic;
30using HeuristicLab.Collections;
31
32namespace HeuristicLab.Analysis {
33  /// <summary>
34  /// A table of data values.
35  /// </summary>
36  [Item("DataTable", "A table of data values.")]
37  [StorableClass]
38  public sealed class DataTable : NamedItem, IStringConvertibleMatrix {
39    public override Image ItemImage {
40      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Performance.ToBitmap(); }
41    }
42
43    [Storable]
44    private NamedItemCollection<DataRow> rows;
45    public NamedItemCollection<DataRow> Rows {
46      get { return rows; }
47    }
48
49    public DataTable()
50      : base() {
51      rows = new NamedItemCollection<DataRow>();
52      this.RegisterRowsEvents();
53    }
54    public DataTable(string name)
55      : base(name) {
56      rows = new NamedItemCollection<DataRow>();
57      this.RegisterRowsEvents();
58    }
59    public DataTable(string name, string description)
60      : base(name, description) {
61      rows = new NamedItemCollection<DataRow>();
62      this.RegisterRowsEvents();
63    }
64
65    private void RegisterRowsEvents() {
66      rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
67      rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
68      rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
69      rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
70    }
71    private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
72      foreach (DataRow row in e.Items)
73        this.RegisterRowEvents(row);
74      this.OnColumnNamesChanged();
75      this.OnReset();
76    }
77    private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
78      foreach (DataRow row in e.Items)
79        this.DeregisterRowEvents(row);
80      this.OnColumnNamesChanged();
81      this.OnReset();
82    }
83    private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
84      foreach (DataRow row in e.OldItems)
85        this.DeregisterRowEvents(row);
86      foreach (DataRow row in e.Items)
87        this.RegisterRowEvents(row);
88      this.OnColumnNamesChanged();
89      this.OnReset();
90    }
91    private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
92      foreach (DataRow row in e.OldItems)
93        this.DeregisterRowEvents(row);
94      foreach (DataRow row in e.Items)
95        this.RegisterRowEvents(row);
96      this.OnColumnNamesChanged();
97      this.OnReset();
98    }
99
100    private void RegisterRowEvents(DataRow row) {
101      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
102      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
103      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
104      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
105      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
106    }
107    private void DeregisterRowEvents(DataRow row) {
108      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
109      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
110      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
111      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
112      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
113    }
114
115    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
116      this.OnReset();
117    }
118    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
119      this.OnReset();
120    }
121    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
122      this.OnReset();
123    }
124    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
125      this.OnReset();
126    }
127    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
128      this.OnReset();
129    }
130
131    public override IDeepCloneable Clone(Cloner cloner) {
132      DataTable clone = new DataTable(Name, Description);
133      cloner.RegisterClonedObject(this, clone);
134      clone.rows = (NamedItemCollection<DataRow>)cloner.Clone(rows);
135      return clone;
136    }
137
138    #region IStringConvertibleMatrix Members
139
140    int IStringConvertibleMatrix.Rows {
141      get { return rows.Max(r => r.Values.Count); }
142      set { throw new NotSupportedException(); }
143    }
144    int IStringConvertibleMatrix.Columns {
145      get { return rows.Count; }
146      set { throw new NotSupportedException(); }
147    }
148    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
149      get { return rows.Select(r => r.Name); }
150      set { throw new NotSupportedException(); }
151    }
152    IEnumerable<string> IStringConvertibleMatrix.RowNames {
153      get { return new List<string>(); }
154      set { throw new NotSupportedException(); }
155    }
156
157    bool IStringConvertibleMatrix.SortableView {
158      get { return true; }
159      set { throw new NotSupportedException(); }
160    }
161    bool IStringConvertibleMatrix.ReadOnly {
162      get { return true; }
163    }
164
165    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
166      if (columnIndex < rows.Count) {
167        string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
168        if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
169          return rows[columnName].Values[rowIndex].ToString();
170      }
171      return string.Empty;
172    }
173
174    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
175      throw new NotSupportedException();
176    }
177    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
178      throw new NotSupportedException();
179    }
180
181    public event EventHandler<EventArgs<int, int>> ItemChanged;
182    private void OnItemChanged(int rowIndex, int columnIndex) {
183      if (ItemChanged != null)
184        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
185      OnToStringChanged();
186    }
187    public event EventHandler Reset;
188    private void OnReset() {
189      if (Reset != null)
190        Reset(this, EventArgs.Empty);
191    }
192    public event EventHandler ColumnNamesChanged;
193    private void OnColumnNamesChanged() {
194      EventHandler handler = ColumnNamesChanged;
195      if (handler != null)
196        handler(this, EventArgs.Empty);
197    }
198    public event EventHandler RowNamesChanged;
199    private void OnRowNamesChanged() {
200      EventHandler handler = RowNamesChanged;
201      if (handler != null)
202        handler(this, EventArgs.Empty);
203    }
204    public event EventHandler SortableViewChanged;
205    private void OnSortableViewChanged() {
206      EventHandler handler = SortableViewChanged;
207      if (handler != null)
208        handler(this, EventArgs.Empty);
209    }
210    #endregion
211  }
212}
Note: See TracBrowser for help on using the repository browser.