2018-11-11 12:38:04 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/FileBitmask.h"
|
2018-12-26 17:11:15 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-11-11 12:38:04 +01:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-11-11 12:38:04 +01:00
|
|
|
namespace td {
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-11-11 12:38:04 +01:00
|
|
|
Bitmask::Bitmask(Decode, Slice data) : data_(zero_one_decode(data)) {
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
|
|
|
Bitmask::Bitmask(Ones, int64 count) : data_(narrow_cast<size_t>((count + 7) / 8), '\0') {
|
2018-11-11 12:38:04 +01:00
|
|
|
for (int64 i = 0; i < count; i++) {
|
|
|
|
set(i);
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-11-11 12:38:04 +01:00
|
|
|
std::string Bitmask::encode() const {
|
2018-12-26 17:11:15 +01:00
|
|
|
// remove zeroes in the end to make encoding deterministic
|
2018-11-11 12:38:04 +01:00
|
|
|
td::Slice data(data_);
|
2018-12-26 17:11:15 +01:00
|
|
|
while (!data.empty() && data.back() == '\0') {
|
2018-11-11 12:38:04 +01:00
|
|
|
data.remove_suffix(1);
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
return zero_one_encode(data);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
|
|
|
int64 Bitmask::get_ready_prefix_size(int64 offset, int64 part_size, int64 file_size) const {
|
2018-11-11 12:38:04 +01:00
|
|
|
auto offset_part = offset / part_size;
|
|
|
|
auto ones = get_ready_parts(offset_part);
|
|
|
|
if (ones == 0) {
|
2018-12-26 17:11:15 +01:00
|
|
|
return 0;
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
auto ready_parts_end = (offset_part + ones) * part_size;
|
|
|
|
if (file_size != 0 && ready_parts_end > file_size) {
|
|
|
|
ready_parts_end = file_size;
|
|
|
|
if (offset > file_size) {
|
|
|
|
offset = file_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto res = ready_parts_end - offset;
|
|
|
|
CHECK(res >= 0);
|
2018-11-11 12:38:04 +01:00
|
|
|
return res;
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 Bitmask::get_total_size(int64 part_size) const {
|
|
|
|
int64 res = 0;
|
|
|
|
for (int64 i = 0; i < size(); i++) {
|
2018-12-26 17:11:15 +01:00
|
|
|
res += static_cast<int64>(get(i));
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
return res * part_size;
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
|
|
|
bool Bitmask::get(int64 offset_part) const {
|
|
|
|
if (offset_part < 0) {
|
2018-11-11 12:38:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
auto index = narrow_cast<size_t>(offset_part / 8);
|
|
|
|
if (index >= data_.size()) {
|
2018-11-11 12:38:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
return (static_cast<uint8>(data_[index]) & (1 << static_cast<int>(offset_part % 8))) != 0;
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
int64 Bitmask::get_ready_parts(int64 offset_part) const {
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 res = 0;
|
2018-12-26 17:11:15 +01:00
|
|
|
while (get(offset_part + res)) {
|
2018-11-11 12:38:04 +01:00
|
|
|
res++;
|
|
|
|
}
|
|
|
|
return res;
|
2018-12-26 17:11:15 +01:00
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
|
|
|
|
std::vector<int32> Bitmask::as_vector() const {
|
|
|
|
std::vector<int32> res;
|
2018-12-26 17:11:15 +01:00
|
|
|
auto size = narrow_cast<int32>(data_.size() * 8);
|
|
|
|
for (int32 i = 0; i < size; i++) {
|
2018-11-11 12:38:04 +01:00
|
|
|
if (get(i)) {
|
|
|
|
res.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
|
|
|
void Bitmask::set(int64 offset_part) {
|
|
|
|
CHECK(offset_part >= 0);
|
|
|
|
auto need_size = narrow_cast<size_t>(offset_part / 8 + 1);
|
2018-11-11 12:38:04 +01:00
|
|
|
if (need_size > data_.size()) {
|
2018-12-26 17:11:15 +01:00
|
|
|
data_.resize(need_size, '\0');
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
data_[need_size - 1] |= (1 << (offset_part % 8));
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64 Bitmask::size() const {
|
2018-12-26 17:11:15 +01:00
|
|
|
return static_cast<int64>(data_.size() * 8);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|