Skip to content

Instantly share code, notes, and snippets.

@connormanning
Last active May 24, 2023 19:14
Show Gist options
  • Save connormanning/a1cc3e421fca3d3713f362af8e92e375 to your computer and use it in GitHub Desktop.
Save connormanning/a1cc3e421fca3d3713f362af8e92e375 to your computer and use it in GitHub Desktop.
Point view sort

Copy

Sort a temporary ordering and then replace m_index when done: success.

    template <typename Compare>
    void stableSort(Compare compare)
    {
        auto order = m_index;
        std::stable_sort(
            order.begin(),
            order.end(),
            [this, &compare](const PointId a, const PointId b)
            {
                return compare(PointRef(*this, a), PointRef(*this, b));
            });
        m_index = order;
    }

Output:

$ ./bin/pdal_filters_sort_test --gtest_filter=SortFilterTest.seventeen
Note: Google Test filter = SortFilterTest.seventeen
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SortFilterTest
[ RUN      ] SortFilterTest.seventeen
Before:
0: 1.44555
1: 15.1574
2: 3.22473
3: 6.76614
4: 12.6397
5: 9.52663
6: 13.7626
7: 8.69911
8: 16.9164
9: 16.4324
10: 7.24286
11: 11.101
12: 16.3461
13: 14.5858
14: 4.99844
15: 7.04896
16: 8.75318

After:
0: 1.44555
1: 3.22473
2: 4.99844
3: 6.76614
4: 7.04896
5: 7.24286
6: 8.69911
7: 8.75318
8: 9.52663
9: 11.101
10: 12.6397
11: 13.7626
12: 14.5858
13: 15.1574
14: 16.3461
15: 16.4324
16: 16.9164
[       OK ] SortFilterTest.seventeen (0 ms)
[----------] 1 test from SortFilterTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

In place

Sort m_index in place: failure.

    template <typename Compare>
    void stableSort(Compare compare)
    {
        std::stable_sort(
            m_index.begin(),
            m_index.end(),
            [this, &compare](const PointId a, const PointId b)
            {
                return compare(PointRef(*this, a), PointRef(*this, b));
            });
    }

Output:

$ ./bin/pdal_filters_sort_test --gtest_filter=SortFilterTest.seventeen
Note: Google Test filter = SortFilterTest.seventeen
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SortFilterTest
[ RUN      ] SortFilterTest.seventeen
Before:
0: 1.44555
1: 15.1574
2: 3.22473
3: 6.76614
4: 12.6397
5: 9.52663
6: 13.7626
7: 8.69911
8: 16.9164
9: 16.4324
10: 7.24286
11: 11.101
12: 16.3461
13: 14.5858
14: 4.99844
15: 7.04896
16: 8.75318

After:
0: 1.44555
1: 3.22473
2: 15.1574
3: 6.76614
4: 9.52663
5: 12.6397
6: 8.69911
7: 13.7626
8: 16.4324
9: 7.24286
10: 11.101
11: 16.3461
12: 14.5858
13: 4.99844
14: 7.04896
15: 8.75318
16: 16.9164
/Users/connor/code/pdal/test/unit/filters/SortFilterTest.cpp:94: Failure
Value of: d1 <= d2
  Actual: false
Expected: true
/Users/connor/code/pdal/test/unit/filters/SortFilterTest.cpp:94: Failure
Value of: d1 <= d2
  Actual: false
Expected: true
/Users/connor/code/pdal/test/unit/filters/SortFilterTest.cpp:94: Failure
Value of: d1 <= d2
  Actual: false
Expected: true
/Users/connor/code/pdal/test/unit/filters/SortFilterTest.cpp:94: Failure
Value of: d1 <= d2
  Actual: false
Expected: true
/Users/connor/code/pdal/test/unit/filters/SortFilterTest.cpp:94: Failure
Value of: d1 <= d2
  Actual: false
Expected: true
[  FAILED  ] SortFilterTest.seventeen (0 ms)
[----------] 1 test from SortFilterTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] SortFilterTest.seventeen

 1 FAILED TEST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment