switch to uuid as range id

This commit is contained in:
Christian Zimmermann 2023-02-21 16:52:05 +01:00
parent 25b5c13608
commit b3d1ced728
7 changed files with 130 additions and 11 deletions

View file

@ -24,6 +24,7 @@
#include "dtype.h"
#include "isq.h"
#include "iter.h"
#include "uuid.h"
#include "base.cc.h"

50
src/include/base/uuid.h Normal file
View file

@ -0,0 +1,50 @@
#ifndef __cxz_uuid_h__
#define __cxz_uuid_h__
//#include <cstdlib>
#include <stdint.h>
#include <functional>
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

View file

@ -24,15 +24,15 @@ namespace CNORXZ
virtual void make() = 0;
RangePtr fromCreated(const TypeInfo& info, const Vector<PtrId>& rids) const;
void addToCreated(const TypeInfo& info, const Vector<PtrId>& rids, const RangePtr& r);
RangePtr fromCreated(const TypeInfo& info, const Vector<Uuid>& rids) const;
void addToCreated(const TypeInfo& info, const Vector<Uuid>& rids, const RangePtr& r);
RangePtr mProd;
private:
// also add single ranges here (PtrId -> own)
// rangeCast: PtrId -> original Range
static Map<SizeT,Map<Vector<PtrId>,RangePtr>> sCreated;
static Map<SizeT,Map<Vector<Uuid>,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<RangeBase> mThis;
RangePtr mRel; // used, if created from another range, to point on it
};

View file

@ -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

60
src/lib/base/uuid.cc Normal file
View file

@ -0,0 +1,60 @@
#include <random>
#include <ctime>
#include <chrono>
#include "base/uuid.h"
#include <iostream>
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<std::chrono::duration<uint64_t,std::nano>>
(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<uint64_t>(time_low) << 32 ) |
( static_cast<uint64_t>(time_mid) << 16 ) |
( static_cast<uint64_t>(time_hi_and_version) ),
( static_cast<uint64_t>(clock_seq_high_and_reserved) << 56 ) |
( static_cast<uint64_t>(clock_seq_low) << 48 ) |
( node & 0xffffffffffff )
};
return id;
}
}

View file

@ -8,7 +8,7 @@ namespace CNORXZ
* RangeFactoryBase *
*************************/
Map<SizeT,Map<Vector<PtrId>,RangePtr>> RangeFactoryBase::sCreated;
Map<SizeT,Map<Vector<Uuid>,RangePtr>> RangeFactoryBase::sCreated;
RangePtr RangeFactoryBase::create()
{
@ -19,7 +19,7 @@ namespace CNORXZ
return mProd;
}
RangePtr RangeFactoryBase::fromCreated(const TypeInfo& info, const Vector<PtrId>& rids) const
RangePtr RangeFactoryBase::fromCreated(const TypeInfo& info, const Vector<Uuid>& 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<PtrId>& rids, const RangePtr& r)
void RangeFactoryBase::addToCreated(const TypeInfo& info, const Vector<Uuid>& 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 != &in;
}
PtrId RangeBase::id() const
Uuid RangeBase::id() const
{
return reinterpret_cast<PtrId>(this);
//return reinterpret_cast<PtrId>(this);
return mId;
}
DIndex RangeBase::begin() const

View file

@ -425,7 +425,7 @@ namespace CNORXZ
void YRangeFactory::make()
{
Vector<PtrId> key(mRVec.size());
Vector<Uuid> key(mRVec.size());
std::transform(mRVec.begin(), mRVec.end(), key.begin(),
[&](const RangePtr& r) { return r->id(); } );
mProd = this->fromCreated(typeid(YRange), key);