2018-09-04 02:02:26 +02:00
|
|
|
/*
|
|
|
|
|
ITER-CHAIN-SEARCH.hpp - chained search with backtracking based on (bidirectional) iterator
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2018, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @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
|
|
|
|
|
** ["Lumiera Forward Iterator"](iter-adapter.hpp) concept. But it has
|
|
|
|
|
** the additional ability to [switch the working direction](\ref IterCursor<IT>::switchDir).
|
|
|
|
|
**
|
|
|
|
|
** @see IterCursor_test
|
|
|
|
|
** @see iter-adapter.hpp
|
|
|
|
|
** @see [usage example](event-log.hpp)
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_LIB_ITER_CHAIN_SEARCH_H
|
|
|
|
|
#define SRC_LIB_ITER_CHAIN_SEARCH_H
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/error.hpp"
|
2018-09-05 06:27:36 +02:00
|
|
|
#include "lib/iter-tree-explorer.hpp"
|
2018-09-04 02:02:26 +02:00
|
|
|
|
|
|
|
|
//#include <type_traits>
|
|
|
|
|
//#include <utility>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2018-09-07 01:56:41 +02:00
|
|
|
namespace iter {
|
2018-09-04 02:02:26 +02:00
|
|
|
|
|
|
|
|
using std::move;
|
2018-09-05 06:27:36 +02:00
|
|
|
using std::forward;
|
2018-09-04 02:02:26 +02:00
|
|
|
using std::string;
|
|
|
|
|
|
2018-09-07 01:56:41 +02:00
|
|
|
|
|
|
|
|
namespace { // implementation helpers...
|
2018-09-04 02:02:26 +02:00
|
|
|
|
2018-09-07 01:56:41 +02:00
|
|
|
template<class SRC>
|
|
|
|
|
auto
|
|
|
|
|
buildSearchFilter (SRC&& dataSource)
|
|
|
|
|
{
|
|
|
|
|
return treeExplore (forward<SRC> (dataSource))
|
|
|
|
|
.mutableFilter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class SRC, class FUN>
|
|
|
|
|
auto
|
|
|
|
|
buildExplorer (SRC&& dataSource, FUN&& expandFunctor)
|
|
|
|
|
{
|
|
|
|
|
return buildSearchFilter (forward<SRC> (dataSource))
|
|
|
|
|
.expand (forward<FUN> (expandFunctor))
|
|
|
|
|
.expandAll();
|
2018-09-05 06:27:36 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 02:02:26 +02:00
|
|
|
/**
|
2018-09-07 03:02:41 +02:00
|
|
|
* @internal helper to rebind on inferred types.
|
|
|
|
|
* @remark we use the TreeExplorer framework to assemble the processing pipeline
|
|
|
|
|
* from suitable building blocks configured with some lambdas. However, we
|
|
|
|
|
* also want to _inherit_ from this filter pipeline, so to expose the typical
|
|
|
|
|
* iterator operations without much ado. Thus we use some (static) helper function
|
|
|
|
|
* templates, instantiate them with the actual source data iterator and pick up
|
|
|
|
|
* the inferred type.
|
2018-09-04 02:02:26 +02:00
|
|
|
*/
|
2018-09-05 06:27:36 +02:00
|
|
|
template<class SRC>
|
2018-09-07 03:02:41 +02:00
|
|
|
struct _IterChainSetup
|
2018-09-04 02:02:26 +02:00
|
|
|
{
|
2018-09-07 00:45:04 +02:00
|
|
|
using Filter = decltype( buildSearchFilter (std::declval<SRC>()) );
|
2018-09-04 02:02:26 +02:00
|
|
|
|
2018-09-07 00:45:04 +02:00
|
|
|
using StepFunctor = typename iter_explorer::_BoundFunctor<Filter(Filter const&), Filter>::Functor;
|
2018-09-04 02:02:26 +02:00
|
|
|
|
2018-09-07 00:45:04 +02:00
|
|
|
using Pipeline = decltype( buildExplorer (std::declval<SRC>(), std::declval<StepFunctor>()) );
|
2018-09-04 02:02:26 +02:00
|
|
|
};
|
|
|
|
|
|
2018-09-07 01:56:41 +02:00
|
|
|
}//(End)implementation helpers
|
|
|
|
|
|
2018-09-04 02:02:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Iterator based linear search mechanism, with the ability to perform consecutive search with backtracking.
|
|
|
|
|
* The IterChainSearch can be configured with a sequence of search goals (filter conditions), and will
|
|
|
|
|
* apply these in succession on the underlying iterator. It will search for the first hit of the first
|
|
|
|
|
* condition, and then continue to search _from there_ matching on the second condition, and so on.
|
|
|
|
|
* After the first combination of matches is exhausted, the search will backtrack and try to evaluate
|
|
|
|
|
* the next combination, leading to a tree of search solutions.
|
|
|
|
|
*/
|
2018-09-05 06:27:36 +02:00
|
|
|
template<class SRC>
|
2018-09-04 02:02:26 +02:00
|
|
|
class IterChainSearch
|
2018-09-07 01:56:41 +02:00
|
|
|
: public _IterChainSetup<SRC>::Pipeline
|
2018-09-04 02:02:26 +02:00
|
|
|
{
|
2018-09-07 03:02:41 +02:00
|
|
|
using _Trait = _IterChainSetup<SRC>;
|
|
|
|
|
using _Base = typename _Trait::Pipeline;
|
|
|
|
|
|
|
|
|
|
using Filter = typename _Trait::Filter;
|
|
|
|
|
using Step = typename _Trait::StepFunctor;
|
2018-09-04 02:02:26 +02:00
|
|
|
|
|
|
|
|
public:
|
2018-09-07 00:45:04 +02:00
|
|
|
using DebugPipeline = _Base;
|
2018-09-04 02:02:26 +02:00
|
|
|
|
2018-09-07 03:02:41 +02:00
|
|
|
using _Base::_Base;
|
2018-09-04 02:02:26 +02:00
|
|
|
|
2018-09-07 03:02:41 +02:00
|
|
|
template<class SEQ>
|
|
|
|
|
explicit
|
|
|
|
|
IterChainSearch (SEQ&& srcData)
|
|
|
|
|
: _Base{move (buildExplorer (forward<SEQ> (srcData)
|
|
|
|
|
,Step{[this](Filter const& curr){ return configureFilterChain(curr); }}))}
|
|
|
|
|
{ }
|
2018-09-04 02:02:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** */
|
|
|
|
|
IterChainSearch&&
|
|
|
|
|
search (string target)
|
|
|
|
|
{
|
2018-09-05 06:27:36 +02:00
|
|
|
TODO ("configure additional chained search condition");
|
2018-09-04 02:02:26 +02:00
|
|
|
return move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IterChainSearch&&
|
|
|
|
|
clearFilter()
|
|
|
|
|
{
|
|
|
|
|
UNIMPLEMENTED ("drop all search condition frames");
|
|
|
|
|
return move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 03:02:41 +02:00
|
|
|
private:
|
|
|
|
|
Filter
|
|
|
|
|
configureFilterChain (Filter const& currentFilterState)
|
2018-09-04 02:02:26 +02:00
|
|
|
{
|
2018-09-07 03:02:41 +02:00
|
|
|
uint depth = this->depth();
|
|
|
|
|
return Filter{}; /////TODO empty filter means recursion end
|
2018-09-04 02:02:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ==== convenient builder free function ==== */
|
|
|
|
|
|
|
|
|
|
/** setup a chain search configuration by suitably wrapping the given container.
|
|
|
|
|
* @return a TreeEplorer, which is an Iterator to yield all the source elements,
|
|
|
|
|
* but may also be used to build an processing pipeline.
|
|
|
|
|
* @warning if you capture the result of this call by an auto variable,
|
|
|
|
|
* be sure to understand that invoking any further builder operation on
|
|
|
|
|
* TreeExplorer will invalidate that variable (by moving it into the
|
|
|
|
|
* augmented iterator returned from such builder call).
|
|
|
|
|
*/
|
2018-09-05 06:27:36 +02:00
|
|
|
template<class SRC>
|
2018-09-04 02:02:26 +02:00
|
|
|
inline auto
|
2018-09-05 06:27:36 +02:00
|
|
|
chainSearch (SRC&& srcData)
|
2018-09-04 02:02:26 +02:00
|
|
|
{
|
2018-09-07 03:02:41 +02:00
|
|
|
return IterChainSearch<SRC>{forward<SRC> (srcData)};
|
2018-09-04 02:02:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-07 01:56:41 +02:00
|
|
|
}} // namespace lib::iter
|
2018-09-04 02:02:26 +02:00
|
|
|
#endif /*SRC_LIB_ITER_CHAIN_SEARCH_H*/
|