Changeset View
Changeset View
Standalone View
Standalone View
src/uint256.h
Show All 21 Lines | protected: | ||||
uint8_t data[WIDTH]; | uint8_t data[WIDTH]; | ||||
public: | public: | ||||
base_blob() { memset(data, 0, sizeof(data)); } | base_blob() { memset(data, 0, sizeof(data)); } | ||||
explicit base_blob(const std::vector<uint8_t> &vch); | explicit base_blob(const std::vector<uint8_t> &vch); | ||||
bool IsNull() const { | bool IsNull() const { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
if (data[i] != 0) return false; | if (data[i] != 0) { | ||||
return false; | |||||
} | |||||
} | |||||
return true; | return true; | ||||
} | } | ||||
void SetNull() { memset(data, 0, sizeof(data)); } | void SetNull() { memset(data, 0, sizeof(data)); } | ||||
inline int Compare(const base_blob &other) const { | inline int Compare(const base_blob &other) const { | ||||
// This doesn't quite work as you'd expect because the comparison use | |||||
// the wrong endianess. | |||||
return memcmp(data, other.data, sizeof(data)); | return memcmp(data, other.data, sizeof(data)); | ||||
} | } | ||||
friend inline bool operator==(const base_blob &a, const base_blob &b) { | friend inline bool operator==(const base_blob &a, const base_blob &b) { | ||||
return a.Compare(b) == 0; | return a.Compare(b) == 0; | ||||
} | } | ||||
friend inline bool operator!=(const base_blob &a, const base_blob &b) { | friend inline bool operator!=(const base_blob &a, const base_blob &b) { | ||||
return a.Compare(b) != 0; | return a.Compare(b) != 0; | ||||
} | } | ||||
friend inline bool operator<(const base_blob &a, const base_blob &b) { | friend inline bool operator<(const base_blob &a, const base_blob &b) { | ||||
return a.Compare(b) < 0; | return a.Compare(b) < 0; | ||||
} | } | ||||
friend inline bool operator>(const base_blob &a, const base_blob &b) { | |||||
return a.Compare(b) > 0; | |||||
} | |||||
std::string GetHex() const; | std::string GetHex() const; | ||||
void SetHex(const char *psz); | void SetHex(const char *psz); | ||||
void SetHex(const std::string &str); | void SetHex(const std::string &str); | ||||
std::string ToString() const; | std::string ToString() const { return GetHex(); } | ||||
uint8_t *begin() { return &data[0]; } | uint8_t *begin() { return &data[0]; } | ||||
uint8_t *end() { return &data[WIDTH]; } | uint8_t *end() { return &data[WIDTH]; } | ||||
const uint8_t *begin() const { return &data[0]; } | const uint8_t *begin() const { return &data[0]; } | ||||
const uint8_t *end() const { return &data[WIDTH]; } | const uint8_t *end() const { return &data[WIDTH]; } | ||||
unsigned int size() const { return sizeof(data); } | unsigned int size() const { return sizeof(data); } | ||||
uint64_t GetUint64(int pos) const { | uint64_t GetUint64(int pos) const { | ||||
const uint8_t *ptr = data + pos * 8; | const uint8_t *ptr = data + pos * 8; | ||||
return ((uint64_t)ptr[0]) | ((uint64_t)ptr[1]) << 8 | | return uint64_t(ptr[0]) | (uint64_t(ptr[1]) << 8) | | ||||
((uint64_t)ptr[2]) << 16 | ((uint64_t)ptr[3]) << 24 | | (uint64_t(ptr[2]) << 16) | (uint64_t(ptr[3]) << 24) | | ||||
((uint64_t)ptr[4]) << 32 | ((uint64_t)ptr[5]) << 40 | | (uint64_t(ptr[4]) << 32) | (uint64_t(ptr[5]) << 40) | | ||||
((uint64_t)ptr[6]) << 48 | ((uint64_t)ptr[7]) << 56; | (uint64_t(ptr[6]) << 48) | (uint64_t(ptr[7]) << 56); | ||||
} | } | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
s.write((char *)data, sizeof(data)); | s.write((char *)data, sizeof(data)); | ||||
} | } | ||||
template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
s.read((char *)data, sizeof(data)); | s.read((char *)data, sizeof(data)); | ||||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |