Skip to content

Commit bb7d78a

Browse files
committed
Make all BufferIterators shaped
1 parent 7a997ad commit bb7d78a

6 files changed

Lines changed: 298 additions & 399 deletions

File tree

dwave/optimization/include/dwave-optimization/array.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,18 @@ struct Update {
363363
class Array {
364364
public:
365365
/// A std::random_access_iterator over the values in the array.
366-
using iterator = BufferIterator<double, double, false>;
366+
using iterator = BufferIterator<double, double>;
367367

368368
/// A std::random_access_iterator over the values in the array.
369-
using const_iterator = BufferIterator<double, double, true>;
369+
using const_iterator = BufferIterator<const double, const double>;
370370

371371
template <class T>
372372
using cache_type = std::unordered_map<const Array*, T>;
373373

374374
template <class T>
375375
using optional_cache_type = std::optional<std::reference_wrapper<cache_type<T>>>;
376376

377-
using View = std::ranges::subrange<const_iterator>;
377+
using View = std::ranges::subrange<const_iterator, const_iterator>;
378378

379379
/// Constant used to signal that the size is based on the state.
380380
static constexpr ssize_t DYNAMIC_SIZE = -1;
@@ -436,16 +436,25 @@ class Array {
436436
// Interface methods ******************************************************
437437

438438
/// Return an iterator to the beginning of the array.
439-
const_iterator begin(const State& state) const {
440-
if (contiguous()) return const_iterator(buff(state));
439+
auto begin(const State& state) const {
440+
if (ndim() == 0) {
441+
// A 0d iterator is pretty ill-defined, so we return a 1d one.
442+
// The iterator doesn't manage the lifespan of the shape/strides
443+
// so we need them to be static here.
444+
static constexpr ssize_t shape = 1;
445+
static constexpr ssize_t strides = 0;
446+
return const_iterator(buff(state), 1, &shape, &strides);
447+
}
441448
return const_iterator(buff(state), ndim(), shape().data(), strides().data());
442449
}
443450

444-
/// Return an iterator to the end of the array.
445-
const_iterator end(const State& state) const { return this->begin(state) + this->size(state); }
451+
/// Return a sentinel indicating the end of the array's state as a flattened array.
452+
auto end(const State& state) const { return begin(state) + size(state); }
446453

447454
/// Return a container-like view over the array.
448-
const View view(const State& state) const { return View(begin(state), end(state)); }
455+
const View view(const State& state) const {
456+
return std::ranges::subrange(begin(state), end(state));
457+
}
449458

450459
/// The number of doubles in the flattened array.
451460
/// If the size is dependent on the state, returns Array::DYNAMIC_SIZE.

0 commit comments

Comments
 (0)