Skip to content

Commit

Permalink
feat: std::map impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed Jun 3, 2024
1 parent c344681 commit 35865a1
Show file tree
Hide file tree
Showing 12 changed files with 737 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Unmanaged/ICppInstanceValueType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Hosihikari.NativeInterop.Unmanaged;

public interface ICppInstanceValueType<TSelf>
where TSelf : unmanaged
{
}

public interface ICopyableCppInstanceValueType<TSelf> : ICppInstanceValueType<TSelf>
where TSelf : unmanaged
{
TSelf Copy();
}
13 changes: 13 additions & 0 deletions src/Unmanaged/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ public static Reference<T> DAccessAsReference<T>(nint address, int offset)
{
return (void*)((nint)address + offset);
}

public static int Memcmp(void* buf1, void* buf2, ulong count)
{
if (count is 0) return 0;

while (--count is not 0 && *(byte*)buf1 == *(byte*)buf2)
{
buf1 = (char*)buf1 + 1;
buf2 = (char*)buf2 + 1;
}

return *(byte*)buf1 - *(byte*)buf2;
}
}
12 changes: 12 additions & 0 deletions src/Unmanaged/NativeAlloc.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using Hosihikari.NativeInterop.Import;
using System.Runtime.CompilerServices;

namespace Hosihikari.NativeInterop.Unmanaged;

public readonly unsafe ref struct NativeAlloc<T> where T : unmanaged
{
private static readonly ulong s_size = (ulong)sizeof(T);

public static T* New()
{
T* ptr = (T*)LibNative.operator_new(s_size);
return ptr;
}

public static T* New(in T val)
{
T* ptr = (T*)LibNative.operator_new(s_size);
Expand All @@ -25,6 +32,11 @@ namespace Hosihikari.NativeInterop.Unmanaged;
return ptr;
}

public static ref T NewAsRef()
{
return ref *New();
}

public static ref T NewAsRef(in T val)
{
return ref *New(val);
Expand Down
13 changes: 13 additions & 0 deletions src/Unmanaged/STL/Interface/IStdIterable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Hosihikari.NativeInterop.Unmanaged.STL.Interface;

internal interface IStdIterable<TIterator, T>
where TIterator : unmanaged, IStdIterator<TIterator, T>
where T : unmanaged
{
public TIterator Begin();
public TIterator End();

public ReverseIterator<TIterator, T> RBegin();

public ReverseIterator<TIterator, T> REnd();
}
14 changes: 14 additions & 0 deletions src/Unmanaged/STL/Interface/IStdIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

using System.Numerics;

namespace Hosihikari.NativeInterop.Unmanaged.STL.Interface;

public interface IStdIterator<TIterator, T> :
IEqualityOperators<TIterator, TIterator, bool>,
IIncrementOperators<TIterator>,
IDecrementOperators<TIterator>
where TIterator : unmanaged, IStdIterator<TIterator, T>
where T : unmanaged
{
public ref T Target { get; }
}
46 changes: 46 additions & 0 deletions src/Unmanaged/STL/ReverseIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Hosihikari.NativeInterop.Unmanaged.STL.Interface;
using System.Numerics;

namespace Hosihikari.NativeInterop.Unmanaged.STL;

public struct ReverseIterator<TIterator, T>(TIterator current) :
IStdIterator<ReverseIterator<TIterator, T>, T>,
IIncrementOperators<ReverseIterator<TIterator, T>>,
IDecrementOperators<ReverseIterator<TIterator, T>>,
IEqualityOperators<ReverseIterator<TIterator, T>, ReverseIterator<TIterator, T>, bool>
where T : unmanaged
where TIterator :
unmanaged,
IStdIterator<TIterator, T>,
IIncrementOperators<TIterator>,
IDecrementOperators<TIterator>,
IEqualityOperators<TIterator, TIterator, bool>
{
private TIterator current = current;

public readonly TIterator Base
{
get => current;
}

public ref T Target => ref current.Target;

public readonly override bool Equals(object? obj)
{
return obj is ReverseIterator<TIterator, T> iterator &&
EqualityComparer<TIterator>.Default.Equals(current, iterator.current);
}

public readonly override int GetHashCode()
{
return HashCode.Combine(current);
}

public static ReverseIterator<TIterator, T> operator ++(ReverseIterator<TIterator, T> value) => new(--value.current);

public static ReverseIterator<TIterator, T> operator --(ReverseIterator<TIterator, T> value) => new(++value.current);

public static bool operator ==(ReverseIterator<TIterator, T> left, ReverseIterator<TIterator, T> right) => left.current == right.current;

public static bool operator !=(ReverseIterator<TIterator, T> left, ReverseIterator<TIterator, T> right) => left.current != right.current;
}
20 changes: 20 additions & 0 deletions src/Unmanaged/STL/StdLess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Hosihikari.NativeInterop.Unmanaged.STL;


public readonly ref struct StdLess<T>
where T : unmanaged, IComparisonOperators<T, T, bool>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Compare(in T left, in T right) => left < right;
}

public readonly ref struct StdLess
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Compare<T>(in T left, in T right)
where T : unmanaged, IComparisonOperators<T, T, bool>
=> left < right;
}
Loading

0 comments on commit 35865a1

Please sign in to comment.