Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/BoolMatrix.cs

Last change on this file was 13841, checked in by jlodewyc, 8 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 
[2694]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2694]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;
[2694]23using System.Text;
[3376]24using HeuristicLab.Common;
[2694]25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13841]27using System.Linq;
[2694]28
29namespace HeuristicLab.Data {
[3048]30  [Item("BoolMatrix", "Represents a matrix of boolean values.")]
[3017]31  [StorableClass]
[3054]32  public class BoolMatrix : ValueTypeMatrix<bool>, IStringConvertibleMatrix {
[4722]33    [StorableConstructor]
34    protected BoolMatrix(bool deserializing) : base(deserializing) { }
35    protected BoolMatrix(BoolMatrix original, Cloner cloner)
36      : base(original, cloner) {
37    }
[3430]38    public BoolMatrix() : base() { }
[3048]39    public BoolMatrix(int rows, int columns) : base(rows, columns) { }
[3310]40    public BoolMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
41    public BoolMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
[3048]42    public BoolMatrix(bool[,] elements) : base(elements) { }
[3310]43    public BoolMatrix(bool[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
44    public BoolMatrix(bool[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
[2694]45
46    public override IDeepCloneable Clone(Cloner cloner) {
[4722]47      return new BoolMatrix(this, cloner);
[2694]48    }
[13841]49        public static BoolMatrix Parse(string val)
50        {
51            try
52            {
53                val = val.Replace("]", string.Empty);
[2694]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                bool[] a = arr.Where(s => s != "").ToList().ConvertAll(s => (bool.Parse(s))).ToArray();
62                bool[,] b = new bool[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 BoolMatrix(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      bool val;
82      bool valid = bool.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.GetBoolFormatPattern());
88        sb.Append("\")");
89        errorMessage = sb.ToString();
90      }
91      return valid;
92    }
[3054]93    protected virtual string GetValue(int rowIndex, int columIndex) {
[2694]94      return this[rowIndex, columIndex].ToString();
95    }
[3054]96    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
[2694]97      bool val;
98      if (bool.TryParse(value, out val)) {
99        this[rowIndex, columnIndex] = val;
100        return true;
101      } else {
102        return false;
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    }
[2694]124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.