refactor: add type-safe array copy function

This commit is contained in:
ikpil 2023-11-11 13:06:42 +09:00 committed by Choi Ikpil
parent a50ba0b1b1
commit 69c8c950d2
23 changed files with 72 additions and 54 deletions

View File

@ -38,7 +38,7 @@ namespace DotRecast.Core.Collections
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)

View File

@ -41,7 +41,7 @@ namespace DotRecast.Core.Collections
}
var tmp = new T[items.Length];
Array.Copy(items, tmp, items.Length);
RcArrays.Copy(items, tmp, items.Length);
return new RcImmutableArray<T>(tmp);
}
}

View File

@ -356,7 +356,7 @@ namespace DotRecast.Core.Compression
return 0;
}
Array.Copy(input, ip, output, op, ctrl);
RcArrays.Copy(input, ip, output, op, ctrl);
ip += ctrl;
op += ctrl;
}
@ -452,7 +452,7 @@ namespace DotRecast.Core.Compression
return 0;
}
Array.Copy(input, ip, output, op, ctrl);
RcArrays.Copy(input, ip, output, op, ctrl);
ip += ctrl;
op += ctrl;
}
@ -498,16 +498,16 @@ namespace DotRecast.Core.Compression
// 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

View File

@ -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];

View File

@ -1145,7 +1145,7 @@ namespace DotRecast.Detour.Crowd
{
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;

View File

@ -54,7 +54,7 @@ namespace DotRecast.Detour.Crowd
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)
{

View File

@ -64,8 +64,8 @@ namespace DotRecast.Detour.Extras.Jumplink
{
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;

View File

@ -9,7 +9,7 @@ namespace DotRecast.Detour.Extras.Jumplink
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++)

View File

@ -37,13 +37,13 @@ namespace DotRecast.Detour.Extras.Unity.Astar
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();
@ -63,7 +63,7 @@ namespace DotRecast.Detour.Extras.Unity.Astar
}
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;

View File

@ -1275,7 +1275,7 @@ namespace DotRecast.Detour.TileCache
// 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)
@ -1434,7 +1434,7 @@ namespace DotRecast.Detour.TileCache
// 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--;
@ -1597,7 +1597,7 @@ namespace DotRecast.Detour.TileCache
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--;
}
@ -1753,7 +1753,7 @@ namespace DotRecast.Detour.TileCache
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

View File

@ -47,7 +47,7 @@ namespace DotRecast.Detour.TileCache.Io.Compress
{
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);
}
}
}

View File

@ -18,6 +18,7 @@ freely, subject to the following restrictions:
*/
using System;
using DotRecast.Core;
using DotRecast.Core.Numerics;
namespace DotRecast.Detour
@ -171,7 +172,7 @@ namespace DotRecast.Detour
}
float[] copied = new float[ii];
Array.Copy(inters, copied, ii);
RcArrays.Copy(inters, copied, ii);
return copied;
}

View File

@ -1251,7 +1251,7 @@ namespace DotRecast.Detour
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))

View File

@ -19,6 +19,7 @@ freely, subject to the following restrictions:
*/
using System;
using DotRecast.Core;
using DotRecast.Core.Numerics;
namespace DotRecast.Detour
@ -468,7 +469,7 @@ namespace DotRecast.Detour
{
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++;
}
}
@ -557,13 +558,13 @@ namespace DotRecast.Detour
// 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
{

View File

@ -136,10 +136,10 @@ namespace DotRecast.Detour
// 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();
@ -256,7 +256,7 @@ namespace DotRecast.Detour
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);
@ -453,7 +453,7 @@ namespace DotRecast.Detour
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))
@ -1822,7 +1822,7 @@ namespace DotRecast.Detour
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.
@ -2873,7 +2873,7 @@ namespace DotRecast.Detour
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;
@ -2904,7 +2904,7 @@ namespace DotRecast.Detour
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))
@ -3033,8 +3033,8 @@ namespace DotRecast.Detour
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;

View File

@ -1,4 +1,5 @@
using System;
using DotRecast.Core;
namespace DotRecast.Recast.Demo.Draw;
@ -24,7 +25,7 @@ public class ArrayBuffer<T>
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;
}

View File

@ -126,7 +126,7 @@ public static class GLU
// 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];

View File

@ -28,7 +28,7 @@ public class DtVoxelTileLZ4DemoCompressor : IRcCompressor
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;
}
}

View File

@ -134,7 +134,7 @@ namespace DotRecast.Recast.Toolset.Tools
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);
}
}

View File

@ -105,7 +105,7 @@ namespace DotRecast.Recast
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;
@ -135,8 +135,8 @@ namespace DotRecast.Recast
}
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;

View File

@ -1562,7 +1562,7 @@ namespace DotRecast.Recast
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;
@ -1587,7 +1587,7 @@ namespace DotRecast.Recast
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;

View File

@ -558,7 +558,7 @@ namespace DotRecast.Recast
n++;
}
Array.Copy(polys, tmp, polys, pa, nvp);
RcArrays.Copy(polys, tmp, polys, pa, nvp);
}
private static int PushFront(int v, int[] arr, int an)
@ -737,7 +737,7 @@ namespace DotRecast.Recast
int p2 = (mesh.npolys - 1) * nvp * 2;
if (p != p2)
{
Array.Copy(mesh.polys, p2, mesh.polys, p, nvp);
RcArrays.Copy(mesh.polys, p2, mesh.polys, p, nvp);
}
Array.Fill(mesh.polys, RC_MESH_NULL_IDX, p + nvp, (p + nvp + nvp) - (p + nvp));
@ -927,7 +927,7 @@ namespace DotRecast.Recast
int last = (npolys - 1) * nvp;
if (pb != last)
{
Array.Copy(polys, last, polys, pb, nvp);
RcArrays.Copy(polys, last, polys, pb, nvp);
}
pregs[bestPb] = pregs[npolys - 1];
@ -1109,7 +1109,7 @@ namespace DotRecast.Recast
int lastPoly = (npolys - 1) * nvp;
if (pb != lastPoly)
{
Array.Copy(polys, lastPoly, polys, pb, nvp);
RcArrays.Copy(polys, lastPoly, polys, pb, nvp);
}
npolys--;
@ -1356,15 +1356,15 @@ namespace DotRecast.Recast
dst.maxEdgeError = src.maxEdgeError;
dst.verts = new int[src.nverts * 3];
Array.Copy(src.verts, 0, dst.verts, 0, dst.verts.Length);
RcArrays.Copy(src.verts, 0, dst.verts, 0, dst.verts.Length);
dst.polys = new int[src.npolys * 2 * src.nvp];
Array.Copy(src.polys, 0, dst.polys, 0, dst.polys.Length);
RcArrays.Copy(src.polys, 0, dst.polys, 0, dst.polys.Length);
dst.regs = new int[src.npolys];
Array.Copy(src.regs, 0, dst.regs, 0, dst.regs.Length);
RcArrays.Copy(src.regs, 0, dst.regs, 0, dst.regs.Length);
dst.areas = new int[src.npolys];
Array.Copy(src.areas, 0, dst.areas, 0, dst.areas.Length);
RcArrays.Copy(src.areas, 0, dst.areas, 0, dst.areas.Length);
dst.flags = new int[src.npolys];
Array.Copy(src.flags, 0, dst.flags, 0, dst.flags.Length);
RcArrays.Copy(src.flags, 0, dst.flags, 0, dst.flags.Length);
return dst;
}
}

View File

@ -47,7 +47,7 @@ namespace DotRecast.Detour.Dynamic.Test.Io
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;
}
}