1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System.Text;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HEAL.Attic;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Data {
|
---|
28 | [Item("PercentArray", "Represents an array of double values in percent.")]
|
---|
29 | [StorableType("8ED7F07C-1DE7-4782-90C0-50395627F8A0")]
|
---|
30 | public class PercentArray : DoubleArray {
|
---|
31 | [StorableConstructor]
|
---|
32 | protected PercentArray(StorableConstructorFlag _) : base(_) { }
|
---|
33 | protected PercentArray(PercentArray original, Cloner cloner)
|
---|
34 | : base(original, cloner) {
|
---|
35 | }
|
---|
36 | public PercentArray() : base() { }
|
---|
37 | public PercentArray(int length) : base(length) { }
|
---|
38 | public PercentArray(double[] elements) : base(elements) { }
|
---|
39 |
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new PercentArray(this, cloner);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override string ToString() {
|
---|
45 | StringBuilder sb = new StringBuilder();
|
---|
46 | sb.Append("[");
|
---|
47 | if (array.Length > 0) {
|
---|
48 | sb.Append(array[0].ToString("#0.#################### %")); // percent format
|
---|
49 | for (int i = 1; i < array.Length; i++)
|
---|
50 | sb.Append(";").Append(array[i].ToString("#0.#################### %")); // percent format
|
---|
51 | }
|
---|
52 | sb.Append("]");
|
---|
53 | return sb.ToString();
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected override bool Validate(string value, out string errorMessage) {
|
---|
57 | value = value.Replace("%", " ");
|
---|
58 | return base.Validate(value, out errorMessage);
|
---|
59 | }
|
---|
60 | protected override string GetValue(int index) {
|
---|
61 | return this[index].ToString("#0.#################### %"); // percent format
|
---|
62 | }
|
---|
63 | protected override bool SetValue(string value, int index) {
|
---|
64 | bool percent = value.Contains("%");
|
---|
65 | value = value.Replace("%", " ");
|
---|
66 | double val;
|
---|
67 | if (double.TryParse(value, out val)) {
|
---|
68 | if (percent) {
|
---|
69 | if (!(val).IsAlmost(this[index] * 100.0))
|
---|
70 | this[index] = val == 0 ? 0 : val / 100.0;
|
---|
71 | } else {
|
---|
72 | this[index] = val;
|
---|
73 | }
|
---|
74 | return true;
|
---|
75 | } else {
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|