File: random_access_view.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (123 lines) | stat: -rw-r--r-- 4,594 bytes parent folder | download | duplicates (8)
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
// Boost.Geometry
// Unit Test

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

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


#include <geometry_test_common.hpp>

#include <boost/variant.hpp>

#include <boost/geometry/views/detail/random_access_view.hpp>
#include <boost/geometry/geometries/geometries.hpp>

using point_t = bg::model::point<double, 2, bg::cs::cartesian>;
using linestring_t = bg::model::linestring<point_t>;
using polygon_t = bg::model::polygon<point_t>;
using variant_t = boost::variant<point_t, linestring_t, polygon_t>;
using gc_t = bg::model::geometry_collection<variant_t>;
using nra_gc_t = bg::model::geometry_collection<variant_t, std::list>;

struct rec_gc_t;
using rec_var_t = boost::variant<point_t, linestring_t, polygon_t, rec_gc_t>;
struct rec_gc_t : std::vector<rec_var_t>
{
    rec_gc_t() = default;
    rec_gc_t(std::initializer_list<rec_var_t> l) : std::vector<rec_var_t>(l) {}
};

struct rec_nra_gc_t;
using rec_nra_var_t = boost::variant<point_t, linestring_t, polygon_t, rec_nra_gc_t>;
struct rec_nra_gc_t : std::list<rec_nra_var_t>
{
    rec_nra_gc_t() = default;
    rec_nra_gc_t(std::initializer_list<rec_nra_var_t> l) : std::list<rec_nra_var_t>(l) {}
};

namespace boost { namespace geometry { namespace traits {
    template<> struct tag<rec_gc_t> { typedef geometry_collection_tag type; };
    template<> struct geometry_types<rec_gc_t> { typedef util::type_sequence<point_t, linestring_t, rec_gc_t, polygon_t> type; };
}}}

namespace boost { namespace geometry { namespace traits {
    template<> struct tag<rec_nra_gc_t> { typedef geometry_collection_tag type; };
    template<> struct geometry_types<rec_nra_gc_t> { typedef util::type_sequence<point_t, linestring_t, rec_nra_gc_t, polygon_t> type; };
}}}

template <typename GC>
GC make_gc()
{
    return GC{
        point_t{0, 0},
        linestring_t{{1, 1}, {2, 2}},
        polygon_t{{{3, 3}, {3, 4}, {4, 4}, {4, 3}, {3, 3}}},
        polygon_t{{{5, 5}, {5, 6}, {6, 6}, {6, 5}, {5, 5}}}
    };
}

template <typename GC>
GC make_rec_gc()
{
    return GC{
        point_t{0, 0},
        GC {
            // This should be put at the end because GC is processed in a BFS manner.
            // So both GCs should be equivalent.
            polygon_t{{{5, 5}, {5, 6}, {6, 6}, {6, 5}, {5, 5}}}
        },
        linestring_t{{1, 1}, {2, 2}},
        polygon_t{{{3, 3}, {3, 4}, {4, 4}, {4, 3}, {3, 3}}},
    };
}

template <typename GC, typename Make>
void test_all(Make make)
{
    using view_t = bg::detail::random_access_view<GC>;

    GC gc = make();
    view_t rav{gc};

    using non_gc_types = bg::util::type_sequence<point_t, linestring_t, polygon_t>;
    using view_types = typename bg::traits::geometry_types<view_t>::type;
    BOOST_STATIC_ASSERT((std::is_same<non_gc_types, view_types>::value));
    BOOST_STATIC_ASSERT(bg::detail::is_random_access_range<view_t>::value);
    BOOST_STATIC_ASSERT(! bg::detail::is_geometry_collection_recursive<view_t>::value);
    
    size_t s = boost::size(gc);
    for (size_t i = 0 ; i < s ; ++i)
    {
        bg::traits::iter_visit<bg::detail::random_access_view<GC>>::apply([&](auto&& g)
        {
            using geom_t = bg::util::remove_cref_t<decltype(g)>;
            if (i == 0) { BOOST_CHECK(bg::util::is_point<geom_t>::value); }
            else if (i == 1) { BOOST_CHECK(bg::util::is_linestring<geom_t>::value); }
            else { BOOST_CHECK(bg::util::is_polygon<geom_t>::value); }
        }, rav.begin() + i);
    }
}

int test_main(int, char* [])
{
    BOOST_STATIC_ASSERT(bg::detail::is_random_access_range<gc_t>::value);
    BOOST_STATIC_ASSERT(! bg::detail::is_random_access_range<nra_gc_t>::value);
    BOOST_STATIC_ASSERT(bg::detail::is_random_access_range<rec_gc_t>::value);
    BOOST_STATIC_ASSERT(! bg::detail::is_random_access_range<rec_nra_gc_t>::value);

    BOOST_STATIC_ASSERT(! bg::detail::is_geometry_collection_recursive<gc_t>::value);
    BOOST_STATIC_ASSERT(! bg::detail::is_geometry_collection_recursive<nra_gc_t>::value);
    BOOST_STATIC_ASSERT(bg::detail::is_geometry_collection_recursive<rec_gc_t>::value);
    BOOST_STATIC_ASSERT(bg::detail::is_geometry_collection_recursive<rec_nra_gc_t>::value);

    test_all<gc_t>(make_gc<gc_t>);
    test_all<nra_gc_t>(make_gc<nra_gc_t>);
    test_all<rec_gc_t>(make_gc<rec_gc_t>);
    test_all<rec_nra_gc_t>(make_gc<rec_nra_gc_t>);

    return 0;
}