Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/BidirectionalDictionary.cs @ 9000

Last change on this file since 9000 was 9000, checked in by abeham, 12 years ago

#1991:

  • Added BidirectionalDictionary for storing 1:1 mapping and BidirectionalLookup for storing M:N mappings
    • I used HashSets in the BidirectionalLookup instead of Lists, because of the difficulty of removing items when custom equality comparers are used.
  • Added unit tests
File size: 5.1 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;
23using System.Collections.Generic;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Collections {
27  [StorableClass]
28  public class BidirectionalDictionary<TFirst, TSecond> {
29    [Storable]
30    private readonly Dictionary<TFirst, TSecond> firstToSecond;
31    [Storable]
32    private readonly Dictionary<TSecond, TFirst> secondToFirst;
33
34    [StorableConstructor]
35    protected BidirectionalDictionary(bool deserializing) : base() { }
36    public BidirectionalDictionary() {
37      firstToSecond = new Dictionary<TFirst, TSecond>();
38      secondToFirst = new Dictionary<TSecond, TFirst>();
39    }
40    public BidirectionalDictionary(IEqualityComparer<TFirst> firstComparer)
41      : base() {
42      firstToSecond = new Dictionary<TFirst, TSecond>(firstComparer);
43      secondToFirst = new Dictionary<TSecond, TFirst>();
44    }
45    public BidirectionalDictionary(IEqualityComparer<TSecond> secondComparer)
46      : base() {
47      firstToSecond = new Dictionary<TFirst, TSecond>();
48      secondToFirst = new Dictionary<TSecond, TFirst>(secondComparer);
49    }
50    public BidirectionalDictionary(IEqualityComparer<TFirst> firstComparer, IEqualityComparer<TSecond> secondComparer)
51      : base() {
52      firstToSecond = new Dictionary<TFirst, TSecond>(firstComparer);
53      secondToFirst = new Dictionary<TSecond, TFirst>(secondComparer);
54    }
55
56    #region Properties
57    public int Count {
58      get { return firstToSecond.Count; }
59    }
60
61    public IEnumerable<TFirst> FirstValues {
62      get { return firstToSecond.Keys; }
63    }
64
65    public IEnumerable<TSecond> SecondValues {
66      get { return secondToFirst.Keys; }
67    }
68
69    public IEnumerable<KeyValuePair<TFirst, TSecond>> FirstEnumerable {
70      get { return firstToSecond; }
71    }
72
73    public IEnumerable<KeyValuePair<TSecond, TFirst>> SecondEnumerable {
74      get { return secondToFirst; }
75    }
76    #endregion
77
78    #region Methods
79    public void Add(TFirst firstValue, TSecond secondValue) {
80      if (firstToSecond.ContainsKey(firstValue))
81        throw new ArgumentException("Could not add first value " + firstValue.ToString() + " because it is already contained in the bidirectional dictionary.");
82      if (secondToFirst.ContainsKey(secondValue))
83        throw new ArgumentException("Could not add second value " + secondValue.ToString() + " because it is already contained in the bidirectional dictionary.");
84
85      firstToSecond.Add(firstValue, secondValue);
86      secondToFirst.Add(secondValue, firstValue);
87    }
88
89    public bool ContainsFirst(TFirst firstValue) {
90      return firstToSecond.ContainsKey(firstValue);
91    }
92
93    public bool ContainsSecond(TSecond secondValue) {
94      return secondToFirst.ContainsKey(secondValue);
95    }
96
97    public TSecond GetByFirst(TFirst firstValue) {
98      return firstToSecond[firstValue];
99    }
100
101    public TFirst GetBySecond(TSecond secondValue) {
102      return secondToFirst[secondValue];
103    }
104
105    public void SetByFirst(TFirst firstValue, TSecond secondValue) {
106      if (secondToFirst.ContainsKey(secondValue))
107        throw new ArgumentException("Could not set second value " + secondValue.ToString() + " because it is already contained in the bidirectional dictionary.");
108
109      RemoveByFirst(firstValue);
110      Add(firstValue, secondValue);
111    }
112
113    public void SetBySecond(TSecond secondValue, TFirst firstValue) {
114      if (firstToSecond.ContainsKey(firstValue))
115        throw new ArgumentException("Could not set first value " + firstValue.ToString() + " because it is already contained in the bidirectional dictionary.");
116
117      RemoveBySecond(secondValue);
118      Add(firstValue, secondValue);
119    }
120
121    public void RemoveByFirst(TFirst firstValue) {
122      if (ContainsFirst(firstValue)) {
123        TSecond secondValue = firstToSecond[firstValue];
124        firstToSecond.Remove(firstValue);
125        secondToFirst.Remove(secondValue);
126      }
127    }
128
129    public void RemoveBySecond(TSecond secondValue) {
130      if (ContainsSecond(secondValue)) {
131        TFirst firstValue = secondToFirst[secondValue];
132        secondToFirst.Remove(secondValue);
133        firstToSecond.Remove(firstValue);
134      }
135    }
136
137    public void Clear() {
138      firstToSecond.Clear();
139      secondToFirst.Clear();
140    }
141    #endregion
142  }
143}
Note: See TracBrowser for help on using the repository browser.