diff options
| -rw-r--r-- | read_helper.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/read_helper.h b/read_helper.h new file mode 100644 index 0000000..311ebbb --- /dev/null +++ b/read_helper.h @@ -0,0 +1,76 @@ + +#ifndef LOCKFREE_READ_HELPER_H +#define LOCKFREE_READ_HELPER_H + +#include <cassert> +#include <cstdio> +#include <random> +#include <vector> +#ifndef P_EFF +#ifndef NDEBUG +#define P_ERR(...) fprintf(stderr, __VA_ARGS__) +#else +#define P_ERR(...) +#endif +#endif + +class ReadHelper { + unsigned long long max_value = 0, min_value = 0x7fffffffff; + + char const *filename; + +public: + ReadHelper(char const *filename) : filename(filename) {} + unsigned long long maxValue() const { return max_value; } + unsigned long long minValue() const { return min_value; } + + std::vector<unsigned long long> population_vector; + inline void store_into_vector(unsigned long long value) { + if (max_value < value) { + max_value = value; + } + if (min_value > value) { + min_value = value; + } + population_vector.push_back(value); + } + + static unsigned long randomRow(unsigned long max_value_) { + std::random_device randomDevice; + std::mt19937 mt19937(randomDevice()); + std::uniform_int_distribution<std::mt19937::result_type> dst(0, max_value_); + return dst(mt19937); + } + + void readFile(long sample_length, unsigned long &total_row, + unsigned long max_number) { + auto read_number = 0UL; + FILE *file = fopen(filename, "r"); + assert(file); + P_ERR("Reading sample"); + for (long long i; + read_number < max_number && fscanf(file, "%lld ", &i) != EOF; + store_into_vector(i)) + read_number++; + + if (total_row > 0) { + P_ERR("\rReading skip"); + read_number = randomRow(total_row - sample_length - max_number - 256); + total_row = read_number; + // fprintf(stderr, "Skip %lu\n", read_number); + for (long long i; read_number > 0 && fscanf(file, "%lld ", &i) != EOF;) + read_number--; + } + + P_ERR("\rReading population"); + read_number = 0; + + auto remain = sample_length + 256; + for (long long i; read_number < remain && fscanf(file, "%lld ", &i) != EOF; + store_into_vector(i)) + read_number++; + fclose(file); + P_ERR("\r"); + } +}; +#endif
\ No newline at end of file |
