source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/SolutionDictionary.cs @ 9723

Last change on this file since 9723 was 9723, checked in by ascheibe, 6 years ago

#1886 worked on solution caching

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.PluginInfrastructure;
27
28namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
29  [Item("SolutionDictionary", "Stores solutions generated by an algorithm.")]
30  [StorableClass]
31  [NonDiscoverableType]
32  public abstract class SolutionDictionary<TKey, TValue> : Item {
33    [Storable]
34    protected Dictionary<TKey, List<TValue>> solutionDictionary;
35
36    public SolutionDictionary() {
37      solutionDictionary = new Dictionary<TKey, List<TValue>>();
38    }
39
40    [StorableConstructor]
41    protected SolutionDictionary(bool deserializing) : base(deserializing) { }
42    protected SolutionDictionary(SolutionDictionary<TKey, TValue> original, Cloner cloner)
43      : base(original, cloner) {
44      this.solutionDictionary = new Dictionary<TKey, List<TValue>>(original.solutionDictionary);
45    }
46
47    public void Add(TKey key, TValue value) {
48      if (solutionDictionary.Count == 0) {
49        var lst = new List<TValue>();
50        lst.Add(value);
51        solutionDictionary.Add(key, lst);
52        return;
53      }
54
55      if (ContainsKey(key)) {
56        if (!solutionDictionary[key].Contains(value))
57          solutionDictionary[key].Add(value);
58      } else {
59        InsertKeyValue(key, value);
60      }
61    }
62
63    protected abstract void InsertKeyValue(TKey key, TValue value);
64
65    public virtual bool ContainsKey(TKey key) {
66      return solutionDictionary.ContainsKey(key);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.