-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathmod_set.cpp
78 lines (66 loc) · 1.88 KB
/
mod_set.cpp
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
/*
Copyright 2019-2022 René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
*/
#include "mod_set.h"
#include <algorithm>
#include <unordered_map>
void b2::set::add(list_cref values)
{
for (auto v : values) elements.insert(value_ref(v));
}
void b2::set::add(const set & value)
{
elements.insert(value.elements.cbegin(), value.elements.cend());
}
bool b2::set::contains(value_ref value) const
{
return elements.find(value) != elements.end();
}
b2::list_ref b2::set::to_list() const
{
list_ref result;
for (auto v : elements) result.push_back(v);
return result;
}
b2::list_ref b2::set::difference(b2::list_cref a, b2::list_cref b)
{
// Somewhat less efficient variation on difference to maintain the order
// of sequence "a".
value_set rhs { b.begin(), b.end() };
list_ref result;
for (auto val : a)
if (rhs.count(val) == 0) result.push_back(val);
return result;
}
b2::list_ref b2::set::intersection(b2::list_cref a, b2::list_cref b)
{
value_set lhs { a.begin(), a.end() };
list_ref result;
for (auto val : b)
if (lhs.count(val) > 0) result.push_back(val);
return result;
}
bool b2::set::equal(b2::list_cref a, b2::list_cref b)
{
std::unordered_map<value_ref, unsigned, value_ref::hash_function,
value_ref::equal_function>
val_count;
for (auto val : a) val_count[val] += 1;
for (auto val : b) val_count[val] += 1;
for (auto & vc : val_count)
if (vc.second == 1) return false;
return true;
}
const char * b2::set_module::init_code = R"jam(
rule __test__ ( )
{
import assert ;
assert.result 0 1 4 6 8 9 : difference 0 1 2 3 4 5 6 7 8 9 : 2 3 5 7 ;
assert.result 2 5 7 : intersection 0 1 2 4 5 6 7 8 9 : 2 3 5 7 ;
assert.true equal : ;
assert.true equal 1 1 2 3 : 3 2 2 1 ;
assert.false equal 2 3 : 3 2 2 1 ;
}
)jam";