Skip to content

Commit

Permalink
Make a const proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstenhater committed Aug 20, 2024
1 parent 867f219 commit 1d422ab
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions arbor/util/piecewise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ struct pw_element_proxy {
extent(pw.extent(i)), value(pw.value(i)) {}

operator pw_element<X>() const { return pw_element<X>{extent, value}; }
operator X() const { return value; }
pw_element_proxy& operator=(X x) {value = std::move(x); return *this; };
operator X&() const { return value; }
pw_element_proxy& operator=(X x) { value = std::move(x); return *this; };

double lower_bound() const { return extent.first; }
double upper_bound() const { return extent.second; }
Expand All @@ -169,6 +169,21 @@ struct pw_element_proxy {
X& value;
};

template <typename X>
struct pw_element_proxy<const X> {
pw_element_proxy(const pw_elements<X>& pw, pw_size_type i):
extent(pw.extent(i)), value(pw.value(i)) {}

operator pw_element<X>() const { return pw_element<X>{extent, value}; }
operator const X&() const { return value; }

double lower_bound() const { return extent.first; }
double upper_bound() const { return extent.second; }

const std::pair<double, double> extent;
const X& value;
};

// Compute indices into vertex set corresponding to elements that cover a point x:

namespace {
Expand Down Expand Up @@ -238,12 +253,12 @@ struct pw_elements {
const_iterator(): pw_(nullptr) {}

using value_type = pw_element<X>;
using pointer = const pointer_proxy<pw_element<X>>;
using reference = pw_element<X>;
using pointer = const pointer_proxy<pw_element_proxy<const X>>;
using reference = pw_element_proxy<const X>;

reference operator[](difference_type j) const { return (*pw_)[j+*c_]; }
reference operator*() const { return (*pw_)[*c_]; }
pointer operator->() const { return pointer{(*pw_)[*c_]}; }
reference operator[](difference_type j) const { return {*pw_, j + *c_}; }
reference operator*() const { return {*pw_, *c_}; }
pointer operator->() const { return pointer{reference{*pw_, *c_}}; }

// (required for iterator_adaptor)
counter<pw_size_type>& inner() { return c_; }
Expand Down

0 comments on commit 1d422ab

Please sign in to comment.