#ifndef LOCKFREE_READ_HELPER_H #define LOCKFREE_READ_HELPER_H #include #include #include #include #ifndef P_EFF #ifndef NDEBUG #define P_ERR(...) fprintf(stderr, __VA_ARGS__) #else #define P_ERR(...) #endif #endif class ReadHelper { typedef unsigned long long key_type; key_type max_value = 0, min_value = 0x7fffffffff; char const *filename; static constexpr size_t REVERSED_BLOCK = 256; public: size_t sample_length, needed_read_length; ReadHelper(char const *filename, size_t sample_length, size_t insertion_length) : filename(filename), sample_length(sample_length), needed_read_length(insertion_length + sample_length) {} unsigned long long maxValue() const { return max_value; } unsigned long long minValue() const { return min_value; } std::vector 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); } void finish_read() { this->population_vector.push_back(this->max_value); this->population_vector.push_back(this->min_value); } static unsigned long randomRow(unsigned long max_value_) { std::random_device randomDevice; std::mt19937 mt19937(randomDevice()); std::uniform_int_distribution dst(0, max_value_); return dst(mt19937); } void readFile(unsigned long &total_row) { this->population_vector.clear(); auto read_number = 0UL; FILE *file = fopen(filename, "r"); assert(file); P_ERR("Reading sample"); for (long long i; read_number < needed_read_length && 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 - needed_read_length - REVERSED_BLOCK); 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 + REVERSED_BLOCK; for (long long i; read_number < remain && fscanf(file, "%lld ", &i) != EOF; store_into_vector(i)) read_number++; fclose(file); P_ERR("\r"); finish_read(); } void split_into(std::vector &sample, std::vector &p) { sample.reserve(sample_length); p.reserve(needed_read_length - sample_length); memcpy(sample.data(), population_vector.data(), sizeof(key_type) * sample_length); memcpy(p.data(), population_vector.data() + sample_length, sizeof(key_type) * (needed_read_length - sample_length)); } void split_into(key_type *&sample, key_type *&p) { sample = new key_type[sample_length]; p = new key_type[needed_read_length - sample_length]; memcpy(sample, population_vector.data(), sizeof(key_type) * sample_length); memcpy(p, population_vector.data() + sample_length, sizeof(key_type) * (needed_read_length - sample_length)); } }; #endif