/
BaseIndex.cs
105 lines (84 loc) · 2.45 KB
/
BaseIndex.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Libplanet.Store
{
public abstract class BaseIndex<TKey, TVal> : IDictionary<TKey, TVal>
{
protected BaseIndex(IStore store)
{
Store = store;
}
public abstract ICollection<TKey> Keys { get; }
public abstract ICollection<TVal> Values { get; }
public abstract int Count { get; }
public abstract bool IsReadOnly { get; }
protected IStore Store { get; }
public abstract TVal this[TKey key] { get; set; }
public void Add(TKey key, TVal value)
{
this[key] = value;
}
public void Add(KeyValuePair<TKey, TVal> item)
{
Add(item.Key, item.Value);
}
public void Clear()
{
foreach (TKey key in Keys)
{
Remove(key);
}
}
public abstract bool Contains(KeyValuePair<TKey, TVal> item);
public abstract bool ContainsKey(TKey key);
public void CopyTo(KeyValuePair<TKey, TVal>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
if (Count > array.Length + arrayIndex)
{
throw new ArgumentException();
}
foreach (KeyValuePair<TKey, TVal> kv in this)
{
array[arrayIndex++] = kv;
}
}
public IEnumerator<KeyValuePair<TKey, TVal>> GetEnumerator()
{
foreach (var key in Keys)
{
yield return new KeyValuePair<TKey, TVal>(key, this[key]);
}
}
public abstract bool Remove(TKey key);
public bool Remove(KeyValuePair<TKey, TVal> item)
{
return Remove(item.Key);
}
public bool TryGetValue(TKey key, out TVal value)
{
try
{
value = this[key];
return true;
}
catch (KeyNotFoundException)
{
value = default(TVal);
return false;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}