From aff049a21202708ff9f35a40853a62ebf81a8f23 Mon Sep 17 00:00:00 2001 From: morzhovets Date: Sun, 14 Jul 2024 00:21:05 +0400 Subject: [PATCH] Update DataColumn classes --- include/momo/DataColumn.h | 650 ++++++++++++++++++++--------------- include/momo/DataSelection.h | 4 +- include/momo/DataTable.h | 16 +- 3 files changed, 392 insertions(+), 278 deletions(-) diff --git a/include/momo/DataColumn.h b/include/momo/DataColumn.h index 75107d83..b77c60ad 100644 --- a/include/momo/DataColumn.h +++ b/include/momo/DataColumn.h @@ -14,8 +14,12 @@ MOMO_DATA_COLUMN_STRING namespace momo: - class DataSettings + class DataEquality + class DataAssignment struct DataStructDefault + class DataColumn + class DataColumnInfo + class DataSettings class DataColumnTraits class DataItemTraits class DataColumnList @@ -34,11 +38,11 @@ #endif #define MOMO_DATA_COLUMN_STRUCT(Struct, name) \ - constexpr momo::internal::DataColumn().name), Struct> \ + constexpr momo::DataColumn().name), Struct> \ name{uint64_t{offsetof(Struct, name)}, #Struct "." #name} #define MOMO_DATA_COLUMN_STRING_TAG(Tag, Type, name) \ - constexpr momo::internal::DataColumn name{#name} + constexpr momo::DataColumn name{#name} #define MOMO_DATA_COLUMN_STRING(Type, name) \ MOMO_DATA_COLUMN_STRING_TAG(momo::DataStructDefault<>, Type, name) @@ -63,353 +67,469 @@ namespace internal } }; - template - class DataOperator + template + struct DataMutable + { + }; + + template + struct DataColumnCodeSelector + { + typedef uint64_t Code; + }; + + template + struct DataColumnItemSelector { + typedef typename TColumn::Item Item; + }; + + template + class DataColumnInfoBase + { + public: + typedef TStruct Struct; + typedef TCode Code; + + private: + template + struct VisitableItemsSelector + { + typedef std::tuple<> VisitableItems; + }; + +#ifndef MOMO_DISABLE_TYPE_INFO + template + struct VisitableItemsSelector> + { + typedef typename Struct::VisitableItems VisitableItems; + }; +#endif + public: - typedef TColumn Column; - typedef TItemArg ItemArg; + typedef typename VisitableItemsSelector::VisitableItems VisitableItems; public: - explicit DataOperator(const Column& column, ItemArg&& itemArg) noexcept - : mColumn(column), - mItemArg(std::forward(itemArg)) + Code GetCode() const noexcept { + return mCode; } -#ifndef MOMO_HAS_GUARANTEED_COPY_ELISION - DataOperator(DataOperator&& oper) noexcept - : mColumn(oper.mColumn), - mItemArg(std::forward(oper.mItemArg)) +#ifndef MOMO_DISABLE_TYPE_INFO + const std::type_info& GetTypeInfo() const noexcept { - //MOMO_ASSERT(false); + return mTypeInfo; } #endif - DataOperator(const DataOperator&) = delete; + protected: + template + explicit DataColumnInfoBase(Code code, Item*) noexcept +#ifndef MOMO_DISABLE_TYPE_INFO + : mTypeInfo(typeid(Item)) +#endif + { + mCode = code; + } - ~DataOperator() = default; + template + void ptVisit(Void* item, const PtrVisitor& ptrVisitor) const + { +#ifndef MOMO_DISABLE_TYPE_INFO + pvVisitRec(item, ptrVisitor); +#else + pvVisit(item, ptrVisitor); +#endif + } - DataOperator& operator=(const DataOperator&) = delete; + private: +#ifndef MOMO_DISABLE_TYPE_INFO + template + EnableIf<(index < std::tuple_size::value)> + pvVisitRec(Void* item, const PtrVisitor& ptrVisitor) const + { + typedef typename std::tuple_element::type Item; + typedef typename std::conditional::value, + const Item*, Item*>::type ItemPtr; + if (typeid(Item) == mTypeInfo) + pvVisit(static_cast(item), ptrVisitor); + else + pvVisitRec(item, ptrVisitor); + } - const Column& GetColumn() const noexcept + template + EnableIf<(index == std::tuple_size::value)> + pvVisitRec(Void* item, const PtrVisitor& ptrVisitor) const { - return mColumn; + pvVisit(item, ptrVisitor); } +#endif - ItemArg&& GetItemArg() const noexcept + template + EnableIf::value> + pvVisit(Item* item, const PtrVisitor& ptrVisitor) const { - return std::forward(mItemArg); + ptrVisitor(item, *static_cast(this)); + } + + template + EnableIf::value && + !IsInvocable::value> + pvVisit(Item* item, const PtrVisitor& ptrVisitor) const + { + ptrVisitor(item); } private: - const Column& mColumn; - ItemArg&& mItemArg; + Code mCode; +#ifndef MOMO_DISABLE_TYPE_INFO + const std::type_info& mTypeInfo; +#endif }; +} + +template +class DataEquality +{ +private: + typedef std::tuple...> Tuple; - template - class DataEqualer : public DataOperator +public: + DataEquality(std::pair::Item&>... pairs) noexcept + : mTuple(pairs...) { - private: - typedef DataOperator Operator; + } - public: - using Operator::Operator; - }; +#ifndef MOMO_HAS_GUARANTEED_COPY_ELISION + DataEquality(DataEquality&&) = default; +#endif + + DataEquality(const DataEquality&) = delete; - template - class DataAssigner : public DataOperator + ~DataEquality() = default; + + DataEquality& operator=(const DataEquality&) = delete; + + template + const typename std::tuple_element::type& Get() const noexcept { - private: - typedef DataOperator Operator; + return std::get(mTuple); + } - public: - using Operator::Operator; - }; + template + DataEquality And(const RightColumn& rightColumn, + const typename internal::DataColumnItemSelector::Item& rightItem) && noexcept + { + return pvAnd(rightColumn, rightItem, + typename internal::SequenceMaker::Sequence()); + } - template - struct DataMutable +private: + template::Item, + size_t... sequence> + DataEquality pvAnd(const RightColumn& rightColumn, + const RightItem& rightItem, internal::Sequence) const noexcept { - }; + return DataEquality( + std::pair::Item&>( + Get().GetColumn(), Get().GetItem())..., + std::pair(rightColumn, rightItem)); + } - template - class DataColumn : public DataColumn, TStruct, TCode> +private: + Tuple mTuple; +}; + +template +class DataEquality +{ +public: + typedef typename internal::DataColumnItemSelector::Item Item; + +public: + DataEquality(const Column& column, const Item& item) noexcept + : mColumn(column), + mItem(item) { - public: - typedef TItem Item; - typedef TStruct Struct; - typedef TCode Code; + } - typedef DataColumn BaseColumn; + DataEquality(std::pair pair) noexcept + : DataEquality(pair.first, pair.second) + { + } + +#ifndef MOMO_HAS_GUARANTEED_COPY_ELISION + DataEquality(DataEquality&&) = default; +#endif - typedef DataColumn, Struct, Code> MutableColumn; + DataEquality(const DataEquality&) = delete; - typedef DataEqualer Equaler; + ~DataEquality() = default; - template - using Assigner = DataAssigner; + DataEquality& operator=(const DataEquality&) = delete; - public: - constexpr explicit DataColumn(const char* name) noexcept - : mCode(static_cast(StrHasher::GetHashCode64(name))), - mName(name) - { - } + const Column& GetColumn() const noexcept + { + return mColumn; + } - constexpr explicit DataColumn(Code code, const char* name = "") noexcept - : mCode(code), - mName(name) - { - } + const Item& GetItem() const noexcept + { + return mItem; + } - //DataColumn(const DataColumn&) = delete; + template + const DataEquality& Get() const noexcept + { + static_assert(index == 0); + return *this; + } - //~DataColumn() = default; + template + DataEquality And(const RightColumn& rightColumn, + const typename internal::DataColumnItemSelector::Item& rightItem) && noexcept + { + typedef typename internal::DataColumnItemSelector::Item RightItem; + return DataEquality( + std::pair(mColumn, mItem), + std::pair(rightColumn, rightItem)); + } - //DataColumn& operator=(const DataColumn&) = delete; + template + friend DataEquality operator&&( + DataEquality leftEquals, DataEquality equal) noexcept + { + return std::move(leftEquals).And(equal.GetColumn(), equal.GetItem()); + } - const BaseColumn& GetBaseColumn() const noexcept - { - return *this; - } +private: + const Column& mColumn; + const Item& mItem; +}; - constexpr Code GetCode() const noexcept - { - return mCode; - } +#ifdef MOMO_HAS_DEDUCTION_GUIDES +template +DataEquality(Column, typename internal::DataColumnItemSelector::Item) + -> DataEquality; +#endif - constexpr const char* GetName() const noexcept - { - return mName; - } +template +class DataAssignment +{ +public: + typedef TColumn Column; + typedef TItemArg ItemArg; - constexpr const MutableColumn& Mutable() const noexcept - { - return *this; - } +public: + DataAssignment(const Column& column, ItemArg&& itemArg) noexcept + : mColumn(column), + mItemArg(std::forward(itemArg)) + { + } - constexpr bool IsMutable() const noexcept - { - return false; - } +#ifndef MOMO_HAS_GUARANTEED_COPY_ELISION + DataAssignment(DataAssignment&&) = default; +#endif - Equaler operator==(const Item& item) const noexcept - { - return Equaler(*this, item); - } + DataAssignment(const DataAssignment&) = delete; - template - Assigner operator=(ItemArg&& itemArg) const noexcept - { - return Assigner(*this, std::forward(itemArg)); - } + ~DataAssignment() = default; - private: - Code mCode; - const char* mName; - }; + DataAssignment& operator=(const DataAssignment&) = delete; - template - class DataColumn, TStruct, TCode> + const Column& GetColumn() const noexcept { - public: - typedef TItem Item; - typedef TStruct Struct; - typedef TCode Code; + return mColumn; + } + + ItemArg&& GetItemArg() const noexcept + { + return std::forward(mItemArg); + } - typedef DataColumn BaseColumn; +private: + const Column& mColumn; + ItemArg&& mItemArg; +}; - public: - const BaseColumn& GetBaseColumn() const noexcept - { - return static_cast(*this); - } +#ifdef MOMO_HAS_DEDUCTION_GUIDES +template +DataAssignment(Column, ItemArg&) + -> DataAssignment; +#endif - constexpr bool IsMutable() const noexcept - { - return true; - } +template +struct DataStructDefault +{ + typedef std::tuple VisitableItems; +}; - protected: - constexpr explicit DataColumn() noexcept - { - } +template, + typename TCode = typename internal::DataColumnCodeSelector::Code> +class DataColumn : public DataColumn, TStruct, TCode> +{ +public: + typedef TItem Item; + typedef TStruct Struct; + typedef TCode Code; - DataColumn(const DataColumn&) = default; + typedef DataColumn BaseColumn; - ~DataColumn() = default; + typedef DataColumn, Struct, Code> MutableColumn; - DataColumn& operator=(const DataColumn&) = default; - }; + typedef DataEquality Equaler; - template - class DataColumnInfoBase + template + using Assigner = DataAssignment; + +public: + constexpr explicit DataColumn(const char* name) noexcept + : mCode(internal::StrHasher::GetHashCode64(name)), + mName(name) { - public: - typedef TStruct Struct; - typedef TCode Code; + static_assert(std::is_same::value); + } - template - using Column = DataColumn; + constexpr explicit DataColumn(Code code, const char* name = "") noexcept + : mCode(code), + mName(name) + { + } - public: - Code GetCode() const noexcept - { - return mCode; - } + //DataColumn(const DataColumn&) = delete; - const char* GetName() const noexcept - { - return mName; - } + //~DataColumn() = default; - template - static Code GetCode(const Column& column) noexcept - { - return column.GetCode(); - } + //DataColumn& operator=(const DataColumn&) = delete; - protected: - explicit DataColumnInfoBase(Code code, const char* name) noexcept - : mCode(code), - mName(name) - { - } + const BaseColumn& GetBaseColumn() const noexcept + { + return *this; + } - template - EnableIf::value> - ptVisit(Item* item, const PtrVisitor& ptrVisitor) const - { - ptrVisitor(item, *static_cast(this)); - } + constexpr Code GetCode() const noexcept + { + return mCode; + } - template - EnableIf::value && - !IsInvocable::value> - ptVisit(Item* item, const PtrVisitor& ptrVisitor) const - { - ptrVisitor(item); - } + constexpr const char* GetName() const noexcept + { + return mName; + } - private: - Code mCode; - const char* mName; - }; + constexpr const MutableColumn& Mutable() const noexcept + { + return *this; + } -#ifdef MOMO_DISABLE_TYPE_INFO - template - class DataColumnInfo : public DataColumnInfoBase + constexpr bool IsMutable() const noexcept { - private: - typedef DataColumnInfoBase ColumnInfoBase; + return false; + } - public: - template - using Column = typename ColumnInfoBase::template Column; + Equaler operator==(const Item& item) const noexcept + { + return Equaler(*this, item); + } - typedef std::tuple<> VisitableItems; + template + Assigner operator=(ItemArg&& itemArg) const noexcept + { + return Assigner(*this, std::forward(itemArg)); + } - public: - template - DataColumnInfo(const Column& column) noexcept - : ColumnInfoBase(column.GetCode(), column.GetName()) - { - } +private: + Code mCode; + const char* mName; +}; - template - void Visit(const void* item, const PtrVisitor& ptrVisitor) const - { - ColumnInfoBase::template ptVisit(item, ptrVisitor); - } +template +class DataColumn, TStruct, TCode> +{ +public: + typedef TItem Item; + typedef TStruct Struct; + typedef TCode Code; - template - void Visit(void* item, const PtrVisitor& ptrVisitor) const - { - ColumnInfoBase::template ptVisit(item, ptrVisitor); - } - }; -#else - template - struct DataVisitableItemsGetter + typedef DataColumn BaseColumn; + +public: + const BaseColumn& GetBaseColumn() const noexcept { - typedef std::tuple<> VisitableItems; - }; + return static_cast(*this); + } - template - struct DataVisitableItemsGetter> + constexpr bool IsMutable() const noexcept { - typedef typename Struct::VisitableItems VisitableItems; - }; + return true; + } - template - class DataColumnInfo : public DataColumnInfoBase +protected: + constexpr explicit DataColumn() noexcept { - private: - typedef DataColumnInfoBase ColumnInfoBase; + } - public: - using typename ColumnInfoBase::Struct; + DataColumn(const DataColumn&) = default; - template - using Column = typename ColumnInfoBase::template Column; + ~DataColumn() = default; - typedef typename DataVisitableItemsGetter::VisitableItems VisitableItems; + DataColumn& operator=(const DataColumn&) = default; +}; - public: - template - DataColumnInfo(const Column& column) noexcept - : ColumnInfoBase(column.GetCode(), column.GetName()), - mTypeInfo(typeid(Item)) - { - } +template, //? + typename TCode = typename internal::DataColumnCodeSelector::Code> +class DataColumnInfo : public internal::DataColumnInfoBase +{ +private: + typedef internal::DataColumnInfoBase ColumnInfoBase; - const std::type_info& GetTypeInfo() const noexcept - { - return mTypeInfo; - } +public: + using typename ColumnInfoBase::Struct; + using typename ColumnInfoBase::Code; - template - void Visit(const void* item, const PtrVisitor& ptrVisitor) const - { - pvVisitRec<0>(item, ptrVisitor); - } + template + using Column = DataColumn; - template - void Visit(void* item, const PtrVisitor& ptrVisitor) const - { - pvVisitRec<0>(item, ptrVisitor); - } +public: + template + DataColumnInfo(const Column& column) noexcept + : ColumnInfoBase(GetCode(column), static_cast(nullptr)), + mName(column.GetName()) + { + } - private: - template - EnableIf<(index < std::tuple_size::value)> - pvVisitRec(Void* item, const PtrVisitor& ptrVisitor) const - { - typedef typename std::tuple_element::type Item; - typedef typename std::conditional::value, - const Item*, Item*>::type ItemPtr; - if (typeid(Item) == mTypeInfo) - ColumnInfoBase::template ptVisit(static_cast(item), ptrVisitor); - else - pvVisitRec(item, ptrVisitor); - } + using ColumnInfoBase::GetCode; - template - EnableIf<(index == std::tuple_size::value)> - pvVisitRec(Void* item, const PtrVisitor& ptrVisitor) const - { - ColumnInfoBase::template ptVisit(item, ptrVisitor); - } + template + static Code GetCode(const Column& column) noexcept + { + return column.GetCode(); + } - private: - const std::type_info& mTypeInfo; - }; -#endif -} + const char* GetName() const noexcept + { + return mName; + } + + template + void Visit(const void* item, const PtrVisitor& ptrVisitor) const + { + ColumnInfoBase::template ptVisit(item, ptrVisitor); + } + + template + void Visit(void* item, const PtrVisitor& ptrVisitor) const + { + ColumnInfoBase::template ptVisit(item, ptrVisitor); + } + +private: + const char* mName; +}; template class DataSettings @@ -425,12 +545,6 @@ class DataSettings typedef ArraySettings<4, true, true> SelectionRawsSettings; }; -template -struct DataStructDefault -{ - typedef std::tuple VisitableItems; -}; - template, size_t tLogVertexCount = 8> class DataColumnTraits @@ -444,7 +558,7 @@ class DataColumnTraits static const size_t maxColumnCount = size_t{1} << (logVertexCount - 1); static const size_t maxCodeParam = 255; - typedef internal::DataColumnInfo ColumnInfo; + typedef DataColumnInfo ColumnInfo; template using Column = typename ColumnInfo::template Column; @@ -1138,7 +1252,7 @@ class DataColumnListStatic typedef TMemManager MemManager; typedef TSettings Settings; - typedef internal::DataColumnInfo ColumnInfo; + typedef DataColumnInfo ColumnInfo; template using Column = typename ColumnInfo::template Column; diff --git a/include/momo/DataSelection.h b/include/momo/DataSelection.h index 14045d03..6dd31e94 100644 --- a/include/momo/DataSelection.h +++ b/include/momo/DataSelection.h @@ -419,7 +419,7 @@ namespace internal using Column = typename ColumnList::template Column; template - using Equaler = internal::DataEqualer, const Item&>; + using Equaler = DataEquality>; protected: typedef internal::VersionKeeper VersionKeeper; @@ -889,7 +889,7 @@ namespace internal const Equaler&... equalers) { const Item& item1 = ColumnList::template GetByOffset(raw, *offsets); - const Item& item2 = equaler.GetItemArg(); + const Item& item2 = equaler.GetItem(); int cmp = DataTraits::Compare(item1, item2); if (cmp != 0) return cmp; diff --git a/include/momo/DataTable.h b/include/momo/DataTable.h index f9afe456..1e067957 100644 --- a/include/momo/DataTable.h +++ b/include/momo/DataTable.h @@ -85,10 +85,10 @@ class DataTable using Column = typename ColumnList::template Column; template - using Equaler = internal::DataEqualer, const Item&>; + using Equaler = DataEquality>; template - using Assigner = internal::DataAssigner, ItemArg>; + using Assigner = DataAssignment, ItemArg>; typedef internal::DataRow Row; typedef internal::DataRowReference RowReference; @@ -1303,7 +1303,7 @@ class DataTable Raw* raw = ConstRowReferenceProxy::GetRaw(rowRef); size_t offset = rowRef.GetColumnList().GetOffset(equaler.GetColumn()); const Item& item = ColumnList::template GetByOffset(raw, offset); - return DataTraits::IsEqual(item, equaler.GetItemArg()) && rowFilter(rowRef); + return DataTraits::IsEqual(item, equaler.GetItem()) && rowFilter(rowRef); }; return pvSelect(newRowFilter, equalers...); } @@ -1344,7 +1344,7 @@ class DataTable { Raw* raw = ConstRowReferenceProxy::GetRaw(rowRef); const Item& item = ColumnList::template GetByOffset(raw, *offsets); - return DataTraits::IsEqual(item, equaler.GetItemArg()) + return DataTraits::IsEqual(item, equaler.GetItem()) && pvIsSatisfied(rowRef, offsets + 1, equalers...); } @@ -1362,7 +1362,7 @@ class DataTable if (mIndexes.ContainsOffset(index, offset)) { auto newTuple = std::tuple_cat(std::move(tuple), - std::make_tuple(std::pair(offset, equaler.GetItemArg()))); + std::make_tuple(std::pair(offset, equaler.GetItem()))); return pvSelectRec(index, offsets + 1, rowFilter, std::move(newTuple), equalers...); } @@ -1372,7 +1372,7 @@ class DataTable { Raw* raw = ConstRowReferenceProxy::GetRaw(rowRef); const Item& item = ColumnList::template GetByOffset(raw, offset); - return DataTraits::IsEqual(item, equaler.GetItemArg()) && rowFilter(rowRef); + return DataTraits::IsEqual(item, equaler.GetItem()) && rowFilter(rowRef); }; return pvSelectRec(index, offsets + 1, newRowFilter, std::move(tuple), equalers...); @@ -1450,14 +1450,14 @@ class DataTable typename internal::SequenceMaker::Sequence(), equalers...); } - template + template RowBoundsProxy pvFindByHash(Index index, internal::Sequence, const Equaler&... equalers) const { auto offsets = pvGetOffsets(equalers...); Index trueIndex = mIndexes.GetTrueIndex(index, offsets); OffsetItemTuple tuple{ - std::pair(offsets[sequence], equalers.GetItemArg())... }; + std::pair(offsets[sequence], equalers.GetItem())... }; auto raws = mIndexes.FindRaws(trueIndex, tuple, VersionKeeper(&mCrew.GetChangeVersion())); return RowBoundsProxy(&GetColumnList(), raws, VersionKeeper(&mCrew.GetRemoveVersion())); }