]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | ||
3 | // Copyright (c) 2020 Barend Gehrels, Amsterdam, the Netherlands. | |
4 | ||
5 | // Use, modification and distribution is subject to the Boost Software License, | |
6 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | ||
10 | #ifndef GEOMETRY_TEST_COUNT_SET_HPP | |
11 | #define GEOMETRY_TEST_COUNT_SET_HPP | |
12 | ||
13 | #include <boost/foreach.hpp> | |
14 | ||
15 | #include <set> | |
16 | #include <ostream> | |
17 | ||
18 | // Structure to manage expectations: sometimes the output might have one or | |
19 | // two rings, both are fine. | |
20 | struct count_set | |
21 | { | |
22 | count_set() | |
23 | { | |
24 | } | |
25 | ||
26 | count_set(int value) | |
27 | { | |
28 | if (value >= 0) | |
29 | { | |
30 | m_values.insert(static_cast<std::size_t>(value)); | |
31 | } | |
32 | else | |
33 | { | |
34 | std::cout << "EMPTY" << std::endl; | |
35 | } | |
36 | } | |
37 | ||
38 | count_set(std::size_t value1, std::size_t value2) | |
39 | { | |
40 | m_values.insert(value1); | |
41 | m_values.insert(value2); | |
42 | } | |
43 | ||
44 | count_set(std::size_t value1, std::size_t value2, std::size_t value3) | |
45 | { | |
46 | m_values.insert(value1); | |
47 | m_values.insert(value2); | |
48 | m_values.insert(value3); | |
49 | } | |
50 | ||
51 | bool empty() const { return m_values.empty(); } | |
52 | ||
53 | bool has(std::size_t value) const | |
54 | { | |
55 | return m_values.count(value) > 0; | |
56 | } | |
57 | ||
58 | friend std::ostream &operator<<(std::ostream &os, const count_set& s) | |
59 | { | |
60 | os << "{"; | |
61 | BOOST_FOREACH(std::size_t const& value, s.m_values) | |
62 | { | |
63 | os << " " << value; | |
64 | } | |
65 | os << "}"; | |
66 | return os; | |
67 | } | |
68 | ||
69 | count_set operator+(const count_set& a) const | |
70 | { | |
71 | count_set result; | |
72 | result.m_values = combine(this->m_values, a.m_values); | |
73 | return result; | |
74 | } | |
75 | ||
76 | private : | |
77 | typedef std::set<std::size_t> set_type; | |
78 | set_type m_values; | |
79 | ||
80 | set_type combine(const set_type& a, const set_type& b) const | |
81 | { | |
82 | set_type result; | |
83 | if (a.size() == 1 && b.size() == 1) | |
84 | { | |
85 | // The common scenario, both have one value | |
86 | result.insert(*a.begin() + *b.begin()); | |
87 | } | |
88 | else if (a.size() > 1 && b.size() == 1) | |
89 | { | |
90 | // One of them is optional, add the second | |
91 | BOOST_FOREACH(std::size_t const& value, a) | |
92 | { | |
93 | result.insert(value + *b.begin()); | |
94 | } | |
95 | } | |
96 | else if (b.size() > 1 && a.size() == 1) | |
97 | { | |
98 | return combine(b, a); | |
99 | } | |
100 | // Else either is empty, or both have values and should be specified | |
101 | return result; | |
102 | } | |
103 | }; | |
104 | ||
105 | inline count_set ignore_count() | |
106 | { | |
107 | return count_set(); | |
108 | } | |
109 | ||
110 | inline count_set optional() | |
111 | { | |
112 | return count_set(0, 1); | |
113 | } | |
114 | ||
115 | #endif // GEOMETRY_TEST_COUNT_SET_HPP |