Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3951 was 3742, checked in by gkronber, 15 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

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