Skip to content

Commit a4dca87

Browse files
committed
Open-code collection data structure rather than using Dictionary+List
This rewrites the core of the type to be based on a custom data structure rather than wrapping Dictionary and List. The core structure is based on both Dictionary in corelib and the OrderedDictionary prototype in corefxlab. This also adds missing TryAdd, Capacity, EnsureCapacity, and TrimExcess members for parity with Dictionary, and fixes debugger views for the Key/ValueCollections.
1 parent a3f12a1 commit a4dca87

9 files changed

+1076
-246
lines changed

src/libraries/System.Collections/ref/System.Collections.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public OrderedDictionary(System.Collections.Generic.IEnumerable<System.Collectio
116116
public OrderedDictionary(System.Collections.Generic.IEqualityComparer<TKey>? comparer) { }
117117
public OrderedDictionary(int capacity) { }
118118
public OrderedDictionary(int capacity, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { }
119+
public int Capacity { get { throw null; } }
119120
public System.Collections.Generic.IEqualityComparer<TKey> Comparer { get { throw null; } }
120121
public int Count { get { throw null; } }
121122
public TValue this[TKey key] { get { throw null; } set { } }
@@ -142,6 +143,7 @@ public void Add(TKey key, TValue value) { }
142143
public void Clear() { }
143144
public bool ContainsKey(TKey key) { throw null; }
144145
public bool ContainsValue(TValue value) { throw null; }
146+
public int EnsureCapacity(int capacity) { throw null; }
145147
public System.Collections.Generic.KeyValuePair<TKey, TValue> GetAt(int index) { throw null; }
146148
public System.Collections.Generic.OrderedDictionary<TKey, TValue>.Enumerator GetEnumerator() { throw null; }
147149
public int IndexOf(TKey key) { throw null; }
@@ -170,6 +172,8 @@ void System.Collections.IDictionary.Remove(object key) { }
170172
void System.Collections.IList.Insert(int index, object? value) { }
171173
void System.Collections.IList.Remove(object? value) { }
172174
public void TrimExcess() { }
175+
public void TrimExcess(int capacity) { }
176+
public bool TryAdd(TKey key, TValue value) { throw null; }
173177
public bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; }
174178
public partial struct Enumerator : System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey, TValue>>, System.Collections.IDictionaryEnumerator, System.Collections.IEnumerator, System.IDisposable
175179
{

src/libraries/System.Collections/src/System.Collections.csproj

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010

1111
<ItemGroup>
1212
<Compile Include="System\Collections\BitArray.cs" />
13-
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\DebugViewDictionaryItem.cs"
14-
Link="Common\System\Collections\Generic\DebugViewDictionaryItem.cs" />
15-
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ICollectionDebugView.cs"
16-
Link="Common\System\Collections\Generic\ICollectionDebugView.cs" />
17-
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\IDictionaryDebugView.cs"
18-
Link="Common\System\Collections\Generic\IDictionaryDebugView.cs" />
19-
<Compile Include="$(CoreLibSharedDir)System\Collections\ObjectModel\CollectionHelpers.cs"
20-
Link="Common\System\Collections\ObjectModel\CollectionHelpers.cs" />
13+
<Compile Include="System\Collections\ThrowHelper.cs" />
14+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\DebugViewDictionaryItem.cs" Link="Common\System\Collections\Generic\DebugViewDictionaryItem.cs" />
15+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ICollectionDebugView.cs" Link="Common\System\Collections\Generic\ICollectionDebugView.cs" />
16+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\IDictionaryDebugView.cs" Link="Common\System\Collections\Generic\IDictionaryDebugView.cs" />
17+
<Compile Include="$(CoreLibSharedDir)System\Collections\ObjectModel\CollectionHelpers.cs" Link="Common\System\Collections\ObjectModel\CollectionHelpers.cs" />
2118
<Compile Include="System\Collections\Generic\LinkedList.cs" />
2219
<Compile Include="System\Collections\Generic\OrderedDictionary.cs" />
2320
<Compile Include="System\Collections\Generic\PriorityQueue.cs" />
@@ -32,14 +29,10 @@
3229
<Compile Include="System\Collections\Generic\StackDebugView.cs" />
3330
<Compile Include="System\Collections\StructuralComparisons.cs" />
3431
<!-- Shared Common -->
35-
<Compile Include="$(CoreLibSharedDir)System\Collections\HashHelpers.cs"
36-
Link="Common\System\Collections\HashHelpers.cs" />
37-
<Compile Include="$(CommonPath)System\Collections\Generic\BitHelper.cs"
38-
Link="Common\System\Collections\Generic\BitHelper.cs" />
39-
<Compile Include="$(CommonPath)System\Collections\Generic\EnumerableHelpers.cs"
40-
Link="Common\System\Collections\Generic\EnumerableHelpers.cs" />
41-
<Compile Include="$(CommonPath)System\Obsoletions.cs"
42-
Link="Common\System\Obsoletions.cs" />
32+
<Compile Include="$(CoreLibSharedDir)System\Collections\HashHelpers.cs" Link="Common\System\Collections\HashHelpers.cs" />
33+
<Compile Include="$(CommonPath)System\Collections\Generic\BitHelper.cs" Link="Common\System\Collections\Generic\BitHelper.cs" />
34+
<Compile Include="$(CommonPath)System\Collections\Generic\EnumerableHelpers.cs" Link="Common\System\Collections\Generic\EnumerableHelpers.cs" />
35+
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="Common\System\Obsoletions.cs" />
4336
</ItemGroup>
4437

4538
<ItemGroup>

0 commit comments

Comments
 (0)