Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/Box2D.NET/B2Arrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using static Box2D.NET.B2Constants;
using static Box2D.NET.B2Cores;

Expand Down Expand Up @@ -35,7 +37,11 @@ public static class B2Arrays
/* Get */
public static ref T b2Array_Get<T>(ref B2Array<T> a, int index)
{
Debug.Assert(0 <= index && index < a.count);
if (0 > index || index >= a.count)
{
throw new IndexOutOfRangeException($"Index is out of range - count({a.count}) index({index})");
}

return ref a.data[index];
}

Expand Down Expand Up @@ -68,20 +74,24 @@ public static ref T b2Array_Get<T>(ref B2Array<T> a, int index)
/* Set */
public static void b2Array_Set<T>(ref B2Array<T> a, int index, T value)
{
Debug.Assert(0 <= index && index < a.count);
a.data[index] = value;
ref T v = ref b2Array_Get(ref a, index);
v = value;
}

/* RemoveSwap */
public static int b2Array_RemoveSwap<T>(ref B2Array<T> a, int index) where T : new()
{
Debug.Assert(0 <= index && index < a.count);
if (0 > index || index >= a.count)
{
throw new IndexOutOfRangeException($"Index is out of range - count({a.count}) index({index})");
}

int movedIndex = B2_NULL_INDEX;
if (index != a.count - 1)
{
movedIndex = a.count - 1;
a.data[index] = a.data[movedIndex];

// fixed, ikpil
if (!typeof(T).IsValueType)
{
Expand All @@ -96,14 +106,19 @@ public static void b2Array_Set<T>(ref B2Array<T> a, int index, T value)
/* Pop */
public static T b2Array_Pop<T>(ref B2Array<T> a) where T : new()
{
Debug.Assert(a.count > 0);
if (0 >= a.count)
{
throw new IndexOutOfRangeException($"Index is out of range - count({a.count})");
}

T value = a.data[a.count - 1];

// fixed, ikpil
if (!typeof(T).IsValueType)
{
a.data[a.count - 1] = new T();
}

a.count -= 1;
return value;
}
Expand All @@ -117,8 +132,11 @@ public static void b2Array_Clear<T>(ref B2Array<T> a)
/* ByteCount */
public static int b2Array_ByteCount<T>(ref B2Array<T> a)
{
// TODO: @ikpil, check
//return (int)( a.capacity * sizeof( T ) );
if (typeof(T).IsValueType)
{
return a.capacity * Marshal.SizeOf<T>();
}

return -1;
}

Expand Down Expand Up @@ -165,4 +183,4 @@ public static void b2Array_Destroy<T>(ref B2Array<T> a)
a.capacity = 0;
}
}
}
}
Loading
Loading