Lumiera  0.pre.03
»edit your freedom«
index-iter.hpp
Go to the documentation of this file.
1 /*
2  INDEX-ITER.hpp - iterator with indexed random-access to referred container
3 
4  Copyright (C)
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
31 #ifndef SRC_LIB_INDEX_ITER_H
32 #define SRC_LIB_INDEX_ITER_H
33 
34 
35 #include "lib/iter-adapter.hpp"
36 
37 
38 namespace lib {
39  namespace iter {
40 
45  template<typename PTR>
47  {
48  PTR data_{};
49  size_t idx_{0};
50 
51  using ResVal = decltype(data_->operator[](0));
52 
53  using value_type = typename meta::RefTraits<ResVal>::Value;
54  using reference = typename meta::RefTraits<ResVal>::Reference;
55 
56  bool
57  checkPoint() const
58  {
59  return isValidIDX(idx_);
60  }
61 
62  reference
63  yield() const
64  {
65  return (*data_)[idx_];
66  }
67 
68  void
69  iterNext()
70  {
71  ++idx_;
72  }
73 
74 
75  bool
76  isValidIDX (size_t idx) const
77  {
78  return bool(data_)
79  and idx < data_->size();
80  }
81 
83 
84 
85  friend bool operator== (IndexAccessCore const& c1, IndexAccessCore const& c2)
86  {
87  return c1.data_ == c2.data_ and (not c1.data_ or c1.idx_ == c2.idx_);
88  }
89  friend bool operator!= (IndexAccessCore const& c1, IndexAccessCore const& c2)
90  {
91  return not (c1 == c2);
92  }
93  };
94  //
95  }//(End)Implementation
96 
97 
98 
111  template<class CON, typename PTR = CON*>
112  class IndexIter
113  : public iter::IndexAccessCore<PTR>::IterWrapper
114  {
115  using _Cor = iter::IndexAccessCore<PTR>;
116  using _Par = typename _Cor::IterWrapper;
117 
118  public:
119  IndexIter() = default;
120  IndexIter (CON& container) : IndexIter{&container}{ };
121  IndexIter (PTR pContainer)
122  : _Par{_Cor{pContainer, 0}}
123  { }
124 
125 
126  size_t
127  getIDX() const
128  {
129  _Par::__throw_if_empty();
130  return _Par::stateCore().idx_;
131  }
132 
133  void
134  setIDX (size_t newIDX)
135  {
136  auto& core = _Par::stateCore();
137  if (not core.isValidIDX (newIDX))
138  throw lumiera::error::Invalid ("Attempt to set index out of bounds",
139  lumiera::error::LUMIERA_ERROR_INDEX_BOUNDS);
140  core.idx_ = newIDX;
141  }
142  };
143 
144 
145 } // namespace lib
146 #endif /*SRC_LIB_INDEX_ITER_H*/
Helper template(s) for creating Lumiera Forward Iterators.
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Another Lumiera Forward Iterator building block, based on incorporating a state type as »*State Core*...
Subscript-index based access to a container, packaged as iterator.
Definition: index-iter.hpp:112
Implementation of a »IterStateCore« to access the container through an embedded index variable...
Definition: index-iter.hpp:46