Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/DoubleArray.cs @ 17399

Last change on this file since 17399 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: 3.4 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
22using System.Text;
[3430]23using HeuristicLab.Common;
[2694]24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13841]26using System;
27using System.Linq;
[2694]28
29namespace HeuristicLab.Data {
[3048]30  [Item("DoubleArray", "Represents an array of double values.")]
[3017]31  [StorableClass]
[3054]32  public class DoubleArray : ValueTypeArray<double>, IStringConvertibleArray {
[4722]33    [StorableConstructor]
34    protected DoubleArray(bool deserializing) : base(deserializing) { }
35    protected DoubleArray(DoubleArray original, Cloner cloner)
36      : base(original, cloner) {
37    }
[3048]38    public DoubleArray() : base() { }
39    public DoubleArray(int length) : base(length) { }
40    public DoubleArray(double[] elements) : base(elements) { }
[2694]41
42    public override IDeepCloneable Clone(Cloner cloner) {
[4722]43      return new DoubleArray(this, cloner);
[2694]44    }
[13841]45        public static DoubleArray Parse(string val)
46        {
47            try
48            {
49                string[] arr = Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
50                double[] a = arr.Where(s => s != "").ToList().ConvertAll(s => double.Parse(s)).ToArray();
51                return new DoubleArray(a);
52            }
53            catch (FormatException e)
54            {
55                return null;
56            }
57        }
[2694]58
[13841]59        protected virtual bool Validate(string value, out string errorMessage) {
[2694]60      double val;
61      bool valid = double.TryParse(value, out val);
62      errorMessage = string.Empty;
63      if (!valid) {
64        StringBuilder sb = new StringBuilder();
65        sb.Append("Invalid Value (Valid Value Format: \"");
66        sb.Append(FormatPatterns.GetDoubleFormatPattern());
67        sb.Append("\")");
68        errorMessage = sb.ToString();
69      }
70      return valid;
71    }
[3054]72    protected virtual string GetValue(int index) {
[12432]73      return this[index].ToString("r");
[2694]74    }
[3054]75    protected virtual bool SetValue(string value, int index) {
[2694]76      double val;
77      if (double.TryParse(value, out val)) {
[2973]78        this[index] = val;
[2694]79        return true;
80      } else {
81        return false;
82      }
83    }
[3054]84
85    #region IStringConvertibleArray Members
86    int IStringConvertibleArray.Length {
87      get { return Length; }
88      set { Length = value; }
89    }
90    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
91      return Validate(value, out errorMessage);
92    }
93    string IStringConvertibleArray.GetValue(int index) {
94      return GetValue(index);
95    }
96    bool IStringConvertibleArray.SetValue(string value, int index) {
97      return SetValue(value, index);
98    }
[2694]99    #endregion
100  }
101}
Note: See TracBrowser for help on using the repository browser.