Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type safety for indices #958

Closed
wants to merge 11 commits into from
10 changes: 10 additions & 0 deletions src/manifold/src/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@
}
}

/**
* @brief Type-safety for half-edge indices.
*
*/
struct HalfedgeIndex {
int ix;
};

inline size_t GetIndex(HalfedgeIndex ix) { return ix.ix; }

Check warning on line 127 in src/manifold/src/shared.h

View check run for this annotation

Codecov / codecov/patch

src/manifold/src/shared.h#L127

Added line #L127 was not covered by tests

/**
* The fundamental component of the halfedge data structure used for storing and
* operating on the Manifold.
Expand Down
15 changes: 10 additions & 5 deletions src/utilities/include/manifold/vec_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

namespace manifold {

/**
* Override this for a custom index type. e.g. HalfedgeIndex
*/
inline size_t GetIndex(size_t x) { return x; }

Check warning on line 24 in src/utilities/include/manifold/vec_view.h

View check run for this annotation

Codecov / codecov/patch

src/utilities/include/manifold/vec_view.h#L24

Added line #L24 was not covered by tests

/**
* View for Vec, can perform offset operation.
* This will be invalidated when the original vector is dropped or changes
* length. Roughly equivalent to std::span<T> from c++20
*/
template <typename T>
template <typename T, typename Ix = size_t>
class VecView {
public:
using Iter = T *;
Expand All @@ -45,14 +50,14 @@
// allows conversion to a const VecView
operator VecView<const T>() const { return {ptr_, size_}; }

inline const T &operator[](size_t i) const {
inline const T &operator[](Ix i) const {
ASSERT(i < size_, std::out_of_range("Vec out of range"));
return ptr_[i];
return ptr_[GetIndex(i)];
}

inline T &operator[](size_t i) {
inline T &operator[](Ix i) {
ASSERT(i < size_, std::out_of_range("Vec out of range"));
return ptr_[i];
return ptr_[GetIndex(i)];
}

IterC cbegin() const { return ptr_; }
Expand Down
Loading