You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7 KiB
C++

#pragma once
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
/**
* @brief Function that generates the next combination of k elements from a set of n elements
*/
bool nextComb(vector<int> &indices, int n) {
int k = indices.size();
int i = n - 1;
int j = k - 1;
while (indices[j] == i) {
if (j == 0)
return 1;
i--;
j--;
}
indices[j]++;
for (int l = 0; l <= k - j - 1; l++) {
indices[j + l] = indices[j] + l;
}
return 0;
}
/**
* @brief Function that returns the intersection of two sets
*/
template <typename S>
unordered_set<S> intersectSets(unordered_set<S> s1, unordered_set<S> s2) {
unordered_set<S> s;
if (s1.size() <= s2.size()) {
for (auto &el : s1) {
if (s2.find(el) != s2.end()) {
s.insert(el);
}
}
}
else {
for (auto &el : s2) {
if (s1.find(el) != s1.end()) {
s.insert(el);
}
}
}
return s;
}
/**
* @brief Function that shuffles the keys of a map and returns them in a vector
*
* @param map: the map to shuffle
* @param seed: the seed for the random number generator
*/
template <typename K, typename V>
vector<K> getShuffleKeys(unordered_map<K, V> map, long seed) {
if (seed != -1)
srand(seed);
else
srand(time(NULL));
vector<K> output;
for (auto& pair : map) {
output.push_back(pair.first);
}
random_shuffle(output.begin(), output.end());
return output;
}