[9000] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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.Linq;
|
---|
| 24 | using System.Collections;
|
---|
[9000] | 25 | using System.Collections.Generic;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Collections {
|
---|
| 29 | [StorableClass]
|
---|
[9009] | 30 | [Serializable]
|
---|
| 31 | public class BidirectionalDictionary<TFirst, TSecond> : IEnumerable<KeyValuePair<TFirst, TSecond>> {
|
---|
[9000] | 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 | }
|
---|
[9009] | 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 | }
|
---|
[9000] | 63 |
|
---|
| 64 | #region Properties
|
---|
| 65 | public int Count {
|
---|
| 66 | get { return firstToSecond.Count; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[10366] | 69 | public IEnumerable<TFirst> FirstKeys {
|
---|
[9000] | 70 | get { return firstToSecond.Keys; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[10366] | 73 | public IEnumerable<TSecond> SecondKeys {
|
---|
[9000] | 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 void Clear() {
|
---|
| 138 | firstToSecond.Clear();
|
---|
| 139 | secondToFirst.Clear();
|
---|
| 140 | }
|
---|
[9009] | 141 |
|
---|
| 142 | public IEnumerator<KeyValuePair<TFirst, TSecond>> GetEnumerator() {
|
---|
| 143 | return firstToSecond.GetEnumerator();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 147 | return GetEnumerator();
|
---|
| 148 | }
|
---|
[9000] | 149 | #endregion
|
---|
| 150 | }
|
---|
| 151 | }
|
---|