/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011-2013 by O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see .
*
******************************************************************************/
#ifndef TRIQS_ARRAY_IMPL_INDEX_STORAGE_PAIR_H
#define TRIQS_ARRAY_IMPL_INDEX_STORAGE_PAIR_H
#include "./common.hpp"
#include "./flags.hpp"
#include "../storages/shared_block.hpp"
#include "./assignment.hpp"
#include "../indexmaps/cuboid/foreach.hpp"
#include "triqs/utility/exceptions.hpp"
#include "triqs/utility/typeid_name.hpp"
#include "triqs/utility/view_tools.hpp"
#include
#include
#include
#ifdef TRIQS_WITH_PYTHON_SUPPORT
#include "../python/numpy_extractor.hpp"
#include "../python/array_view_to_python.hpp"
#endif
namespace triqs { namespace arrays {
template auto get_shape (A const & x) DECL_AND_RETURN(x.domain().lengths());
template size_t first_dim (A const & x) { return x.domain().lengths()[0];}
template size_t second_dim (A const & x) { return x.domain().lengths()[1];}
template size_t third_dim (A const & x) { return x.domain().lengths()[2];}
template size_t fourth_dim (A const & x) { return x.domain().lengths()[3];}
template size_t fifth_dim (A const & x) { return x.domain().lengths()[4];}
template size_t sixth_dim (A const & x) { return x.domain().lengths()[5];}
template size_t seventh_dim (A const & x) { return x.domain().lengths()[6];}
template class iterator_adapter;
template struct ISPViewType;
template
class indexmap_storage_pair : Tag::indexmap_storage_pair, TRIQS_CONCEPT_TAG_NAME(MutableCuboidArray) {
public :
typedef typename StorageType::value_type value_type;
static_assert(std::is_constructible::value, "array/array_view and const operate only on values");
static_assert(!std::is_const::value, "no const type");
typedef StorageType storage_type;
typedef IndexMapType indexmap_type;
static constexpr unsigned int rank = IndexMapType::domain_type::rank;
static constexpr ull_t opt_flags = OptionFlags;
static constexpr ull_t traversal_order = TraversalOrder;
protected:
indexmap_type indexmap_;
storage_type storage_;
// ------------------------------- constructors --------------------------------------------
indexmap_storage_pair() {}
indexmap_storage_pair (indexmap_type const & IM, storage_type const & ST): indexmap_(IM),storage_(ST) {deleg(IM,ST);}
indexmap_storage_pair (indexmap_type && IM, storage_type && ST) : indexmap_(std::move(IM)),storage_(std::move(ST)){deleg(IM,ST);}
indexmap_storage_pair (indexmap_type const & IM, storage_type && ST) : indexmap_(IM),storage_(std::move(ST)) {deleg(IM,ST);}
/// The storage is allocated from the size of IM.
indexmap_storage_pair (const indexmap_type & IM): indexmap_(IM),storage_(){
this->storage_ = StorageType(this->indexmap_.domain().number_of_elements(), typename flags::init_tag::type() );
}
private :
void deleg (const indexmap_type & IM, const storage_type & ST) {
//std::cerr << " construct ISP && ST "<< storage_type::is_weak<< std::endl;
#ifdef TRIQS_ARRAYS_CHECK_IM_STORAGE_COMPAT
if (ST.size() != IM.domain().number_of_elements())
TRIQS_RUNTIME_ERROR<<"index_storage_pair construction : storage and indices are not compatible";
#endif
}
public:
/// Shallow copy
indexmap_storage_pair(const indexmap_storage_pair & X):indexmap_(X.indexmap()),storage_(X.storage_){}
indexmap_storage_pair(indexmap_storage_pair && X):indexmap_(std::move(X.indexmap())),storage_(std::move(X.storage_)){}
protected:
#ifdef TRIQS_WITH_PYTHON_SUPPORT
indexmap_storage_pair (PyObject * X, bool allow_copy, const char * name ) {
//std::cout << " Enter IPS ref count = "<< X->ob_refcnt << std::endl;
try {
numpy_interface::numpy_extractor E(X, allow_copy);
this->indexmap_ = E.indexmap(); this->storage_ = E.storage();
}
catch(numpy_interface::copy_exception s){// intercept only this one...
TRIQS_RUNTIME_ERROR<< " construction of a "<< name <<" from a numpy "
<<"\n T = "<< triqs::utility::typeid_name(value_type())
// lead to a nasty link pb ???
// linker search for IndexMapType::domain_type::rank in triqs.so
// and can not resolve it ???
//<<"\n rank = "<< IndexMapType::domain_type::rank//this->rank
<<"\n OptionFlags = "<< OptionFlags
<<"\nfrom the python object \n"<< numpy_interface::object_to_string(X)
<<"\nThe error was :\n "<::type type;
};
#ifndef TRIQS_ARRAYS_SLICE_DEFAUT_IS_SHARED
template // non const version
typename boost::lazy_enable_if_c<
(!clef::is_any_lazy::value) && (indexmaps::slicer::r_type::domain_type::rank!=0) && !flags::is_const(OptionFlags)
, result_of_call_as_view
>::type // enable_if
operator()(Args const & ... args) & {
return typename result_of_call_as_view::type ( indexmaps::slicer::invoke(indexmap_,args...), storage()); }
template // const version
typename boost::lazy_enable_if_c<
(!clef::is_any_lazy::value) && (indexmaps::slicer::r_type::domain_type::rank!=0)
, result_of_call_as_view
>::type // enable_if
operator()(Args const & ... args) const & {
return typename result_of_call_as_view::type ( indexmaps::slicer::invoke(indexmap_,args...), storage()); }
template // rvalue version : same value of weak as this
typename boost::lazy_enable_if_c<
(!clef::is_any_lazy::value) && (indexmaps::slicer::r_type::domain_type::rank!=0)
, result_of_call_as_view
>::type // enable_if
operator()(Args const & ... args) && {
//std::cerr << "slicing a temporary"<< this->storage().is_weak<< result_of_call_as_view::type::storage_type::is_weak << std::endl;
return typename result_of_call_as_view::type ( indexmaps::slicer::invoke(indexmap_,args...), std::move(storage()));
}
/// Equivalent to make_view
typename ISPViewType::type
operator()() const & { return *this; }
typename ISPViewType::type
operator()() & { return *this; }
typename ISPViewType::type
operator()() && { return *this; }
// Interaction with the CLEF library : calling with any clef expression as argument build a new clef expression
// NB : this is ok because indexmap_storage_pair has a shallow copy constructor ...
// Correction : no copy, just a ref...
// so A(i_) if A is an array will NOT copy the data....
template< typename... Args>
typename clef::_result_of::make_expr_call::type
operator()( Args&&... args ) const & {
static_assert(sizeof...(Args) <= indexmap_type::rank, "Incorrect number of variable in call");// not perfect : ellipsis ...
return make_expr_call(*this,args...);
}
template< typename... Args>
typename clef::_result_of::make_expr_call::type
operator()( Args&&... args ) & {
static_assert(sizeof...(Args) <= indexmap_type::rank, "Incorrect number of variable in call");// not perfect : ellipsis ...
return make_expr_call(*this,args...);
}
template< typename... Args>
typename clef::_result_of::make_expr_call::type
operator()( Args&&... args ) && {
static_assert(sizeof...(Args) <= indexmap_type::rank, "Incorrect number of variable in call");// not perfect : ellipsis ...
return make_expr_call(std::move(*this),args...);
}
#else
template // non const version
typename boost::lazy_enable_if_c<
(!clef::is_any_lazy::value) && (indexmaps::slicer::r_type::domain_type::rank!=0)
, result_of_call_as_view
>::type // enable_if
operator()(Args const & ... args) {
return typename result_of_call_as_view::type ( indexmaps::slicer::invoke(indexmap_,args...), storage()); }
template // const version
typename boost::lazy_enable_if_c<
(!clef::is_any_lazy::value) && (indexmaps::slicer::r_type::domain_type::rank!=0)
, result_of_call_as_view
>::type // enable_if
operator()(Args const & ... args) const {
return typename result_of_call_as_view::type ( indexmaps::slicer::invoke(indexmap_,args...), storage()); }
/// Equivalent to make_view
//typename ISPViewType::type,domain_type::rank, OptionFlags, TraversalOrder, ViewTag >::type
typename ISPViewType::type
operator()() const { return *this; }
typename ISPViewType::type
operator()() { return *this; }
// Interaction with the CLEF library : calling with any clef expression as argument build a new clef expression
// NB : this is ok because indexmap_storage_pair has a shallow copy constructor ...
// Correction : no copy, just a ref...
// so A(i_) if A is an array will NOT copy the data....
template< typename... Args>
typename clef::_result_of::make_expr_call::type
operator()( Args&&... args ) const {
static_assert(sizeof...(Args) <= indexmap_type::rank, "Incorrect number of variable in call");// not perfect : ellipsis ...
return make_expr_call(*this,args...);
}
template< typename... Args>
typename clef::_result_of::make_expr_call::type
operator()( Args&&... args ) {
static_assert(sizeof...(Args) <= indexmap_type::rank, "Incorrect number of variable in call");// not perfect : ellipsis ...
return make_expr_call(*this,args...);
}
#endif
template friend void triqs_clef_auto_assign (indexmap_storage_pair & x, Fnt f) { assign_foreach(x,f);}
// ------------------------------- Iterators --------------------------------------------
typedef iterator_adapter const_iterator;
typedef iterator_adapter iterator;
const_iterator begin() const {return const_iterator(indexmap(),storage(),false);}
const_iterator end() const {return const_iterator(indexmap(),storage(),true);}
const_iterator cbegin() const {return const_iterator(indexmap(),storage(),false);}
const_iterator cend() const {return const_iterator(indexmap(),storage(),true);}
iterator begin() {return iterator(indexmap(),storage(),false);}
iterator end() {return iterator(indexmap(),storage(),true);}
protected:
// ------------------------------- resize --------------------------------------------
//
void resize (domain_type const & d) {
this->indexmap_ = IndexMapType(d);// build a new one with the lengths of IND
// optimisation. Construct a storage only if the new index is not compatible (size mismatch).
if (this->storage_.size() != this->indexmap_.domain().number_of_elements())
this->storage_ = StorageType(this->indexmap_.domain().number_of_elements(), typename flags::init_tag::type() );
}
template
void resize_and_clone_data( Xtype const & X) { indexmap_ = X.indexmap(); storage_ = X.storage().clone(); }
// BOOST Serialization
friend class boost::serialization::access;
template
void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::make_nvp("storage",this->storage_);
ar & boost::serialization::make_nvp("indexmap",this->indexmap_);
}
// pretty print of the array
friend std::ostream & operator << (std::ostream & out, const indexmap_storage_pair & A) {
if (A.storage().size()==0) out<<"empty ";
else indexmaps::pretty_print(out,A);
return out;
}
};// end class
}}//namespace triqs::arrays
#endif