aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-04-23 20:55:47 +0200
committerDavid Robillard <d@drobilla.net>2019-04-23 20:55:47 +0200
commit66f8ba7f06b9369276e6210f756b075591efa6fa (patch)
tree6c9601a60226a8e38dd1c227b10fb0e2f06e8d4b
parent045128813d1a69a6483ff2f8f870562b96be0a65 (diff)
downloadchilbert-66f8ba7f06b9369276e6210f756b075591efa6fa.tar.gz
chilbert-66f8ba7f06b9369276e6210f756b075591efa6fa.tar.bz2
chilbert-66f8ba7f06b9369276e6210f756b075591efa6fa.zip
Support using any array-like type for points
-rw-r--r--bin/chilbert_obj.cpp2
-rw-r--r--bin/chilbert_svg.cpp2
-rw-r--r--chilbert/chilbert.hpp16
-rw-r--r--chilbert/chilbert.ipp41
-rw-r--r--test/bench_hilbert.cpp6
-rw-r--r--test/test_hilbert.cpp12
6 files changed, 39 insertions, 40 deletions
diff --git a/bin/chilbert_obj.cpp b/bin/chilbert_obj.cpp
index 85fe95a..0f31fbd 100644
--- a/bin/chilbert_obj.cpp
+++ b/bin/chilbert_obj.cpp
@@ -39,7 +39,7 @@ main(int argc, char** argv)
// Vertices
for (uint64_t i = 0; i < num_points; ++i) {
std::array<uint32_t, 3> point;
- chilbert::index_to_coords(point.data(), 16, 3, i);
+ chilbert::index_to_coords(point, 16, 3, i);
printf("v %u %u %u\n", point[0], point[1], point[2]);
}
diff --git a/bin/chilbert_svg.cpp b/bin/chilbert_svg.cpp
index 1151a1a..dd4a3d4 100644
--- a/bin/chilbert_svg.cpp
+++ b/bin/chilbert_svg.cpp
@@ -52,7 +52,7 @@ main(int argc, char** argv)
// One polyline through all vertices
for (uint64_t i = 0; i <= num_points; ++i) {
std::array<uint32_t, 2> point;
- chilbert::index_to_coords(point.data(), 32, 2, i);
+ chilbert::index_to_coords(point, 32, 2, i);
printf("%u,%u ", point[0], point[1]);
}
diff --git a/chilbert/chilbert.hpp b/chilbert/chilbert.hpp
index 7a69ab2..e8d10f4 100644
--- a/chilbert/chilbert.hpp
+++ b/chilbert/chilbert.hpp
@@ -25,7 +25,7 @@ namespace chilbert {
/** Map the point `p` to a Hilbert Index.
*
- * @tparam P Type used to represent a value in a point dimension.
+ * @tparam P Array-like type with [] operator that represents a point.
* @tparam H Hilbert Index.
*
* @param p Point with `n` dimensions.
@@ -34,11 +34,11 @@ namespace chilbert {
* @param[out] h Hilbert Index.
*/
template <class P, class H>
-inline void coords_to_index(const P* p, size_t m, size_t n, H& h);
+inline void coords_to_index(const P& p, size_t m, size_t n, H& h);
/** Map the Hilbert Index `p` to a point.
*
- * @tparam P Type used to represent a value in a point dimension.
+ * @tparam P Array-like type with [] operator that represents a point.
* @tparam H Hilbert Index.
*
* @param[out] p Point with `n` dimensions.
@@ -47,11 +47,11 @@ inline void coords_to_index(const P* p, size_t m, size_t n, H& h);
* @param h Hilbert Index.
*/
template <class P, class H>
-inline void index_to_coords(P* p, size_t m, size_t n, const H& h);
+inline void index_to_coords(P& p, size_t m, size_t n, const H& h);
/** Map the point `p` to a Compact Hilbert Index.
*
- * @tparam P Type used to represent a value in a point dimension.
+ * @tparam P Array-like type with [] operator that represents a point.
* @tparam H Compact Hilbert Index.
*
* @param p Point with `n` dimensions.
@@ -62,7 +62,7 @@ inline void index_to_coords(P* p, size_t m, size_t n, const H& h);
* @param m Optional largest precision in `m`.
*/
template <class P, class H>
-inline void coords_to_compact_index(const P* p,
+inline void coords_to_compact_index(const P& p,
const size_t* ms,
size_t n,
H& hc,
@@ -71,7 +71,7 @@ inline void coords_to_compact_index(const P* p,
/** Map the Compact Hilbert Index `hc` to a point.
*
- * @tparam P Type used to represent a value in a point dimension.
+ * @tparam P Array-like type with [] operator that represents a point.
* @tparam H Compact Hilbert Index.
*
* @param[out] p Point with `n` dimensions.
@@ -82,7 +82,7 @@ inline void coords_to_compact_index(const P* p,
* @param m Optional largest precision in `m`.
*/
template <class P, class H>
-inline void compact_index_to_coords(P* p,
+inline void compact_index_to_coords(P& p,
const size_t* ms,
size_t n,
const H& hc,
diff --git a/chilbert/chilbert.ipp b/chilbert/chilbert.ipp
index a62a258..fca7516 100644
--- a/chilbert/chilbert.ipp
+++ b/chilbert/chilbert.ipp
@@ -112,7 +112,7 @@ set_bits(H& h, const size_t n, const size_t i, const I& w)
*/
template <class P, class I>
inline void
-get_location(const P* const p, const size_t n, const size_t i, I& l)
+get_location(const P& p, const size_t n, const size_t i, I& l)
{
for (size_t j = 0; j < n; ++j) {
set_bit(l, j, test_bit(p[j], i));
@@ -128,7 +128,7 @@ get_location(const P* const p, const size_t n, const size_t i, I& l)
*/
template <class P, class I>
inline void
-set_location(P* const p, const size_t n, const size_t i, const I& l)
+set_location(P& p, const size_t n, const size_t i, const I& l)
{
for (size_t j = 0; j < n; ++j) {
set_bit(p[j], i, test_bit(l, j));
@@ -140,6 +140,7 @@ template <class I>
inline void
transform(const I& e, const size_t d, const size_t n, I& a)
{
+ (void)n;
assert(a.size() == n);
a ^= e;
a.rotr(d);
@@ -201,12 +202,12 @@ update2(const I& l, const I& t, const size_t n, I& e, size_t& d)
template <class P, class H, class I>
inline void
-coords_to_index(const P* const p,
- const size_t m,
- const size_t n,
- H& h,
- I&& scratch,
- size_t* const ds = nullptr)
+coords_to_index(const P& p,
+ const size_t m,
+ const size_t n,
+ H& h,
+ I&& scratch,
+ size_t* const ds = nullptr)
{
I e{std::move(scratch)};
I l{e};
@@ -252,7 +253,7 @@ coords_to_index(const P* const p,
template <class P, class H, class I>
inline void
-index_to_coords(P* p, const size_t m, const size_t n, const H& h, I&& scratch)
+index_to_coords(P& p, const size_t m, const size_t n, const H& h, I&& scratch)
{
I e{std::move(scratch)};
I l{e};
@@ -294,7 +295,7 @@ index_to_coords(P* p, const size_t m, const size_t n, const H& h, I&& scratch)
template <class P, class HC, class I>
inline void
-coords_to_compact_index(const P* const p,
+coords_to_compact_index(const P& p,
const size_t* const ms,
const size_t n,
HC& hc,
@@ -340,7 +341,7 @@ coords_to_compact_index(const P* const p,
template <class P, class HC, class I>
inline void
-compact_index_to_coords(P* const p,
+compact_index_to_coords(P& p,
const size_t* ms,
const size_t n,
const HC& hc,
@@ -416,7 +417,7 @@ compact_index_to_coords(P* const p,
template <class P, class H>
inline void
-coords_to_index(const P* const p, const size_t m, const size_t n, H& h)
+coords_to_index(const P& p, const size_t m, const size_t n, H& h)
{
assert(detail::num_bits(h) >= n * m);
assert(detail::num_bits(p[0]) >= m);
@@ -433,7 +434,7 @@ coords_to_index(const P* const p, const size_t m, const size_t n, H& h)
template <class P, class H>
inline void
-index_to_coords(P* const p, const size_t m, const size_t n, const H& h)
+index_to_coords(P& p, const size_t m, const size_t n, const H& h)
{
assert(m > 0);
assert(n > 0);
@@ -452,7 +453,7 @@ index_to_coords(P* const p, const size_t m, const size_t n, const H& h)
template <class P, class HC>
inline void
-coords_to_compact_index(const P* const p,
+coords_to_compact_index(const P& p,
const size_t* const ms,
size_t n,
HC& hc,
@@ -474,12 +475,12 @@ coords_to_compact_index(const P* const p,
template <class P, class HC>
inline void
-compact_index_to_coords(P* const p,
- const size_t* ms,
- const size_t n,
- const HC& hc,
- const size_t M,
- const size_t m)
+compact_index_to_coords(P& p,
+ const size_t* const ms,
+ const size_t n,
+ const HC& hc,
+ const size_t M,
+ const size_t m)
{
assert(hc.size() >= std::accumulate(ms, ms + n, size_t(0)));
diff --git a/test/bench_hilbert.cpp b/test/bench_hilbert.cpp
index c5f1ad9..e216efe 100644
--- a/test/bench_hilbert.cpp
+++ b/test/bench_hilbert.cpp
@@ -33,8 +33,7 @@ struct BenchCoordsToIndex
const auto p = make_random_point<M, D>(ctx);
H ha = make_zero_bitvec<H, D * M>();
- return run_bench(
- [&](auto) { chilbert::coords_to_index(p.data(), M, D, ha); });
+ return run_bench([&](auto) { chilbert::coords_to_index(p, M, D, ha); });
}
};
@@ -46,8 +45,7 @@ struct BenchIndexToCoords
auto p = make_random_point<M, D>(ctx);
const H ha = make_random_bitvec<H, D * M>(ctx);
- return run_bench(
- [&](auto) { chilbert::index_to_coords(p.data(), M, D, ha); });
+ return run_bench([&](auto) { chilbert::index_to_coords(p, M, D, ha); });
}
};
diff --git a/test/test_hilbert.cpp b/test/test_hilbert.cpp
index 1d32668..5d1f52a 100644
--- a/test/test_hilbert.cpp
+++ b/test/test_hilbert.cpp
@@ -92,12 +92,12 @@ test_standard(Context& ctx)
H ha = make_zero_bitvec<H, D * M>();
assert(ha.size() >= D * M);
- chilbert::coords_to_index(pa.data(), M, D, ha);
+ chilbert::coords_to_index(pa, M, D, ha);
{
// Ensure unmapping results in the original point
auto pa_out = make_random_point<M, D>(ctx);
- chilbert::index_to_coords(pa_out.data(), M, D, ha);
+ chilbert::index_to_coords(pa_out, M, D, ha);
assert(pa_out == pa);
}
@@ -110,7 +110,7 @@ test_standard(Context& ctx)
// Unmap next hilbert index to a point
auto pb = make_random_point<M, D>(ctx);
- chilbert::index_to_coords(pb.data(), M, D, hb);
+ chilbert::index_to_coords(pb, M, D, hb);
// Ensure next point is 1 unit of distance away from first
assert(squared_distance(pa, pb) == 1);
@@ -128,12 +128,12 @@ test_compact(Context& ctx)
T ha = make_zero_bitvec<T, D * M>();
assert(ha.size() >= D * M);
- chilbert::coords_to_compact_index(pa.data(), ms.data(), D, ha);
+ chilbert::coords_to_compact_index(pa, ms.data(), D, ha);
{
// Ensure unmapping results in the original point
auto pa_out = make_random_point<M, D>(ctx);
- chilbert::compact_index_to_coords(pa_out.data(), ms.data(), D, ha);
+ chilbert::compact_index_to_coords(pa_out, ms.data(), D, ha);
assert(pa_out == pa);
}
@@ -146,7 +146,7 @@ test_compact(Context& ctx)
// Unmap next hilbert index to a point
auto pb = make_random_point<M, D>(ctx);
- chilbert::compact_index_to_coords(pb.data(), ms.data(), D, hb);
+ chilbert::compact_index_to_coords(pb, ms.data(), D, hb);
// Ensure next point is 1 unit of distance away from first
assert(squared_distance(pa, pb) == 1);