From b384133553910844bc3f3c140613a93dc9a40b3d Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 5 Aug 2023 08:42:21 +0900 Subject: [PATCH] add shared comparer --- src/DotRecast.Detour/BVItemXComparer.cs | 6 ++++++ src/DotRecast.Detour/BVItemYComparer.cs | 6 ++++++ src/DotRecast.Detour/BVItemZComparer.cs | 6 ++++++ src/DotRecast.Detour/DtNavMeshBuilder.cs | 6 +++--- .../Geom/BoundsItemXComparer.cs | 18 ++++++++++++++++++ .../Geom/BoundsItemYComparer.cs | 18 ++++++++++++++++++ src/DotRecast.Recast/Geom/CompareItemX.cs | 12 ------------ src/DotRecast.Recast/Geom/CompareItemY.cs | 12 ------------ src/DotRecast.Recast/Geom/RcChunkyTriMesh.cs | 4 ++-- src/DotRecast.Recast/RcContourHoleComparer.cs | 6 ++++++ .../RcPotentialDiagonalComparer.cs | 6 ++++++ src/DotRecast.Recast/RecastContour.cs | 4 ++-- 12 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 src/DotRecast.Recast/Geom/BoundsItemXComparer.cs create mode 100644 src/DotRecast.Recast/Geom/BoundsItemYComparer.cs delete mode 100644 src/DotRecast.Recast/Geom/CompareItemX.cs delete mode 100644 src/DotRecast.Recast/Geom/CompareItemY.cs diff --git a/src/DotRecast.Detour/BVItemXComparer.cs b/src/DotRecast.Detour/BVItemXComparer.cs index ab0eb7f..7f24ff8 100644 --- a/src/DotRecast.Detour/BVItemXComparer.cs +++ b/src/DotRecast.Detour/BVItemXComparer.cs @@ -4,6 +4,12 @@ namespace DotRecast.Detour { public class BVItemXComparer : IComparer { + public static readonly BVItemXComparer Shared = new BVItemXComparer(); + + private BVItemXComparer() + { + } + public int Compare(BVItem a, BVItem b) { return a.bmin[0].CompareTo(b.bmin[0]); diff --git a/src/DotRecast.Detour/BVItemYComparer.cs b/src/DotRecast.Detour/BVItemYComparer.cs index c481dac..40d7d8e 100644 --- a/src/DotRecast.Detour/BVItemYComparer.cs +++ b/src/DotRecast.Detour/BVItemYComparer.cs @@ -4,6 +4,12 @@ namespace DotRecast.Detour { public class BVItemYComparer : IComparer { + public static readonly BVItemYComparer Shared = new BVItemYComparer(); + + private BVItemYComparer() + { + } + public int Compare(BVItem a, BVItem b) { return a.bmin[1].CompareTo(b.bmin[1]); diff --git a/src/DotRecast.Detour/BVItemZComparer.cs b/src/DotRecast.Detour/BVItemZComparer.cs index 8c9bf84..103d051 100644 --- a/src/DotRecast.Detour/BVItemZComparer.cs +++ b/src/DotRecast.Detour/BVItemZComparer.cs @@ -4,6 +4,12 @@ namespace DotRecast.Detour { public class BVItemZComparer : IComparer { + public static readonly BVItemZComparer Shared = new BVItemZComparer(); + + private BVItemZComparer() + { + } + public int Compare(BVItem a, BVItem b) { return a.bmin[2].CompareTo(b.bmin[2]); diff --git a/src/DotRecast.Detour/DtNavMeshBuilder.cs b/src/DotRecast.Detour/DtNavMeshBuilder.cs index 9ac7609..9311fdf 100644 --- a/src/DotRecast.Detour/DtNavMeshBuilder.cs +++ b/src/DotRecast.Detour/DtNavMeshBuilder.cs @@ -116,17 +116,17 @@ namespace DotRecast.Detour if (axis == 0) { // Sort along x-axis - Array.Sort(items, imin, inum, new BVItemXComparer()); + Array.Sort(items, imin, inum, BVItemXComparer.Shared); } else if (axis == 1) { // Sort along y-axis - Array.Sort(items, imin, inum, new BVItemYComparer()); + Array.Sort(items, imin, inum, BVItemYComparer.Shared); } else { // Sort along z-axis - Array.Sort(items, imin, inum, new BVItemZComparer()); + Array.Sort(items, imin, inum, BVItemZComparer.Shared); } int isplit = imin + inum / 2; diff --git a/src/DotRecast.Recast/Geom/BoundsItemXComparer.cs b/src/DotRecast.Recast/Geom/BoundsItemXComparer.cs new file mode 100644 index 0000000..bb036ff --- /dev/null +++ b/src/DotRecast.Recast/Geom/BoundsItemXComparer.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; + +namespace DotRecast.Recast.Geom +{ + public class BoundsItemXComparer : IComparer + { + public static readonly BoundsItemXComparer Shared = new BoundsItemXComparer(); + + private BoundsItemXComparer() + { + } + + public int Compare(BoundsItem a, BoundsItem b) + { + return a.bmin.x.CompareTo(b.bmin.x); + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast/Geom/BoundsItemYComparer.cs b/src/DotRecast.Recast/Geom/BoundsItemYComparer.cs new file mode 100644 index 0000000..974d527 --- /dev/null +++ b/src/DotRecast.Recast/Geom/BoundsItemYComparer.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; + +namespace DotRecast.Recast.Geom +{ + public class BoundsItemYComparer : IComparer + { + public static readonly BoundsItemYComparer Shared = new BoundsItemYComparer(); + + private BoundsItemYComparer() + { + } + + public int Compare(BoundsItem a, BoundsItem b) + { + return a.bmin.y.CompareTo(b.bmin.y); + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast/Geom/CompareItemX.cs b/src/DotRecast.Recast/Geom/CompareItemX.cs deleted file mode 100644 index 4364bbf..0000000 --- a/src/DotRecast.Recast/Geom/CompareItemX.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; - -namespace DotRecast.Recast.Geom -{ - public class CompareItemX : IComparer - { - public int Compare(BoundsItem a, BoundsItem b) - { - return a.bmin.x.CompareTo(b.bmin.x); - } - } -} \ No newline at end of file diff --git a/src/DotRecast.Recast/Geom/CompareItemY.cs b/src/DotRecast.Recast/Geom/CompareItemY.cs deleted file mode 100644 index a00c31b..0000000 --- a/src/DotRecast.Recast/Geom/CompareItemY.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; - -namespace DotRecast.Recast.Geom -{ - public class CompareItemY : IComparer - { - public int Compare(BoundsItem a, BoundsItem b) - { - return a.bmin.y.CompareTo(b.bmin.y); - } - } -} \ No newline at end of file diff --git a/src/DotRecast.Recast/Geom/RcChunkyTriMesh.cs b/src/DotRecast.Recast/Geom/RcChunkyTriMesh.cs index 6fcffca..b28964d 100644 --- a/src/DotRecast.Recast/Geom/RcChunkyTriMesh.cs +++ b/src/DotRecast.Recast/Geom/RcChunkyTriMesh.cs @@ -102,12 +102,12 @@ namespace DotRecast.Recast.Geom if (axis == 0) { - Array.Sort(items, imin, imax - imin, new CompareItemX()); + Array.Sort(items, imin, imax - imin, BoundsItemXComparer.Shared); // Sort along x-axis } else if (axis == 1) { - Array.Sort(items, imin, imax - imin, new CompareItemY()); + Array.Sort(items, imin, imax - imin, BoundsItemYComparer.Shared); // Sort along y-axis } diff --git a/src/DotRecast.Recast/RcContourHoleComparer.cs b/src/DotRecast.Recast/RcContourHoleComparer.cs index 93eb73b..ec21512 100644 --- a/src/DotRecast.Recast/RcContourHoleComparer.cs +++ b/src/DotRecast.Recast/RcContourHoleComparer.cs @@ -4,6 +4,12 @@ namespace DotRecast.Recast { public class RcContourHoleComparer : IComparer { + public static readonly RcContourHoleComparer Shared = new RcContourHoleComparer(); + + private RcContourHoleComparer() + { + } + public int Compare(RcContourHole a, RcContourHole b) { if (a.minx == b.minx) diff --git a/src/DotRecast.Recast/RcPotentialDiagonalComparer.cs b/src/DotRecast.Recast/RcPotentialDiagonalComparer.cs index 408bfd5..66dd128 100644 --- a/src/DotRecast.Recast/RcPotentialDiagonalComparer.cs +++ b/src/DotRecast.Recast/RcPotentialDiagonalComparer.cs @@ -4,6 +4,12 @@ namespace DotRecast.Recast { public class RcPotentialDiagonalComparer : IComparer { + public static readonly RcPotentialDiagonalComparer Shared = new RcPotentialDiagonalComparer(); + + private RcPotentialDiagonalComparer() + { + } + public int Compare(RcPotentialDiagonal va, RcPotentialDiagonal vb) { RcPotentialDiagonal a = va; diff --git a/src/DotRecast.Recast/RecastContour.cs b/src/DotRecast.Recast/RecastContour.cs index 7a8b523..dd84a87 100644 --- a/src/DotRecast.Recast/RecastContour.cs +++ b/src/DotRecast.Recast/RecastContour.cs @@ -619,7 +619,7 @@ namespace DotRecast.Recast region.holes[i].leftmost = minleft[2]; } - Array.Sort(region.holes, new RcContourHoleComparer()); + Array.Sort(region.holes, RcContourHoleComparer.Shared); int maxVerts = region.outline.nverts; for (int i = 0; i < region.nholes; i++) @@ -665,7 +665,7 @@ namespace DotRecast.Recast } // Sort potential diagonals by distance, we want to make the connection as short as possible. - Array.Sort(diags, 0, ndiags, new RcPotentialDiagonalComparer()); + Array.Sort(diags, 0, ndiags, RcPotentialDiagonalComparer.Shared); // Find a diagonal that is not intersecting the outline not the remaining holes. index = -1;