Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5552


Ignore:
Timestamp:
02/23/11 13:54:20 (14 years ago)
Author:
mkommend
Message:

#1418: Refactored Dataset.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r5484 r5552  
    3939      data = original.data;
    4040    }
     41    public override IDeepCloneable Clone(Cloner cloner) {
     42      return new Dataset(this, cloner);
     43    }
     44
    4145    public Dataset(IEnumerable<string> variableNames, double[,] data)
    4246      : base() {
     
    4650      }
    4751      this.data = (double[,])data.Clone();
    48       this.variableNames = variableNames.ToArray();
     52      VariableNames = variableNames;
    4953    }
    5054
     55
     56    private Dictionary<string, int> variableNames;
    5157    [Storable]
    52     private string[] variableNames;
    5358    public IEnumerable<string> VariableNames {
    54       get { return variableNames; }
     59      get { return variableNames.Keys; }
     60      private set {
     61        if (variableNames != null) throw new InvalidOperationException("VariableNames can only be set once.");
     62        this.variableNames = new Dictionary<string, int>();
     63        int i = 0;
     64        foreach (string variableName in value) {
     65          this.variableNames.Add(variableName, i);
     66          i++;
     67        }
     68      }
    5569    }
    5670
     
    7589      return GetVariableValues(variableIndex, 0, Rows);
    7690    }
    77     public double[] GetVariableValues(int variableIndex, int start, int end) {
    78       if (start < 0 || !(start <= end))
    79         throw new ArgumentException("Start must be between 0 and end (" + end + ").");
    80       if (end > Rows || end < start)
    81         throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
    82 
    83       double[] values = new double[end - start];
    84       for (int i = 0; i < end - start; i++)
    85         values[i] = data[i + start, variableIndex];
    86       return values;
    87     }
    8891    public double[] GetVariableValues(string variableName) {
    8992      return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
     93    }
     94    public double[] GetVariableValues(int variableIndex, int start, int end) {
     95      return GetEnumeratedVariableValues(variableIndex, start, end).ToArray();
    9096    }
    9197    public double[] GetVariableValues(string variableName, int start, int end) {
     
    101107      if (end > Rows || end < start)
    102108        throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
    103       for (int i = 0; i < end - start; i++)
    104         yield return data[i + start, variableIndex];
     109
     110      for (int i = start; i < end; i++)
     111        yield return data[i, variableIndex];
    105112    }
    106113    public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) {
     
    120127
    121128    public string GetVariableName(int variableIndex) {
    122       return variableNames[variableIndex];
     129      return VariableNames.ElementAt(variableIndex);
    123130    }
    124 
    125131    public int GetVariableIndex(string variableName) {
    126       for (int i = 0; i < variableNames.Length; i++) {
    127         if (variableNames[i].Equals(variableName)) return i;
     132      try {
     133        return variableNames[variableName];
    128134      }
    129       throw new ArgumentException("The variable name " + variableName + " was not found.");
    130     }
    131 
    132     public double[,] GetClonedData() {
    133       return (double[,])data.Clone();
    134     }
    135 
    136     public override IDeepCloneable Clone(Cloner cloner) {
    137       return new Dataset(this, cloner);
    138     }
    139 
    140     public event EventHandler Reset;
    141     private void OnReset(EventArgs e) {
    142       var listeners = Reset;
    143       if (listeners != null) listeners(this, e);
     135      catch (KeyNotFoundException ex) {
     136        throw new ArgumentException("The variable name " + variableName + " was not found.", ex);
     137      }
    144138    }
    145139
    146140    #region IStringConvertibleMatrix Members
    147 
    148141    public int Rows {
    149142      get { return data.GetLength(0); }
    150143      set { throw new NotSupportedException(); }
    151144    }
    152 
    153145    public int Columns {
    154146      get { return data.GetLength(1); }
     
    160152      set { throw new NotSupportedException(); }
    161153    }
    162 
    163154    public bool ReadOnly {
    164155      get { return true; }
     
    169160      set { throw new NotSupportedException(); }
    170161    }
    171 
    172162    IEnumerable<string> IStringConvertibleMatrix.RowNames {
    173       get { return new List<string>(); }
     163      get { return Enumerable.Empty<string>(); }
    174164      set { throw new NotSupportedException(); }
    175     }
    176 
    177     public bool Validate(string value, out string errorMessage) {
    178       throw new NotSupportedException();
    179165    }
    180166
     
    182168      return data[rowIndex, columnIndex].ToString();
    183169    }
    184 
    185170    public bool SetValue(string value, int rowIndex, int columnIndex) {
    186171      throw new NotSupportedException();
    187172    }
     173    public bool Validate(string value, out string errorMessage) {
     174      throw new NotSupportedException();
     175    }
    188176
    189     public event EventHandler ColumnsChanged;
    190     private void OnColumnsChanged() {
    191       var handler = ColumnsChanged;
    192       if (handler != null) handler(this, EventArgs.Empty);
    193     }
    194     public event EventHandler RowsChanged;
    195     private void OnRowsChanged() {
    196       var handler = RowsChanged;
    197       if (handler != null) handler(this, EventArgs.Empty);
    198     }
    199     public event EventHandler ColumnNamesChanged;
    200     private void OnColumnNamesChanged() {
    201       EventHandler listeners = ColumnNamesChanged;
    202       if (listeners != null)
    203         listeners(this, EventArgs.Empty);
    204     }
    205     public event EventHandler RowNamesChanged;
    206     private void OnRowNamesChanged() {
    207       EventHandler listeners = RowNamesChanged;
    208       if (listeners != null)
    209         listeners(this, EventArgs.Empty);
    210     }
    211     public event EventHandler SortableViewChanged;
    212     private void OnSortableViewChanged() {
    213       EventHandler listeners = SortableViewChanged;
    214       if (listeners != null)
    215         listeners(this, EventArgs.Empty);
    216     }
    217     public event EventHandler<EventArgs<int, int>> ItemChanged;
    218     private void OnItemChanged(int rowIndex, int columnIndex) {
    219       var listeners = ItemChanged;
    220       if (listeners != null)
    221         listeners(this, new EventArgs<int, int>(rowIndex, columnIndex));
    222       OnToStringChanged();
    223     }
     177    public event EventHandler ColumnsChanged { add { } remove { } }
     178    public event EventHandler RowsChanged { add { } remove { } }
     179    public event EventHandler ColumnNamesChanged { add { } remove { } }
     180    public event EventHandler RowNamesChanged { add { } remove { } }
     181    public event EventHandler SortableViewChanged { add { } remove { } }
     182    public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
    224183    #endregion
    225184  }
Note: See TracChangeset for help on using the changeset viewer.