File: count_set.hpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (109 lines) | stat: -rw-r--r-- 2,564 bytes parent folder | download | duplicates (6)
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
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2020 Barend Gehrels, Amsterdam, the Netherlands.

// 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)


#ifndef GEOMETRY_TEST_COUNT_SET_HPP
#define GEOMETRY_TEST_COUNT_SET_HPP

#include <set>
#include <ostream>

// Structure to manage expectations: sometimes the output might have one or
// two rings, both are fine.
struct count_set
{
    count_set()
    {
    }

    count_set(int value)
    {
        if (value >= 0)
        {
            m_values.insert(static_cast<std::size_t>(value));
        }
    }

    count_set(std::size_t value1, std::size_t value2)
    {
        m_values.insert(value1);
        m_values.insert(value2);
    }

    count_set(std::size_t value1, std::size_t value2, std::size_t value3)
    {
        m_values.insert(value1);
        m_values.insert(value2);
        m_values.insert(value3);
    }

    bool empty() const { return m_values.empty(); }

    bool has(std::size_t value) const
    {
        return m_values.count(value) > 0;
    }

    friend std::ostream &operator<<(std::ostream &os, const count_set& s)
    {
       os << "{";
       for (std::size_t const& value : s.m_values)
       {
           os << " " << value;
       }
       os << "}";
       return os;
    }

    count_set operator+(const count_set& a) const
    {
        count_set result;
        result.m_values = combine(this->m_values, a.m_values);
        return result;
    }

private :
    typedef std::set<std::size_t> set_type;
    set_type m_values;

    set_type combine(const set_type& a, const set_type& b) const
    {
        set_type result;
        if (a.size() == 1 && b.size() == 1)
        {
            // The common scenario, both have one value
            result.insert(*a.begin() + *b.begin());
        }
        else if (a.size() > 1 && b.size() == 1)
        {
            // One of them is optional, add the second
            for (std::size_t const& value : a)
            {
                result.insert(value + *b.begin());
            }
        }
        else if (b.size() > 1 && a.size() == 1)
        {
            return combine(b, a);
        }
        // Else either is empty, or both have values and should be specified
        return result;
    }
};

inline count_set ignore_count()
{
    return count_set();
}

inline count_set optional()
{
    return count_set(0, 1);
}

#endif // GEOMETRY_TEST_COUNT_SET_HPP