Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data/3.3/IntMatrix.cs @ 9286

Last change on this file since 9286 was 9286, checked in by sforsten, 11 years ago

#2018:

  • added new methods to the interface IStringConvertibleMatrix as well as two structs. Also the event ItemChanged has been changed to ItemsChanged
  • class ValueTypeMatrix now implements IStringConvertibleMatrix instead of the classes which inherit from it
  • small changes have been applied to a lot of classes to correctly implement the changed interface IStringConvertibleMatrix
  • solution file, Build.cmd and PreBuildEvent.cmd have been added
File size: 3.8 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("IntMatrix", "Represents a matrix of integer values.")]
32  [StorableClass]
33  public class IntMatrix : ValueTypeMatrix<int> {
34    [StorableConstructor]
35    protected IntMatrix(bool deserializing) : base(deserializing) { }
36    protected IntMatrix(IntMatrix original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public IntMatrix() : base() { }
40    public IntMatrix(int rows, int columns) : base(rows, columns) { }
41    public IntMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
42    public IntMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
43    public IntMatrix(int[,] elements) : base(elements) { }
44    public IntMatrix(int[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
45    public IntMatrix(int[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new IntMatrix(this, cloner);
49    }
50
51    protected override bool Validate(string value, out string errorMessage) {
52      int val;
53      bool valid = int.TryParse(value, out val);
54      errorMessage = string.Empty;
55      if (!valid) {
56        StringBuilder sb = new StringBuilder();
57        sb.Append("Invalid Value (Valid Value Format: \"");
58        sb.Append(FormatPatterns.GetIntFormatPattern());
59        sb.Append("\")");
60        errorMessage = sb.ToString();
61      }
62      return valid;
63    }
64    protected override string GetValue(int rowIndex, int columIndex) {
65      return this[rowIndex, columIndex].ToString();
66    }
67    protected override bool SetValue(string value, int rowIndex, int columnIndex) {
68      int val;
69      if (int.TryParse(value, out val)) {
70        this[rowIndex, columnIndex] = val;
71        return true;
72      } else {
73        return false;
74      }
75    }
76    protected override bool SetValue(IEnumerable<RowColumnValue> rowColumnValues) {
77      if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
78      List<Tuple<Position, int>> newValues = new List<Tuple<Position, int>>();
79      int parsed;
80      foreach (var curValue in rowColumnValues) {
81        if (int.TryParse(curValue.Value, out parsed)) {
82          newValues.Add(new Tuple<Position, int>(curValue.Position, parsed));
83        } else {
84          return false;
85        }
86      }
87      Position pos;
88      foreach (var curValue in newValues) {
89        pos = curValue.Item1;
90        matrix[pos.Row, pos.Column] = curValue.Item2;
91      }
92      OnItemsChanged(rowColumnValues.Select(x => x.Position));
93      return true;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.