Skip to content

Commit

Permalink
Walk indices backward in STLIterator
Browse files Browse the repository at this point in the history
It isn't faster or slower, but it's simpler. It's also what we do in ply_xxx().

* ra/ply.hh (STLIterator): As stated.
* test/iterator-small.cc: Fix test, depended on internal detail.
  • Loading branch information
lloda committed Nov 28, 2023
1 parent 5cd0b51 commit a601ef3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
4 changes: 2 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ checkbox [C-ct] flip TODO
- [ ] remove inheritance relationship of Container on View
- [ ] make View be View<pointer> and not View<value_type>
- [ ] Should be able to turn ravel iterators (e.g. View::begin()) into array iterators. Ptr()
doesn't work for that. Or to obtain STLIterator from any array iterator. That may come of
merging STLIterator with plyers.
doesn't work for that.
- This has become more feasible after changing the iterator interface from flat to saveload,
main obstacle atm seems to be the need to support copy. <2023-11-20 Mon 12:45>
- One can now create a ravel iterator from an IteratorConcept <2023-11-28 Tue 16:37>
- [ ] gemv(conj(a), b) should work. Beat View-like selectors down an Expr??
- [ ] port some of the View ops to generic Iterator. reverse, transpose, etc. seem easy
enough. Only it kind of bothers me that they need their own Expr-like types while on Views
Expand Down
22 changes: 6 additions & 16 deletions ra/ply.hh
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ early(IteratorConcept auto && a, auto && def) { return ply(RA_FWD(a), Default {


// --------------------
// STLIterator for CellSmall / CellBig. FIXME make it work for any IteratorConcept.
// iterator adapter for the standard library
// --------------------

template <IteratorConcept A>
Expand All @@ -346,18 +346,8 @@ struct STLIterator
shape_type ind;
bool over;

STLIterator(A a_)
: a(a_),
ind([&] {
if constexpr (ANY==rank_s<A>()) {
return shape_type(rank(a), 0);
} else {
return shape_type {0};
}
}()),
// [ra12] mark empty range. FIXME make 0==size() more efficient.
over(0==ra::size(a))
{}
STLIterator(A a_): a(a_), ind(ra::shape(a_)), over(0==ra::size(a)) {}
constexpr STLIterator(STLIterator && it) = default;
constexpr STLIterator(STLIterator const & it) = delete;
constexpr STLIterator & operator=(STLIterator && it) = default;
Expand All @@ -369,11 +359,11 @@ struct STLIterator
next(rank_t k)
{
for (; k>=0; --k) {
if (++ind[k]<a.len(k)) {
if (--ind[k]>0) {
a.adv(k, 1);
return;
} else {
ind[k] = 0;
ind[k] = a.len(k);
a.adv(k, 1-a.len(k));
}
}
Expand All @@ -384,10 +374,10 @@ struct STLIterator
next()
{
if constexpr (k>=0) {
if (++ind[k]<a.len(k)) {
if (--ind[k]>0) {
a.adv(k, 1);
} else {
ind[k] = 0;
ind[k] = a.len(k);
a.adv(k, 1-a.len(k));
next<k-1>();
}
Expand Down
9 changes: 6 additions & 3 deletions test/iterator-small.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ int main()
++i;
}
}
tr.section("STL style with non-default steps, which keeps indices (as internal detail)");
tr.section("STL style with non-default steps");
{
ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
auto b = transpose<1, 0>(a);
tr.test_eq(ra::start({0, 0}), ra::start(b.begin().ind));
tr.test_eq(ra::start({0, 1}), ra::start((++b.begin()).ind));

// we don't necessarily walk ind this way.
// tr.test_eq(ra::start({0, 0}), ra::start(b.begin().ind));
// tr.test_eq(ra::start({0, 1}), ra::start((++b.begin()).ind));

tr.test(!std::is_same_v<int *, decltype(b.begin())>);
int bref[6] = {1, 4, 2, 5, 3, 6};
for (int i=0; auto && bi: b) {
Expand Down

0 comments on commit a601ef3

Please sign in to comment.