-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleSubSetIterator.H
51 lines (46 loc) · 1.23 KB
/
simpleSubSetIterator.H
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
#include <vector>
#include <functional>
/*
* SUBSET ITERATOR
* 09/15/2018
* Iterate on all subsets of a given vector container
* Uses simple binary coding.
* @author: Arnav Kansal <[email protected]>
*
*/
/* for comparison only, does a lot of unnecessary allocations */
template <typename T>
class SubSets{
public:
// experimental, enter vector of size < 63 only
SubSets(const std::vector<T>& container) : container_(container){
capacity_ = (1L << container.size());
};
const std::vector<std::reference_wrapper<T>>& current() const{
return peek_;
};
void next(){
set_ = (set_ + 1) % capacity_;
construct();
}
bool isStart() const {return set_ == 0;}
private:
uint64_t set_ = 0;
uint64_t capacity_;
// storage for actual set
std::vector<T> container_;
// peek vector which offers access to next subset
std::vector<std::reference_wrapper<T>> peek_;
__attribute__ ((always_inline)) void construct(){
peek_.clear();
int ct = 0;
uint64_t temp = set_;
while(temp){
if(temp & 1){
peek_.push_back(container_[ct]);
}
temp = temp >> 1;
ct++;
}
}
};