-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoleculemultiset.cpp
132 lines (101 loc) · 3.4 KB
/
moleculemultiset.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
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
#include "moleculemultiset.h"
#include <random>
#include <iostream>
#include <memory>
int rand_between(int begin,int end){
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(begin, end);
return dist(mt);
}
moleculeMultiset::moleculeMultiset(){}
moleculeMultiset::~moleculeMultiset(){}
void moleculeMultiset::inject(molecule_pointer mol,int mult = 1){
for (int i = 0; i< mult; i++){
this->multiset.insert(mol);
}
}
int moleculeMultiset::expel(const molecule_pointer mol, int mult = 1){
int total = 0;
unorderedMultiset::iterator it;
for (int i = 0; i < mult; i++){
// symbol s = (*this->multiset.begin())[0];
// symbol s2 = mol[0];
it = this->multiset.find(mol);
// std::cout << "expel find " << mol << " " << *it << (it == this->multiset.end())<< '\n';
if (it != this->multiset.end()){
this->multiset.erase(mol);
total++;
}
else{
return total;
}
}
return total;
}
// https://stackoverflow.com/questions/27024269/select-random-element-in-an-unordered-map
const molecule_pointer moleculeMultiset::rndMol(){
if (this->multiset.empty()){
// I know this is fucked but what do I do.
molecule_pointer mol = std::make_shared<molecule>();
return mol;
}else{
// unorderedMultiset::iterator it = this->multiset.begin();
// molecule mol = *it;
// std::cout<< "rndmol " << mol << " " << this->multiset.size() << "\n";
// return mol;
// for (auto r : this->multiset){
// for (auto k : *r.mol_ptr){
// std::cout << *k << " ";
// }
// std::cout << r.mol_ptr << "\n";
// }
unorderedMultiset::iterator random_it = this->multiset.begin();
// 'advance' the iterator n times
std::advance(random_it,rand_between(0, this->multiset.size()-1));
// for (auto r : this->multiset){
// for (auto k : *r.mol_ptr){
// std::cout << *k << " ";
// }
// std::cout << r.mol_ptr << "\n";
// }
// unorderedMultiset::iterator random_it = std::next(std::begin(this->multiset), rand_between(0, this->multiset.size()-1));
// molecule mol = *random_it;
// if (this->multiset.find(mol) == this->multiset.end()){
// for (auto i : *mol.mol_ptr){
// std::cout << *i << '\n';
// }
// std::cout << mol.mol_ptr<< "test fail \n";
// exit(0);
// }
auto t = *random_it;
return *random_it;
}
}
const molecule_pointer moleculeMultiset::expelrnd(){
const molecule_pointer mol = this->rndMol();
this->expel(mol);
return mol;
}
int moleculeMultiset::mult(molecule_pointer& mol){
if (mol->vector.empty()){
return this->multiset.size();
}
return this->multiset.count(mol);
}
int moleculeMultiset::mult(){
return this->multiset.size();
}
molecule::molecule(){
// std::cout << "constuct\n";
};
molecule::~molecule(){
// std::cout << "destuct\n";
};
bool molecule::operator==(const molecule& other) const{
return std::equal(this->vector.begin(), this->vector.end(), other.vector.begin(),
[](const std::shared_ptr<symbol>& item1, const std::shared_ptr<symbol>
& item2) -> bool{
return (*item1 == *item2);
});
}