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
|
/**
** \file misc/set.hxx
** \brief misc::set: wrapper around std::set.
**/
#pragma once
#include <algorithm>
#include <ostream>
#include <misc/contract.hh>
#include <misc/set.hh>
namespace misc
{
template <class Key, class Compare, class Allocator>
template <typename Iter>
requires Iterator<Iter, Key>
inline set<Key, Compare, Allocator>::set(Iter first, Iter last)
{
this->insert(first, last);
}
/* Useful to convert a std::vector or other Compare in misc::set. */
template <class Key, class Compare, class Allocator>
template <typename Container>
requires ConstIterableType<Container, Key>
inline set<Key, Compare, Allocator>::set(const Container v)
{
for (const Key& x : v)
this->insert(x);
}
template <class Key, class Compare, class Allocator>
inline bool set<Key, Compare, Allocator>::has(const Key& k) const
{
return set_type::find(k) != this->end();
}
/* \brief Return the addition of \a data into \a this.
\a data must not yet be part of \a this. */
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>
set<Key, Compare, Allocator>::operator+(const Key& data) const
{
precondition(!(data % *this));
set<Key, Compare, Allocator> res(*this);
res.insert(data);
return res;
}
/* \brief Insert \a data in \a this.
\a data must not yet be part of \a this. */
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>&
set<Key, Compare, Allocator>::operator+=(const Key& data)
{
precondition(!(data % *this));
this->insert(data);
return *this;
}
/* \brief Return the removal of \a data into \a this.
\a data must be part of \a this. */
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>
set<Key, Compare, Allocator>::operator-(const Key& data) const
{
precondition(data % *this);
set<Key, Compare, Allocator> res(*this);
res.erase(data);
return res;
}
/* \brief Remove \a data from \a this.
\a data must be part of \a this. */
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>&
set<Key, Compare, Allocator>::operator-=(const Key& data)
{
precondition(data % *this);
this->erase(data);
return *this;
}
// Union with another set \a s.
// FIXME: Deprecate this use, it ought to be direct sum.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator> set<Key, Compare, Allocator>::operator+(
const set<Key, Compare, Allocator>& s) const
{
return set_union(*this, s);
}
// In place union with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>& set<Key, Compare, Allocator>::operator+=(
const set<Key, Compare, Allocator>& s)
{
return *this = *this + s;
}
// Subtraction with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator> set<Key, Compare, Allocator>::operator-(
const set<Key, Compare, Allocator>& s) const
{
return set_difference(*this, s);
}
// In place subtraction with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>& set<Key, Compare, Allocator>::operator-=(
const set<Key, Compare, Allocator>& s)
{
*this = *this - s;
return *this;
}
// Union with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator> set<Key, Compare, Allocator>::operator|(
const set<Key, Compare, Allocator>& s) const
{
return *this + s;
}
// In place union with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>& set<Key, Compare, Allocator>::operator|=(
const set<Key, Compare, Allocator>& s)
{
return *this += s;
}
// Intersection with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator> set<Key, Compare, Allocator>::operator&(
const set<Key, Compare, Allocator>& s) const
{
return set_intersection(*this, s);
}
// In place intersection with another set \a s.
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>& set<Key, Compare, Allocator>::operator&=(
const set<Key, Compare, Allocator>& s)
{
return *this = *this & s;
}
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>
set_difference(const set<Key, Compare, Allocator>& s1,
const set<Key, Compare, Allocator>& s2)
{
set<Key, Compare, Allocator> res;
// Specifying the key_comp to use is required: without it, the
// first set is properly ordered using its own key_comp, but
// neither the second set nor the resulting set are ordering
// using it. Even if s1, s2, and res do use the same Compare!
std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter(res, res.begin()), s1.key_comp());
return res;
}
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>
set_intersection(const set<Key, Compare, Allocator>& s1,
const set<Key, Compare, Allocator>& s2)
{
set<Key, Compare, Allocator> res;
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter(res, res.begin()), s1.key_comp());
return res;
}
template <class Key, class Compare, class Allocator>
inline set<Key, Compare, Allocator>
set_union(const set<Key, Compare, Allocator>& s1,
const set<Key, Compare, Allocator>& s2)
{
set<Key, Compare, Allocator> res;
std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter(res, res.begin()), s1.key_comp());
return res;
}
template <class Key, class Compare, class Allocator>
inline std::ostream& operator<<(std::ostream& ostr,
const set<Key, Compare, Allocator>& s)
{
for (auto i = s.begin(); i != s.end();)
{
ostr << *i;
if (++i != s.end())
ostr << ", ";
}
return ostr;
}
template <class Key, class Compare, class Allocator>
inline bool operator%(const Key& k, const set<Key, Compare, Allocator>& s)
{
return s.has(k);
}
} // namespace misc
|