Skip to content

Commit

Permalink
#462: preparatory cleanup of rlookup.h
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartenHilferink committed Aug 31, 2023
1 parent 9ea8ffe commit 472ef41
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 428 deletions.
516 changes: 166 additions & 350 deletions clc/dll/include/rlookup.h

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions clc/dll/src/RLookup.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
// Copyright (C) 2023 Object Vision b.v.
// License: GNU GPL 3
/////////////////////////////////////////////////////////////////////////////

#include "ClcPCH.h"
#pragma hdrstop

//#define MG_REDUCE_CODE_HACK

#include "Rlookup.ipp"

// *****************************************************************************
// RLookupOperator
// *****************************************************************************
namespace
{

CommonOperGroup cog_rlookup("rlookup", oper_policy::dynamic_result_class);
CommonOperGroup cog_rlookup("rlookup", oper_policy::dynamic_result_class);

template <class V>
struct RLookupOperator : public SearchIndexOperatorImpl<V, rlookup_dispatcher>
{
RLookupOperator()
: SearchIndexOperatorImpl<V, rlookup_dispatcher>(&cog_rlookup)
{}
};
template <class V>
struct RLookupOperator : public SearchIndexOperatorImpl<V, rlookup_dispatcher>
{
RLookupOperator()
: SearchIndexOperatorImpl<V, rlookup_dispatcher>(&cog_rlookup)
{}
};

// *****************************************************************************
// INSTANTIATION
// *****************************************************************************
// *****************************************************************************
// INSTANTIATION
// *****************************************************************************

namespace
{
#if defined(DMS_64)
tl_oper::inst_tuple<typelists::value_elements, RLookupOperator<_>> rlookupInstances;
#else
tl_oper::inst_tuple<typelists::fields, RLookupOperator<_>> rlookupInstances;
#endif

} // end anonymous namespace

/******************************************************************************/
72 changes: 34 additions & 38 deletions clc/dll/src/RLookup.ipp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (C) 2023 Object Vision b.v.
// License: GNU GPL 3
/////////////////////////////////////////////////////////////////////////////

#include "ClcPCH.h"
#pragma hdrstop

Expand Down Expand Up @@ -68,19 +72,18 @@ public:
AbstrDataItem* res = AsDataItem(resultHolder.GetNew());

tile_id nrTiles = arg1_DomainUnit->GetNrTiles();
bool arg1HasUndefined = arg1A->HasUndefinedValues();

auto index = MakeIndex(arg2A, arg2_DomainUnit);
const AbstrUnit* arg2Domain = arg2A->GetAbstrDomainUnit();
auto arg2DomainRange = arg2Lock->GetTiledRangeData();
if (IsMultiThreaded3() && (nrTiles > 1) && (LTF_ElementWeight(arg1A) <= LTF_ElementWeight(res)) && (nrTiles > arg2DomainRange->GetNrTiles()))
AsDataItem(resultHolder.GetOld())->m_DataObject = CreateFutureTileIndexer(res->GetLazyCalculatedState(), arg2_DomainUnit, arg1A, arg1HasUndefined, arg2Domain, arg2DomainRange, std::move(index) MG_DEBUG_ALLOCATOR_SRC("res->md_FullName + RLookup()"));
AsDataItem(resultHolder.GetOld())->m_DataObject = CreateFutureTileIndexer(res->GetLazyCalculatedState(), arg2_DomainUnit, arg1A, arg2Domain, arg2DomainRange, std::move(index) MG_DEBUG_ALLOCATOR_SRC("res->md_FullName + RLookup()"));
else
{
DataWriteLock resLock(res);
parallel_tileloop(nrTiles, [this, &resLock, arg1A, arg1HasUndefined, arg2Domain, &index](tile_id t)->void
parallel_tileloop(nrTiles, [this, &resLock, arg1A, arg2Domain, &index](tile_id t)->void
{
this->Calculate(resLock.get(), arg1A, arg1HasUndefined, arg2Domain, index, t);
this->Calculate(resLock.get(), arg1A, arg2Domain, index, t);
}
);
resLock.Commit();
Expand All @@ -89,8 +92,8 @@ public:
return true;
}
virtual std::any MakeIndex(const AbstrDataItem* arg2A, const AbstrUnit* arg2_DomainUnit) const = 0;
virtual auto CreateFutureTileIndexer(bool lazy, const AbstrUnit* valuesUnitA, const AbstrDataItem* arg1A, bool arg1HasUndefined, const AbstrUnit* arg2Domain, const AbstrTileRangeData* arg2DomainRange, std::any index MG_DEBUG_ALLOCATOR_SRC_ARG) const->SharedPtr<const AbstrDataObject> = 0;
virtual void Calculate(AbstrDataObject* resObj, const AbstrDataItem* arg1A, bool arg1HasUndefined, const AbstrUnit* arg2Domain, const std::any&, tile_id t) const =0;
virtual auto CreateFutureTileIndexer(bool lazy, const AbstrUnit* valuesUnitA, const AbstrDataItem* arg1A, const AbstrUnit* arg2Domain, const AbstrTileRangeData* arg2DomainRange, std::any index MG_DEBUG_ALLOCATOR_SRC_ARG) const->SharedPtr<const AbstrDataObject> = 0;
virtual void Calculate(AbstrDataObject* resObj, const AbstrDataItem* arg1A, const AbstrUnit* arg2Domain, const std::any&, tile_id t) const =0;
};

template <class V>
Expand All @@ -109,22 +112,9 @@ public:
std::any result;
visit<typelists::domain_elements>(arg2DomainA, [&result, &values]<typename E>(const Unit<E>* arg2Domain)
{
using index_type = typename cardinality_type<E>::type;
result = make_index_array < index_type, V>(values);
result = make_index_array< index_type_t<E>, V>(values);
});
return result;
/* REMOVE
UInt32 bitSize = arg2_DomainUnit->GetValueType()->GetBitSize();
auto values = const_array_cast<V>(arg2A)->GetDataRead();
if (bitSize > 32)
return make_index_array<UInt64, V>(values);
else if (bitSize > 16 || bitSize < 8)
return make_index_array<UInt32, V>(values);
if (bitSize > 8)
return make_index_array<UInt16, V>(values);
else
return make_index_array<UInt8, V>(values);
*/
}
};

Expand All @@ -138,7 +128,7 @@ public:
: SearchIndexOperator<V>(og)
{}

auto CreateFutureTileIndexer(bool lazy, const AbstrUnit* valuesUnitA, const AbstrDataItem* arg1A, bool arg1HasUndefined, const AbstrUnit* arg2DomainA, const AbstrTileRangeData* arg2DomainRange, std::any indexBox MG_DEBUG_ALLOCATOR_SRC_ARG) const -> SharedPtr<const AbstrDataObject> override
auto CreateFutureTileIndexer(bool lazy, const AbstrUnit* valuesUnitA, const AbstrDataItem* arg1A, const AbstrUnit* arg2DomainA, const AbstrTileRangeData* arg2DomainRange, std::any indexBox MG_DEBUG_ALLOCATOR_SRC_ARG) const -> SharedPtr<const AbstrDataObject> override
{
auto tileRangeData = AsUnit(arg1A->GetAbstrDomainUnit()->GetCurrRangeItem())->GetTiledRangeData();
// auto valuesUnit = debug_cast<const Unit<field_of_t<ResultValueType>>*>(valuesUnitA);
Expand All @@ -150,51 +140,57 @@ public:
std::unique_ptr<AbstrDataObject> futureTileFunctor;

visit<typelists::domain_elements>(arg2DomainA
, [&futureTileFunctor, lazy, arg2DomainRange, arg1, arg1HasUndefined, indexBoxPtr, tileRangeData MG_DEBUG_ALLOCATOR_SRC_PARAM]<typename E>(const Unit<E>* arg2Domain)
, [&futureTileFunctor, lazy, arg2DomainRange, arg1, indexBoxPtr, tileRangeData MG_DEBUG_ALLOCATOR_SRC_PARAM]<typename E>(const Unit<E>* arg2Domain)
{
using index_type = typename cardinality_type<E>::type;
using index_type = index_type_t<E>;
using index_tile = indexed_tile_t<index_type, V>;
using res_seq_t = sequence_traits<E>::seq_t;

futureTileFunctor = make_unique_FutureTileFunctor<E, prepare_data, false>(lazy, tileRangeData, get_range_ptr_of_valuesunit(arg2Domain)
, [arg1](tile_id t) { return arg1->GetFutureTile(t); }
, [arg1HasUndefined, arg2DomainRange = dynamic_cast<const typename Unit<E>::range_data_t*>(arg2DomainRange)->GetRange(), indexBoxPtr](res_seq_t resData, prepare_data arg1FutureData)
, [arg2DomainRange = dynamic_cast<const typename Unit<E>::range_data_t*>(arg2DomainRange)->GetRange(), indexBoxPtr](res_seq_t resData, prepare_data arg1FutureData)
{
auto arg1Data = arg1FutureData->GetTile();

static_assert(!std::is_same_v<E, WPoint> || std::is_same_v<index_type, UInt32>);
static_assert(std::is_same_v<index_type, index_tile::first_type::value_type>);

auto indexPtr = std::any_cast<index_tile>(indexBoxPtr.get());
dms_assert(indexPtr);

IndexApplicator indexApplicator;
indexApplicator(resData, arg1Data.get_view(), indexPtr->second.get_view(), arg2DomainRange, indexPtr->first, arg1HasUndefined);
assert(indexPtr);

dms_assert(resData.size() == arg1Data.size());
CalcTile<res_seq_t, E>(resData, arg1Data, indexPtr, arg2DomainRange);
}
MG_DEBUG_ALLOCATOR_SRC_PARAM
);
});

return futureTileFunctor.release();
}
void Calculate(AbstrDataObject* resObj, const AbstrDataItem* arg1A, bool arg1HasUndefined, const AbstrUnit* arg2DomainA, const std::any& indexBox, tile_id t) const override

template <typename ResultIndexView, typename E>
static void CalcTile(ResultIndexView resData, typename sequence_traits<V>::cseq_t arg1Data, const indexed_tile_t<index_type_t<E>, V>* indexPtr, typename Unit<E>::range_t arg2DomainRange)
{
IndexApplicator indexApplicator;
indexApplicator.apply<ResultIndexView, typename sequence_traits<V>::cseq_t>(resData, std::move(arg1Data), indexPtr->second.get_view(), arg2DomainRange, indexPtr->first);

assert(resData.size() == arg1Data.size());
}

void Calculate(AbstrDataObject* resObj, const AbstrDataItem* arg1A, const AbstrUnit* arg2DomainA, const std::any& indexBox, tile_id t) const override
{
visit<typelists::domain_elements>(arg2DomainA,
[resObj, arg1Data = const_array_cast<V>(arg1A)->GetTile(t), &indexBox, arg1HasUndefined, t]<typename E>(const Unit<E>*arg2Domain)
[resObj, arg1Data = const_array_cast<V>(arg1A)->GetTile(t), &indexBox, t]<typename E>(const Unit<E>*arg2Domain)
{
auto resData = mutable_array_cast<E>(resObj)->GetWritableTile(t);

using index_type = typename cardinality_type<E>::type;

auto indexedTilePtr = std::any_cast<indexed_tile_t<index_type, V>>(&indexBox);
dms_assert(indexedTilePtr);
using index_type = index_type_t<E>;
using index_tile = indexed_tile_t<index_type, V>;
using res_seq_t = sequence_traits<E>::seq_t;

IndexApplicator indexApplicator;
indexApplicator(resData.get_view(), arg1Data.get_view(), indexedTilePtr->second.get_view(), arg2Domain->GetRange(), indexedTilePtr->first, arg1HasUndefined);
auto indexPtr = std::any_cast<index_tile>(&indexBox);
assert(indexPtr);

dms_assert(resData.size() == arg1Data.size());
CalcTile<res_seq_t, E>(resData.get_view(), arg1Data.get_view(), indexPtr, arg2Domain->GetRange());
}
);
}
Expand Down
9 changes: 9 additions & 0 deletions rtc/dll/src/geo/BitValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,13 @@ template <bit_size_t N> inline bool IsDefined(bit_value<N>)
struct dont_instantiate_this; return dont_instantiate_this();
}

template <typename T>
inline bool IsBitValueOrDefined(const T& v)
{
if constexpr (has_undefines_v<T>)
return IsDefined(v);
else
return true;
}

#endif // !defined(__RTC_GEO_BITVALUE_H)
2 changes: 1 addition & 1 deletion rtc/dll/src/geo/ElemTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ template <typename T> struct cardinality_type<Point<T>> : square_type < typename
template <typename T> struct cardinality_type<Range<T>> : cardinality_type<T> {};
template <typename T> struct cardinality_type<std::vector<T>> : cardinality_type<T> {};


template <typename T> using cardinality_type_t = typename cardinality_type<T>::type;
//----------------------------------------------------------------------
// Section : composite type predicates
//----------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions rtc/dll/src/geo/Undefined.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ template <> struct has_min_as_null< Int32> : std::true_type {};
template <> struct has_min_as_null< Int16> : std::true_type {};
template <> struct has_min_as_null< Int8 > : std::true_type {};


#define UNDEFINED_VALUE_STRING "null"
#define UNDEFINED_VALUE_STRING_LEN (sizeof(UNDEFINED_VALUE_STRING)-1)

inline CharPtr UndefinedValue(const CharPtr*) { return nullptr; }
inline std::string_view UndefinedValue(const std::string_view*) { return { UNDEFINED_VALUE_STRING, 4 }; }

template <typename T> constexpr bool has_undefines_v = !is_bitvalue_v<T>;
template <typename T> constexpr bool has_min_as_null_v = has_min_as_null<T>::value;
template <typename T> using has_undefines = std::bool_constant<has_undefines_v<T>>;

//----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion rtc/dll/src/set/DataCompare.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ template <typename T> struct DataCompareImpl<T, true> {
};

template <typename T>
struct DataCompare : DataCompareImpl<T, has_undefines<T>::value && !has_min_as_null<T>::value && has_fixed_elem_size<T>::value> {};
struct DataCompare : DataCompareImpl<T, has_undefines_v<T> && !has_min_as_null_v<T> && has_fixed_elem_size_v<T>> {};

template <typename T>
struct DataCompare<Couple<T> >
Expand Down
3 changes: 1 addition & 2 deletions shv/dll/src/FontIndexCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ granted by an additional written contract for support, assistance and/or develop
// struct : FontIndexCache
//----------------------------------------------------------------------

typedef boost::tuple<Int32, TokenID, UInt16> FontKeyType; // first == Int32Height of Characters in Pixels; second == TokenID van FontName; third == UInt16 is angle in tenths of degrees counter-clockwise from x-basis
using FontKeyType = boost::tuple<Int32, TokenID, UInt16>; // first == Int32Height of Characters in Pixels; second == TokenID van FontName; third == UInt16 is angle in tenths of degrees counter-clockwise from x-basis
template <> constexpr bool has_undefines_v<FontKeyType> = false;


struct FontIndexCache : ResourceIndexCache
{
FontIndexCache(
Expand Down
1 change: 0 additions & 1 deletion shv/dll/src/PenIndexCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ struct PenKeyType

template <> constexpr bool has_undefines_v<PenKeyType> = false;

//template <> struct has_undefines<PenKeyType> : boost::false_type {};
//----------------------------------------------------------------------
// struct : PenIndexCache
//----------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion shv/dll/src/ResourceIndexCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ granted by an additional written contract for support, assistance and/or develop
#include <vector>

#include "ShvBase.h"
#include "rlookup.h"

class Theme;
class AbstrUnit;
Expand Down Expand Up @@ -90,7 +91,7 @@ void MakeKeyIndex(std::vector<resource_index_t>& keyIndices, KeyType& keys)
if (keys.size() > 1)
{
keyIndices.resize(orgKeys.size());
rlookup2index_array_unchecked(keyIndices, orgKeys, keys);
rlookup2index_array(keyIndices, orgKeys, keys);
}
else
keyIndices.clear();
Expand Down
19 changes: 2 additions & 17 deletions shv/dll/src/ThemeValueGetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@
// MakeClassIndexArray
//----------------------------------------------------------------------

template <typename II, typename T, typename TI>
typename std::enable_if<has_undefines_v<typename std::iterator_traits<TI>::value_type>, UInt32 >::type
classify2index_best(II ib, II ie, const T& dataValue, TI classBoundsPtr)
{
return classify2index_checked(ib, ie, dataValue, classBoundsPtr);
}

template <typename II, typename T, typename TI>
typename std::enable_if<!has_undefines_v<typename std::iterator_traits<TI>::value_type>, UInt32 >::type
classify2index_best(II ib, II ie, const T& dataValue, TI classBoundsPtr)
{
return classify2index(ib, ie, dataValue, classBoundsPtr);
}

template <typename ThemeValuesType, typename IndexType = SizeT>
struct ClassifyFunc
{
Expand All @@ -54,7 +40,7 @@ struct ClassifyFunc
}
IndexType operator ()( typename param_type<ThemeValuesType>::type value) const
{
return Convert<IndexType>(classify2index_best(
return Convert<IndexType>(classify2index(
begin_ptr(m_Index)
, end_ptr (m_Index)
, value
Expand All @@ -79,14 +65,13 @@ MakeClassIndexArray(ThemeClassPairType tcp)
SharedArrayPtr<typename sequence_traits<ClassIdType>::value_type>
resultingArray(SharedArray<typename sequence_traits<ClassIdType>::value_type>::Create(themeData.size(), false) );

classify2index_range_best(
classify2index_range(
begin_ptr(resultingArray)
, end_ptr (resultingArray)
, begin_ptr(classifyFunc.m_Index)
, end_ptr (classifyFunc.m_Index)
, begin_ptr(themeData)
, begin_ptr(classifyFunc.m_ClassBreakData)
, tcp.first->HasUndefinedValues()
);

return resultingArray; // ownership is transferred to caller
Expand Down

0 comments on commit 472ef41

Please sign in to comment.