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
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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
[3308]22using System.Collections.Generic;
[2]23using System.Text;
[3376]24using HeuristicLab.Common;
[2]25using HeuristicLab.Core;
[1853]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13841]27using System.Linq;
[2]28
29namespace HeuristicLab.Data {
[3048]30  [Item("IntMatrix", "Represents a matrix of integer values.")]
[3017]31  [StorableClass]
[3054]32  public class IntMatrix : ValueTypeMatrix<int>, IStringConvertibleMatrix {
[4722]33    [StorableConstructor]
34    protected IntMatrix(bool deserializing) : base(deserializing) { }
35    protected IntMatrix(IntMatrix original, Cloner cloner)
36      : base(original, cloner) {
37    }
[3048]38    public IntMatrix() : base() { }
39    public IntMatrix(int rows, int columns) : base(rows, columns) { }
[3310]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) { }
[3048]42    public IntMatrix(int[,] elements) : base(elements) { }
[3310]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) { }
[2677]45
46    public override IDeepCloneable Clone(Cloner cloner) {
[4722]47      return new IntMatrix(this, cloner);
[2]48    }
[13841]49        public static IntMatrix Parse(string val)
50        {
51            try
52            {
53                val = val.Replace("]", string.Empty);
[2]54
[13841]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) {
[2694]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;
[2]92    }
[3054]93    protected virtual string GetValue(int rowIndex, int columIndex) {
[2677]94      return this[rowIndex, columIndex].ToString();
95    }
[3054]96    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
[2694]97      int val;
98      if (int.TryParse(value, out val)) {
99        this[rowIndex, columnIndex] = val;
[2677]100        return true;
101      } else {
102        return false;
[2]103      }
104    }
[3054]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    }
[2677]124    #endregion
[2]125  }
126}
Note: See TracBrowser for help on using the repository browser.