Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/IntMatrix.cs @ 17578

Last change on this file since 17578 was 13841, checked in by jlodewyc, 9 years ago

#2582 More parameter datatypes, splitting fileopening service, approving users, reopen last file, change name tasks and repetitions

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System.Linq;
28
29namespace HeuristicLab.Data {
30  [Item("IntMatrix", "Represents a matrix of integer values.")]
31  [StorableClass]
32  public class IntMatrix : ValueTypeMatrix<int>, IStringConvertibleMatrix {
33    [StorableConstructor]
34    protected IntMatrix(bool deserializing) : base(deserializing) { }
35    protected IntMatrix(IntMatrix original, Cloner cloner)
36      : base(original, cloner) {
37    }
38    public IntMatrix() : base() { }
39    public IntMatrix(int rows, int columns) : base(rows, columns) { }
40    public IntMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
41    public IntMatrix(int rows, int columns, IEnumerable<string> columnNames,IEnumerable<string> rowNames) : base(rows, columns, columnNames,rowNames) { }
42    public IntMatrix(int[,] elements) : base(elements) { }
43    public IntMatrix(int[,] elements, IEnumerable<string> columnNames) : base(elements,columnNames) { }
44    public IntMatrix(int[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements,columnNames,rowNames) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new IntMatrix(this, cloner);
48    }
49        public static IntMatrix Parse(string val)
50        {
51            try
52            {
53                val = val.Replace("]", string.Empty);
54
55                string[] parts = val.Split('[');
56                var r = parts.Length;
57                var c = parts[1].Split(';').Length;
58                val = val.Replace("\n", ";");
59                val = val.Replace("[", string.Empty);
60                string[] arr = System.Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
61                int[] a = arr.Where(s => s != "").ToList().ConvertAll(s => int.Parse(s)).ToArray();
62                int[,] b = new int[r, c];
63                var rowcount = 0;
64                for (int i = 0; i < a.Length;)
65                {
66                    for (int j = 0; j < c; j++)
67                    {
68                        b[rowcount, j] = a[i];
69                        i++;
70                    }
71                    rowcount++;
72                }
73                return new IntMatrix(b);
74            }
75            catch (System.FormatException e)
76            {
77                return null;
78            }
79        }
80        protected virtual bool Validate(string value, out string errorMessage) {
81      int val;
82      bool valid = int.TryParse(value, out val);
83      errorMessage = string.Empty;
84      if (!valid) {
85        StringBuilder sb = new StringBuilder();
86        sb.Append("Invalid Value (Valid Value Format: \"");
87        sb.Append(FormatPatterns.GetIntFormatPattern());
88        sb.Append("\")");
89        errorMessage = sb.ToString();
90      }
91      return valid;
92    }
93    protected virtual string GetValue(int rowIndex, int columIndex) {
94      return this[rowIndex, columIndex].ToString();
95    }
96    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
97      int val;
98      if (int.TryParse(value, out val)) {
99        this[rowIndex, columnIndex] = val;
100        return true;
101      } else {
102        return false;
103      }
104    }
105
106    #region IStringConvertibleMatrix Members
107    int IStringConvertibleMatrix.Rows {
108      get { return Rows; }
109      set { Rows = value; }
110    }
111    int IStringConvertibleMatrix.Columns {
112      get { return Columns; }
113      set { Columns = value; }
114    }
115    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
116      return Validate(value, out errorMessage);
117    }
118    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
119      return GetValue(rowIndex, columIndex);
120    }
121    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
122      return SetValue(value, rowIndex, columnIndex);
123    }
124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.