[9000] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9000] | 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;
|
---|
[9009] | 23 | using System.Collections;
|
---|
[9000] | 24 | using System.Collections.Generic;
|
---|
[17097] | 25 | using HEAL.Attic;
|
---|
[9000] | 26 |
|
---|
| 27 | namespace HeuristicLab.Collections {
|
---|
[17097] | 28 | [StorableType("6D8354FC-521A-4D6B-A544-2C9892D0A36C")]
|
---|
[9009] | 29 | [Serializable]
|
---|
| 30 | public class BidirectionalDictionary<TFirst, TSecond> : IEnumerable<KeyValuePair<TFirst, TSecond>> {
|
---|
[9000] | 31 | [Storable]
|
---|
| 32 | private readonly Dictionary<TFirst, TSecond> firstToSecond;
|
---|
| 33 | [Storable]
|
---|
| 34 | private readonly Dictionary<TSecond, TFirst> secondToFirst;
|
---|
| 35 |
|
---|
| 36 | [StorableConstructor]
|
---|
[17097] | 37 | protected BidirectionalDictionary(StorableConstructorFlag _) : base() { }
|
---|
[9000] | 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 | }
|
---|
[9009] | 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 | }
|
---|
[9000] | 62 |
|
---|
| 63 | #region Properties
|
---|
| 64 | public int Count {
|
---|
| 65 | get { return firstToSecond.Count; }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[10376] | 68 | public IEnumerable<TFirst> FirstKeys {
|
---|
[9000] | 69 | get { return firstToSecond.Keys; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[10376] | 72 | public IEnumerable<TSecond> SecondKeys {
|
---|
[9000] | 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 | }
|
---|
[9009] | 140 |
|
---|
| 141 | public IEnumerator<KeyValuePair<TFirst, TSecond>> GetEnumerator() {
|
---|
| 142 | return firstToSecond.GetEnumerator();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 146 | return GetEnumerator();
|
---|
| 147 | }
|
---|
[9000] | 148 | #endregion
|
---|
| 149 | }
|
---|
| 150 | }
|
---|