Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17583 was 17564, checked in by mkommend, 4 years ago

#2971: Minor changes in interval collection and interval interpreter.

File size: 3.7 KB
RevLine 
[16830]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
[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    }
[17506]36    private IDictionary<string, Interval> intervals { get; } = new Dictionary<string, Interval>();
[16426]37
[16548]38    [Storable(Name = "StorableIntervalInformation")]
[16590]39    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
[16548]40      get {
[16590]41        var l = new List<KeyValuePair<string, double[]>>();
[17506]42        foreach (var varInt in intervals)
[16590]43
44          l.Add(new KeyValuePair<string, double[]>(varInt.Key,
45            new double[] { varInt.Value.LowerBound, varInt.Value.UpperBound }));
46        return l.ToArray();
[16548]47      }
48
49      set {
50        foreach (var varInt in value)
[17506]51          intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
[16548]52      }
53    }
54
[17564]55    public int Count => intervals.Count;
56
[16904]57    public IntervalCollection() : base() { }
[16628]58    [StorableConstructor]
[16904]59    protected IntervalCollection(StorableConstructorFlag _) : base(_) { }
[16426]60
[16904]61    protected IntervalCollection(IntervalCollection original, Cloner cloner) : base(original, cloner) {
[17506]62      foreach (var keyValuePair in original.intervals) {
63        intervals.Add(keyValuePair.Key, new Interval(keyValuePair.Value.LowerBound, keyValuePair.Value.UpperBound));
[16548]64      }
65    }
[17145]66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new IntervalCollection(this, cloner);
68    }
[16548]69
[17506]70    public IntervalCollection(IDictionary<string, Interval> intervals) {
71      this.intervals = intervals;
[16918]72    }
73
[17506]74    public Interval GetInterval(string identifier) {
75      if (!intervals.ContainsKey(identifier)) throw new ArgumentException($"The given identifier:{ identifier } is not present!");
[16925]76
[17506]77      return intervals[identifier];
[16918]78    }
79
[17506]80    public void SetInterval(string identifier, Interval interval) {
81      intervals[identifier] = interval;
[16925]82    }
83
[17506]84    public void AddInterval(string identifier, Interval interval) {
85      intervals.Add(identifier, interval);
[16925]86    }
87
[17506]88    public void DeleteInterval(string identifier) {
89      intervals.Remove(identifier);
[16925]90    }
91
[17564]92    public IReadOnlyDictionary<string, Interval> GetReadonlyDictionary() {
93      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
[16918]94    }
[17564]95
96    public IEnumerable<Tuple<string, Interval>> GetVariableIntervals() {
97      foreach (var variableInterval in intervals)
98        yield return Tuple.Create(variableInterval.Key, variableInterval.Value);
99    }
[16426]100  }
101}
102
Note: See TracBrowser for help on using the repository browser.