iterator_for.hpp 935 Bytes
Newer Older
Paul's avatar
Paul committed
1
2
3
#ifndef MIGRAPH_GUARD_RTGLIB_ITERATOR_FOR_HPP
#define MIGRAPH_GUARD_RTGLIB_ITERATOR_FOR_HPP

Paul's avatar
Paul committed
4
5
#include <cassert>
#include <type_traits>
6
#include <migraph/config.hpp>
Paul's avatar
Paul committed
7

8
namespace migraph { inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
11
12
template <class T>
struct iterator_for_range
{
Paul's avatar
Paul committed
13
    T* base;
Paul's avatar
Paul committed
14
    using base_iterator = std::remove_reference_t<decltype(base->begin())>;
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
17
    struct iterator
    {
Paul's avatar
Paul committed
18
        base_iterator i;
Paul's avatar
Paul committed
19
20
21
        base_iterator operator*() { return i; }
        base_iterator operator++() { return ++i; }
        bool operator!=(const iterator& rhs) { return i != rhs.i; }
Paul's avatar
Paul committed
22
23
    };

Paul's avatar
Paul committed
24
25
26
27
28
29
30
31
32
33
    iterator begin()
    {
        assert(base != nullptr);
        return {base->begin()};
    }
    iterator end()
    {
        assert(base != nullptr);
        return {base->end()};
    }
Paul's avatar
Paul committed
34
35
};
template <class T>
Paul's avatar
Paul committed
36
iterator_for_range<T> iterator_for(T& x)
Paul's avatar
Paul committed
37
38
39
40
{
    return {&x};
}

41
} // inline namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
42
43
44
} // namespace migraph

#endif