Skip to content

Commit

Permalink
Open-code collection data structure rather than using Dictionary+List
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephentoub committed Jun 13, 2024
1 parent a3f12a1 commit a4dca87
Show file tree
Hide file tree
Showing 9 changed files with 1,076 additions and 246 deletions.
4 changes: 4 additions & 0 deletions src/libraries/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public OrderedDictionary(System.Collections.Generic.IEnumerable<System.Collectio
public OrderedDictionary(System.Collections.Generic.IEqualityComparer<TKey>? comparer) { }
public OrderedDictionary(int capacity) { }
public OrderedDictionary(int capacity, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { }
public int Capacity { get { throw null; } }
public System.Collections.Generic.IEqualityComparer<TKey> Comparer { get { throw null; } }
public int Count { get { throw null; } }
public TValue this[TKey key] { get { throw null; } set { } }
Expand All @@ -142,6 +143,7 @@ public void Add(TKey key, TValue value) { }
public void Clear() { }
public bool ContainsKey(TKey key) { throw null; }
public bool ContainsValue(TValue value) { throw null; }
public int EnsureCapacity(int capacity) { throw null; }
public System.Collections.Generic.KeyValuePair<TKey, TValue> GetAt(int index) { throw null; }
public System.Collections.Generic.OrderedDictionary<TKey, TValue>.Enumerator GetEnumerator() { throw null; }
public int IndexOf(TKey key) { throw null; }
Expand Down Expand Up @@ -170,6 +172,8 @@ void System.Collections.IDictionary.Remove(object key) { }
void System.Collections.IList.Insert(int index, object? value) { }
void System.Collections.IList.Remove(object? value) { }
public void TrimExcess() { }
public void TrimExcess(int capacity) { }
public bool TryAdd(TKey key, TValue value) { throw null; }
public bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; }
public partial struct Enumerator : System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey, TValue>>, System.Collections.IDictionaryEnumerator, System.Collections.IEnumerator, System.IDisposable
{
Expand Down
25 changes: 9 additions & 16 deletions src/libraries/System.Collections/src/System.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@

<ItemGroup>
<Compile Include="System\Collections\BitArray.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\DebugViewDictionaryItem.cs"
Link="Common\System\Collections\Generic\DebugViewDictionaryItem.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ICollectionDebugView.cs"
Link="Common\System\Collections\Generic\ICollectionDebugView.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\IDictionaryDebugView.cs"
Link="Common\System\Collections\Generic\IDictionaryDebugView.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\ObjectModel\CollectionHelpers.cs"
Link="Common\System\Collections\ObjectModel\CollectionHelpers.cs" />
<Compile Include="System\Collections\ThrowHelper.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\DebugViewDictionaryItem.cs" Link="Common\System\Collections\Generic\DebugViewDictionaryItem.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ICollectionDebugView.cs" Link="Common\System\Collections\Generic\ICollectionDebugView.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\IDictionaryDebugView.cs" Link="Common\System\Collections\Generic\IDictionaryDebugView.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\ObjectModel\CollectionHelpers.cs" Link="Common\System\Collections\ObjectModel\CollectionHelpers.cs" />
<Compile Include="System\Collections\Generic\LinkedList.cs" />
<Compile Include="System\Collections\Generic\OrderedDictionary.cs" />
<Compile Include="System\Collections\Generic\PriorityQueue.cs" />
Expand All @@ -32,14 +29,10 @@
<Compile Include="System\Collections\Generic\StackDebugView.cs" />
<Compile Include="System\Collections\StructuralComparisons.cs" />
<!-- Shared Common -->
<Compile Include="$(CoreLibSharedDir)System\Collections\HashHelpers.cs"
Link="Common\System\Collections\HashHelpers.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\BitHelper.cs"
Link="Common\System\Collections\Generic\BitHelper.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\EnumerableHelpers.cs"
Link="Common\System\Collections\Generic\EnumerableHelpers.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\HashHelpers.cs" Link="Common\System\Collections\HashHelpers.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\BitHelper.cs" Link="Common\System\Collections\Generic\BitHelper.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\EnumerableHelpers.cs" Link="Common\System\Collections\Generic\EnumerableHelpers.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="Common\System\Obsoletions.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a4dca87

Please sign in to comment.