Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs @ 16925

Last change on this file since 16925 was 16925, checked in by chaider, 5 years ago

#2971 Set the dictionary to private, renamed methods, added methods to operate on dictionary

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Collections;
28using HEAL.Attic;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [Item("NamedIntervals", "Represents variables with their interval ranges.")]
32  [StorableType("230B4E4B-41E5-4D33-9BC3-E2DAADDCA5AE")]
33  public class IntervalCollection : Item {
34    public static new System.Drawing.Image StaticItemImage {
35      get => HeuristicLab.Common.Resources.VSImageLibrary.Object;
36    }
37    private IDictionary<string, Interval> VariableIntervals { get; } = new Dictionary<string, Interval>();
38
39    [Storable(Name = "StorableIntervalInformation")]
40    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
41      get {
42        var l = new List<KeyValuePair<string, double[]>>();
43        foreach (var varInt in VariableIntervals)
44
45          l.Add(new KeyValuePair<string, double[]>(varInt.Key,
46            new double[] { varInt.Value.LowerBound, varInt.Value.UpperBound }));
47        return l.ToArray();
48      }
49
50      set {
51        foreach (var varInt in value)
52          VariableIntervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
53      }
54    }
55
56    public IntervalCollection() : base() { }
57    [StorableConstructor]
58    protected IntervalCollection(StorableConstructorFlag _) : base(_) { }
59
60    protected IntervalCollection(IntervalCollection original, Cloner cloner) : base(original, cloner) {
61      foreach (var keyValuePair in original.VariableIntervals) {
62        VariableIntervals.Add(keyValuePair.Key, new Interval(keyValuePair.Value.LowerBound, keyValuePair.Value.UpperBound));
63      }
64    }
65
66    public IntervalCollection(IDictionary<string, Interval> variableIntervals) {
67      this.VariableIntervals = variableIntervals;
68    }
69
70    public Interval GetInterval(string variable) {
71      if (!VariableIntervals.ContainsKey(variable)) throw new ArgumentException($"The given variable:{ variable } is not present!");
72
73      return VariableIntervals[variable];
74    }
75
76    public void SetInterval(string key, Interval interval) {
77      VariableIntervals[key] = interval;
78    }
79
80    public void AddInterval(string key, Interval interval) {
81      VariableIntervals.Add(key, interval);
82    }
83
84    public void DeleteInterval(string key) {
85      VariableIntervals.Remove(key);
86    }
87
88    public IDictionary<string, Interval> GetIntervals() {
89      return VariableIntervals;
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new IntervalCollection(this, cloner);
94    }
95
96    public bool ReadOnly { get; }
97
98  }
99}
100
Note: See TracBrowser for help on using the repository browser.