ClickHouse/src/Common/RangeGenerator.h

47 lines
928 B
C++
Raw Normal View History

2022-03-21 14:52:26 +00:00
#pragma once
#include <optional>
#include <cmath>
namespace DB
{
class RangeGenerator
{
public:
explicit RangeGenerator(size_t total_size_, size_t range_step_, size_t range_start = 0)
: from(range_start), range_step(range_step_), total_size(total_size_)
{
}
2022-04-21 03:33:15 +00:00
size_t totalRanges() const { return static_cast<size_t>(ceil(static_cast<float>(total_size - from) / range_step)); }
2022-03-21 14:52:26 +00:00
using Range = std::pair<size_t, size_t>;
// return upper exclusive range of values, i.e. [from_range, to_range>
std::optional<Range> nextRange()
{
if (from >= total_size)
{
return std::nullopt;
}
auto to = from + range_step;
if (to >= total_size)
{
to = total_size;
}
Range range{from, to};
from = to;
2022-03-28 08:19:23 +00:00
return range;
2022-03-21 14:52:26 +00:00
}
private:
size_t from;
size_t range_step;
size_t total_size;
};
}