Skip to content

Commit 224b8bc

Browse files
committed
подсчет вхождений Колонок в Индекс: корректное удаление
1 parent aad5791 commit 224b8bc

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/OneScript.StandardLibrary/Collections/Indexes/CollectionIndex.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace OneScript.StandardLibrary.Collections.Indexes
1818
[ContextClass("ИндексКоллекции", "CollectionIndex")]
1919
public class CollectionIndex : AutoCollectionContext<CollectionIndex, IValue>
2020
{
21-
readonly List<IValue> _fields = new List<IValue>();
21+
private readonly List<IValue> _fields = new List<IValue>();
2222
private readonly IIndexCollectionSource _source;
2323

2424
private readonly Dictionary<CollectionIndexKey, HashSet<IValue>> _data =
@@ -29,7 +29,7 @@ public CollectionIndex(IIndexCollectionSource source, IEnumerable<IValue> fields
2929
foreach (var field in fields)
3030
{
3131
if (field is ValueTable.ValueTableColumn column)
32-
column.IsIndexable = true;
32+
column.AddToIndex();
3333
_fields.Add(field);
3434
}
3535

@@ -65,11 +65,26 @@ internal void FieldRemoved(IValue field)
6565
{
6666
if (_fields.Contains(field))
6767
{
68-
while (_fields.Contains(field)) _fields.Remove(field);
68+
while (_fields.Contains(field))
69+
{
70+
if (field is ValueTable.ValueTableColumn column)
71+
column.DeleteFromIndex();
72+
73+
_fields.Remove(field);
74+
}
6975
Rebuild();
7076
}
7177
}
7278

79+
internal void ExcludeFields()
80+
{
81+
foreach (var field in _fields)
82+
{
83+
if (field is ValueTable.ValueTableColumn column)
84+
column.AddToIndex();
85+
}
86+
}
87+
7388
internal void ElementAdded(PropertyNameIndexAccessor element)
7489
{
7590
var key = CollectionIndexKey.Extract(element, _fields);

src/OneScript.StandardLibrary/Collections/ValueTable/CollectionIndexes.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ public override int Count()
4545
[ContextMethod("Удалить", "Delete")]
4646
public void Delete(IValue index)
4747
{
48-
_indexes.Remove(GetIndex(index));
48+
var idx = GetIndex(index);
49+
idx.ExcludeFields();
50+
_indexes.Remove(idx);
4951
}
5052

5153
[ContextMethod("Очистить", "Clear")]
5254
public void Clear()
5355
{
56+
foreach (var idx in _indexes)
57+
idx.ExcludeFields();
58+
5459
_indexes.Clear();
5560
}
5661

src/OneScript.StandardLibrary/Collections/ValueTable/ValueTableColumn.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ namespace OneScript.StandardLibrary.Collections.ValueTable
2020
public class ValueTableColumn : AutoContext<ValueTableColumn>
2121
{
2222
private string _title;
23-
private string _name;
23+
private string _name;
2424
private readonly TypeDescription _valueType;
2525
private readonly WeakReference _owner;
2626

27-
public bool IsIndexable { get; internal set; }
28-
27+
private int _indicesCount = 0;
28+
public bool IsIndexable => _indicesCount != 0;
29+
public void AddToIndex() { _indicesCount++; }
30+
public void DeleteFromIndex() { if (_indicesCount != 0) _indicesCount--; }
31+
2932
public ValueTableColumn(ValueTableColumnCollection owner, string name, string title, TypeDescription type, int width)
3033
{
3134
_name = name;
3235
_title = title;
3336
_valueType = type ?? new TypeDescription();
3437
Width = width;
35-
IsIndexable = false;
3638

3739
_owner = new WeakReference(owner);
3840
}

0 commit comments

Comments
 (0)