Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data/3.3/DoubleArray.cs @ 9308

Last change on this file since 9308 was 9308, checked in by sforsten, 12 years ago

#2018:

  • added column names to the ValueTypeArray and StringArray
  • added batch update methods to IStringConvertibleArray similar to methods in IStringConvertibleMatrix
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Data {
31  [Item("DoubleArray", "Represents an array of double values.")]
32  [StorableClass]
33  public class DoubleArray : ValueTypeArray<double> {
34    [StorableConstructor]
35    protected DoubleArray(bool deserializing) : base(deserializing) { }
36    protected DoubleArray(DoubleArray original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public DoubleArray() : base() { }
40    public DoubleArray(int length) : base(length) { }
41    public DoubleArray(double[] elements) : base(elements) { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new DoubleArray(this, cloner);
45    }
46
47    protected override bool Validate(string value, out string errorMessage) {
48      double val;
49      bool valid = double.TryParse(value, out val);
50      errorMessage = string.Empty;
51      if (!valid) {
52        StringBuilder sb = new StringBuilder();
53        sb.Append("Invalid Value (Valid Value Format: \"");
54        sb.Append(FormatPatterns.GetDoubleFormatPattern());
55        sb.Append("\")");
56        errorMessage = sb.ToString();
57      }
58      return valid;
59    }
60    protected override string GetValue(int index) {
61      return this[index].ToString();
62    }
63    protected override bool SetValue(string value, int index) {
64      if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
65      double val;
66      if (double.TryParse(value, out val)) {
67        this[index] = val;
68        return true;
69      } else {
70        return false;
71      }
72    }
73
74    protected override bool SetValues(IEnumerable<ArrayValue<string>> arrayValues) {
75      if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
76      double parsed;
77      if (!arrayValues.All(v => double.TryParse(v.Value, out parsed))) return false;
78      List<int> positions = new List<int>();
79      foreach (var v in arrayValues) {
80        parsed = double.Parse(v.Value);
81        if (array[v.Index] != parsed) {
82          array[v.Index] = parsed;
83          positions.Add(v.Index);
84        }
85      }
86      OnItemsChanged(positions);
87      return true;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.