Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add type-safe array copy function #17

Merged
merged 1 commit into from
Nov 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public bool Contains(T item)
public void CopyTo(T[] array, int arrayIndex)
{
var self = this;
Array.Copy(self._array!, 0, array, arrayIndex, self.Length);
RcArrays.Copy(self._array!, 0, array, arrayIndex, self.Length);
}

public void Add(T item)
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Core/Collections/RcImmutableArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static RcImmutableArray<T> Create<T>(params T[] items)
}

var tmp = new T[items.Length];
Array.Copy(items, tmp, items.Length);
RcArrays.Copy(items, tmp, items.Length);
return new RcImmutableArray<T>(tmp);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/DotRecast.Core/Compression/FastLZ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static long DecompressLevel1(byte[] input, long inputOffset, long length,
return 0;
}

Array.Copy(input, ip, output, op, ctrl);
RcArrays.Copy(input, ip, output, op, ctrl);
ip += ctrl;
op += ctrl;
}
Expand Down Expand Up @@ -452,7 +452,7 @@ public static long DecompressLevel2(byte[] input, long inputOffset, long length,
return 0;
}

Array.Copy(input, ip, output, op, ctrl);
RcArrays.Copy(input, ip, output, op, ctrl);
ip += ctrl;
op += ctrl;
}
Expand Down Expand Up @@ -498,16 +498,16 @@ public static void SmallCopy(byte[] dest, long destOffset, byte[] src, long srcO
// if (count >= 4)
// {
// count -= count % 4;
// Array.Copy(src, srcOffset, dest, destOffset, count);
// RcArrays.Copy(src, srcOffset, dest, destOffset, count);
// }
Array.Copy(src, srcOffset, dest, destOffset, count);
RcArrays.Copy(src, srcOffset, dest, destOffset, count);
}

// special case of memcpy: exactly MAX_COPY bytes
// flz_maxcopy
static void MaxCopy(byte[] dest, long destOffset, byte[] src, long secOffset)
{
Array.Copy(src, secOffset, dest, destOffset, MAX_COPY);
RcArrays.Copy(src, secOffset, dest, destOffset, MAX_COPY);
}

// flz_literals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
using System;
using System.Runtime.CompilerServices;

namespace DotRecast.Core
{
public static class RcArrayUtils
public static class RcArrays
{
// Type Safe Copy
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy<T>(T[] sourceArray, long sourceIndex, T[] destinationArray, long destinationIndex, long length)
{
Array.Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
}

// Type Safe Copy
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy<T>(T[] sourceArray, T[] destinationArray, long length)
{
Array.Copy(sourceArray, destinationArray, length);
}

public static T[] CopyOf<T>(T[] source, int startIdx, int length)
{
var deatArr = new T[length];
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour.Crowd/DtCrowd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ private void PlanVelocity(DtCrowdAgentDebugInfo debug, IList<DtCrowdAgent> agent
{
RcVec3f[] s = ag.boundary.GetSegment(j);
RcVec3f s3 = s[1];
//Array.Copy(s, 3, s3, 0, 3);
//RcArrays.Copy(s, 3, s3, 0, 3);
if (DtUtils.TriArea2D(ag.npos, s[0], s3) < 0.0f)
{
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour.Crowd/DtLocalBoundary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void AddSegment(float dist, RcSegmentVert s)
DtSegment seg = new DtSegment();
seg.s[0] = s.vmin;
seg.s[1] = s.vmax;
//Array.Copy(s, seg.s, 6);
//RcArrays.Copy(s, seg.s, 6);
seg.d = dist;
if (0 == m_segs.Count)
{
Expand Down
4 changes: 2 additions & 2 deletions src/DotRecast.Detour.Extras/Jumplink/JumpLinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private List<JumpLink> BuildJumpLinks(JumpLinkBuilderConfig acfg, EdgeSampler es
{
JumpLink link = new JumpLink();
links.Add(link);
link.startSamples = RcArrayUtils.CopyOf(es.start.gsamples, js.startSample, js.samples);
link.endSamples = RcArrayUtils.CopyOf(end.gsamples, js.startSample, js.samples);
link.startSamples = RcArrays.CopyOf(es.start.gsamples, js.startSample, js.samples);
link.endSamples = RcArrays.CopyOf(end.gsamples, js.startSample, js.samples);
link.start = es.start;
link.end = end;
link.trajectory = es.trajectory;
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour.Extras/Jumplink/JumpSegmentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class JumpSegmentBuilder
public JumpSegment[] Build(JumpLinkBuilderConfig acfg, EdgeSampler es)
{
int n = es.end[0].gsamples.Length;
int[][] sampleGrid = RcArrayUtils.Of<int>(n, es.end.Count);
int[][] sampleGrid = RcArrays.Of<int>(n, es.end.Count);
for (int j = 0; j < es.end.Count; j++)
{
for (int i = 0; i < n; i++)
Expand Down
6 changes: 3 additions & 3 deletions src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public void Build(GraphMeshData graphData, NodeLink2[] links, int nodeOffset)
if (startNode != null && endNode != null)
{
// FIXME: Optimise
startTile.polys = RcArrayUtils.CopyOf(startTile.polys, startTile.polys.Length + 1);
startTile.polys = RcArrays.CopyOf(startTile.polys, startTile.polys.Length + 1);
int poly = startTile.header.polyCount;
startTile.polys[poly] = new DtPoly(poly, 2);
startTile.polys[poly].verts[0] = startTile.header.vertCount;
startTile.polys[poly].verts[1] = startTile.header.vertCount + 1;
startTile.polys[poly].SetPolyType(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION);
startTile.verts = RcArrayUtils.CopyOf(startTile.verts, startTile.verts.Length + 6);
startTile.verts = RcArrays.CopyOf(startTile.verts, startTile.verts.Length + 6);
startTile.header.polyCount++;
startTile.header.vertCount += 2;
DtOffMeshConnection connection = new DtOffMeshConnection();
Expand All @@ -63,7 +63,7 @@ public void Build(GraphMeshData graphData, NodeLink2[] links, int nodeOffset)
}
else
{
startTile.offMeshCons = RcArrayUtils.CopyOf(startTile.offMeshCons, startTile.offMeshCons.Length + 1);
startTile.offMeshCons = RcArrays.CopyOf(startTile.offMeshCons, startTile.offMeshCons.Length + 1);
}

startTile.offMeshCons[startTile.offMeshCons.Length - 1] = connection;
Expand Down
8 changes: 4 additions & 4 deletions src/DotRecast.Detour.TileCache/DtTileCacheBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ private void MergePolys(int[] polys, int pa, int pb, int ea, int eb, int maxVert
// Add pb
for (int i = 0; i < nb - 1; ++i)
tmp[n++] = polys[pb + (eb + 1 + i) % nb];
Array.Copy(tmp, 0, polys, pa, maxVertsPerPoly);
RcArrays.Copy(tmp, 0, polys, pa, maxVertsPerPoly);
}

private int PushFront(int v, List<int> arr)
Expand Down Expand Up @@ -1434,7 +1434,7 @@ private void RemoveVertex(DtTileCachePolyMesh mesh, int rem, int maxTris)

// Remove the polygon.
int p2 = (mesh.npolys - 1) * maxVertsPerPoly * 2;
Array.Copy(mesh.polys, p2, mesh.polys, p, maxVertsPerPoly);
RcArrays.Copy(mesh.polys, p2, mesh.polys, p, maxVertsPerPoly);
Array.Fill(mesh.polys, DT_TILECACHE_NULL_IDX, p + maxVertsPerPoly, maxVertsPerPoly);
mesh.areas[i] = mesh.areas[mesh.npolys - 1];
mesh.npolys--;
Expand Down Expand Up @@ -1597,7 +1597,7 @@ private void RemoveVertex(DtTileCachePolyMesh mesh, int rem, int maxTris)
int pa = bestPa * maxVertsPerPoly;
int pb = bestPb * maxVertsPerPoly;
MergePolys(polys, pa, pb, bestEa, bestEb, maxVertsPerPoly);
Array.Copy(polys, (npolys - 1) * maxVertsPerPoly, polys, pb, maxVertsPerPoly);
RcArrays.Copy(polys, (npolys - 1) * maxVertsPerPoly, polys, pb, maxVertsPerPoly);
pareas[bestPb] = pareas[npolys - 1];
npolys--;
}
Expand Down Expand Up @@ -1753,7 +1753,7 @@ public DtTileCachePolyMesh BuildTileCachePolyMesh(DtTileCacheContourSet lcset, i
int pa = bestPa * maxVertsPerPoly;
int pb = bestPb * maxVertsPerPoly;
MergePolys(polys, pa, pb, bestEa, bestEb, maxVertsPerPoly);
Array.Copy(polys, (npolys - 1) * maxVertsPerPoly, polys, pb, maxVertsPerPoly);
RcArrays.Copy(polys, (npolys - 1) * maxVertsPerPoly, polys, pb, maxVertsPerPoly);
npolys--;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public byte[] Compress(byte[] buf)
{
byte[] output = new byte[FastLZ.EstimateCompressedSize(buf.Length)];
long len = FastLZ.CompressLevel(2, buf, 0, buf.Length, output);
return RcArrayUtils.CopyOf(output, len);
return RcArrays.CopyOf(output, len);
}
}
}
3 changes: 2 additions & 1 deletion src/DotRecast.Detour/DtConvexConvexIntersections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 3. This notice may not be removed or altered from any source distribution.
*/

using System;
using DotRecast.Core;
using DotRecast.Core.Numerics;

namespace DotRecast.Detour
Expand Down Expand Up @@ -171,7 +172,7 @@ public static float[] Intersect(float[] p, float[] q)
}

float[] copied = new float[ii];
Array.Copy(inters, copied, ii);
RcArrays.Copy(inters, copied, ii);
return copied;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour/DtNavMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ public bool GetPolyHeight(DtMeshTile tile, DtPoly poly, RcVec3f pos, out float h
int nv = poly.vertCount;
for (int i = 0; i < nv; ++i)
{
Array.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
}

if (!DtUtils.PointInPolygon(pos, verts, nv))
Expand Down
7 changes: 4 additions & 3 deletions src/DotRecast.Detour/DtNavMeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 3. This notice may not be removed or altered from any source distribution.
*/

using System;
using DotRecast.Core;
using DotRecast.Core.Numerics;

namespace DotRecast.Detour
Expand Down Expand Up @@ -468,7 +469,7 @@ public static DtMeshData CreateNavMeshData(DtNavMeshCreateParams option)
{
int linkv = i * 2 * 3;
int v = (offMeshVertsBase + n * 2) * 3;
Array.Copy(option.offMeshConVerts, linkv, navVerts, v, 6);
RcArrays.Copy(option.offMeshConVerts, linkv, navVerts, v, 6);
n++;
}
}
Expand Down Expand Up @@ -557,13 +558,13 @@ public static DtMeshData CreateNavMeshData(DtNavMeshCreateParams option)
// nav poly verts.
if (ndv - nv != 0)
{
Array.Copy(option.detailVerts, (vb + nv) * 3, navDVerts, vbase * 3, 3 * (ndv - nv));
RcArrays.Copy(option.detailVerts, (vb + nv) * 3, navDVerts, vbase * 3, 3 * (ndv - nv));
vbase += ndv - nv;
}
}

// Store triangles.
Array.Copy(option.detailTris, 0, navDTris, 0, 4 * option.detailTriCount);
RcArrays.Copy(option.detailTris, 0, navDTris, 0, 4 * option.detailTriCount);
}
else
{
Expand Down
18 changes: 9 additions & 9 deletions src/DotRecast.Detour/DtNavMeshQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ public DtStatus FindRandomPoint(IDtQueryFilter filter, IRcRand frand, out long r
// Randomly pick point on polygon.
float[] verts = new float[3 * m_nav.GetMaxVertsPerPoly()];
float[] areas = new float[m_nav.GetMaxVertsPerPoly()];
Array.Copy(tile.data.verts, poly.verts[0] * 3, verts, 0, 3);
RcArrays.Copy(tile.data.verts, poly.verts[0] * 3, verts, 0, 3);
for (int j = 1; j < poly.vertCount; ++j)
{
Array.Copy(tile.data.verts, poly.verts[j] * 3, verts, j * 3, 3);
RcArrays.Copy(tile.data.verts, poly.verts[j] * 3, verts, j * 3, 3);
}

float s = frand.Next();
Expand Down Expand Up @@ -256,7 +256,7 @@ public DtStatus FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, fl
float[] polyVerts = new float[bestPoly.vertCount * 3];
for (int j = 0; j < bestPoly.vertCount; ++j)
{
Array.Copy(bestTile.data.verts, bestPoly.verts[j] * 3, polyVerts, j * 3, 3);
RcArrays.Copy(bestTile.data.verts, bestPoly.verts[j] * 3, polyVerts, j * 3, 3);
}

float[] constrainedVerts = constraint.Apply(polyVerts, centerPos, maxRadius);
Expand Down Expand Up @@ -453,7 +453,7 @@ public DtStatus ClosestPointOnPolyBoundary(long refs, RcVec3f pos, out RcVec3f c
int nv = poly.vertCount;
for (int i = 0; i < nv; ++i)
{
Array.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
}

if (DtUtils.DistancePtPolyEdgesSqr(pos, verts, nv, edged, edget))
Expand Down Expand Up @@ -1822,7 +1822,7 @@ public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos
int nverts = curPoly.vertCount;
for (int i = 0; i < nverts; ++i)
{
Array.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3);
RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3);
}

// If target is inside the poly, stop search.
Expand Down Expand Up @@ -2873,7 +2873,7 @@ public DtStatus FindLocalNeighbourhood(long startRef, RcVec3f centerPos, float r
int npa = neighbourPoly.vertCount;
for (int k = 0; k < npa; ++k)
{
Array.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3);
RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3);
}

bool overlap = false;
Expand Down Expand Up @@ -2904,7 +2904,7 @@ public DtStatus FindLocalNeighbourhood(long startRef, RcVec3f centerPos, float r
int npb = pastPoly.vertCount;
for (int k = 0; k < npb; ++k)
{
Array.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3);
RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3);
}

if (DtUtils.OverlapPolyPoly2D(pa, npa, pb, npb))
Expand Down Expand Up @@ -3033,8 +3033,8 @@ public DtStatus GetPolyWallSegments(long refs, bool storePortals, IDtQueryFilter
var seg = new RcSegmentVert();
seg.vmin = RcVecUtils.Create(tile.data.verts, ivj);
seg.vmax = RcVecUtils.Create(tile.data.verts, ivi);
// Array.Copy(tile.data.verts, ivj, seg, 0, 3);
// Array.Copy(tile.data.verts, ivi, seg, 3, 3);
// RcArrays.Copy(tile.data.verts, ivj, seg, 0, 3);
// RcArrays.Copy(tile.data.verts, ivi, seg, 3, 3);
segmentVerts.Add(seg);
segmentRefs.Add(neiRef);
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/DotRecast.Recast.Demo/Draw/ArrayBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using DotRecast.Core;

namespace DotRecast.Recast.Demo.Draw;

Expand All @@ -24,7 +25,7 @@ public void Add(T item)
if (_items.Length <= _size)
{
var temp = new T[(int)(_size * 1.5)];
Array.Copy(_items, 0, temp, 0, _items.Length);
RcArrays.Copy(_items, 0, temp, 0, _items.Length);
_items = temp;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast.Demo/Draw/GLU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static void MultiplyMatrixByVector4by4OpenGL_FLOAT(float[] resultvector, float[]
// This code comes directly from GLU except that it is for float
static int GlhInvertMatrixf2(float[] m, float[] @out)
{
float[][] wtmp = RcArrayUtils.Of<float>(4, 8);
float[][] wtmp = RcArrays.Of<float>(4, 8);
float m0, m1, m2, m3, s;
float[] r0, r1, r2, r3;
r0 = wtmp[0];
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast.Demo/DtVoxelTileLZ4DemoCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public byte[] Compress(byte[] data)
byte[] compressed = LZ4Pickler.Pickle(data, LZ4Level.L12_MAX);
byte[] result = new byte[4 + compressed.Length];
RcByteUtils.PutInt(compressed.Length, result, 0, RcByteOrder.BIG_ENDIAN);
Array.Copy(compressed, 0, result, 4, compressed.Length);
RcArrays.Copy(compressed, 0, result, 4, compressed.Length);
return result;
}
}
2 changes: 1 addition & 1 deletion src/DotRecast.Recast.Toolset/Tools/RcConvexVolumeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static RcConvexVolume CreateConvexVolume(List<RcVec3f> pts, List<int> hul
int noffset = RcAreas.OffsetPoly(verts, hull.Count, polyOffset, offset, offset.Length);
if (noffset > 0)
{
verts = RcArrayUtils.CopyOf(offset, 0, noffset * 3);
verts = RcArrays.CopyOf(offset, 0, noffset * 3);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/DotRecast.Recast/RcFilledVolumeRasterization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void RasterizeBox(RcHeightfield hf, RcVec3f center, RcVec3f[] half
bounds[5] = Math.Max(bounds[5], vertices[i * 3 + 2]);
}

float[][] planes = RcArrayUtils.Of<float>(6, 4);
float[][] planes = RcArrays.Of<float>(6, 4);
for (int i = 0; i < 6; i++)
{
float m = i < 3 ? -1 : 1;
Expand Down Expand Up @@ -135,8 +135,8 @@ public static void RasterizeConvex(RcHeightfield hf, float[] vertices, int[] tri
}


float[][] planes = RcArrayUtils.Of<float>(triangles.Length, 4);
float[][] triBounds = RcArrayUtils.Of<float>(triangles.Length / 3, 4);
float[][] planes = RcArrays.Of<float>(triangles.Length, 4);
float[][] triBounds = RcArrays.Of<float>(triangles.Length / 3, 4);
for (int i = 0, j = 0; i < triangles.Length; i += 3, j++)
{
int a = triangles[i] * 3;
Expand Down
4 changes: 2 additions & 2 deletions src/DotRecast.Recast/RcMeshDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ public static RcPolyMeshDetail BuildPolyMeshDetail(RcTelemetry ctx, RcPolyMesh m
float[] newv = new float[vcap * 3];
if (dmesh.nverts != 0)
{
Array.Copy(dmesh.verts, 0, newv, 0, 3 * dmesh.nverts);
RcArrays.Copy(dmesh.verts, 0, newv, 0, 3 * dmesh.nverts);
}

dmesh.verts = newv;
Expand All @@ -1587,7 +1587,7 @@ public static RcPolyMeshDetail BuildPolyMeshDetail(RcTelemetry ctx, RcPolyMesh m
int[] newt = new int[tcap * 4];
if (dmesh.ntris != 0)
{
Array.Copy(dmesh.tris, 0, newt, 0, 4 * dmesh.ntris);
RcArrays.Copy(dmesh.tris, 0, newt, 0, 4 * dmesh.ntris);
}

dmesh.tris = newt;
Expand Down
Loading