forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayDictionary{TKey,TValue}.Collection.cs
103 lines (79 loc) · 3.21 KB
/
ArrayDictionary{TKey,TValue}.Collection.cs
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
using System.Collections.Generic;
namespace System.Collections.ArrayBased
{
public partial class ArrayDictionary<TKey, TValue>
{
public Collection AsCollection()
=> new Collection(this);
public static explicit operator Collection(ArrayDictionary<TKey, TValue> source)
=> new Collection(source);
public readonly struct Collection : ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
{
private readonly ArrayDictionary<TKey, TValue> source;
public Collection(ArrayDictionary<TKey, TValue> source)
{
this.source = source ?? throw new ArgumentNullException(nameof(source));
}
public int Count
{
get
{
unchecked
{
return (int)this.source.Count;
}
}
}
public bool IsReadOnly => false;
public void Add(KeyValuePair<TKey, TValue> item)
=> this.source.Add(item.Key, item.Value);
public void Clear()
=> this.source.Clear();
public bool Contains(KeyValuePair<TKey, TValue> item)
{
if (!this.source.TryGetValue(item.Key, out var value))
return false;
return EqualityComparer<TValue>.Default.Equals(value, item.Value);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
=> this.source.CopyTo(array, arrayIndex);
public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (!this.source.TryGetValue(item.Key, out var value))
return false;
if (!EqualityComparer<TValue>.Default.Equals(value, item.Value))
return false;
this.source.Remove(item.Key);
return true;
}
public Enumerator GetEnumerator()
=> new Enumerator(this.source);
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public readonly struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
{
private readonly ArrayDictionary<TKey, TValue>.Enumerator source;
public Enumerator(ArrayDictionary<TKey, TValue> source)
{
this.source = (source ?? Empty).GetEnumerator();
}
public KeyValuePair<TKey, TValue> Current
{
get => this.source.Current;
}
object IEnumerator.Current
{
get => this.Current;
}
public void Dispose()
=> this.source.Dispose();
public bool MoveNext()
=> this.source.MoveNext();
public void Reset()
=> this.source.Reset();
}
}
}
}