Skip to content

Commit 06e3f33

Browse files
committed
Immutable points sorting
1 parent cda848c commit 06e3f33

5 files changed

Lines changed: 71 additions & 71 deletions

File tree

src/main/java/com/github/refhumbold/algolib/geometry/dim2/ClosestPoints.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ public static Pair<Point2D, Point2D> findClosestPoints(List<Point2D> points)
2424
if(points.isEmpty())
2525
throw new NoSuchElementException("Sequence contains no points");
2626

27-
List<Point2D> pointsX = new ArrayList<>(points);
28-
List<Point2D> pointsY = new ArrayList<>(points);
29-
30-
Geometry2D.sortByY(pointsX);
31-
Geometry2D.sortByX(pointsX);
32-
Geometry2D.sortByY(pointsY);
27+
List<Point2D> pointsY = Geometry2D.sortByY(points);
28+
List<Point2D> pointsX = Geometry2D.sortByX(pointsY);
3329

3430
return searchClosest(pointsX, pointsY, 0, pointsX.size());
3531
}

src/main/java/com/github/refhumbold/algolib/geometry/dim2/Geometry2D.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56
import com.github.refhumbold.algolib.geometry.GeometryComparator;
67

78
/** Algorithms for basic geometrical operations in 2D. */
@@ -10,43 +11,38 @@ public final class Geometry2D
1011
private static final GeometryComparator comparator = new GeometryComparator();
1112

1213
/**
13-
* Mutably sorts given points by their X coordinate. Sorting is guaranteed to be stable.
14+
* Immutably sorts given points by their X coordinate. Sorting is guaranteed to be stable.
1415
* @param points the points
1516
*/
16-
public static void sortByX(List<Point2D> points)
17+
public static List<Point2D> sortByX(List<Point2D> points)
1718
{
18-
points.sort(Comparator.comparing(pt -> pt.x, comparator));
19+
return points.stream()
20+
.sorted(Comparator.comparing(pt -> pt.x, comparator))
21+
.collect(Collectors.toList());
1922
}
2023

2124
/**
22-
* Mutably sorts given points by their Y coordinate. Sorting is guaranteed to be stable.
25+
* Immutably sorts given points by their Y coordinate. Sorting is guaranteed to be stable.
2326
* @param points the points
2427
*/
25-
public static void sortByY(List<Point2D> points)
28+
public static List<Point2D> sortByY(List<Point2D> points)
2629
{
27-
points.sort(Comparator.comparing(pt -> pt.y, comparator));
30+
return points.stream()
31+
.sorted(Comparator.comparing(pt -> pt.y, comparator))
32+
.collect(Collectors.toList());
2833
}
2934

3035
/**
31-
* Mutably sorts given points by their polar coordinates. First sorts by angle, then by radius.
36+
* Immutably sorts given points by their polar coordinates. First sorts by angle, then by radius.
3237
* Sorting is guaranteed to be stable.
3338
* @param points the points
3439
*/
35-
public static void sortByAngle(List<Point2D> points)
40+
public static List<Point2D> sortByAngle(List<Point2D> points)
3641
{
37-
points.sort((pt1, pt2) -> pt1.angle().equals(pt2.angle()) ? comparator.compare(pt1.radius(),
38-
pt2.radius()) : pt1.angle().compareTo(pt2.angle()));
39-
}
40-
41-
/**
42-
* Mutably sorts given points by their polar coordinates around given central point.
43-
* First sorts by angle, then by radius. Sorting is guaranteed to be stable.
44-
* @param points the points
45-
* @param centre the central point
46-
*/
47-
public static void sortByAngleAround(List<Point2D> points, Point2D centre)
48-
{
49-
Vector2D translation = Vector2D.between(centre, Point2D.ZERO);
42+
return points.stream()
43+
.sorted((pt1, pt2) -> pt1.angle().equals(pt2.angle()) ? comparator.compare(
44+
pt1.radius(), pt2.radius()) : pt1.angle().compareTo(pt2.angle()))
45+
.collect(Collectors.toList());
5046
}
5147

5248
/**

src/main/java/com/github/refhumbold/algolib/geometry/dim3/Geometry3D.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56
import com.github.refhumbold.algolib.geometry.GeometryComparator;
67

78
/** Algorithms for basic geometrical operations in 3D. */
@@ -10,30 +11,36 @@ public final class Geometry3D
1011
private static final GeometryComparator comparator = new GeometryComparator();
1112

1213
/**
13-
* Mutably sorts given points by their X coordinate. Sorting is guaranteed to be stable.
14+
* Immutably sorts given points by their X coordinate. Sorting is guaranteed to be stable.
1415
* @param points the points
1516
*/
16-
public static void sortByX(List<Point3D> points)
17+
public static List<Point3D> sortByX(List<Point3D> points)
1718
{
18-
points.sort(Comparator.comparing(pt -> pt.x, comparator));
19+
return points.stream()
20+
.sorted(Comparator.comparing(pt -> pt.x, comparator))
21+
.collect(Collectors.toList());
1922
}
2023

2124
/**
22-
* Mutably sorts given points by their Y coordinate. Sorting is guaranteed to be stable.
25+
* Immutably sorts given points by their Y coordinate. Sorting is guaranteed to be stable.
2326
* @param points the points
2427
*/
25-
public static void sortByY(List<Point3D> points)
28+
public static List<Point3D> sortByY(List<Point3D> points)
2629
{
27-
points.sort(Comparator.comparing(pt -> pt.y, comparator));
30+
return points.stream()
31+
.sorted(Comparator.comparing(pt -> pt.y, comparator))
32+
.collect(Collectors.toList());
2833
}
2934

3035
/**
31-
* Mutably sorts given points by their Z coordinate. Sorting is guaranteed to be stable.
36+
* Immutably sorts given points by their Z coordinate. Sorting is guaranteed to be stable.
3237
* @param points the points
3338
*/
34-
public static void sortByZ(List<Point3D> points)
39+
public static List<Point3D> sortByZ(List<Point3D> points)
3540
{
36-
points.sort(Comparator.comparing(pt -> pt.z, comparator));
41+
return points.stream()
42+
.sorted(Comparator.comparing(pt -> pt.z, comparator))
43+
.collect(Collectors.toList());
3744
}
3845

3946
/**

src/test/java/com/github/refhumbold/algolib/geometry/dim2/Geometry2DTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ public void sortByX_ThenSortedStablyAscending()
1818
Point2D.of(3.0, 2.0), Point2D.of(2.0, -3.0), Point2D.of(-3.0, -2.0));
1919

2020
// when
21-
Geometry2D.sortByX(sequence);
21+
List<Point2D> result = Geometry2D.sortByX(sequence);
2222

2323
// then
24-
Assertions.assertThat(sequence)
24+
Assertions.assertThat(result)
25+
.isNotSameAs(sequence)
2526
.containsExactly(Point2D.of(-3.0, 2.0), Point2D.of(-3.0, -2.0),
2627
Point2D.of(-2.0, -3.0), Point2D.of(-2.0, 3.0), Point2D.ZERO,
2728
Point2D.of(2.0, 3.0), Point2D.of(2.0, -3.0), Point2D.of(3.0, -2.0),
@@ -38,10 +39,11 @@ public void sortByY_ThenSortedStablyAscending()
3839
Point2D.of(3.0, 2.0), Point2D.of(2.0, -3.0), Point2D.of(-3.0, -2.0));
3940

4041
// when
41-
Geometry2D.sortByY(sequence);
42+
List<Point2D> result = Geometry2D.sortByY(sequence);
4243

4344
// then
44-
Assertions.assertThat(sequence)
45+
Assertions.assertThat(result)
46+
.isNotSameAs(sequence)
4547
.containsExactly(Point2D.of(-2.0, -3.0), Point2D.of(2.0, -3.0),
4648
Point2D.of(3.0, -2.0), Point2D.of(-3.0, -2.0), Point2D.ZERO,
4749
Point2D.of(-3.0, 2.0), Point2D.of(3.0, 2.0), Point2D.of(2.0, 3.0),
@@ -58,10 +60,11 @@ public void sortByAngle_ThenSortedAscending()
5860
Point2D.of(3.0, 2.0), Point2D.of(2.0, -3.0), Point2D.of(-3.0, -2.0));
5961

6062
// when
61-
Geometry2D.sortByAngle(sequence);
63+
List<Point2D> result = Geometry2D.sortByAngle(sequence);
6264

6365
// then
64-
Assertions.assertThat(sequence)
66+
Assertions.assertThat(result)
67+
.isNotSameAs(sequence)
6568
.containsExactly(Point2D.ZERO, Point2D.of(3.0, 2.0), Point2D.of(2.0, 3.0),
6669
Point2D.of(-2.0, 3.0), Point2D.of(-3.0, 2.0), Point2D.of(-3.0, -2.0),
6770
Point2D.of(-2.0, -3.0), Point2D.of(2.0, -3.0), Point2D.of(3.0, -2.0));
@@ -76,10 +79,11 @@ public void sortByAngle_WhenEqualAngles_ThenCompareRadius()
7679
Point2D.of(-3.0, -3.0), Point2D.of(4.0, 4.0));
7780

7881
// when
79-
Geometry2D.sortByAngle(sequence);
82+
List<Point2D> result = Geometry2D.sortByAngle(sequence);
8083

8184
// then
82-
Assertions.assertThat(sequence)
85+
Assertions.assertThat(result)
86+
.isNotSameAs(sequence)
8387
.containsExactly(Point2D.ZERO, Point2D.of(1.0, 1.0), Point2D.of(4.0, 4.0),
8488
Point2D.of(-2.0, -2.0), Point2D.of(-3.0, -3.0));
8589
}

src/test/java/com/github/refhumbold/algolib/geometry/dim3/Geometry3DTest.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,53 @@ public void sortByX_ThenSortedStablyAscending()
1313
{
1414
// given
1515
List<Point3D> sequence =
16-
Arrays.asList(Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0),
17-
Point3D.of(-2.0, -3.0, 5.0), Point3D.of(2.0, -3.0, -5.0),
18-
Point3D.of(-2.0, -3.0, -5.0), Point3D.of(3.0, 2.0, 5.0),
19-
Point3D.of(-3.0, 2.0, 5.0));
16+
Arrays.asList(Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0), Point3D.of(-2.0, -3.0, 5.0),
17+
Point3D.of(2.0, -3.0, -5.0), Point3D.of(-2.0, -3.0, -5.0),
18+
Point3D.of(3.0, 2.0, 5.0), Point3D.of(-3.0, 2.0, 5.0));
2019
// when
21-
Geometry3D.sortByX(sequence);
20+
List<Point3D> result = Geometry3D.sortByX(sequence);
2221
// then
23-
Assertions.assertThat(sequence)
22+
Assertions.assertThat(result)
23+
.isNotSameAs(sequence)
2424
.containsExactly(Point3D.of(-3.0, 2.0, 5.0), Point3D.of(-2.0, -3.0, 5.0),
25-
Point3D.of(-2.0, -3.0, -5.0), Point3D.ZERO,
26-
Point3D.of(2.0, 3.0, -5.0), Point3D.of(2.0, -3.0, -5.0),
27-
Point3D.of(3.0, 2.0, 5.0));
25+
Point3D.of(-2.0, -3.0, -5.0), Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0),
26+
Point3D.of(2.0, -3.0, -5.0), Point3D.of(3.0, 2.0, 5.0));
2827
}
2928

3029
@Test
3130
public void sortByY_ThenSortedStablyAscending()
3231
{
3332
// given
3433
List<Point3D> sequence =
35-
Arrays.asList(Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0),
36-
Point3D.of(-2.0, -3.0, 5.0), Point3D.of(2.0, -3.0, -5.0),
37-
Point3D.of(-2.0, -3.0, -5.0), Point3D.of(3.0, 2.0, 5.0),
38-
Point3D.of(-3.0, 2.0, 5.0));
34+
Arrays.asList(Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0), Point3D.of(-2.0, -3.0, 5.0),
35+
Point3D.of(2.0, -3.0, -5.0), Point3D.of(-2.0, -3.0, -5.0),
36+
Point3D.of(3.0, 2.0, 5.0), Point3D.of(-3.0, 2.0, 5.0));
3937
// when
40-
Geometry3D.sortByY(sequence);
38+
List<Point3D> result = Geometry3D.sortByY(sequence);
4139
// then
42-
Assertions.assertThat(sequence)
40+
Assertions.assertThat(result)
41+
.isNotSameAs(sequence)
4342
.containsExactly(Point3D.of(-2.0, -3.0, 5.0), Point3D.of(2.0, -3.0, -5.0),
44-
Point3D.of(-2.0, -3.0, -5.0), Point3D.ZERO,
45-
Point3D.of(3.0, 2.0, 5.0), Point3D.of(-3.0, 2.0, 5.0),
46-
Point3D.of(2.0, 3.0, -5.0));
43+
Point3D.of(-2.0, -3.0, -5.0), Point3D.ZERO, Point3D.of(3.0, 2.0, 5.0),
44+
Point3D.of(-3.0, 2.0, 5.0), Point3D.of(2.0, 3.0, -5.0));
4745
}
4846

4947
@Test
5048
public void sortByZ_ThenSortedStablyAscending()
5149
{
5250
// given
5351
List<Point3D> sequence =
54-
Arrays.asList(Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0),
55-
Point3D.of(-2.0, -3.0, 5.0), Point3D.of(2.0, -3.0, -5.0),
56-
Point3D.of(-2.0, -3.0, -5.0), Point3D.of(3.0, 2.0, 5.0),
57-
Point3D.of(-3.0, 2.0, 5.0));
52+
Arrays.asList(Point3D.ZERO, Point3D.of(2.0, 3.0, -5.0), Point3D.of(-2.0, -3.0, 5.0),
53+
Point3D.of(2.0, -3.0, -5.0), Point3D.of(-2.0, -3.0, -5.0),
54+
Point3D.of(3.0, 2.0, 5.0), Point3D.of(-3.0, 2.0, 5.0));
5855
// when
59-
Geometry3D.sortByZ(sequence);
56+
List<Point3D> result = Geometry3D.sortByZ(sequence);
6057
// then
61-
Assertions.assertThat(sequence)
58+
Assertions.assertThat(result)
59+
.isNotSameAs(sequence)
6260
.containsExactly(Point3D.of(2.0, 3.0, -5.0), Point3D.of(2.0, -3.0, -5.0),
63-
Point3D.of(-2.0, -3.0, -5.0), Point3D.ZERO,
64-
Point3D.of(-2.0, -3.0, 5.0), Point3D.of(3.0, 2.0, 5.0),
65-
Point3D.of(-3.0, 2.0, 5.0));
61+
Point3D.of(-2.0, -3.0, -5.0), Point3D.ZERO, Point3D.of(-2.0, -3.0, 5.0),
62+
Point3D.of(3.0, 2.0, 5.0), Point3D.of(-3.0, 2.0, 5.0));
6663
}
6764

6865
@Test

0 commit comments

Comments
 (0)