forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionIndex.cs
More file actions
148 lines (128 loc) · 4.52 KB
/
CollectionIndex.cs
File metadata and controls
148 lines (128 loc) · 4.52 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
144
145
146
147
148
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System.Collections.Generic;
using System.Linq;
using OneScript.Contexts;
using OneScript.Exceptions;
using OneScript.Values;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
namespace OneScript.StandardLibrary.Collections.Indexes
{
[ContextClass("ИндексКоллекции", "CollectionIndex")]
public class CollectionIndex : AutoCollectionContext<CollectionIndex, IValue>
{
private readonly List<IValue> _fields = new List<IValue>();
private readonly IIndexCollectionSource _source;
private readonly Dictionary<CollectionIndexKey, HashSet<IValue>> _data =
new Dictionary<CollectionIndexKey, HashSet<IValue>>();
public CollectionIndex(IIndexCollectionSource source, IEnumerable<IValue> fields)
{
foreach (var field in fields)
{
if (field is ValueTable.ValueTableColumn column)
column.AddToIndex();
_fields.Add(field);
}
_source = source;
foreach (var value in _source)
{
ElementAdded(value);
}
}
internal bool CanBeUsedFor(IEnumerable<IValue> searchFields)
{
return _fields.Count > 0 && _fields.All(f => searchFields.Contains(f));
}
private CollectionIndexKey IndexKey(PropertyNameIndexAccessor source)
{
return CollectionIndexKey.Extract(source, _fields);
}
public override string ToString()
{
return string.Join(", ", _fields.Select(field => _source.GetName(field)));
}
public IEnumerable<IValue> GetData(PropertyNameIndexAccessor searchCriteria)
{
var key = IndexKey(searchCriteria);
return _data.TryGetValue(key, out var filteredData) ? filteredData : Enumerable.Empty<IValue>();
}
internal void FieldRemoved(IValue field)
{
if (_fields.Contains(field))
{
while (_fields.Contains(field))
{
if (field is ValueTable.ValueTableColumn column)
column.DeleteFromIndex();
_fields.Remove(field);
}
Rebuild();
}
}
internal void ExcludeFields()
{
foreach (var field in _fields)
{
if (field is ValueTable.ValueTableColumn column)
column.DeleteFromIndex();
}
}
internal void ElementAdded(PropertyNameIndexAccessor element)
{
var key = CollectionIndexKey.Extract(element, _fields);
if (_data.TryGetValue(key, out var set))
{
set.Add(element);
}
else
{
_data.Add(key, new HashSet<IValue> { element });
}
}
internal void ElementRemoved(PropertyNameIndexAccessor element)
{
var key = CollectionIndexKey.Extract(element, _fields);
if (_data.TryGetValue(key, out var set))
{
set.Remove(element);
}
}
internal void Clear() => _data.Clear();
internal void Rebuild()
{
_data.Clear();
foreach (var value in _source)
{
ElementAdded(value);
}
}
public override IValue GetIndexedValue(IValue index)
{
if (index is BslNumericValue numericValue)
{
var numeric = numericValue.AsNumber();
if (numeric >= 0 && numeric < _fields.Count)
{
return ValueFactory.Create(_source.GetName(_fields[decimal.ToInt32(numeric)]));
}
}
throw RuntimeException.InvalidArgumentValue();
}
public override int Count()
{
return _fields.Count;
}
public override IEnumerator<IValue> GetEnumerator()
{
foreach (var field in _fields)
{
yield return ValueFactory.Create(_source.GetName(field));
}
}
}
}