-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOrderedDictionary.cs
More file actions
143 lines (89 loc) · 3.63 KB
/
OrderedDictionary.cs
File metadata and controls
143 lines (89 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Gsemac.Collections {
public class OrderedDictionary<TKey, TValue> :
IDictionary<TKey, TValue> {
// Public members
public TValue this[TKey key] {
get => baseDictionary[key];
set => SetValue(key, value);
}
public ICollection<TKey> Keys => new LazyReadOnlyCollection<TKey>(orderedKeys);
public ICollection<TValue> Values => new LazyReadOnlyCollection<TValue>(orderedKeys.Select(key => baseDictionary[key]));
public int Count => baseDictionary.Count;
public bool IsReadOnly => baseDictionary.IsReadOnly;
public OrderedDictionary() :
this(EqualityComparer<TKey>.Default) {
}
public OrderedDictionary(IDictionary<TKey, TValue> baseDictionary) {
if (baseDictionary is null)
throw new ArgumentNullException(nameof(baseDictionary));
this.baseDictionary = baseDictionary;
}
public OrderedDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) :
this(collection, EqualityComparer<TKey>.Default) {
}
public OrderedDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) :
this(comparer) {
AddRange(collection);
}
public OrderedDictionary(IEqualityComparer<TKey> comparer) {
baseDictionary = new Dictionary<TKey, TValue>(comparer);
}
public OrderedDictionary(int capacity) {
baseDictionary = new Dictionary<TKey, TValue>(capacity);
}
public OrderedDictionary(int capacity, IEqualityComparer<TKey> comparer) {
baseDictionary = new Dictionary<TKey, TValue>(capacity, comparer);
}
public void Add(TKey key, TValue value) {
SetValue(key, value);
}
public void Add(KeyValuePair<TKey, TValue> item) {
SetValue(item.Key, item.Value);
}
public void Clear() {
orderedKeys.Clear();
baseDictionary.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item) {
return baseDictionary.Contains(item);
}
public bool ContainsKey(TKey key) {
return baseDictionary.ContainsKey(key);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
baseDictionary.ToArray().CopyTo(array, arrayIndex);
}
public bool Remove(TKey key) {
orderedKeys.Remove(key);
return baseDictionary.Remove(key);
}
public bool Remove(KeyValuePair<TKey, TValue> item) {
return Remove(item.Key);
}
public bool TryGetValue(TKey key, out TValue value) {
return baseDictionary.TryGetValue(key, out value);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
return baseDictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// Private members
private readonly IList<TKey> orderedKeys = new List<TKey>();
private readonly IDictionary<TKey, TValue> baseDictionary;
private void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> collection) {
foreach (var pair in collection)
Add(pair);
}
private void SetValue(TKey key, TValue value) {
Remove(key);
orderedKeys.Add(key);
baseDictionary.Add(key, value);
}
}
}