Skip to content

Commit

Permalink
introduce namespaces: cista::offset and cista::raw
Browse files Browse the repository at this point in the history
namespaced deserialize functions, unchecked_deserialize
  • Loading branch information
felixguendling authored Dec 23, 2018
1 parent c829cd0 commit 8ef0536
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 285 deletions.
52 changes: 52 additions & 0 deletions include/cista/containers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "cista/containers/string.h"
#include "cista/containers/unique_ptr.h"
#include "cista/containers/vector.h"

// Helper macro to prevent copy&paste.
#define CISTA_DEFINITIONS \
template <typename T> \
using unique_ptr = cista::basic_unique_ptr<T, ptr<T>>; \
\
template <typename T> \
using vector = cista::basic_vector<T, ptr<T>>; \
\
using string = cista::basic_string<ptr<char const>>; \
\
template <typename T, typename... Args> \
unique_ptr<T> make_unique(Args&&... args) { \
return unique_ptr<T>{new T{std::forward<Args>(args)...}, true}; \
}

namespace cista {

// =============================================================================
// Offset based data structures:
// [+] can be read without any deserialization step
// [+] suitable for shared memory applications
// [-] slower at runtime (pointers needs to be resolved using on more add)
// -----------------------------------------------------------------------------
namespace offset {

template <typename T>
using ptr = cista::offset_ptr<T>;

CISTA_DEFINITIONS
} // namespace offset

// =============================================================================
// Raw data structures:
// [-] deserialize step takes time (but still very fast also for GBs of data)
// [-] the buffer containing the serialized data needs to be modified
// [+] fast runtime access (raw access)
// -----------------------------------------------------------------------------
namespace raw {

template <typename T>
using ptr = T*;

CISTA_DEFINITIONS
} // namespace raw

} // namespace cista
6 changes: 5 additions & 1 deletion include/cista/containers/offset_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ struct offset_ptr {
offset_ptr(offset_ptr&& o) : offset_ptr{o.get()} {}
offset_ptr& operator=(offset_ptr const& o) {
offset_ = ptr_to_offset(o.get());
return *this;
}
offset_ptr& operator=(offset_ptr&& o) {
offset_ = ptr_to_offset(o.get());
return *this;
}
offset_ptr& operator=(offset_ptr&& o) { offset_ = ptr_to_offset(o.get()); }

offset_t ptr_to_offset(T const* p) const {
return p == nullptr
Expand Down
5 changes: 1 addition & 4 deletions include/cista/containers/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ struct basic_string {
heap h_;
stack s_;
};
}; // namespace cista

using string = basic_string<char const*>;
using o_string = basic_string<offset_ptr<char const>>;
};

} // namespace cista
30 changes: 9 additions & 21 deletions include/cista/containers/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,32 @@
namespace cista {

template <typename T, typename Ptr = T*>
struct unique_ptr {
struct basic_unique_ptr {
using value_t = T;

unique_ptr() = default;
basic_unique_ptr() = default;

explicit unique_ptr(T* el, bool take_ownership = true)
explicit basic_unique_ptr(T* el, bool take_ownership = true)
: el_{el}, self_allocated_{take_ownership} {}

unique_ptr(unique_ptr const&) = delete;
unique_ptr& operator=(unique_ptr const&) = delete;
basic_unique_ptr(basic_unique_ptr const&) = delete;
basic_unique_ptr& operator=(basic_unique_ptr const&) = delete;

unique_ptr(unique_ptr&& o) : el_{o.el_}, self_allocated_{o.self_allocated_} {
basic_unique_ptr(basic_unique_ptr&& o)
: el_{o.el_}, self_allocated_{o.self_allocated_} {
o.el_ = nullptr;
o.self_allocated_ = false;
}

unique_ptr& operator=(unique_ptr&& o) {
basic_unique_ptr& operator=(basic_unique_ptr&& o) {
el_ = o.el_;
self_allocated_ = o.self_allocated_;
o.el_ = nullptr;
o.self_allocated_ = false;
return *this;
}

~unique_ptr() {
~basic_unique_ptr() {
if (self_allocated_ && el_ != nullptr) {
delete get();
}
Expand All @@ -51,17 +52,4 @@ struct unique_ptr {
uint32_t __fill_2__{0};
};

template <typename T>
using o_unique_ptr = unique_ptr<T, offset_ptr<T>>;

template <typename T, typename... Args>
unique_ptr<T> make_unique(Args&&... args) {
return unique_ptr<T>{new T{std::forward<Args>(args)...}, true};
}

template <typename T, typename... Args>
o_unique_ptr<T> make_o_unique(Args&&... args) {
return o_unique_ptr<T>{new T{std::forward<Args>(args)...}, true};
}

} // namespace cista
55 changes: 30 additions & 25 deletions include/cista/containers/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace cista {

template <typename T, typename Ptr = T*, typename TemplateSizeType = uint32_t>
struct vector {
struct basic_vector {
using size_type = TemplateSizeType;
using value_type = T;
using iterator = T*;
Expand All @@ -32,21 +32,21 @@ struct vector {
return n;
}

explicit vector(TemplateSizeType size = 0) { resize(size); }
explicit basic_vector(TemplateSizeType size = 0) { resize(size); }

explicit vector(const char* str) {
explicit basic_vector(const char* str) {
auto length = static_cast<size_type>(std::strlen(str) + 1);
reserve(length);
std::memcpy(el_, str, length);
used_size_ = length;
}

template <typename It>
vector(It begin_it, It end_it) {
basic_vector(It begin_it, It end_it) {
set(begin_it, end_it);
}

vector(vector&& arr) noexcept
basic_vector(basic_vector&& arr) noexcept
: el_(arr.el_),
used_size_(arr.used_size_),
allocated_size_(arr.allocated_size_),
Expand All @@ -57,9 +57,9 @@ struct vector {
arr.allocated_size_ = 0;
}

vector(vector const& arr) { set(std::begin(arr), std::end(arr)); }
basic_vector(basic_vector const& arr) { set(std::begin(arr), std::end(arr)); }

vector& operator=(vector&& arr) noexcept {
basic_vector& operator=(basic_vector&& arr) noexcept {
deallocate();

el_ = arr.el_;
Expand All @@ -75,12 +75,12 @@ struct vector {
return *this;
}

vector& operator=(vector const& arr) {
basic_vector& operator=(basic_vector const& arr) {
set(std::begin(arr), std::end(arr));
return *this;
}

~vector() { deallocate(); }
~basic_vector() { deallocate(); }

void deallocate() {
if (!self_allocated_ || el_ == nullptr) {
Expand Down Expand Up @@ -114,11 +114,11 @@ struct vector {
}
std::reverse_iterator<T*> rend() { return std::reverse_iterator<T*>(el_); }

friend T const* begin(vector const& a) { return a.begin(); }
friend T const* end(vector const& a) { return a.end(); }
friend T const* begin(basic_vector const& a) { return a.begin(); }
friend T const* end(basic_vector const& a) { return a.end(); }

friend T* begin(vector& a) { return a.begin(); }
friend T* end(vector& a) { return a.end(); }
friend T* begin(basic_vector& a) { return a.begin(); }
friend T* end(basic_vector& a) { return a.end(); }

inline T const& operator[](size_t index) const { return el_[index]; }
inline T& operator[](size_t index) { return el_[index]; }
Expand All @@ -132,8 +132,8 @@ struct vector {
inline TemplateSizeType size() const { return used_size_; }
inline bool empty() const { return size() == 0; }

vector& operator=(std::string const& str) {
*this = vector(str.c_str());
basic_vector& operator=(std::string const& str) {
*this = basic_vector(str.c_str());
return *this;
}

Expand Down Expand Up @@ -243,30 +243,35 @@ struct vector {
uint32_t __fill_2__{0};
};

template <typename T>
inline bool operator==(vector<T> const& a, vector<T> const& b) {
template <typename T, typename Ptr, typename TemplateSizeType>
inline bool operator==(basic_vector<T, Ptr, TemplateSizeType> const& a,
basic_vector<T, Ptr, TemplateSizeType> const& b) {
return a.size() == b.size() &&
std::equal(std::begin(a), std::end(a), std::begin(b));
}

template <typename T>
inline bool operator<(vector<T> const& a, vector<T> const& b) {
template <typename T, typename Ptr, typename TemplateSizeType>
inline bool operator<(basic_vector<T, Ptr, TemplateSizeType> const& a,
basic_vector<T, Ptr, TemplateSizeType> const& b) {
return std::lexicographical_compare(std::begin(a), std::end(a), std::begin(b),
std::end(b));
}

template <typename T>
inline bool operator<=(vector<T> const& a, vector<T> const& b) {
template <typename T, typename Ptr, typename TemplateSizeType>
inline bool operator<=(basic_vector<T, Ptr, TemplateSizeType> const& a,
basic_vector<T, Ptr, TemplateSizeType> const& b) {
return !(a > b);
}

template <typename T>
inline bool operator>(vector<T> const& a, vector<T> const& b) {
template <typename T, typename Ptr, typename TemplateSizeType>
inline bool operator>(basic_vector<T, Ptr, TemplateSizeType> const& a,
basic_vector<T, Ptr, TemplateSizeType> const& b) {
return b < a;
}

template <typename T>
inline bool operator>=(vector<T> const& a, vector<T> const& b) {
template <typename T, typename Ptr, typename TemplateSizeType>
inline bool operator>=(basic_vector<T, Ptr, TemplateSizeType> const& a,
basic_vector<T, Ptr, TemplateSizeType> const& b) {
return !(a < b);
}

Expand Down
Loading

0 comments on commit 8ef0536

Please sign in to comment.