Created
March 3, 2020 09:35
-
-
Save HamedMasafi/333d175004e8504e672d37204210ca17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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