Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/BidirectionalLookup.cs @ 2910

Last change on this file since 2910 was 2853, checked in by mkommend, 14 years ago

added first version of mapping for the graph visualization (ticket #867)

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