summaryrefslogtreecommitdiff
path: root/boost/geometry/views/detail/random_access_view.hpp
blob: e7c1227554cdb281139191aaf69b04a0290b00a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Boost.Geometry

// Copyright (c) 2022-2023, Oracle and/or its affiliates.
// Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html

#ifndef BOOST_GEOMETRY_VIEWS_DETAIL_RANDOM_ACCESS_VIEW_HPP
#define BOOST_GEOMETRY_VIEWS_DETAIL_RANDOM_ACCESS_VIEW_HPP

#include <vector>

#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/size.hpp>

#include <boost/geometry/algorithms/detail/visit.hpp>
#include <boost/geometry/core/geometry_types.hpp>
#include <boost/geometry/core/tag.hpp>
#include <boost/geometry/core/tags.hpp>
#include <boost/geometry/util/sequence.hpp>
#include <boost/geometry/util/type_traits.hpp>

namespace boost { namespace geometry
{

#ifndef DOXYGEN_NO_DETAIL
namespace detail
{


template <typename Range>
struct is_random_access_range
    : std::is_convertible
        <
            typename boost::iterator_traversal
                <
                    typename boost::range_iterator<Range>::type
                >::type,
            boost::random_access_traversal_tag
        >
{};

template <typename Geometry>
struct is_geometry_collection_recursive
    : util::bool_constant
        <
            util::is_geometry_collection
                <
                    typename util::sequence_find_if
                        <
                            typename traits::geometry_types<std::remove_const_t<Geometry>>::type,
                            util::is_geometry_collection
                        >::type
                >::value
        >
{};

template
<
    typename GeometryCollection,
    bool IsRandomAccess = is_random_access_range<GeometryCollection>::value,
    bool IsRecursive = is_geometry_collection_recursive<GeometryCollection>::value
>
class random_access_view
    : public std::vector<typename boost::range_iterator<GeometryCollection>::type>
{
    // NOTE: An alternative would be to implement iterator holding base iterators
    //   to geometry collections of lower levels to process after the current level
    //   of bfs traversal is finished.

    using base_t = std::vector<typename boost::range_iterator<GeometryCollection>::type>;

public:
    random_access_view(GeometryCollection & geometry)
    {
        this->reserve(boost::size(geometry));
        detail::visit_breadth_first_impl<true>::apply([&](auto&&, auto iter)
        {
            this->push_back(iter);
            return true;
        }, geometry);
    }
};

template <typename GeometryCollection>
class random_access_view<GeometryCollection, true, false>
{
public:
    using iterator = typename boost::range_iterator<GeometryCollection>::type;
    using const_iterator = typename boost::range_const_iterator<GeometryCollection>::type;

    random_access_view(GeometryCollection & geometry)
        : m_begin(boost::begin(geometry))
        , m_end(boost::end(geometry))
    {}

    iterator begin() { return m_begin; }
    iterator end() { return m_end; }
    const_iterator begin() const { return m_begin; }
    const_iterator end() const { return m_end; }

private:
    iterator m_begin, m_end;
};


template <typename GeometryCollection>
struct random_access_view_iter_visit
{
    template <typename Function, typename Iterator>
    static void apply(Function && function, Iterator iterator)
    {
        geometry::traits::iter_visit
            <
                std::remove_const_t<GeometryCollection>
            >::apply(std::forward<Function>(function), *iterator);
    }
};


template <typename ...Ts>
struct remove_geometry_collections_pack
{
    using type = util::type_sequence<>;
};

template <typename T, typename ...Ts>
struct remove_geometry_collections_pack<T, Ts...>
{
    using next_sequence = typename remove_geometry_collections_pack<Ts...>::type;
    using type = std::conditional_t
        <
            util::is_geometry_collection<T>::value,
            next_sequence,
            typename util::sequence_merge<util::type_sequence<T>, next_sequence>::type
        >;
};

template <typename Types>
struct remove_geometry_collections;

template <typename ...Ts>
struct remove_geometry_collections<util::type_sequence<Ts...>>
    : remove_geometry_collections_pack<Ts...>
{};


} // namespace detail
#endif // DOXYGEN_NO_DETAIL


#ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
namespace traits
{

template<typename GeometryCollection, bool IsRandomAccess, bool IsRecursive>
struct tag<geometry::detail::random_access_view<GeometryCollection, IsRandomAccess, IsRecursive>>
{
    using type = geometry_collection_tag;
};


template <typename GeometryCollection>
struct iter_visit<geometry::detail::random_access_view<GeometryCollection, false, false>>
    : geometry::detail::random_access_view_iter_visit<GeometryCollection>
{};

template <typename GeometryCollection>
struct iter_visit<geometry::detail::random_access_view<GeometryCollection, true, true>>
    : geometry::detail::random_access_view_iter_visit<GeometryCollection>
{};

template <typename GeometryCollection>
struct iter_visit<geometry::detail::random_access_view<GeometryCollection, false, true>>
    : geometry::detail::random_access_view_iter_visit<GeometryCollection>
{};

template <typename GeometryCollection>
struct iter_visit<geometry::detail::random_access_view<GeometryCollection, true, false>>
{
    template <typename Function, typename Iterator>
    static void apply(Function && function, Iterator iterator)
    {
        geometry::traits::iter_visit
            <
                std::remove_const_t<GeometryCollection>
            >::apply(std::forward<Function>(function), iterator);
    }
};


template <typename GeometryCollection, bool IsRandomAccess>
struct geometry_types<geometry::detail::random_access_view<GeometryCollection, IsRandomAccess, false>>
    : traits::geometry_types
        <
            std::remove_const_t<GeometryCollection>
        >
{};

template <typename GeometryCollection, bool IsRandomAccess>
struct geometry_types<geometry::detail::random_access_view<GeometryCollection, IsRandomAccess, true>>
    : geometry::detail::remove_geometry_collections
        <
            typename traits::geometry_types
                <
                    std::remove_const_t<GeometryCollection>
                >::type
        >
{};

} // namespace traits
#endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS


}} // namespace boost::geometry


#endif // BOOST_GEOMETRY_VIEWS_DETAIL_RANDOM_ACCESS_VIEW_HPP