diff --git a/src/include/base/base.h b/src/include/base/base.h index c75373f..f5630af 100644 --- a/src/include/base/base.h +++ b/src/include/base/base.h @@ -24,6 +24,7 @@ #include "dtype.h" #include "isq.h" #include "iter.h" +#include "uuid.h" #include "base.cc.h" diff --git a/src/include/base/uuid.h b/src/include/base/uuid.h new file mode 100644 index 0000000..30db6cb --- /dev/null +++ b/src/include/base/uuid.h @@ -0,0 +1,50 @@ + +#ifndef __cxz_uuid_h__ +#define __cxz_uuid_h__ + +//#include +#include +#include + +namespace CNORXZ +{ + struct Uuid + { + uint64_t i1; + uint64_t i2; + }; + + Uuid mkUuid(); + + inline bool operator==(const Uuid& a, const Uuid& b) + { + return a.i1 == b.i1 and a.i2 == b.i2; + } + + inline bool operator!=(const Uuid& a, const Uuid& b) + { + return a.i1 != b.i1 or a.i2 != b.i2; + } + + inline bool operator<(const Uuid& a, const Uuid& b) + { + return (a.i1 == b.i1) ? a.i2 < b.i2 : a.i1 < b.i1; + } + + inline bool operator>(const Uuid& a, const Uuid& b) + { + return (a.i1 == b.i1) ? a.i2 > b.i2 : a.i1 > b.i1; + } + + inline bool operator<=(const Uuid& a, const Uuid& b) + { + return not( (a.i1 == b.i1) ? a.i2 > b.i2 : a.i1 > b.i1 ); + } + + inline bool operator>=(const Uuid& a, const Uuid& b) + { + return not( (a.i1 == b.i1) ? a.i2 < b.i2 : a.i1 < b.i1 ); + } +} + +#endif diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index cb51dc8..1dfbdf2 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -24,15 +24,15 @@ namespace CNORXZ virtual void make() = 0; - RangePtr fromCreated(const TypeInfo& info, const Vector& rids) const; - void addToCreated(const TypeInfo& info, const Vector& rids, const RangePtr& r); + RangePtr fromCreated(const TypeInfo& info, const Vector& rids) const; + void addToCreated(const TypeInfo& info, const Vector& rids, const RangePtr& r); RangePtr mProd; private: // also add single ranges here (PtrId -> own) // rangeCast: PtrId -> original Range - static Map,RangePtr>> sCreated; + static Map,RangePtr>> sCreated; }; @@ -58,7 +58,7 @@ namespace CNORXZ bool operator==(const RangeBase& in) const; bool operator!=(const RangeBase& in) const; - PtrId id() const; + Uuid id() const; DIndex begin() const; DIndex end() const; RangePtr orig() const; @@ -67,9 +67,10 @@ namespace CNORXZ protected: - RangeBase() = default; + RangeBase(); RangeBase(const RangePtr& rel); // delete copy/move??? + Uuid mId = {0,0}; Wptr mThis; RangePtr mRel; // used, if created from another range, to point on it }; diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 850022c..453861a 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -1,6 +1,7 @@ set(libcnorxz_a_SOURCES ${CMAKE_SOURCE_DIR}/src/lib/base/to_string.cc + ${CMAKE_SOURCE_DIR}/src/lib/base/uuid.cc ${CMAKE_SOURCE_DIR}/src/lib/memory/memcount.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/yrange.cc diff --git a/src/lib/base/uuid.cc b/src/lib/base/uuid.cc new file mode 100644 index 0000000..7ecbced --- /dev/null +++ b/src/lib/base/uuid.cc @@ -0,0 +1,60 @@ + +#include +#include +#include +#include "base/uuid.h" + +#include + +namespace CNORXZ +{ + + constexpr std::tm mkTRef() + { + std::tm o = {}; + o.tm_sec = 0; + o.tm_min = 0; + o.tm_hour = 0; + o.tm_mday = 1; + o.tm_mon = 0; + o.tm_year = 70; + o.tm_wday = 4; + o.tm_yday = 0; + o.tm_isdst = 0; + return o; + } + + // the node bits are created randomly (no search for mac or similar) + Uuid mkUuid() + { + static constexpr uint64_t diff_1970_1582_ns = + 141427ull * 86400ull * 1000ull * 1000ull * 1000ull; + static std::tm tref_1970 = mkTRef(); + static uint16_t version = 1; + std::tm tref_1970_tmp = tref_1970; + const auto ref = std::chrono::system_clock::from_time_t(std::mktime(&tref_1970_tmp)); + const auto now = std::chrono::system_clock::now(); + const uint64_t diff_ns = std::chrono::duration_cast> + (now - ref).count() + diff_1970_1582_ns; + + // following nomenclature of wikipedia + const uint32_t time_low = diff_ns & 0xffffffff; + const uint16_t time_mid = (diff_ns >> 32) & 0xffff; + const uint16_t time_hi_and_version = ((diff_ns >> 48) & 0x0fff) | ( version << 12 ); + const uint8_t clock_seq_high_and_reserved = ((diff_ns >> 8) & 0x3f) | ( 0x02 << 6 ); + const uint8_t clock_seq_low = diff_ns & 0xff; + + static std::mt19937_64 gen {diff_ns}; + const uint64_t node = gen(); + + const Uuid id = + { ( static_cast(time_low) << 32 ) | + ( static_cast(time_mid) << 16 ) | + ( static_cast(time_hi_and_version) ), + ( static_cast(clock_seq_high_and_reserved) << 56 ) | + ( static_cast(clock_seq_low) << 48 ) | + ( node & 0xffffffffffff ) + }; + return id; + } +} diff --git a/src/lib/ranges/range_base.cc b/src/lib/ranges/range_base.cc index 8441e22..b2a333d 100644 --- a/src/lib/ranges/range_base.cc +++ b/src/lib/ranges/range_base.cc @@ -8,7 +8,7 @@ namespace CNORXZ * RangeFactoryBase * *************************/ - Map,RangePtr>> RangeFactoryBase::sCreated; + Map,RangePtr>> RangeFactoryBase::sCreated; RangePtr RangeFactoryBase::create() { @@ -19,7 +19,7 @@ namespace CNORXZ return mProd; } - RangePtr RangeFactoryBase::fromCreated(const TypeInfo& info, const Vector& rids) const + RangePtr RangeFactoryBase::fromCreated(const TypeInfo& info, const Vector& rids) const { RangePtr out = nullptr; if(sCreated.count(info.hash_code()) != 0){ @@ -30,7 +30,7 @@ namespace CNORXZ return out; } - void RangeFactoryBase::addToCreated(const TypeInfo& info, const Vector& rids, const RangePtr& r) + void RangeFactoryBase::addToCreated(const TypeInfo& info, const Vector& rids, const RangePtr& r) { sCreated[info.hash_code()][rids] = r; } @@ -39,6 +39,11 @@ namespace CNORXZ * RangeBase * ******************/ + RangeBase::RangeBase() + { + mId = mkUuid(); + } + RangePtr RangeBase::sub(SizeT num) const { return nullptr; @@ -54,9 +59,10 @@ namespace CNORXZ return this != ∈ } - PtrId RangeBase::id() const + Uuid RangeBase::id() const { - return reinterpret_cast(this); + //return reinterpret_cast(this); + return mId; } DIndex RangeBase::begin() const diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 634d9fe..d3597c4 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -425,7 +425,7 @@ namespace CNORXZ void YRangeFactory::make() { - Vector key(mRVec.size()); + Vector key(mRVec.size()); std::transform(mRVec.begin(), mRVec.end(), key.begin(), [&](const RangePtr& r) { return r->id(); } ); mProd = this->fromCreated(typeid(YRange), key);