summaryrefslogtreecommitdiff
path: root/src/compat/iter.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat/iter.hpp')
-rw-r--r--src/compat/iter.hpp66
1 files changed, 61 insertions, 5 deletions
diff --git a/src/compat/iter.hpp b/src/compat/iter.hpp
index 7793d90..ff146a0 100644
--- a/src/compat/iter.hpp
+++ b/src/compat/iter.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_COMPAT_ITER_HPP
-#define TMWA_COMPAT_ITER_HPP
+#pragma once
// iter.hpp - tools for dealing with iterators
//
// Copyright © 2012-2014 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,11 +18,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-# include "../sanity.hpp"
+#include "fwd.hpp"
-# include <iterator>
+#include <iterator>
+namespace tmwa
+{
/// Simple class to use a pair of iterators with foreach
template<class It>
class IteratorPair
@@ -94,4 +95,59 @@ IteratorPair<ValueIterator<T>> value_range(T b, T e)
return {b, e};
}
-#endif // TMWA_COMPAT_ITER_HPP
+
+template<class T, class F, class C>
+class FilterIterator
+{
+ F filter;
+ C *container;
+
+ using InnerIterator = decltype(std::begin(*container));
+ InnerIterator impl;
+public:
+ void post_adv()
+ {
+ while (impl != std::end(*container))
+ {
+ if (filter(*impl))
+ break;
+ ++impl;
+ }
+ }
+
+ FilterIterator(C *c, F f)
+ : filter(f), container(c), impl(std::begin(*c))
+ {
+ post_adv();
+ }
+
+ void operator ++()
+ {
+ ++impl;
+ post_adv();
+ }
+
+ T operator *()
+ {
+ return *impl;
+ }
+
+ friend
+ bool operator != (FilterIterator l, FilterIterator)
+ {
+ return l.impl != std::end(*l.container);
+ }
+};
+
+template<class T>
+bool is_truthy(T v)
+{
+ return v;
+}
+
+template<class T, class F=decltype(is_truthy<T>)*, class C>
+IteratorPair<FilterIterator<T, F, C>> filter_iterator(C *c, F f=is_truthy<T>)
+{
+ return {FilterIterator<T, F, C>(c, f), FilterIterator<T, F, C>(c, f)};
+}
+} // namespace tmwa