1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Collections;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Collections {
|
---|
29 | [StorableClass]
|
---|
30 | [Serializable]
|
---|
31 | public class BidirectionalDictionary<TFirst, TSecond> : IEnumerable<KeyValuePair<TFirst, TSecond>> {
|
---|
32 | [Storable]
|
---|
33 | private readonly Dictionary<TFirst, TSecond> firstToSecond;
|
---|
34 | [Storable]
|
---|
35 | private readonly Dictionary<TSecond, TFirst> secondToFirst;
|
---|
36 |
|
---|
37 | [StorableConstructor]
|
---|
38 | protected BidirectionalDictionary(bool deserializing) : base() { }
|
---|
39 | public BidirectionalDictionary() {
|
---|
40 | firstToSecond = new Dictionary<TFirst, TSecond>();
|
---|
41 | secondToFirst = new Dictionary<TSecond, TFirst>();
|
---|
42 | }
|
---|
43 | public BidirectionalDictionary(IEqualityComparer<TFirst> firstComparer)
|
---|
44 | : base() {
|
---|
45 | firstToSecond = new Dictionary<TFirst, TSecond>(firstComparer);
|
---|
46 | secondToFirst = new Dictionary<TSecond, TFirst>();
|
---|
47 | }
|
---|
48 | public BidirectionalDictionary(IEqualityComparer<TSecond> secondComparer)
|
---|
49 | : base() {
|
---|
50 | firstToSecond = new Dictionary<TFirst, TSecond>();
|
---|
51 | secondToFirst = new Dictionary<TSecond, TFirst>(secondComparer);
|
---|
52 | }
|
---|
53 | public BidirectionalDictionary(IEqualityComparer<TFirst> firstComparer, IEqualityComparer<TSecond> secondComparer)
|
---|
54 | : base() {
|
---|
55 | firstToSecond = new Dictionary<TFirst, TSecond>(firstComparer);
|
---|
56 | secondToFirst = new Dictionary<TSecond, TFirst>(secondComparer);
|
---|
57 | }
|
---|
58 | public BidirectionalDictionary(BidirectionalDictionary<TFirst, TSecond> other)
|
---|
59 | : base() {
|
---|
60 | firstToSecond = new Dictionary<TFirst, TSecond>(other.firstToSecond, other.firstToSecond.Comparer);
|
---|
61 | secondToFirst = new Dictionary<TSecond, TFirst>(other.secondToFirst, other.secondToFirst.Comparer);
|
---|
62 | }
|
---|
63 |
|
---|
64 | #region Properties
|
---|
65 | public int Count {
|
---|
66 | get { return firstToSecond.Count; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public IEnumerable<TFirst> FirstKeys {
|
---|
70 | get { return firstToSecond.Keys; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public IEnumerable<TSecond> SecondKeys {
|
---|
74 | get { return secondToFirst.Keys; }
|
---|
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 bool TryGetByFirst(TFirst key, out TSecond secondValue) {
|
---|
138 | return firstToSecond.TryGetValue(key, out secondValue);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public bool TryGetBySecond(TSecond key, out TFirst firstValue) {
|
---|
142 | return secondToFirst.TryGetValue(key, out firstValue);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void Clear() {
|
---|
146 | firstToSecond.Clear();
|
---|
147 | secondToFirst.Clear();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public IEnumerator<KeyValuePair<TFirst, TSecond>> GetEnumerator() {
|
---|
151 | return firstToSecond.GetEnumerator();
|
---|
152 | }
|
---|
153 |
|
---|
154 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
155 | return GetEnumerator();
|
---|
156 | }
|
---|
157 | #endregion
|
---|
158 | }
|
---|
159 | }
|
---|