-
Notifications
You must be signed in to change notification settings - Fork 1
/
subset.cc
69 lines (61 loc) · 2.1 KB
/
subset.cc
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
// Percy++ Copyright 2007,2012 Ian Goldberg <[email protected]>,
// Casey Devet <[email protected]>,
// Paul Hendry <[email protected]>,
// Ryan Henry <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// There is a copy of the GNU General Public License in the COPYING file
// packaged with this plugin; if you cannot find it, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301 USA
#include <NTL/ZZ.h> // For RandomBnd()
#include "subset.h"
NTL_CLIENT
// Operations on subsets of the set of servers. These subsets are
// stored as sorted vectors of nservers_t.
// Append k random elements of G[offset..end] to I
void random_subset(const vector<nservers_t> &G,
vector<nservers_t> &I, nservers_t k, nservers_t offset)
{
if (k == 0) return;
// How many elements are there to choose from?
nservers_t n = G.size() - offset;
if (n == 0) return;
// With probability k/n, choose G[offset]
if (RandomBnd(n) < k) {
I.push_back(G[offset]);
random_subset(G, I, k-1, offset+1);
} else {
random_subset(G, I, k, offset+1);
}
}
// Compute the intersection of the two *strictly sorted* vectors v1 and v2
vector<nservers_t> intersect(const vector<nservers_t> &v1,
const vector<nservers_t> &v2)
{
vector<nservers_t> intersection;
vector<nservers_t>::const_iterator v1p, v2p;
v1p = v1.begin();
v2p = v2.begin();
while (v1p != v1.end() && v2p != v2.end()) {
if (*v1p == *v2p) {
// This is an element of the intersection
intersection.push_back(*v1p);
++v1p;
++v2p;
} else if (*v1p > *v2p) {
++v2p;
} else { // *v1p < *v2p
++v1p;
}
}
return intersection;
}