Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
filter_iterator.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
45# ifndef FILTER_ITERATOR_H
46# define FILTER_ITERATOR_H
47
48# include <type_traits>
49# include <utility>
50# include <stdexcept>
51# include <ah-concepts.H>
52# include <ah-errors.H>
53# include <ah-errors.H>
54
55namespace Aleph
56{
57
68template <class Container>
69inline constexpr bool filter_iterator_keeps_copy =
70 std::is_pointer_v<Container> or
71 requires { requires Container::filter_iterator_keeps_a_copy; };
72
73
145template <class Container, class It, class Show_Item>
146class Filter_Iterator : public It
147{
148 Show_Item show_item;
149
150 static constexpr bool keeps_copy = filter_iterator_keeps_copy<Container>;
151
152 // the container itself, or its address
153 std::conditional_t<keeps_copy, Container, const Container *> container_ptr{};
154
155 static auto kept(const Container & c) noexcept
156 {
157 if constexpr (keeps_copy)
158 return c;
159 else
160 return &c;
161 }
162
163 bool container_set() const noexcept
164 {
165 if constexpr (keeps_copy and not std::is_pointer_v<Container>)
166 return not container_ptr.is_null_view();
167 else
168 return container_ptr != nullptr;
169 }
170
172 void *cookie = nullptr;
173
174 // what the filter says of an item cannot throw
175 static constexpr bool shows_ne =
177
178 // the base iterator has get_curr_ne() and next_ne(), which do from an
179 // item what get_curr() and next() do, without their checks
180 static constexpr bool unchecked = AlephUncheckedIterator<It>;
181
182 // the item of the base iterator, which has one
183 decltype(auto) base_curr()
184 {
185 if constexpr (unchecked)
186 return this->It::get_curr_ne();
187 else
188 return this->It::get_curr();
189 }
190
201 {
202 while (this->It::has_curr() and not static_cast<bool>(show_item(base_curr())))
203 if constexpr (unchecked)
204 this->It::next_ne();
205 else
206 this->It::next();
207 }
208
209 // forward() without a current item: past the end, the base iterator
210 // throws std::overflow_error, and the iterator stays there; before the
211 // first item, as an array iterator can be, it goes to that item. Out of
212 // line, as the loops that ask has_curr() first never come here.
213 [[gnu::noinline, gnu::cold]] void forward_without_curr()
214 {
215 try
216 {
217 this->It::next();
218 }
219 catch (const std::overflow_error &)
220 {
221 return; // past the end, as it was
222 }
224 }
225
235 void forward()
236 {
237 if constexpr (unchecked)
238 {
239 if (not this->It::has_curr()) [[unlikely]]
240 return forward_without_curr();
241 do
242 this->It::next_ne();
243 while (this->It::has_curr()
244 and not static_cast<bool>(show_item(this->It::get_curr_ne())));
245 }
246 else
247 {
248 try
249 {
250 this->It::next();
251 }
252 catch (const std::overflow_error &)
253 {
254 return; // past the end, as it was
255 }
257 }
258 }
259
270 {
271 this->It::reset_last();
272 while (this->It::has_curr() and not static_cast<bool>(show_item(this->It::get_curr())))
273 this->It::prev();
274 }
275
286 void backward()
287 {
288 try
289 {
290 this->It::prev();
291 }
292 catch (const std::underflow_error &)
293 {
294 return; // before the first item, as it was
295 }
296 while (this->It::has_curr() and not static_cast<bool>(show_item(this->It::get_curr())))
297 this->It::prev();
298 }
299
300public:
307 static constexpr bool goes_back = requires (It & i) { i.prev(); };
308
312 static constexpr bool goes_to_last = requires (It & i) { i.reset_last(); i.prev(); };
313
315 using Item_Type = typename It::Item_Type;
316
319
322
324 using Filter_Type = Show_Item;
325
332 const Container & get_container() const
333 {
334 ah_domain_error_if(not container_set()) << "Filter_Iterator: no container set";
335 if constexpr (keeps_copy)
336 return container_ptr;
337 else
338 return *container_ptr;
339 }
340
345 bool has_container() const noexcept { return container_set(); }
346
353 It & get_iterator() noexcept { return *this; }
354
359 const It & get_iterator() const noexcept { return *this; }
360
369 Show_Item & get_filter() noexcept { return show_item; }
370
375 const Show_Item & get_filter() const noexcept { return show_item; }
376
389 void set_filter(Show_Item si)
390 requires std::is_move_assignable_v<Show_Item>
391 { show_item = std::move(si); }
392
400 void set_cookie(void *__cookie) noexcept { cookie = __cookie; }
401
406 void * get_cookie() const noexcept { return cookie; }
407
419 Filter_Iterator(Show_Item si = Show_Item()) noexcept
420 : show_item(si)
421 {
422 // empty
423 }
424
435 Filter_Iterator(const Container & c, Show_Item si = Show_Item())
436 : It(c), show_item(si), container_ptr(kept(c))
437 {
439 }
440
446 Filter_Iterator(Container & c, Show_Item si = Show_Item())
447 : It(c), show_item(si), container_ptr(kept(c))
448 {
450 }
451
462 Filter_Iterator(const Container & c, void *__cookie,
463 Show_Item si = Show_Item())
464 : Filter_Iterator(c, si)
465 {
466 cookie = __cookie;
467 }
468
470 Filter_Iterator(Container & c, void *__cookie,
471 Show_Item si = Show_Item())
472 : Filter_Iterator(c, si)
473 {
474 cookie = __cookie;
475 }
476
485 void next() { forward(); }
486
493 void next_ne() noexcept(shows_ne) { forward(); }
494
506 void prev() requires goes_back { backward(); }
507
514 void prev_ne() noexcept(shows_ne) requires goes_back { backward(); }
515
522 {
523 this->It::reset_first();
525 }
526
535 void reset_last() requires goes_to_last { goto_last_valid_item(); }
536
547 size_t count()
548 {
549 size_t n = 0;
550 for (reset_first(); this->has_curr(); next())
551 ++n;
552 return n;
553 }
554
564 bool empty()
565 {
566 reset_first();
567 return not this->has_curr();
568 }
569
584 template <typename Op>
585 void for_each(Op op)
586 {
587 for (reset_first(); this->has_curr(); next())
588 op(this->get_curr());
589 }
590
602 template <typename Pred>
603 bool find_if(Pred pred)
604 {
605 for (reset_first(); this->has_curr(); next())
606 if (pred(this->get_curr()))
607 return true;
608 return false;
609 }
610};
611
619template <typename T>
621{
623 bool operator()(const T &) const noexcept { return true; }
624};
625
633template <typename T>
635{
637 bool operator()(const T &) const noexcept { return false; }
638};
639
640} // end namespace Aleph
641
642# endif // FILTER_ITERATOR_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
Generic filter iterator wrapper.
void reset_last()
Resets the iterator to the last filtered element.
void set_cookie(void *__cookie) noexcept
Sets the cookie pointer.
void next()
Advances the iterator to the next filtered element.
const Container & get_container() const
Returns a const reference to the container being iterated over.
Filter_Iterator(const Container &c, Show_Item si=Show_Item())
Constructs a filter iterator over a container.
static constexpr bool unchecked
It & get_iterator() noexcept
Returns a reference to the underlying base iterator.
static constexpr bool goes_to_last
Whether the base iterator goes to its last item and moves back, as reset_last() needs.
void prev()
Moves the iterator backward to the previous filtered element.
Filter_Iterator(Container &c, Show_Item si=Show_Item())
Filter over the modifiable container c.
Filter_Iterator(const Container &c, void *__cookie, Show_Item si=Show_Item())
Constructs a filter iterator with a cookie.
const It & get_iterator() const noexcept
Returns a const reference to the underlying base iterator.
static constexpr bool goes_back
Whether the base iterator moves back, as prev() and prev_ne() need.
bool container_set() const noexcept
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Show_Item Filter_Type
The type of the filter functor.
static constexpr bool shows_ne
decltype(auto) base_curr()
Filter_Iterator(Container &c, void *__cookie, Show_Item si=Show_Item())
See Filter_Iterator(const Container &, void *, Show_Item); over a modifiable container.
typename It::Item_Type Item_Type
The type of element returned by get_curr()
static auto kept(const Container &c) noexcept
Filter_Iterator(Show_Item si=Show_Item()) noexcept
Default constructor.
static constexpr bool keeps_copy
void * get_cookie() const noexcept
Gets the cookie pointer.
bool empty()
Check if there are no elements that pass the filter.
size_t count()
Count the number of elements that pass the filter.
void backward()
Move backward to the previous element that passes the filter.
void * cookie
User-defined pointer for extensibility (e.g., passing context data to filter)
void reset_first()
Resets the iterator to the first filtered element.
bool has_container() const noexcept
Check if a container has been set.
void set_filter(Show_Item si)
Sets a new filter functor.
void prev_ne() noexcept(shows_ne)
Moves the iterator backward, as prev() does.
void forward()
Move forward to the next element that passes the filter.
void goto_first_valid_item()
Advance to the first element that passes the filter.
const Show_Item & get_filter() const noexcept
Returns a const reference to the filter functor.
void for_each(Op op)
Apply a function to all filtered elements.
std::conditional_t< keeps_copy, Container, const Container * > container_ptr
bool find_if(Pred pred)
Find the first element that satisfies an additional predicate.
Show_Item & get_filter() noexcept
Returns a reference to the filter functor.
void goto_last_valid_item()
Move to the last element that passes the filter.
Slinknc * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:1005
Slinknc * get_curr() const
Definition htlist.H:997
bool has_curr() const noexcept
Definition htlist.H:969
An AlephIterator whose get_curr_ne() gives what get_curr() gives, without its check.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:192
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
constexpr bool filter_iterator_keeps_copy
Whether a Filter_Iterator keeps a copy of its container, and not its address.
Default filter functor that shows all elements.
bool operator()(const T &) const noexcept
Always returns true (shows all elements).
Filter functor that hides all elements.
bool operator()(const T &) const noexcept
Always returns false (hides all elements).
Filter_Iterator< DynList< int >, DynList< int >::Iterator, Par > It
Definition test_htlist.C:61