Skip to content
Closed
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
22 changes: 11 additions & 11 deletions benchmark/CDT.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class BenchmarkInputReader
/// Reads a CDT input file.
/// Format: <c>nVerts nEdges\n x y\n … v1 v2\n …</c>
/// </summary>
public static (List<V2d<double>> Vertices, List<Edge> Edges) Read(string fileName)
public static (V2d<double>[] Vertices, Edge[] Edges) Read(string fileName)
{
var path = Path.Combine(AppContext.BaseDirectory, "inputs", fileName);

Expand All @@ -31,20 +31,20 @@ public static (List<V2d<double>> Vertices, List<Edge> Edges) Read(string fileNam
int nVerts = int.Parse(header[0]);
int nEdges = int.Parse(header[1]);

var verts = new List<V2d<double>>(nVerts);
var verts = new V2d<double>[nVerts];
for (int i = 0; i < nVerts; i++)
{
var tok = sr.ReadLine()!.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
verts.Add(new V2d<double>(
verts[i] = new V2d<double>(
double.Parse(tok[0], System.Globalization.CultureInfo.InvariantCulture),
double.Parse(tok[1], System.Globalization.CultureInfo.InvariantCulture)));
double.Parse(tok[1], System.Globalization.CultureInfo.InvariantCulture));
}

var edges = new List<Edge>(nEdges);
var edges = new Edge[nEdges];
for (int i = 0; i < nEdges; i++)
{
var tok = sr.ReadLine()!.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
edges.Add(new Edge(int.Parse(tok[0]), int.Parse(tok[1])));
edges[i] = new Edge(int.Parse(tok[0]), int.Parse(tok[1]));
}

return (verts, edges);
Expand All @@ -66,8 +66,8 @@ public static (List<V2d<double>> Vertices, List<Edge> Edges) Read(string fileNam
[ShortRunJob]
public class ConstrainedSwedenBenchmarks
{
private List<V2d<double>> _vertices = null!;
private List<Edge> _edges = null!;
private V2d<double>[] _vertices = null!;
private Edge[] _edges = null!;

[GlobalSetup]
public void Setup()
Expand Down Expand Up @@ -193,8 +193,8 @@ public Triangulation<double> EraseOuterTriangles_Auto()
[ShortRunJob]
public class SmallDatasetBenchmarks
{
private List<V2d<double>> _vertices = null!;
private List<Edge> _edges = null!;
private V2d<double>[] _vertices = null!;
private Edge[] _edges = null!;

[GlobalSetup]
public void Setup()
Expand Down Expand Up @@ -236,7 +236,7 @@ public Triangulation<double> FloatVsDouble_Double()
[BenchmarkCategory("FloatVsDouble")]
public Triangulation<float> FloatVsDouble_Float()
{
var vf = _vertices.Select(v => new V2d<float>((float)v.X, (float)v.Y)).ToList();
var vf = Array.ConvertAll(_vertices, v => new V2d<float>((float)v.X, (float)v.Y));
var ef = _edges;
var cdt = new Triangulation<float>(VertexInsertionOrder.Auto);
cdt.InsertVertices(vf);
Expand Down
37 changes: 22 additions & 15 deletions src/CDT.Core/CdtUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.ObjectModel;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CDT.Predicates;

namespace CDT;
Expand All @@ -24,9 +25,14 @@ public sealed class DuplicatesInfo

internal DuplicatesInfo(int[] mapping, List<int> duplicates)
{
_mapping = mapping;
_duplicates = duplicates;
Mapping = Array.AsReadOnly(mapping);
Duplicates = duplicates.AsReadOnly();
}

internal readonly int[] _mapping;
internal readonly List<int> _duplicates;
}

/// <summary>
Expand All @@ -50,10 +56,10 @@ public static class CdtUtils
/// A <see cref="DuplicatesInfo"/> containing a mapping from each original
/// index to its canonical index, and the list of duplicate indices.
/// </returns>
public static DuplicatesInfo FindDuplicates<T>(IReadOnlyList<V2d<T>> vertices)
public static DuplicatesInfo FindDuplicates<T>(ReadOnlySpan<V2d<T>> vertices)
where T : IFloatingPoint<T>
{
int n = vertices.Count;
int n = vertices.Length;
var mapping = new int[n];
var duplicates = new List<int>();
var posToIndex = new Dictionary<(T, T), int>(n);
Expand Down Expand Up @@ -84,11 +90,12 @@ public static DuplicatesInfo FindDuplicates<T>(IReadOnlyList<V2d<T>> vertices)
/// <param name="vertices">The vertex list to modify in-place.</param>
/// <param name="duplicates">The list of duplicate indices to remove.</param>
/// <remarks>The <paramref name="vertices"/> list is mutated in-place.</remarks>
public static void RemoveDuplicates<T>(List<V2d<T>> vertices, IReadOnlyList<int> duplicates)
public static void RemoveDuplicates<T>(List<V2d<T>> vertices, ReadOnlySpan<int> duplicates)
where T : IFloatingPoint<T>
{
if (duplicates.Count == 0) return;
var toRemove = new HashSet<int>(duplicates);
if (duplicates.Length == 0) return;
var toRemove = new HashSet<int>(duplicates.Length);
foreach (int d in duplicates) toRemove.Add(d);
int write = 0;
for (int i = 0; i < vertices.Count; i++)
{
Expand All @@ -112,9 +119,9 @@ public static DuplicatesInfo RemoveDuplicatesAndRemapEdges<T>(
List<Edge> edges)
where T : IFloatingPoint<T>
{
var info = FindDuplicates(vertices);
RemoveDuplicates(vertices, info.Duplicates);
RemapEdges(edges, info.Mapping);
var info = FindDuplicates(CollectionsMarshal.AsSpan(vertices));
RemoveDuplicates(vertices, CollectionsMarshal.AsSpan(info._duplicates));
RemapEdges(edges, info._mapping);
return info;
}

Expand All @@ -130,7 +137,7 @@ public static DuplicatesInfo RemoveDuplicatesAndRemapEdges<T>(
/// Vertex index mapping: for each old vertex index <c>i</c>, <c>mapping[i]</c> is the new index.
/// </param>
/// <remarks>The <paramref name="edges"/> list is mutated in-place.</remarks>
public static void RemapEdges(List<Edge> edges, IReadOnlyList<int> mapping)
public static void RemapEdges(List<Edge> edges, ReadOnlySpan<int> mapping)
{
for (int i = 0; i < edges.Count; i++)
{
Expand All @@ -145,9 +152,9 @@ public static void RemapEdges(List<Edge> edges, IReadOnlyList<int> mapping)
/// <summary>Extracts all unique edges from a triangle list.</summary>
/// <param name="triangles">The triangle list to extract edges from.</param>
/// <returns>A set containing all unique edges in the triangulation.</returns>
public static HashSet<Edge> ExtractEdgesFromTriangles(IReadOnlyList<Triangle> triangles)
public static HashSet<Edge> ExtractEdgesFromTriangles(ReadOnlySpan<Triangle> triangles)
{
var edges = new HashSet<Edge>(triangles.Count * 3);
var edges = new HashSet<Edge>(triangles.Length * 3);
foreach (var t in triangles)
{
edges.Add(new Edge(t.V0, t.V1));
Expand All @@ -167,12 +174,12 @@ public static HashSet<Edge> ExtractEdgesFromTriangles(IReadOnlyList<Triangle> tr
/// A read-only list of read-only lists: for each vertex index, the list of adjacent triangle indices.
/// </returns>
public static IReadOnlyList<IReadOnlyList<int>> CalculateTrianglesByVertex(
IReadOnlyList<Triangle> triangles,
ReadOnlySpan<Triangle> triangles,
int verticesCount)
{
var result = new List<int>[verticesCount];
for (int i = 0; i < verticesCount; i++) result[i] = new List<int>();
for (int i = 0; i < triangles.Count; i++)
for (int i = 0; i < triangles.Length; i++)
{
var t = triangles[i];
result[t.V0].Add(i);
Expand Down Expand Up @@ -605,11 +612,11 @@ public static V2d<T> IntersectionPosition<T>(V2d<T> a, V2d<T> b, V2d<T> c, V2d<T
/// <param name="aTris">Triangle indices adjacent to vertex A.</param>
/// <param name="bTris">Triangle indices adjacent to vertex B.</param>
/// <returns><c>true</c> if any triangle is shared between the two lists.</returns>
internal static bool VerticesShareEdge(List<int> aTris, List<int> bTris)
internal static bool VerticesShareEdge(ReadOnlySpan<int> aTris, ReadOnlySpan<int> bTris)
{
foreach (int t in aTris)
{
if (bTris.Contains(t)) return true;
if (MemoryExtensions.Contains(bTris, t)) return true;
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/CDT.Core/KdTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public KdTree(T minX, T minY, T maxX, T maxY)
public int Size => _size;

/// <summary>Inserts point at index <paramref name="iPoint"/> from the external buffer.</summary>
public void Insert(int iPoint, IReadOnlyList<V2d<T>> points)
public void Insert(int iPoint, ReadOnlySpan<V2d<T>> points)
{
_size++;
T px = points[iPoint].X, py = points[iPoint].Y;
Expand Down Expand Up @@ -149,7 +149,7 @@ public void Insert(int iPoint, IReadOnlyList<V2d<T>> points)
}

/// <summary>Finds the nearest point to <paramref name="points"/> in the external buffer.</summary>
public int Nearest(T qx, T qy, IReadOnlyList<V2d<T>> points)
public int Nearest(T qx, T qy, ReadOnlySpan<V2d<T>> points)
{
int resultIdx = 0;
T minDistSq = T.MaxValue;
Expand Down Expand Up @@ -311,7 +311,7 @@ private void ExtendTree(T px, T py)
_root = newRoot;
}

private void InitializeRootBox(IReadOnlyList<V2d<T>> points)
private void InitializeRootBox(ReadOnlySpan<V2d<T>> points)
{
Node rootNode = _nodes[_root];
T mxn = points[rootNode.Data![0]].X, myn = points[rootNode.Data[0]].Y;
Expand Down
Loading