-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexedCollection.IndexByCriterion.cs
141 lines (119 loc) · 4.69 KB
/
IndexedCollection.IndexByCriterion.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
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
using System;
using System.Collections.Generic;
namespace IndexedCollection
{
public partial class IndexedCollection<TItem>
{
public class IndexByCriterion<TQueryKey, TCriterionKey> : IIndex
{
private readonly Func<TItem, TCriterionKey> _selector;
private readonly Func<TCriterionKey, TQueryKey, bool> _criterion;
private readonly IndexedCollection<TItem> _collection;
private readonly Dictionary<TCriterionKey, OEntry> _odict =
new Dictionary<TCriterionKey, OEntry>();
private readonly Dictionary<TQueryKey, Buffer<Entry>> _dict =
new Dictionary<TQueryKey, Buffer<Entry>>();
private readonly Buffer<(Buffer<Entry> list, int index)> _backIndexes = new Buffer<(Buffer<Entry>, int)>();
class OEntry
{
public Buffer<Entry> Entries = new Buffer<Entry>();
public Buffer<TQueryKey> Keys = new Buffer<TQueryKey>();
}
public IndexByCriterion(Func<TItem, TCriterionKey> selector,
Func<TCriterionKey, TQueryKey, bool> criterion,
IndexedCollection<TItem> collection)
{
_selector = selector;
_collection = collection;
_criterion = criterion;
}
public IEnumerable<Entry> Get(TQueryKey key)
{
if(_dict.TryGetValue(key, out var entries)) return entries;
entries = new Buffer<Entry>();
_dict.Add(key, entries);
foreach(var entry in _collection._values)
{
var criterionKey = _selector(entry.Item);
if(_criterion(criterionKey, key))
{
var innerIndex = entries.Count;
entries.Add(entry);
_backIndexes.SetAtAndResize(entry.Index, (entries, innerIndex));
}
}
foreach(var o in _odict)
{
if(_criterion(o.Key, key))
{
o.Value.Keys.Add(key);
}
}
return entries;
}
public bool RemoveByKey(TQueryKey key)
{
if(!_dict.TryGetValue(key, out var values)) return false;
var buffer = new Buffer<Entry>();
buffer.Refill(values);
foreach(var value in buffer)
{
_collection.Remove(value);
}
return true;
}
void IIndex.Remove(Entry item)
{
var tuple = _backIndexes.Array[item.Index];
var innerIndex = tuple.index;
var list = tuple.list;
if(list == null) return;
list.RemoveAndMixOrder(innerIndex);
_backIndexes.SetAtAndResize(item.Index, default);
if(list.Count > 0 && innerIndex < list.Count)
{
_backIndexes.SetAtAndResize(list.Array[innerIndex].Index, tuple);
}
}
void IIndex.Add(Entry entry)
{
var criterionKey = _selector(entry.Item);
if(!_odict.TryGetValue(criterionKey, out var oEntry))
{
oEntry = new OEntry();
_odict.Add(criterionKey, oEntry);
foreach(var key in _dict.Keys)
{
if(_criterion(criterionKey, key))
{
oEntry.Keys.Add(key);
}
}
}
oEntry.Entries.Add(entry);
_backIndexes.SetAtAndResize(entry.Index, default);
foreach(var key in oEntry.Keys)
{
var entries = _dict[key];
var innerIndex = entries.Count;
entries.Add(entry);
_backIndexes.SetAtAndResize(entry.Index, (entries, innerIndex));
}
}
void IIndex.Rebuild()
{
var indexer = (IIndex)this;
indexer.Clear();
foreach(var value in _collection._values)
{
indexer.Add(value);
}
}
void IIndex.Clear()
{
_odict.Clear();
_dict.Clear();
}
}
}
}