Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3027-NormalDistribution/HeuristicLab.Collections/3.3/BidirectionalDictionary.cs @ 17456

Last change on this file since 17456 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
24using System.Collections.Generic;
25using HEAL.Attic;
26
27namespace HeuristicLab.Collections {
28  [StorableType("6D8354FC-521A-4D6B-A544-2C9892D0A36C")]
29  [Serializable]
30  public class BidirectionalDictionary<TFirst, TSecond> : IEnumerable<KeyValuePair<TFirst, TSecond>> {
31    [Storable]
32    private readonly Dictionary<TFirst, TSecond> firstToSecond;
33    [Storable]
34    private readonly Dictionary<TSecond, TFirst> secondToFirst;
35
36    [StorableConstructor]
37    protected BidirectionalDictionary(StorableConstructorFlag _) : base() { }
38    public BidirectionalDictionary() {
39      firstToSecond = new Dictionary<TFirst, TSecond>();
40      secondToFirst = new Dictionary<TSecond, TFirst>();
41    }
42    public BidirectionalDictionary(IEqualityComparer<TFirst> firstComparer)
43      : base() {
44      firstToSecond = new Dictionary<TFirst, TSecond>(firstComparer);
45      secondToFirst = new Dictionary<TSecond, TFirst>();
46    }
47    public BidirectionalDictionary(IEqualityComparer<TSecond> secondComparer)
48      : base() {
49      firstToSecond = new Dictionary<TFirst, TSecond>();
50      secondToFirst = new Dictionary<TSecond, TFirst>(secondComparer);
51    }
52    public BidirectionalDictionary(IEqualityComparer<TFirst> firstComparer, IEqualityComparer<TSecond> secondComparer)
53      : base() {
54      firstToSecond = new Dictionary<TFirst, TSecond>(firstComparer);
55      secondToFirst = new Dictionary<TSecond, TFirst>(secondComparer);
56    }
57    public BidirectionalDictionary(BidirectionalDictionary<TFirst, TSecond> other)
58      : base() {
59      firstToSecond = new Dictionary<TFirst, TSecond>(other.firstToSecond, other.firstToSecond.Comparer);
60      secondToFirst = new Dictionary<TSecond, TFirst>(other.secondToFirst, other.secondToFirst.Comparer);
61    }
62
63    #region Properties
64    public int Count {
65      get { return firstToSecond.Count; }
66    }
67
68    public IEnumerable<TFirst> FirstKeys {
69      get { return firstToSecond.Keys; }
70    }
71
72    public IEnumerable<TSecond> SecondKeys {
73      get { return secondToFirst.Keys; }
74    }
75    #endregion
76
77    #region Methods
78    public void Add(TFirst firstValue, TSecond secondValue) {
79      if (firstToSecond.ContainsKey(firstValue))
80        throw new ArgumentException("Could not add first value " + firstValue.ToString() + " because it is already contained in the bidirectional dictionary.");
81      if (secondToFirst.ContainsKey(secondValue))
82        throw new ArgumentException("Could not add second value " + secondValue.ToString() + " because it is already contained in the bidirectional dictionary.");
83
84      firstToSecond.Add(firstValue, secondValue);
85      secondToFirst.Add(secondValue, firstValue);
86    }
87
88    public bool ContainsFirst(TFirst firstValue) {
89      return firstToSecond.ContainsKey(firstValue);
90    }
91
92    public bool ContainsSecond(TSecond secondValue) {
93      return secondToFirst.ContainsKey(secondValue);
94    }
95
96    public TSecond GetByFirst(TFirst firstValue) {
97      return firstToSecond[firstValue];
98    }
99
100    public TFirst GetBySecond(TSecond secondValue) {
101      return secondToFirst[secondValue];
102    }
103
104    public void SetByFirst(TFirst firstValue, TSecond secondValue) {
105      if (secondToFirst.ContainsKey(secondValue))
106        throw new ArgumentException("Could not set second value " + secondValue.ToString() + " because it is already contained in the bidirectional dictionary.");
107
108      RemoveByFirst(firstValue);
109      Add(firstValue, secondValue);
110    }
111
112    public void SetBySecond(TSecond secondValue, TFirst firstValue) {
113      if (firstToSecond.ContainsKey(firstValue))
114        throw new ArgumentException("Could not set first value " + firstValue.ToString() + " because it is already contained in the bidirectional dictionary.");
115
116      RemoveBySecond(secondValue);
117      Add(firstValue, secondValue);
118    }
119
120    public void RemoveByFirst(TFirst firstValue) {
121      if (ContainsFirst(firstValue)) {
122        TSecond secondValue = firstToSecond[firstValue];
123        firstToSecond.Remove(firstValue);
124        secondToFirst.Remove(secondValue);
125      }
126    }
127
128    public void RemoveBySecond(TSecond secondValue) {
129      if (ContainsSecond(secondValue)) {
130        TFirst firstValue = secondToFirst[secondValue];
131        secondToFirst.Remove(secondValue);
132        firstToSecond.Remove(firstValue);
133      }
134    }
135
136    public void Clear() {
137      firstToSecond.Clear();
138      secondToFirst.Clear();
139    }
140
141    public IEnumerator<KeyValuePair<TFirst, TSecond>> GetEnumerator() {
142      return firstToSecond.GetEnumerator();
143    }
144
145    IEnumerator IEnumerable.GetEnumerator() {
146      return GetEnumerator();
147    }
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.