aboutsummaryrefslogtreecommitdiff
path: root/include/salticidae/stream.h
diff options
context:
space:
mode:
authorDeterminant <ted.sybil@gmail.com>2018-07-12 17:39:48 -0400
committerDeterminant <ted.sybil@gmail.com>2018-07-12 17:39:48 -0400
commit33faa8e355dc47a126790f1ea3e52d1813aedc69 (patch)
tree151c047fd4b8b8a10be2ca45931c482ceb79b451 /include/salticidae/stream.h
parenta0ea3b5fb7eaedb9b6789657bed998585bb98b77 (diff)
support array type in smart pointers
Diffstat (limited to 'include/salticidae/stream.h')
-rw-r--r--include/salticidae/stream.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h
index ffffbe2..9e4791c 100644
--- a/include/salticidae/stream.h
+++ b/include/salticidae/stream.h
@@ -26,6 +26,7 @@
#define _SALTICIDAE_STREAM_H
#include "salticidae/type.h"
+#include "salticidae/ref.h"
#include "salticidae/crypto.h"
namespace salticidae {
@@ -258,6 +259,84 @@ class Blob {
}
};
+template<typename T = uint64_t>
+class Bits {
+ using _impl_type = T;
+ static const size_t bit_per_datum = sizeof(_impl_type) * 8;
+ BoxObj<_impl_type[]> data;
+ size_t nbits;
+ size_t ndata;
+
+ public:
+
+ Bits(): data(nullptr) {}
+ Bits(const bytearray_t &arr) {
+ load(&*arr.begin(), arr.size());
+ }
+
+ Bits(const uint8_t *arr, size_t len) { load(arr, len); }
+ Bits(size_t nbits): nbits(nbits) {
+ ndata = (nbits + bit_per_datum - 1) / bit_per_datum;
+ data = new _impl_type[ndata];
+ }
+
+ ~Bits() {}
+
+ void load(const uint8_t *arr, size_t len) {
+ nbits = len * 8;
+ ndata = (len + sizeof(_impl_type) - 1) /
+ sizeof(_impl_type);
+ data = new _impl_type[ndata];
+
+ uint8_t *end = arr + len;
+ for (_impl_type *ptr = data; ptr < data + ndata;)
+ {
+ _impl_type x = 0;
+ for (unsigned j = 0, k = 0; j < sizeof(_impl_type); j++, k += 8)
+ if (arr < end) x |= *(arr++) << k;
+ *(ptr++) = x;
+ }
+ }
+
+ bool is_null() const { return data == nullptr; }
+
+ size_t cheap_hash() const { return *data; }
+
+ void serialize(DataStream &s) const {
+ s << htole(nbits);
+ if (data)
+ {
+ for (const _impl_type *ptr = data; ptr < data + ndata; ptr++)
+ s << htole(*ptr);
+ }
+ else
+ {
+ for (const _impl_type *ptr = data; ptr < data + ndata; ptr++)
+ s << htole((_impl_type)0);
+ }
+ }
+
+ void unserialize(DataStream &s) {
+ _impl_type x;
+ s >> x;
+ nbits = letoh(x);
+ ndata = (nbits + bit_per_datum - 1) / bit_per_datum;
+ data = new _impl_type[ndata];
+ for (_impl_type *ptr = data; ptr < data + ndata; ptr++)
+ {
+ s >> x;
+ *ptr = letoh(x);
+ }
+ }
+
+ operator bytearray_t () const & {
+ DataStream s;
+ s << *this;
+ return std::move(s);
+ }
+};
+
+
const size_t ENT_HASH_LENGTH = 256 / 8;
uint256_t DataStream::get_hash() const {