Move UInt to UInt.h

GitOrigin-RevId: 52b6d36d1ece8c267f5548000b2592ffd3a62ded
This commit is contained in:
levlam 2018-12-20 00:18:53 +03:00
parent bc991da146
commit b676fe509a
24 changed files with 114 additions and 82 deletions

View File

@ -12,6 +12,7 @@
#include "td/utils/port/thread.h"
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
#include "td/utils/UInt.h"
#include <openssl/sha.h>

View File

@ -13,6 +13,7 @@
#include "td/utils/int_types.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
namespace td {

View File

@ -16,6 +16,7 @@
#include "td/utils/Random.h"
#include "td/utils/Status.h"
#include "td/utils/Time.h"
#include "td/utils/UInt.h"
#include "td/mtproto/mtproto_api.h"

View File

@ -15,6 +15,7 @@
#include "td/utils/crypto.h"
#include "td/utils/port/detail/PollableFd.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
namespace td {
namespace mtproto {

View File

@ -13,6 +13,7 @@
#include "td/utils/logging.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
#include <tuple>

View File

@ -10,6 +10,7 @@
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
#include <utility>

View File

@ -42,6 +42,7 @@
#include "td/utils/Time.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/tl_parsers.h"
#include "td/utils/UInt.h"
#include <algorithm>
#include <memory>

View File

@ -12,6 +12,7 @@
#include "td/utils/port/FileFd.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
namespace td {
// Types

View File

@ -23,6 +23,7 @@
#include "td/utils/port/Stat.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/Slice.h"
#include "td/utils/UInt.h"
#include <tuple>

View File

@ -24,6 +24,7 @@
#include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/tl_storers.h"
#include "td/utils/UInt.h"
#include "td/utils/Variant.h"
#include <tuple>

View File

@ -11,6 +11,7 @@
#include "td/utils/port/FileFd.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
#include <utility>

View File

@ -8,7 +8,7 @@
#include "td/tl/TlObject.h"
#include "td/utils/int_types.h"
#include "td/utils/UInt.h"
#include <cstdint>
#include <string>

View File

@ -18,6 +18,7 @@
#include "td/utils/port/FileFd.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
#include <functional>

View File

@ -207,6 +207,7 @@ set(TDUTILS_SOURCE
td/utils/tl_storers.h
td/utils/translit.h
td/utils/type_traits.h
td/utils/UInt.h
td/utils/unicode.h
td/utils/unique_ptr.h
td/utils/utf8.h

View File

@ -11,6 +11,7 @@
#include "td/utils/crypto.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
namespace td {

View File

@ -308,21 +308,4 @@ inline MutableSlice as_slice(MutableSlice slice) {
return slice;
}
template <size_t N>
Slice UInt<N>::as_slice() const {
return Slice(raw, N / 8);
}
template <size_t N>
MutableSlice UInt<N>::as_slice() {
return MutableSlice(raw, N / 8);
}
template <size_t N>
Slice as_slice(const UInt<N> &value) {
return value.as_slice();
}
template <size_t N>
MutableSlice as_slice(UInt<N> &value) {
return value.as_slice();
}
} // namespace td

93
tdutils/td/utils/UInt.h Normal file
View File

@ -0,0 +1,93 @@
//
// 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)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include <cstring>
namespace td {
template <size_t size>
struct UInt {
static_assert(size % 8 == 0, "size should be divisible by 8");
uint8 raw[size / 8];
Slice as_slice() const {
return Slice(raw, size / 8);
}
MutableSlice as_slice() {
return MutableSlice(raw, size / 8);
}
bool is_zero() const {
for (size_t i = 0; i < size / 8; i++) {
if (raw[i] != 0) {
return false;
}
}
return true;
}
void set_zero() {
for (size_t i = 0; i < size / 8; i++) {
raw[i] = 0;
}
}
static UInt zero() {
UInt v;
v.set_zero();
return v;
}
};
template <size_t size>
bool operator==(const UInt<size> &a, const UInt<size> &b) {
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0;
}
template <size_t size>
bool operator!=(const UInt<size> &a, const UInt<size> &b) {
return !(a == b);
}
template <size_t size>
td::UInt<size> operator^(const UInt<size> &a, const UInt<size> &b) {
td::UInt<size> res;
for (size_t i = 0; i < size / 8; i++) {
res.raw[i] = static_cast<uint8>(a.raw[i] ^ b.raw[i]);
}
return res;
}
template <size_t size>
int get_kth_bit(const UInt<size> &a, uint32 bit) {
uint8 b = a.raw[bit / 8];
bit &= 7;
return (b >> (7 - bit)) & 1;
}
template <size_t size>
Slice as_slice(const UInt<size> &value) {
return value.as_slice();
}
template <size_t size>
MutableSlice as_slice(UInt<size> &value) {
return value.as_slice();
}
template <size_t size>
bool operator<(const UInt<size> &a, const UInt<size> &b) {
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0;
}
using UInt128 = UInt<128>;
using UInt256 = UInt<256>;
} // namespace td

View File

@ -45,6 +45,7 @@
#include "td/utils/int_types.h"
#include "td/utils/unique_ptr.h"
#include <cstring> // temporary for std::memcpy
#include <string>
#include <vector>

View File

@ -10,6 +10,7 @@
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
namespace td {

View File

@ -10,7 +10,6 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
namespace td {
@ -43,67 +42,4 @@ static_assert(static_cast<char>(-256) == 0, "Unexpected cast to char implementat
#pragma warning(pop)
#endif
class Slice;
class MutableSlice;
template <size_t size>
struct UInt {
static_assert(size % 8 == 0, "size should be divisible by 8");
uint8 raw[size / 8];
Slice as_slice() const;
MutableSlice as_slice();
bool is_zero() const {
for (size_t i = 0; i * 8 < size; i++) {
if (raw[i] != 0) {
return false;
}
}
return true;
}
void set_zero() {
for (size_t i = 0; i * 8 < size; i++) {
raw[i] = 0;
}
}
static UInt zero() {
UInt v;
v.set_zero();
return v;
}
};
template <size_t size>
bool operator==(const UInt<size> &a, const UInt<size> &b) {
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0;
}
template <size_t size>
bool operator!=(const UInt<size> &a, const UInt<size> &b) {
return !(a == b);
}
template <size_t size>
td::UInt<size> operator^(const UInt<size> &a, const UInt<size> &b) {
td::UInt<size> res;
for (size_t i = 0; i * 8 < size; i++) {
res.raw[i] = static_cast<uint8>(a.raw[i] ^ b.raw[i]);
}
return res;
}
template <size_t size>
int get_kth_bit(const UInt<size> &a, uint32 bit) {
uint8 b = a.raw[bit / 8];
bit &= 7;
return (b >> (7 - bit)) & 1;
}
template <size_t size>
bool operator<(const UInt<size> &a, const UInt<size> &b) {
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0;
}
using UInt128 = UInt<128>;
using UInt256 = UInt<256>;
} // namespace td

View File

@ -13,6 +13,7 @@
#include "td/utils/misc.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
#include "td/utils/utf8.h"
#include <array>

View File

@ -12,6 +12,7 @@
#include "td/utils/misc.h"
#include "td/utils/Slice.h"
#include "td/utils/StorerBase.h"
#include "td/utils/UInt.h"
#include <cstring>

View File

@ -9,6 +9,7 @@
#include "td/utils/crypto.h"
#include "td/utils/Slice.h"
#include "td/utils/tests.h"
#include "td/utils/UInt.h"
#include <limits>

View File

@ -30,6 +30,7 @@
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/UInt.h"
#include "test/data.h"