-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathKeyCollection.cs
More file actions
104 lines (86 loc) · 2.35 KB
/
KeyCollection.cs
File metadata and controls
104 lines (86 loc) · 2.35 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Runtime.Collections;
namespace Waher.Persistence.Files
{
internal class KeyCollection : ICollection<string>
{
private readonly StringDictionary dictionary;
public KeyCollection(StringDictionary Dictionary)
{
this.dictionary = Dictionary;
}
public int Count => this.dictionary.Count;
public bool IsReadOnly => true;
public void Add(string item)
{
throw new NotSupportedException("Collection is read-only.");
}
public void Clear()
{
throw new NotSupportedException("Collection is read-only.");
}
public bool Contains(string item)
{
return this.dictionary.ContainsKey(item);
}
public void CopyTo(string[] array, int arrayIndex)
{
Task Task = this.CopyToAsync(array, arrayIndex);
FilesProvider.Wait(Task, this.dictionary.DictionaryFile.TimeoutMilliseconds);
}
/// <summary>
/// Copies the keys of the dicitionary to an array.
/// </summary>
/// <param name="array">Array</param>
/// <param name="arrayIndex">Start index</param>
public async Task CopyToAsync(string[] array, int arrayIndex)
{
await this.dictionary.DictionaryFile.BeginRead();
try
{
ObjectBTreeFileCursor<KeyValuePair<string, object>> e = await this.dictionary.GetEnumeratorLocked();
while (await e.MoveNextAsyncLocked())
array[arrayIndex++] = e.Current.Key;
}
finally
{
await this.dictionary.DictionaryFile.EndRead();
}
}
public IEnumerator<string> GetEnumerator()
{
return new KeyEnumeration(this.dictionary.GetEnumerator());
}
public bool Remove(string item)
{
return this.dictionary.Remove(item);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new KeyEnumeration(this.dictionary.GetEnumerator());
}
/// <summary>
/// Gets all keys.
/// </summary>
/// <returns>Array of keys.</returns>
public async Task<string[]> GetKeysAsync()
{
ChunkedList<string> Result = new ChunkedList<string>();
await this.dictionary.DictionaryFile.BeginRead();
try
{
ObjectBTreeFileCursor<KeyValuePair<string, object>> e = await this.dictionary.GetEnumeratorLocked();
while (await e.MoveNextAsyncLocked())
Result.Add(e.Current.Key);
}
finally
{
await this.dictionary.DictionaryFile.EndRead();
}
return Result.ToArray();
}
}
}