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

Make const element proxy. #2388

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions arbor/util/piecewise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@

#include "util/iterutil.hpp"
#include "util/transform.hpp"
#include "util/meta.hpp"
#include "util/partition.hpp"

namespace arb {
Expand Down Expand Up @@ -160,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 @@ -170,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 @@ -239,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
12 changes: 9 additions & 3 deletions arbor/util/pw_over_cable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ struct get_value {
// Convert mcable_map values to a piecewise function over an mcable.
// The projection gives the map from the values in the mcable_map to the values in the piecewise function.
template <typename T, typename U, typename Proj = impl::get_value>
util::pw_elements<U> pw_over_cable(const mcable_map<T>& mm, mcable cable, U dflt_value, Proj projection = Proj{}) {
util::pw_elements<U> pw_over_cable(const mcable_map<T>& mm,
mcable cable,
U dflt_value,
Proj projection = Proj{}) {
using value_type = typename mcable_map<T>::value_type;
msize_t bid = cable.branch;

Expand All @@ -26,8 +29,11 @@ util::pw_elements<U> pw_over_cable(const mcable_map<T>& mm, mcable cable, U dflt
as_branch(msize_t x): value(x) {}
};

auto map_on_branch = util::make_range(
std::equal_range(mm.begin(), mm.end(), bid, [](as_branch a, as_branch b) { return a.value<b.value; }));
auto map_on_branch = util::make_range(std::equal_range(mm.begin(), mm.end(),
bid,
[](as_branch a, as_branch b) {
return a.value < b.value;
}));

if (map_on_branch.empty()) {
return util::pw_elements<U>({cable.prox_pos, cable.dist_pos}, {dflt_value});
Expand Down