Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4555 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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