Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Operators.Views.GraphVisualization/3.3/BidirectionalLookup.cs @ 11637

Last change on this file since 11637 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Operators.Views.GraphVisualization {
27  [StorableClass]
28  public class BidirectionalLookup<TFirst, TSecond> {
29    [Storable]
30    private Dictionary<TFirst, TSecond> firstToSecond;
31    [Storable]
32    private Dictionary<TSecond, TFirst> secondToFirst;
33
34    [StorableConstructor]
35    protected BidirectionalLookup(bool deserializing) : base() { }
36
37    public BidirectionalLookup() {
38      this.firstToSecond = new Dictionary<TFirst, TSecond>();
39      this.secondToFirst = new Dictionary<TSecond, TFirst>();
40    }
41
42    #region properties
43    public int Count {
44      get { return this.firstToSecond.Count; }
45    }
46
47    public IEnumerable<TFirst> FirstValues {
48      get { return this.firstToSecond.Keys; }
49    }
50
51    public IEnumerable<TSecond> SecondValues {
52      get { return this.secondToFirst.Keys; }
53    }
54
55    public IEnumerable<KeyValuePair<TFirst, TSecond>> FirstEnumerable {
56      get { return this.firstToSecond; }
57    }
58
59    public IEnumerable<KeyValuePair<TSecond, TFirst>> SecondEnumerable {
60      get { return this.secondToFirst; }
61    }
62    #endregion
63
64
65    #region methods
66    public void Add(TFirst firstValue, TSecond secondValue) {
67      if (this.firstToSecond.ContainsKey(firstValue))
68        throw new ArgumentException("Could not add first value " + firstValue.ToString() + " because it is already contained in the bidirectional lookup.");
69      if (this.secondToFirst.ContainsKey(secondValue))
70        throw new ArgumentException("Could not add second value " + secondValue.ToString() + " because it is already contained in the bidirectional lookup.");
71
72      firstToSecond.Add(firstValue, secondValue);
73      secondToFirst.Add(secondValue, firstValue);
74    }
75
76    public bool ContainsFirst(TFirst firstValue) {
77      return this.firstToSecond.ContainsKey(firstValue);
78    }
79
80    public bool ContainsSecond(TSecond secondValue) {
81      return this.secondToFirst.ContainsKey(secondValue);
82    }
83
84    public TSecond GetByFirst(TFirst firstValue) {
85      return this.firstToSecond[firstValue];
86    }
87
88    public TFirst GetBySecond(TSecond secondValue) {
89      return this.secondToFirst[secondValue];
90    }
91
92    public void SetByFirst(TFirst firstValue, TSecond secondValue) {
93      if (this.secondToFirst.ContainsKey(secondValue))
94        throw new ArgumentException("Could not set second value " + secondValue.ToString() + " because it is already contained in the bidirectional lookup.");
95
96      this.RemoveByFirst(firstValue);
97      this.Add(firstValue, secondValue);
98    }
99
100    public void SetBySecond(TSecond secondValue, TFirst firstValue) {
101      if (this.firstToSecond.ContainsKey(firstValue))
102        throw new ArgumentException("Could not set first value " + firstValue.ToString() + " because it is already contained in the bidirectional lookup.");
103
104      this.RemoveBySecond(secondValue);
105      this.Add(firstValue, secondValue);
106    }
107
108    public void RemoveByFirst(TFirst firstValue) {
109      if (this.ContainsFirst(firstValue)) {
110        TSecond secondValue = this.firstToSecond[firstValue];
111        this.firstToSecond.Remove(firstValue);
112        this.secondToFirst.Remove(secondValue);
113      }
114    }
115
116    public void RemoveBySecond(TSecond secondValue) {
117      if (this.ContainsSecond(secondValue)) {
118        TFirst firstValue = this.secondToFirst[secondValue];
119        this.secondToFirst.Remove(secondValue);
120        this.firstToSecond.Remove(firstValue);
121      }
122    }
123
124    public void Clear() {
125      this.firstToSecond.Clear();
126      this.secondToFirst.Clear();
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.