Skip to content

Instantly share code, notes, and snippets.

@HamedMasafi
Created March 3, 2020 09:35
Show Gist options
  • Save HamedMasafi/333d175004e8504e672d37204210ca17 to your computer and use it in GitHub Desktop.
Save HamedMasafi/333d175004e8504e672d37204210ca17 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <iostream>
#include <array>
#include <limits>
#include <functional>
#include <random>
template <typename T, std::size_t Size = (std::numeric_limits<T>::max() / 8) + 1>
class number_stack_check
{
char* array_ = new char[Size];
public:
const size_t size = Size;
bool check(const T &value) {
auto div = std::lldiv(value, 8);
auto exists = array_[div.quot] & (1 << div.rem);
array_[div.quot] |= (1 << div.rem);
return exists;
}
};
template <typename T>
void begin_test(const uint32_t &test_iteration_count = 10000)
{
number_stack_check<T> c;
std::cout << "Memory usage: " << c.size << std::endl;
std::function<void(const T&)> call = [&c](const T &n){
std::cout << std::boolalpha << "Check " << n << ":\t"
<< c.check(n) << std::endl;
};
using lim = std::numeric_limits<T>;
std::default_random_engine generator;
std::uniform_int_distribution<int> random(lim::min(), lim::max());
for (uint32_t i = 0; i < test_iteration_count; ++i) {
call(random(generator));
}
}
int main()
{
begin_test<uint32_t>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment