forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionIndexes.cs
More file actions
162 lines (139 loc) · 4.71 KB
/
CollectionIndexes.cs
File metadata and controls
162 lines (139 loc) · 4.71 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*----------------------------------------------------------
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.StandardLibrary.Collections.Exceptions;
using OneScript.StandardLibrary.Collections.Indexes;
using OneScript.Values;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
namespace OneScript.StandardLibrary.Collections.ValueTable
{
[ContextClass("ИндексыКоллекции", "CollectionIndexes")]
public class CollectionIndexes : AutoCollectionContext<CollectionIndexes, CollectionIndex>
{
readonly List<CollectionIndex> _indexes = new List<CollectionIndex>();
private readonly IIndexCollectionSource _owner;
public CollectionIndexes(IIndexCollectionSource owner)
{
_owner = owner;
}
[ContextMethod("Добавить", "Add")]
public CollectionIndex Add(string columns)
{
var newIndex = new CollectionIndex(_owner, BuildFieldList(_owner, columns));
_indexes.Add(newIndex);
return newIndex;
}
[ContextMethod("Количество", "Count")]
public override int Count()
{
return _indexes.Count;
}
[ContextMethod("Удалить", "Delete")]
public void Delete(IValue index)
{
var idx = GetIndex(index);
idx.ExcludeFields();
_indexes.Remove(idx);
}
[ContextMethod("Очистить", "Clear")]
public void Clear()
{
foreach (var idx in _indexes)
idx.ExcludeFields();
_indexes.Clear();
}
public override IValue GetIndexedValue(IValue index)
{
return GetIndex(index);
}
private CollectionIndex GetIndex(IValue index)
{
if (index is CollectionIndex collectionIndex)
{
if (_indexes.Contains(collectionIndex))
{
return collectionIndex;
}
throw RuntimeException.InvalidArgumentValue();
}
if (index is BslNumericValue numberValue)
{
var number = numberValue.AsNumber();
if (number >= 0 && number < _indexes.Count)
{
return _indexes[decimal.ToInt32(number)];
}
throw RuntimeException.InvalidArgumentValue();
}
throw RuntimeException.InvalidArgumentType();
}
public void Rebuild()
{
foreach (var index in _indexes)
{
index.Rebuild();
}
}
internal void ClearIndexes()
{
foreach (var index in _indexes)
{
index.Clear();
}
}
internal void FieldRemoved(IValue field)
{
foreach (var index in _indexes)
{
index.FieldRemoved(field);
}
}
internal void ElementAdded(PropertyNameIndexAccessor element)
{
foreach (var index in _indexes)
{
index.ElementAdded(element);
}
}
internal void ElementRemoved(PropertyNameIndexAccessor element)
{
foreach (var index in _indexes)
{
index.ElementRemoved(element);
}
}
public CollectionIndex FindSuitableIndex(IEnumerable<IValue> searchFields)
{
return _indexes.FirstOrDefault(index => index.CanBeUsedFor(searchFields));
}
private static List<IValue> BuildFieldList(IIndexCollectionSource source, string fieldList)
{
var fields = new List<IValue>();
if (string.IsNullOrEmpty(fieldList))
return fields;
var fieldNames = fieldList.Split(',', System.StringSplitOptions.RemoveEmptyEntries);
foreach (var fieldName in fieldNames)
{
var name = fieldName.Trim();
var field = source.GetField(name) ?? throw ColumnException.WrongColumnName(fieldName);
fields.Add(field);
}
return fields;
}
public override IEnumerator<CollectionIndex> GetEnumerator()
{
foreach (var item in _indexes)
{
yield return item;
}
}
}
}