2015-12-06 00:10:27 +01:00
|
|
|
|
/*
|
|
|
|
|
|
ITER-CURSOR.hpp - wrap bidirectional STL container iterators
|
|
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2015, Hermann Vosseler <Ichthyostega@web.de>
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file iter-cursor.hpp
|
|
|
|
|
|
** An iterator with the ability to switch direction.
|
|
|
|
|
|
** This wrapper relies on the ability of typical STL container iterators
|
|
|
|
|
|
** to work in both directions, similar to std::reverse_iterator.
|
|
|
|
|
|
** Yet it is a single, self-contained element and in compliance to the
|
2018-09-21 13:46:42 +02:00
|
|
|
|
** ["Lumiera Forward Iterator"](\ref iter-adapter.hpp) concept. But it has
|
2017-04-02 04:22:51 +02:00
|
|
|
|
** the additional ability to [switch the working direction](\ref IterCursor<IT>::switchDir).
|
2015-12-06 00:10:27 +01:00
|
|
|
|
**
|
|
|
|
|
|
** @see IterCursor_test
|
|
|
|
|
|
** @see iter-adapter.hpp
|
2018-09-21 13:46:42 +02:00
|
|
|
|
** @see [usage example](\ref event-log.hpp)
|
2015-12-06 00:10:27 +01:00
|
|
|
|
**
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_LIB_ITER_CURSOR_H
|
|
|
|
|
|
#define SRC_LIB_ITER_CURSOR_H
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/error.hpp"
|
|
|
|
|
|
#include "lib/iter-adapter.hpp"
|
|
|
|
|
|
|
2015-12-06 02:28:47 +01:00
|
|
|
|
#include <type_traits>
|
2015-12-06 00:10:27 +01:00
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace iter {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @internal implementation for a "gear switching" iterator,
|
|
|
|
|
|
* based on STL container iterators.
|
|
|
|
|
|
*/
|
|
|
|
|
|
template<class IT>
|
|
|
|
|
|
class CursorGear
|
|
|
|
|
|
{
|
|
|
|
|
|
bool backwards_{false};
|
|
|
|
|
|
const IT start_;
|
|
|
|
|
|
const IT end_;
|
|
|
|
|
|
|
|
|
|
|
|
IT pos_;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2026-03-28 18:09:24 +01:00
|
|
|
|
using pointer = iter::Trait<IT>::pointer;
|
|
|
|
|
|
using reference = iter::Trait<IT>::reference;
|
|
|
|
|
|
using value_type = iter::Trait<IT>::value_type;
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorGear()
|
|
|
|
|
|
: start_()
|
|
|
|
|
|
, end_()
|
|
|
|
|
|
, pos_()
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
CursorGear (IT&& begin, IT&& end)
|
|
|
|
|
|
: start_(std::forward<IT>(begin))
|
|
|
|
|
|
, end_(std::forward<IT>(end))
|
|
|
|
|
|
, pos_(start_)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
// using default copy/assignment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-12-06 03:35:07 +01:00
|
|
|
|
reverse(bool backwards)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (backwards != backwards_) reverse();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
reverse()
|
2015-12-06 00:10:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (start_ == end_) return;
|
2015-12-06 02:28:47 +01:00
|
|
|
|
if (backwards_)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pos_ != start_) --pos_;
|
|
|
|
|
|
backwards_ = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pos_ != end_) ++pos_;
|
|
|
|
|
|
backwards_ = true;
|
|
|
|
|
|
}
|
2015-12-06 00:10:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* === Iteration control API for IterStateWrapper == */
|
|
|
|
|
|
|
2017-12-05 03:28:00 +01:00
|
|
|
|
bool
|
|
|
|
|
|
checkPoint() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return backwards_? pos_ != start_
|
|
|
|
|
|
: pos_ != end_;
|
|
|
|
|
|
}
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
2017-12-05 03:28:00 +01:00
|
|
|
|
reference
|
|
|
|
|
|
yield() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return backwards_? *(pos_-1)
|
|
|
|
|
|
: *(pos_);
|
|
|
|
|
|
}
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
2017-12-05 03:28:00 +01:00
|
|
|
|
void
|
|
|
|
|
|
iterNext()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (backwards_)
|
|
|
|
|
|
--pos_;
|
|
|
|
|
|
else
|
|
|
|
|
|
++pos_;
|
|
|
|
|
|
}
|
2015-12-06 02:28:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
friend bool
|
|
|
|
|
|
operator== (CursorGear const& g1, CursorGear const& g2)
|
|
|
|
|
|
{
|
2025-06-07 23:59:57 +02:00
|
|
|
|
return (not g1.checkPoint() and not g2.checkPoint()) // note: all exhausted iters are equal
|
2015-12-06 02:28:47 +01:00
|
|
|
|
or ( g1.pos_ == g2.pos_
|
|
|
|
|
|
and g1.backwards_ == g2.backwards_
|
|
|
|
|
|
and g1.start_ == g2.start_
|
|
|
|
|
|
and g1.end_ == g2.end_
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2015-12-06 00:10:27 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace iter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* A cursor-like iterator with the ability to switch iteration direction.
|
2015-12-06 02:28:47 +01:00
|
|
|
|
* It can be built on top of any bidirectional STL iterator or similar entity,
|
|
|
|
|
|
* which has an `--` operator. Initially, IterCursor will operate in forward
|
|
|
|
|
|
* direction; irrespective of the current direction, it always fulfils the
|
2018-09-21 13:46:42 +02:00
|
|
|
|
* ["Lumiera Forward Iterator"](\ref iter-adapter.hpp) concept, i.e. it can be
|
2015-12-06 02:28:47 +01:00
|
|
|
|
* iterated until exhaustion, in which case it will evaluate to bool(false).
|
|
|
|
|
|
* @note IterCursor instances can be equality compared, also taking the
|
|
|
|
|
|
* current direction into account. As a special case, all
|
|
|
|
|
|
* exhausted iterators are treated as equal.
|
2015-12-06 00:10:27 +01:00
|
|
|
|
*/
|
|
|
|
|
|
template<class IT>
|
|
|
|
|
|
class IterCursor
|
2024-11-26 22:15:33 +01:00
|
|
|
|
: public IterStateWrapper<iter::CursorGear<IT>>
|
2015-12-06 00:10:27 +01:00
|
|
|
|
{
|
2015-12-06 02:28:47 +01:00
|
|
|
|
using _Core = iter::CursorGear<IT>;
|
2024-11-26 22:15:33 +01:00
|
|
|
|
using _Parent = IterStateWrapper<_Core>;
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
|
|
|
|
|
public:
|
2018-09-03 03:32:18 +02:00
|
|
|
|
IterCursor() =default;
|
|
|
|
|
|
IterCursor (IterCursor&&) =default;
|
|
|
|
|
|
IterCursor (IterCursor const&) =default;
|
|
|
|
|
|
IterCursor& operator= (IterCursor&&) =default;
|
|
|
|
|
|
IterCursor& operator= (IterCursor const&) =default;
|
|
|
|
|
|
|
2015-12-06 00:10:27 +01:00
|
|
|
|
|
|
|
|
|
|
template<class CON>
|
|
|
|
|
|
explicit
|
|
|
|
|
|
IterCursor (CON& container)
|
2015-12-06 02:28:47 +01:00
|
|
|
|
: _Parent(_Core(container.begin(), container.end()))
|
2015-12-06 00:10:27 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
IterCursor (IT&& begin, IT&& end)
|
2015-12-06 02:28:47 +01:00
|
|
|
|
: _Parent(_Core(std::forward<IT>(begin), std::forward<IT>(end)))
|
2015-12-06 00:10:27 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* change the orientation of iteration.
|
|
|
|
|
|
* A forward oriented iteration will continue backwards,
|
|
|
|
|
|
* and vice versa. This operation can even be invoked
|
|
|
|
|
|
* on an already exhausted iterator, in which case
|
|
|
|
|
|
* it will turn back in reversed direction.
|
|
|
|
|
|
*/
|
|
|
|
|
|
IterCursor&
|
|
|
|
|
|
switchDir()
|
|
|
|
|
|
{
|
2015-12-06 03:35:07 +01:00
|
|
|
|
this->stateCore().reverse();
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IterCursor&
|
|
|
|
|
|
switchForwards()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->stateCore().reverse(false);
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IterCursor&
|
|
|
|
|
|
switchBackwards()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->stateCore().reverse(true);
|
2015-12-06 00:10:27 +01:00
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lib
|
|
|
|
|
|
#endif /*SRC_LIB_ITER_CURSOR_H*/
|