Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs @ 17956

Last change on this file since 17956 was 17906, checked in by gkronber, 4 years ago

#3073 bugfixing

File size: 4.4 KB
RevLine 
[16830]1#region License Information
2/* HeuristicLab
[17902]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[16830]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
[16918]21
22using System;
[16830]23using System.Collections.Generic;
[17564]24using System.Linq;
[17145]25using HEAL.Attic;
[16830]26using HeuristicLab.Common;
[16426]27using HeuristicLab.Core;
28
[16830]29namespace HeuristicLab.Problems.DataAnalysis {
[17145]30  [Item("IntervalCollection", "Represents variables with their interval ranges.")]
[16628]31  [StorableType("230B4E4B-41E5-4D33-9BC3-E2DAADDCA5AE")]
[16904]32  public class IntervalCollection : Item {
[16851]33    public static new System.Drawing.Image StaticItemImage {
34      get => HeuristicLab.Common.Resources.VSImageLibrary.Object;
35    }
[16426]36
[17902]37    private IDictionary<string, Interval> intervals { get; set; } = new Dictionary<string, Interval>();
38
39    [Storable(OldName = "StorableIntervalInformation")]
[16590]40    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
[17902]41      set {
42        foreach (var varInt in value)
43          intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
44      }
45    }
46
47    [Storable]
48    private object[] StorableIntervals {
[16548]49      get {
[17902]50        var names = intervals.Keys.ToArray();
51        var lowerBounds = intervals.Values.Select(i => i.LowerBound).ToArray();
52        var upperBounds = intervals.Values.Select(i => i.UpperBound).ToArray();
[16590]53
[17902]54        return new object[] { names, lowerBounds, upperBounds };
[16548]55      }
56
57      set {
[17902]58        var names = (string[])value[0];
59        var lowerBounds = (double[])value[1];
60        var upperBounds = (double[])value[2];
61
62        for (int i = 0; i < names.Length; i++) {
63          intervals.Add(names[i], new Interval(lowerBounds[i], upperBounds[i]));
64        }
[16548]65      }
66    }
67
[17564]68    public int Count => intervals.Count;
69
[16904]70    public IntervalCollection() : base() { }
[16628]71    [StorableConstructor]
[16904]72    protected IntervalCollection(StorableConstructorFlag _) : base(_) { }
[16426]73
[16904]74    protected IntervalCollection(IntervalCollection original, Cloner cloner) : base(original, cloner) {
[17506]75      foreach (var keyValuePair in original.intervals) {
76        intervals.Add(keyValuePair.Key, new Interval(keyValuePair.Value.LowerBound, keyValuePair.Value.UpperBound));
[16548]77      }
78    }
[17145]79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new IntervalCollection(this, cloner);
81    }
[16548]82
[17506]83    public IntervalCollection(IDictionary<string, Interval> intervals) {
84      this.intervals = intervals;
[16918]85    }
86
[17506]87    public Interval GetInterval(string identifier) {
88      if (!intervals.ContainsKey(identifier)) throw new ArgumentException($"The given identifier:{ identifier } is not present!");
[16925]89
[17506]90      return intervals[identifier];
[16918]91    }
92
[17506]93    public void SetInterval(string identifier, Interval interval) {
94      intervals[identifier] = interval;
[17902]95      RaiseChanged();
[16925]96    }
97
[17506]98    public void AddInterval(string identifier, Interval interval) {
99      intervals.Add(identifier, interval);
[17902]100      RaiseChanged();
[16925]101    }
102
[17506]103    public void DeleteInterval(string identifier) {
104      intervals.Remove(identifier);
[17902]105      RaiseChanged();
[16925]106    }
107
[17564]108    public IReadOnlyDictionary<string, Interval> GetReadonlyDictionary() {
109      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
[16918]110    }
[17564]111
[17902]112    public IDictionary<string, Interval> GetDictionary() {
113      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
114    }
115
[17564]116    public IEnumerable<Tuple<string, Interval>> GetVariableIntervals() {
117      foreach (var variableInterval in intervals)
118        yield return Tuple.Create(variableInterval.Key, variableInterval.Value);
119    }
[17902]120
121    public event EventHandler Changed;
122    private void RaiseChanged() {
123      var handler = Changed;
124      if (handler != null)
125        handler(this, EventArgs.Empty);
126    }
127
[16426]128  }
129}
130
Note: See TracBrowser for help on using the repository browser.