iterator_for.hpp 928 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
9
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
10

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

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

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

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

#endif