|
1 | | -/*---------------------------------------------------------- |
2 | | -This Source Code Form is subject to the terms of the |
3 | | -Mozilla Public License, v.2.0. If a copy of the MPL |
4 | | -was not distributed with this file, You can obtain one |
5 | | -at http://mozilla.org/MPL/2.0/. |
6 | | -----------------------------------------------------------*/ |
7 | | - |
8 | | -using System.Collections.Generic; |
9 | | -using System.Linq; |
10 | | -using OneScript.Contexts; |
11 | | -using OneScript.Exceptions; |
12 | | -using OneScript.Values; |
13 | | -using ScriptEngine.Machine; |
14 | | -using ScriptEngine.Machine.Contexts; |
15 | | - |
16 | | -namespace OneScript.StandardLibrary.Collections.Indexes |
17 | | -{ |
18 | | - [ContextClass("ИндексКоллекции", "CollectionIndex")] |
19 | | - public class CollectionIndex : AutoCollectionContext<CollectionIndex, IValue> |
20 | | - { |
21 | | - private readonly List<IValue> _fields = new List<IValue>(); |
22 | | - private readonly IIndexCollectionSource _source; |
23 | | - |
24 | | - private readonly Dictionary<CollectionIndexKey, HashSet<IValue>> _data = |
25 | | - new Dictionary<CollectionIndexKey, HashSet<IValue>>(); |
26 | | - |
27 | | - public CollectionIndex(IIndexCollectionSource source, IEnumerable<IValue> fields) |
28 | | - { |
29 | | - foreach (var field in fields) |
| 1 | +/*---------------------------------------------------------- |
| 2 | +This Source Code Form is subject to the terms of the |
| 3 | +Mozilla Public License, v.2.0. If a copy of the MPL |
| 4 | +was not distributed with this file, You can obtain one |
| 5 | +at http://mozilla.org/MPL/2.0/. |
| 6 | +----------------------------------------------------------*/ |
| 7 | + |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using OneScript.Contexts; |
| 11 | +using OneScript.Exceptions; |
| 12 | +using OneScript.Values; |
| 13 | +using ScriptEngine.Machine; |
| 14 | +using ScriptEngine.Machine.Contexts; |
| 15 | + |
| 16 | +namespace OneScript.StandardLibrary.Collections.Indexes |
| 17 | +{ |
| 18 | + [ContextClass("ИндексКоллекции", "CollectionIndex")] |
| 19 | + public class CollectionIndex : AutoCollectionContext<CollectionIndex, IValue> |
| 20 | + { |
| 21 | + private readonly List<IValue> _fields = new List<IValue>(); |
| 22 | + private readonly IIndexCollectionSource _source; |
| 23 | + |
| 24 | + private readonly Dictionary<CollectionIndexKey, HashSet<IValue>> _data = |
| 25 | + new Dictionary<CollectionIndexKey, HashSet<IValue>>(); |
| 26 | + |
| 27 | + public CollectionIndex(IIndexCollectionSource source, IEnumerable<IValue> fields) |
| 28 | + { |
| 29 | + foreach (var field in fields) |
30 | 30 | { |
31 | 31 | if (field is ValueTable.ValueTableColumn column) |
32 | 32 | column.AddToIndex(); |
33 | 33 | _fields.Add(field); |
34 | | - } |
35 | | - |
36 | | - _source = source; |
37 | | - foreach (var value in _source) |
38 | | - { |
39 | | - ElementAdded(value); |
40 | 34 | } |
41 | | - } |
42 | | - |
43 | | - internal bool CanBeUsedFor(IEnumerable<IValue> searchFields) |
44 | | - { |
45 | | - return _fields.Count != 0 && _fields.All(f => searchFields.Contains(f)); |
46 | | - } |
47 | | - |
48 | | - private CollectionIndexKey IndexKey(PropertyNameIndexAccessor source) |
49 | | - { |
50 | | - return CollectionIndexKey.Extract(source, _fields); |
51 | | - } |
52 | | - |
53 | | - public override string ToString() |
54 | | - { |
55 | | - return string.Join(", ", _fields.Select(field => _source.GetName(field))); |
56 | | - } |
57 | | - |
58 | | - public IEnumerable<IValue> GetData(PropertyNameIndexAccessor searchCriteria) |
59 | | - { |
60 | | - var key = IndexKey(searchCriteria); |
61 | | - return _data.TryGetValue(key, out var filteredData) ? filteredData : Enumerable.Empty<IValue>(); |
62 | | - } |
63 | | - |
64 | | - internal void FieldRemoved(IValue field) |
65 | | - { |
66 | | - if (_fields.Contains(field)) |
67 | | - { |
| 35 | + |
| 36 | + _source = source; |
| 37 | + foreach (var value in _source) |
| 38 | + { |
| 39 | + ElementAdded(value); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + internal bool CanBeUsedFor(IEnumerable<IValue> searchFields) |
| 44 | + { |
| 45 | + return _fields.Count != 0 && _fields.All(f => searchFields.Contains(f)); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Поля, входящие в индекс (в порядке объявления индекса). |
| 50 | + /// </summary> |
| 51 | + internal IReadOnlyList<IValue> GetIndexedFields() => _fields; |
| 52 | + |
| 53 | + private CollectionIndexKey IndexKey(PropertyNameIndexAccessor source) |
| 54 | + { |
| 55 | + return CollectionIndexKey.Extract(source, _fields); |
| 56 | + } |
| 57 | + |
| 58 | + public override string ToString() |
| 59 | + { |
| 60 | + return string.Join(", ", _fields.Select(field => _source.GetName(field))); |
| 61 | + } |
| 62 | + |
| 63 | + public IEnumerable<IValue> GetData(PropertyNameIndexAccessor searchCriteria) |
| 64 | + { |
| 65 | + var key = IndexKey(searchCriteria); |
| 66 | + return _data.TryGetValue(key, out var filteredData) ? filteredData : Enumerable.Empty<IValue>(); |
| 67 | + } |
| 68 | + |
| 69 | + internal void FieldRemoved(IValue field) |
| 70 | + { |
| 71 | + if (_fields.Contains(field)) |
| 72 | + { |
68 | 73 | while (_fields.Contains(field)) |
69 | 74 | { |
70 | 75 | if (field is ValueTable.ValueTableColumn column) |
71 | 76 | column.DeleteFromIndex(); |
72 | 77 |
|
73 | 78 | _fields.Remove(field); |
74 | | - } |
75 | | - Rebuild(); |
76 | | - } |
77 | | - } |
78 | | - |
| 79 | + } |
| 80 | + Rebuild(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
79 | 84 | internal void ExcludeFields() |
80 | 85 | { |
81 | | - foreach (var field in _fields) |
| 86 | + foreach (var field in _fields) |
82 | 87 | { |
83 | 88 | if (field is ValueTable.ValueTableColumn column) |
84 | 89 | column.DeleteFromIndex(); |
85 | 90 | } |
86 | | - } |
87 | | - |
88 | | - internal void ElementAdded(PropertyNameIndexAccessor element) |
89 | | - { |
90 | | - var key = CollectionIndexKey.Extract(element, _fields); |
91 | | - if (_data.TryGetValue(key, out var set)) |
92 | | - { |
93 | | - set.Add(element); |
94 | | - } |
95 | | - else |
96 | | - { |
97 | | - _data.Add(key, new HashSet<IValue> { element }); |
98 | | - } |
99 | | - } |
100 | | - |
101 | | - internal void ElementRemoved(PropertyNameIndexAccessor element) |
102 | | - { |
103 | | - var key = CollectionIndexKey.Extract(element, _fields); |
104 | | - if (_data.TryGetValue(key, out var set)) |
105 | | - { |
106 | | - set.Remove(element); |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - internal void Clear() => _data.Clear(); |
111 | | - |
112 | | - internal void Rebuild() |
113 | | - { |
114 | | - _data.Clear(); |
115 | | - foreach (var value in _source) |
116 | | - { |
117 | | - ElementAdded(value); |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - public override IValue GetIndexedValue(IValue index) |
122 | | - { |
123 | | - if (index is BslNumericValue numericValue) |
124 | | - { |
125 | | - var numeric = numericValue.AsNumber(); |
126 | | - if (numeric >= 0 && numeric < _fields.Count) |
127 | | - { |
128 | | - |
129 | | - return ValueFactory.Create(_source.GetName(_fields[decimal.ToInt32(numeric)])); |
130 | | - } |
131 | | - } |
132 | | - throw RuntimeException.InvalidArgumentValue(); |
133 | | - } |
134 | | - |
135 | | - public override int Count() |
136 | | - { |
137 | | - return _fields.Count; |
138 | | - } |
139 | | - |
140 | | - public override IEnumerator<IValue> GetEnumerator() |
141 | | - { |
142 | | - foreach (var field in _fields) |
143 | | - { |
144 | | - yield return ValueFactory.Create(_source.GetName(field)); |
145 | | - } |
146 | | - } |
147 | | - } |
148 | | -} |
| 91 | + } |
| 92 | + |
| 93 | + internal void ElementAdded(PropertyNameIndexAccessor element) |
| 94 | + { |
| 95 | + var key = CollectionIndexKey.Extract(element, _fields); |
| 96 | + if (_data.TryGetValue(key, out var set)) |
| 97 | + { |
| 98 | + set.Add(element); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + _data.Add(key, new HashSet<IValue> { element }); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + internal void ElementRemoved(PropertyNameIndexAccessor element) |
| 107 | + { |
| 108 | + var key = CollectionIndexKey.Extract(element, _fields); |
| 109 | + if (_data.TryGetValue(key, out var set)) |
| 110 | + { |
| 111 | + set.Remove(element); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + internal void Clear() => _data.Clear(); |
| 116 | + |
| 117 | + internal void Rebuild() |
| 118 | + { |
| 119 | + _data.Clear(); |
| 120 | + foreach (var value in _source) |
| 121 | + { |
| 122 | + ElementAdded(value); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public override IValue GetIndexedValue(IValue index) |
| 127 | + { |
| 128 | + if (index is BslNumericValue numericValue) |
| 129 | + { |
| 130 | + var numeric = numericValue.AsNumber(); |
| 131 | + if (numeric >= 0 && numeric < _fields.Count) |
| 132 | + { |
| 133 | + |
| 134 | + return ValueFactory.Create(_source.GetName(_fields[decimal.ToInt32(numeric)])); |
| 135 | + } |
| 136 | + } |
| 137 | + throw RuntimeException.InvalidArgumentValue(); |
| 138 | + } |
| 139 | + |
| 140 | + public override int Count() |
| 141 | + { |
| 142 | + return _fields.Count; |
| 143 | + } |
| 144 | + |
| 145 | + public override IEnumerator<IValue> GetEnumerator() |
| 146 | + { |
| 147 | + foreach (var field in _fields) |
| 148 | + { |
| 149 | + yield return ValueFactory.Create(_source.GetName(field)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments