Skip to content

Commit e4fd7fb

Browse files
committed
ok, type level stuff
1 parent af3e491 commit e4fd7fb

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/FastExpressionCompiler/ImTools.cs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ internal static ref T ThrowIndexOutOfBounds<T>(int index, int capacity) =>
216216
throw new IndexOutOfRangeException($"Index {index} is out of range for Stack{capacity}<{typeof(T)},..>.");
217217
}
218218

219+
public interface IStack<T, TSize, TStack> : IStack<T, TStack>
220+
where TSize : struct, ISize
221+
where TStack : struct, IStack<T, TSize, TStack>
222+
{
223+
}
224+
219225
/// <summary>Abstracts over collection of the items on stack of the fixed Capacity,
220226
/// to be used as a part of the hybrid data structures which grow from stack to heap</summary>
221227
public interface IStack<T, TStack>
@@ -253,17 +259,33 @@ public interface ISize8Plus : ISize4Plus { }
253259
public interface ISize16Plus : ISize8Plus { }
254260

255261
/// <summary>Marker for collection or container holding 4 items</summary>
256-
public interface ISize2 : ISize2Plus { }
262+
public struct Size2 : ISize2Plus
263+
{
264+
/// <summary>Returns the size of the collection or container</summary>
265+
public int Size => 2;
266+
}
257267
/// <summary>Marker for collection or container holding 4 items</summary>
258-
public interface ISize4 : ISize4Plus { }
268+
public struct Size4 : ISize4Plus
269+
{
270+
/// <summary>Returns the size of the collection or container</summary>
271+
public int Size => 4;
272+
}
259273
/// <summary>Marker for collection or container holding 8 items</summary>
260-
public interface ISize8 : ISize8Plus { }
274+
public struct Size8 : ISize8Plus
275+
{
276+
/// <summary>Returns the size of the collection or container</summary>
277+
public int Size => 8;
278+
}
261279
/// <summary>Marker for collection or container holding 16 items</summary>
262-
public interface ISize16 : ISize16Plus { }
280+
public struct Size16 : ISize16Plus
281+
{
282+
/// <summary>Returns the size of the collection or container</summary>
283+
public int Size => 16;
284+
}
263285

264286
/// <summary>Implementation of `IStack` for 2 items on stack</summary>
265287
[StructLayout(LayoutKind.Sequential, Pack = 1)]
266-
public struct Stack2<T> : IStack<T, Stack2<T>>, ISize2
288+
public struct Stack2<T> : IStack<T, Size2, Stack2<T>>
267289
{
268290
/// <inheritdoc/>
269291
public int Capacity => 2;
@@ -308,7 +330,7 @@ public ref T this[int index]
308330

309331
/// <summary>Implementation of `IStack` for 4 items on stack</summary>
310332
[StructLayout(LayoutKind.Sequential, Pack = 1)]
311-
public struct Stack4<T> : IStack<T, Stack4<T>>, ISize4
333+
public struct Stack4<T> : IStack<T, Size4, Stack4<T>>
312334
{
313335
/// <inheritdoc/>
314336
public int Capacity => 4;
@@ -355,7 +377,7 @@ public ref T this[int index]
355377

356378
/// <summary>Implementation of `IStack` for 8 items on stack</summary>
357379
[StructLayout(LayoutKind.Sequential, Pack = 1)]
358-
public struct Stack8<T> : IStack<T, Stack8<T>>, ISize8
380+
public struct Stack8<T> : IStack<T, Size8, Stack8<T>>
359381
{
360382
/// <inheritdoc/>
361383
public int Capacity => 8;
@@ -406,7 +428,7 @@ public ref T this[int index]
406428

407429
/// <summary>Implementation of `IStack` for 16 items on stack</summary>
408430
[StructLayout(LayoutKind.Sequential, Pack = 1)]
409-
public struct Stack16<T> : IStack<T, Stack16<T>>, ISize16
431+
public struct Stack16<T> : IStack<T, Size16, Stack16<T>>
410432
{
411433
/// <inheritdoc/>
412434
public int Capacity => 16;
@@ -791,7 +813,11 @@ public int GetHashCode((A, B, C) key) =>
791813

792814
/// <summary>Add the Infer parameter to `T Method<T>(..., Infer{T} _)` to enable type inference for T,
793815
/// by calling it as `var t = Method(..., default(Infer{T}))`</summary>
794-
public interface Use<T> { }
816+
public class Use<T>
817+
{
818+
public static readonly Use<T> It = new Use<T>();
819+
private Use() { }
820+
}
795821

796822
/// <summary>Configuration and the tools for the SmallMap and friends</summary>
797823
public static class SmallMap
@@ -946,18 +972,15 @@ public static ref TEntry TryGetEntryRef_loop<K, TEntry, TEq, TStackHashes, TStac
946972
}
947973

948974
/// <summary>Lookup for the K in the TStackEntries, first by calculating it hash with TEq and searching the hash in the TStackHashes</summary>
949-
public static ref TEntry TryGetEntryRef_ILP<K, TEntry, TEq, TStackHashes, TStackEntries>(
975+
public static ref TEntry TryGetEntryRef_ILP<K, TEntry, TEq, TStackHashes, TStackEntries, TCap>(
950976
this ref TStackEntries entries, ref TStackHashes hashes, K key, out bool found,
951-
TEq eq = default, Use<TEntry> _ = default)//, Use<TCap> _cap = default)
977+
TEq eq = default, Use<TEntry> _ = default, Use<TCap> _cap = default)
952978
where TEntry : struct, IEntry<K>
953979
where TEq : struct, IEq<K>
954-
where TStackHashes : struct, IStack<int, TStackHashes>//, TCap
955-
where TStackEntries : struct, IStack<TEntry, TStackEntries>//, TCap
956-
// where TCap : ISize4Plus
980+
where TStackHashes : struct, IStack<int, TCap, TStackHashes>
981+
where TStackEntries : struct, IStack<TEntry, TCap, TStackEntries>
982+
where TCap : struct, ISize4Plus
957983
{
958-
Debug.Assert(hashes.Capacity == entries.Capacity,
959-
"Expecting that the hashes and entries stacks have the same capacity");
960-
961984
var hash = eq.GetHashCode(key);
962985

963986
for (var i = 0; i < hashes.Capacity; i += 4)

test/FastExpressionCompiler.IssueTests/Issue476_System_ExecutionEngineException_with_nullables_on_repeated_calls_to_ConcurrentDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void TestSmallMap_Lookup_ILP(TestContext t)
105105
{
106106
ref var e = ref entries.TryGetEntryRef_ILP(
107107
ref hashes, i, out var found, default(IntEq),
108-
default(Use<SmallMap.Entry<int>>));
108+
Use<SmallMap.Entry<int>>.It, Use<Size8>.It);
109109
if (found)
110110
sum += e.Key;
111111
}

0 commit comments

Comments
 (0)