make multi ranges of specific sub range instances unique

This commit is contained in:
Christian Zimmermann 2018-07-16 17:40:39 +02:00
parent 41f2e60d6e
commit c6914831af
5 changed files with 96 additions and 2 deletions

View file

@ -6,12 +6,14 @@
#include <cstdlib> #include <cstdlib>
#include <tuple> #include <tuple>
#include <memory> #include <memory>
#include <map>
//#include "base_def.h" //#include "base_def.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/index_base.h" #include "ranges/index_base.h"
#include "rpack_num.h" #include "ranges/rpack_num.h"
#include "ranges/multi_range_factory_product_map.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -118,6 +120,7 @@ namespace MultiArrayTools
* MultiRangeFactory * * MultiRangeFactory *
*************************/ *************************/
// NOT THREAD SAVE
template <class... Ranges> template <class... Ranges>
class MultiRangeFactory : public RangeFactoryBase class MultiRangeFactory : public RangeFactoryBase
{ {
@ -132,6 +135,11 @@ namespace MultiArrayTools
MultiRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr); MultiRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr);
virtual std::shared_ptr<RangeBase> create() override; virtual std::shared_ptr<RangeBase> create() override;
private:
std::shared_ptr<RangeBase> checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& ptp);
}; };
/****************** /******************
@ -449,9 +457,33 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
std::shared_ptr<RangeBase> MultiRangeFactory<Ranges...>::create() std::shared_ptr<RangeBase> MultiRangeFactory<Ranges...>::create()
{ {
mProd = checkIfCreated( std::dynamic_pointer_cast<oType>( mProd )->mSpace );
setSelf(); setSelf();
return mProd; return mProd;
} }
template <class... Ranges>
std::shared_ptr<RangeBase> MultiRangeFactory<Ranges...>::checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& ptp)
{
std::shared_ptr<RangeBase> out;
bool check = false;
for(auto& x: MultiRangeFactoryProductMap::mAleadyCreated){
if(x.second.size() == sizeof...(Ranges)){
check = RPackNum<sizeof...(Ranges)-1>::checkIfCreated(ptp, x.second);
if(check){
out = x.first;
break;
}
}
}
if(not check){
std::vector<std::intptr_t> pv(sizeof...(Ranges));
RPackNum<sizeof...(Ranges)-1>::RangesToVec(ptp, pv);
MultiRangeFactoryProductMap::mAleadyCreated[mProd] = pv;
out = mProd;
}
return out;
}
/****************** /******************
* MultiRange * * MultiRange *

View file

@ -0,0 +1,25 @@
#ifndef __multi_range_factory_product_map_h__
#define __multi_range_factory_product_map_h__
#include <memory>
#include <vector>
#include <map>
#include "ranges/rbase_def.h"
namespace MultiArrayTools
{
class MultiRangeFactoryProductMap
{
public:
template <class... Ranges>
friend class MultiRangeFactory;
private:
static std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > mAleadyCreated;
};
}
#endif

View file

@ -227,7 +227,14 @@ namespace MultiArrayHelper
std::vector<std::shared_ptr<RangeBase> >& v) std::vector<std::shared_ptr<RangeBase> >& v)
{ {
setRangeToVec(v, std::get<N>(rst)); setRangeToVec(v, std::get<N>(rst));
//v[N] = std::get<N>(rst); RPackNum<N-1>::RangesToVec(rst, v);
}
template <class... Ranges>
static inline void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<std::intptr_t>& v)
{
v[N] = reinterpret_cast<std::intptr_t>( std::get<N>(rst).get() );
RPackNum<N-1>::RangesToVec(rst, v); RPackNum<N-1>::RangesToVec(rst, v);
} }
@ -285,6 +292,14 @@ namespace MultiArrayHelper
resolveSetRange(std::get<tps-N-1>(rtp), orig, off, size); resolveSetRange(std::get<tps-N-1>(rtp), orig, off, size);
RPackNum<N-1>::resolveRangeType(orig, rtp, off+size, sizes...); RPackNum<N-1>::resolveRangeType(orig, rtp, off+size, sizes...);
} }
template <class... Ranges>
static inline bool checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& p,
const std::vector<std::intptr_t>& a)
{
return reinterpret_cast<std::intptr_t>( std::get<N>(p).get() ) == a[N] and
RPackNum<N-1>::checkIfCreated(p,a);
}
}; };
@ -416,6 +431,13 @@ namespace MultiArrayHelper
return 1; return 1;
} }
template <class... Ranges>
static inline void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<std::intptr_t>& v)
{
v[0] = reinterpret_cast<std::intptr_t>( std::get<0>(rst).get() );;
}
template <class... Ranges> template <class... Ranges>
static inline void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst, static inline void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<std::shared_ptr<RangeBase> >& v) std::vector<std::shared_ptr<RangeBase> >& v)
@ -472,6 +494,13 @@ namespace MultiArrayHelper
resolveSetRange(std::get<tps-1>(rtp), orig, off, size); resolveSetRange(std::get<tps-1>(rtp), orig, off, size);
} }
template <class... Ranges>
static inline bool checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& p,
const std::vector<std::intptr_t>& a)
{
return reinterpret_cast<std::intptr_t>( std::get<0>(p).get() ) == a[0];
}
}; };
template <IndexType IT> template <IndexType IT>

View file

@ -2,6 +2,7 @@
set(libmultiarray_a_SOURCES set(libmultiarray_a_SOURCES
${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/anonymous_range.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/anonymous_range.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/multi_range_factory_product_map.cc
) )
file(GLOB cc_files "${CMAKE_SOURCE_DIR}/src/lib/ranges/range_types/*.cc") file(GLOB cc_files "${CMAKE_SOURCE_DIR}/src/lib/ranges/range_types/*.cc")

View file

@ -0,0 +1,7 @@
#include "ranges/multi_range_factory_product_map.h"
namespace MultiArrayTools
{
std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > MultiRangeFactoryProductMap::mAleadyCreated;
}