-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e184e8c
commit 94525b6
Showing
2 changed files
with
28 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef CP_ALGO_UTIL_COMPRESS_COORDS_HPP | ||
#define CP_ALGO_UTIL_COMPRESS_COORDS_HPP | ||
#include <algorithm> | ||
#include <vector> | ||
namespace cp_algo { | ||
std::vector<int> compress_coords(auto &coords) { | ||
static_assert(std::is_pointer_v<std::ranges::range_value_t<decltype(coords)>>); | ||
std::vector<int> original; | ||
original.reserve(size(coords)); | ||
std::ranges::sort(coords, {}, [](int* x) {return *x;}); | ||
int idx = -1, prev = -1; | ||
for(auto x: coords) { | ||
if(*x != prev) { | ||
idx++; | ||
prev = *x; | ||
original.push_back(*x); | ||
} | ||
*x = idx; | ||
} | ||
return original; | ||
} | ||
} | ||
#endif // CP_ALGO_UTIL_COMPRESS_COORDS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters