1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-01 07:49:55 +00:00

Merge pull request #226 from jruderman/sort_empty

Fix UB when shuffling empty vecs
This commit is contained in:
Zeno Rogue 2021-07-12 09:45:58 +02:00 committed by GitHub
commit 457b918697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 7 additions and 7 deletions

View File

@ -2001,8 +2001,8 @@ EX void generate_mines() {
for(cell *c: currentmap->allcells()) for(cell *c: currentmap->allcells())
if(c->wall == waMineUnknown) if(c->wall == waMineUnknown)
candidates.push_back(c); candidates.push_back(c);
hrandom_shuffle(candidates);
bounded_mine_max = isize(candidates); bounded_mine_max = isize(candidates);
hrandom_shuffle(&candidates[0], bounded_mine_max);
bounded_mine_quantity = int(bounded_mine_max * bounded_mine_percentage + 0.5); bounded_mine_quantity = int(bounded_mine_max * bounded_mine_percentage + 0.5);
for(int i=0; i<bounded_mine_quantity; i++) candidates[i]->wall = waMineMine; for(int i=0; i<bounded_mine_quantity; i++) candidates[i]->wall = waMineMine;
} }

View File

@ -1847,7 +1847,7 @@ EX namespace hive {
} }
} }
hrandom_shuffle(&bugtomove[0], isize(bugtomove)); hrandom_shuffle(bugtomove);
sort(bugtomove.begin(), bugtomove.end()); sort(bugtomove.begin(), bugtomove.end());
int battlecount = 0; int battlecount = 0;
@ -3611,7 +3611,7 @@ EX namespace windmap {
vector<bool> inqueue(N, true); vector<bool> inqueue(N, true);
vector<int> tocheck; vector<int> tocheck;
for(int i=0; i<N; i++) tocheck.push_back(i); for(int i=0; i<N; i++) tocheck.push_back(i);
hrandom_shuffle(&tocheck[0], isize(tocheck)); hrandom_shuffle(tocheck);
for(int a=0; a<isize(tocheck); a++) { for(int a=0; a<isize(tocheck); a++) {
if(a >= 200*N) { printf("does not converge\n"); break; } if(a >= 200*N) { printf("does not converge\n"); break; }

View File

@ -852,7 +852,7 @@ EX void ambush(cell *c, int dogs) {
int v = valence(); int v = valence();
if(v > 4) { if(v > 4) {
for(cell *c: cl.lst) if(cl.getdist(c) == d) around.push_back(c); for(cell *c: cl.lst) if(cl.getdist(c) == d) around.push_back(c);
hrandom_shuffle(&around[0], isize(around)); hrandom_shuffle(around);
} }
else { else {
for(int tries=0; tries<10000; tries++) { for(int tries=0; tries<10000; tries++) {

View File

@ -44,7 +44,7 @@ EX int hrand(int i) {
#if HDR #if HDR
template<class T, class... U> T pick(T x, U... u) { std::initializer_list<T> i = {x,u...}; return *(i.begin() + hrand(1+sizeof...(u))); } template<class T, class... U> T pick(T x, U... u) { std::initializer_list<T> i = {x,u...}; return *(i.begin() + hrand(1+sizeof...(u))); }
template<class T> void hrandom_shuffle(T* x, int n) { for(int k=1; k<n; k++) swap(x[k], x[hrand(k+1)]); } template<class T> void hrandom_shuffle(T* x, int n) { for(int k=1; k<n; k++) swap(x[k], x[hrand(k+1)]); }
template<class T> void hrandom_shuffle(T& container) { hrandom_shuffle(&container[0], isize(container)); } template<class T> void hrandom_shuffle(T& container) { hrandom_shuffle(container.data(), isize(container)); }
template<class U> auto hrand_elt(U& container) -> decltype(container[0]) { return container[hrand(isize(container))]; } template<class U> auto hrand_elt(U& container) -> decltype(container[0]) { return container[hrand(isize(container))]; }
template<class T, class U> T hrand_elt(U& container, T default_value) { template<class T, class U> T hrand_elt(U& container, T default_value) {
if(container.empty()) return default_value; if(container.empty()) return default_value;

View File

@ -331,7 +331,7 @@ void find_track(cell *start, int sign, int len) {
} }
EX void block_cells(vector<cell*> to_block, function<bool(cell*)> blockbound) { EX void block_cells(vector<cell*> to_block, function<bool(cell*)> blockbound) {
hrandom_shuffle(&to_block[0], isize(to_block)); hrandom_shuffle(to_block);
for(cell *c: to_block) switch(specialland) { for(cell *c: to_block) switch(specialland) {
case laIce: case laIce:

View File

@ -1253,7 +1253,7 @@ void fillgroups() {
do_classify(); do_classify();
vector<int> samples_to_sort; vector<int> samples_to_sort;
for(int i=0; i<samples; i++) samples_to_sort.push_back(i); for(int i=0; i<samples; i++) samples_to_sort.push_back(i);
hrandom_shuffle(&samples_to_sort[0], samples); hrandom_shuffle(samples_to_sort);
for(int i=0; i<samples; i++) if(net[bids[i]].drawn_samples < net[bids[i]].max_group_here) for(int i=0; i<samples; i++) if(net[bids[i]].drawn_samples < net[bids[i]].max_group_here)
showsample(i); showsample(i);
distribute_neurons(); distribute_neurons();