2013-07-17 19:24:07 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
|
|
*
|
2013-10-22 14:11:07 +02:00
|
|
|
* Copyright (C) 2011-2013 by O. Parcollet
|
2013-07-17 19:24:07 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#ifndef TRIQS_ARRAYS_ARRAY_H
|
|
|
|
#define TRIQS_ARRAYS_ARRAY_H
|
2013-08-21 16:17:51 +02:00
|
|
|
#include <triqs/utility/first_include.hpp>
|
2013-07-17 19:24:07 +02:00
|
|
|
#include "indexmaps/cuboid/map.hpp"
|
|
|
|
#include "indexmaps/cuboid/slice.hpp"
|
|
|
|
#include "impl/indexmap_storage_pair.hpp"
|
|
|
|
#include "impl/assignment.hpp"
|
|
|
|
#include "impl/flags.hpp"
|
|
|
|
namespace triqs { namespace arrays {
|
|
|
|
|
2013-10-06 22:10:42 +02:00
|
|
|
template <typename ValueType, int Rank, ull_t Opt=0, ull_t TraversalOrder= 0, bool Borrowed=false, bool IsConst=false > class array_view;
|
2013-07-17 19:24:07 +02:00
|
|
|
template <typename ValueType, int Rank, ull_t Opt=0, ull_t TraversalOrder= 0> class array;
|
|
|
|
|
|
|
|
// ---------------------- array_view --------------------------------
|
|
|
|
|
|
|
|
#define IMPL_TYPE indexmap_storage_pair< indexmaps::cuboid::map<Rank,Opt,TraversalOrder>, \
|
2014-03-02 14:14:59 +01:00
|
|
|
storages::shared_block<ValueType, Borrowed>, Opt, TraversalOrder, IsConst, true, Tag::array_view >
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-10-06 22:10:42 +02:00
|
|
|
template <typename ValueType, int Rank, ull_t Opt, ull_t TraversalOrder, bool Borrowed, bool IsConst>
|
|
|
|
class array_view: Tag::array_view, TRIQS_CONCEPT_TAG_NAME(MutableArray), public IMPL_TYPE {
|
2013-07-17 19:24:07 +02:00
|
|
|
static_assert( Rank>0, " Rank must be >0");
|
|
|
|
public:
|
|
|
|
typedef typename IMPL_TYPE::indexmap_type indexmap_type;
|
|
|
|
typedef typename IMPL_TYPE::storage_type storage_type;
|
2013-08-20 16:15:43 +02:00
|
|
|
typedef array <ValueType,Rank,Opt,TraversalOrder> regular_type;
|
2013-06-25 22:29:14 +02:00
|
|
|
typedef array_view<ValueType,Rank,Opt,TraversalOrder> view_type;
|
2013-10-06 22:10:42 +02:00
|
|
|
typedef array_view<ValueType, Rank, Opt, TraversalOrder, false, true> const_view_type;
|
2013-06-25 22:29:14 +02:00
|
|
|
typedef array_view<ValueType,Rank,Opt,TraversalOrder,true> weak_view_type;
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-10-06 22:10:42 +02:00
|
|
|
/// Build from an IndexMap and a storage
|
|
|
|
template <typename S> array_view(indexmap_type const& Ind, S const& Mem) : IMPL_TYPE(Ind, Mem) {}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/// Copy constructor
|
|
|
|
array_view(array_view const & X): IMPL_TYPE(X.indexmap(),X.storage()) {}
|
|
|
|
|
|
|
|
/// Build from anything that has an indexmap and a storage compatible with this class
|
2013-10-06 22:10:42 +02:00
|
|
|
template<typename ISP> array_view(const ISP & X): IMPL_TYPE(X.indexmap(),X.storage()) {
|
|
|
|
// to be activated
|
|
|
|
static_assert(IsConst || (!ISP::is_const), "Can not construct a non const view from a const one !");
|
|
|
|
}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
#ifdef TRIQS_WITH_PYTHON_SUPPORT
|
|
|
|
/// Build from a numpy.array (X is a borrowed reference) : throws if X is not a numpy.array
|
|
|
|
explicit array_view (PyObject * X): IMPL_TYPE(X, false, "array_view "){}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
array_view () = delete;
|
|
|
|
|
|
|
|
// Move
|
|
|
|
array_view(array_view && X) { this->swap_me(X); }
|
|
|
|
|
|
|
|
/// Swap
|
|
|
|
friend void swap( array_view & A, array_view & B) { A.swap_me(B);}
|
|
|
|
|
|
|
|
/// Rebind the view
|
2013-10-22 14:11:07 +02:00
|
|
|
void rebind(array_view const& X) {
|
|
|
|
this->indexmap_ = X.indexmap_;
|
|
|
|
this->storage_ = X.storage_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rebind the other view, iif this is const, and the other is not.
|
|
|
|
template <bool C = IsConst> ENABLE_IFC(C) rebind(array_view<ValueType, Rank, Opt, TraversalOrder, Borrowed, !IsConst> const& X) {
|
|
|
|
this->indexmap_ = X.indexmap_;
|
|
|
|
this->storage_ = X.storage_;
|
|
|
|
}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/// Assignment. The size of the array MUST match exactly, except in the empty case
|
|
|
|
template<typename RHS> array_view & operator=(RHS const & X) { triqs_arrays_assign_delegation(*this,X); return *this; }
|
|
|
|
|
|
|
|
///
|
|
|
|
array_view & operator=(array_view const & X) { triqs_arrays_assign_delegation(*this,X); return *this; } //without this, the standard = is synthetized...
|
|
|
|
|
|
|
|
// Move assignment not defined : will use the copy = since view must copy data
|
|
|
|
|
|
|
|
TRIQS_DEFINE_COMPOUND_OPERATORS(array_view);
|
2013-06-25 22:29:14 +02:00
|
|
|
// to forbid serialization of views...
|
|
|
|
//template<class Archive> void serialize(Archive & ar, const unsigned int version) = delete;
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
};
|
|
|
|
|
2013-06-25 22:29:14 +02:00
|
|
|
#undef IMPL_TYPE
|
2013-08-21 17:25:45 +02:00
|
|
|
|
|
|
|
template <typename ValueType, int Rank, ull_t Opt=0, ull_t TraversalOrder=0, bool Borrowed=false>
|
2013-10-06 22:10:42 +02:00
|
|
|
using array_const_view = array_view<ValueType, Rank, Opt, TraversalOrder, Borrowed, true>;
|
2013-08-21 17:25:45 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
//------------------------------- array ---------------------------------------------------
|
|
|
|
|
2013-06-25 22:29:14 +02:00
|
|
|
#define IMPL_TYPE indexmap_storage_pair< indexmaps::cuboid::map<Rank,Opt,TraversalOrder>, \
|
2014-03-02 14:14:59 +01:00
|
|
|
storages::shared_block<ValueType>, Opt, TraversalOrder, false, false, Tag::array_view >
|
2013-06-25 22:29:14 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
template <typename ValueType, int Rank, ull_t Opt, ull_t TraversalOrder>
|
2013-08-29 18:02:19 +02:00
|
|
|
class array: Tag::array, TRIQS_CONCEPT_TAG_NAME(MutableArray), public IMPL_TYPE {
|
2013-07-17 19:24:07 +02:00
|
|
|
public:
|
|
|
|
typedef typename IMPL_TYPE::value_type value_type;
|
|
|
|
typedef typename IMPL_TYPE::storage_type storage_type;
|
|
|
|
typedef typename IMPL_TYPE::indexmap_type indexmap_type;
|
2013-10-06 22:10:42 +02:00
|
|
|
typedef array<ValueType, Rank, Opt, TraversalOrder> regular_type;
|
|
|
|
typedef array_view<ValueType, Rank, Opt, TraversalOrder> view_type;
|
|
|
|
typedef array_view<ValueType, Rank, Opt, TraversalOrder, false, true> const_view_type;
|
|
|
|
typedef array_view<ValueType, Rank, Opt, TraversalOrder, true> weak_view_type;
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/// Empty array.
|
|
|
|
explicit array(memory_layout<Rank> ml = memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order)) :IMPL_TYPE(ml){}
|
|
|
|
|
|
|
|
/// From a domain
|
|
|
|
explicit array( typename indexmap_type::domain_type const & dom, memory_layout<Rank> ml = memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order)):
|
|
|
|
IMPL_TYPE(indexmap_type(dom,ml)){}
|
|
|
|
|
|
|
|
#ifdef TRIQS_DOXYGEN
|
|
|
|
/// Construction from the dimensions. NB : the number of parameters must be exactly rank (checked at compile time).
|
|
|
|
array (size_t I_1, .... , size_t I_rank);
|
|
|
|
#else
|
|
|
|
#define IMPL(z, NN, unused) \
|
|
|
|
explicit array (BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(NN), size_t I_),memory_layout<Rank> ml = memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order)): \
|
|
|
|
IMPL_TYPE(indexmap_type(mini_vector<size_t,BOOST_PP_INC(NN)>(BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(NN), I_)),ml)) {\
|
|
|
|
static_assert(IMPL_TYPE::rank-1==NN,"array : incorrect number of variables in constructor");}
|
|
|
|
BOOST_PP_REPEAT(ARRAY_NRANK_MAX , IMPL, nil)
|
|
|
|
#undef IMPL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Makes a true (deep) copy of the data.
|
|
|
|
array(const array & X): IMPL_TYPE(X.indexmap(),X.storage().clone()) {}
|
|
|
|
|
|
|
|
// Move
|
|
|
|
explicit array(array && X) { this->swap_me(X); }
|
|
|
|
|
2013-09-28 22:53:00 +02:00
|
|
|
// from a temporary storage and an indexmap. Used for reshaping a temporary array
|
|
|
|
explicit array(typename indexmap_type::domain_type const & dom, storage_type && sto,
|
|
|
|
memory_layout<Rank> ml = memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order)):
|
|
|
|
IMPL_TYPE(indexmap_type(dom,ml), std::move(sto)){}
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
/**
|
|
|
|
* Build a new array from X.domain() and fill it with by evaluating X. X can be :
|
|
|
|
* - another type of array, array_view, matrix,.... (any <IndexMap, Storage> pair)
|
|
|
|
* - a expression : e.g. array<int> A = B+ 2*C;
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2013-08-29 18:02:19 +02:00
|
|
|
array(const T & X, TYPE_ENABLE_IF(memory_layout<Rank>, ImmutableCuboidArray<T>) ml = memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order)):
|
2013-07-17 19:24:07 +02:00
|
|
|
IMPL_TYPE(indexmap_type(X.domain(),ml)) { triqs_arrays_assign_delegation(*this,X); }
|
|
|
|
|
|
|
|
#ifdef TRIQS_WITH_PYTHON_SUPPORT
|
|
|
|
///Build from a numpy.array X (or any object from which numpy can make a numpy.array). Makes a copy.
|
|
|
|
explicit array (PyObject * X): IMPL_TYPE(X, true, "array "){}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// build from a init_list
|
|
|
|
template<typename T, int R=Rank>
|
|
|
|
array(std::initializer_list<T> const & l, typename std::enable_if<R==1>::type * dummy =0):
|
|
|
|
IMPL_TYPE(indexmap_type(mini_vector<size_t,1>(l.size()),memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order))) {
|
|
|
|
size_t i=0;
|
|
|
|
for (auto const & x : l) (*this)(i++) = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, int R=Rank>
|
|
|
|
array (std::initializer_list<std::initializer_list<T>> const & l, typename std::enable_if<R==2>::type * dummy =0):
|
|
|
|
IMPL_TYPE(memory_layout<Rank>(IMPL_TYPE::indexmap_type::traversal_order)) {
|
|
|
|
size_t i=0,j=0; int s=-1;
|
|
|
|
for (auto const & l1 : l) { if (s==-1) s= l1.size(); else if (s != l1.size()) TRIQS_RUNTIME_ERROR << "initializer list not rectangular !";}
|
|
|
|
IMPL_TYPE::resize(typename IMPL_TYPE::domain_type (mini_vector<size_t,2>(l.size(),s)));
|
|
|
|
for (auto const & l1 : l) {
|
|
|
|
for (auto const & x : l1) { (*this)(i,j++) = x;}
|
|
|
|
j=0; ++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the array. NB : all references to the storage is invalidated.
|
|
|
|
* Does not initialize the array by default: to resize and init, do resize(IND).init()
|
|
|
|
*/
|
|
|
|
array & resize (const indexmaps::cuboid::domain_t<IMPL_TYPE::rank> & l) { IMPL_TYPE::resize(l); return *this; }
|
|
|
|
|
|
|
|
/// Assignement resizes the array. All references to the storage are therefore invalidated.
|
|
|
|
array & operator=(const array & X) { IMPL_TYPE::resize_and_clone_data(X); return *this; }
|
|
|
|
|
|
|
|
/// Move assignment
|
|
|
|
array & operator=(array && X) { this->swap_me(X); return *this;}
|
|
|
|
|
|
|
|
/// Swap
|
|
|
|
friend void swap( array & A, array & B) { A.swap_me(B);}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignement resizes the array (if necessary).
|
|
|
|
* All references to the storage are therefore invalidated.
|
|
|
|
* NB : to avoid that, do make_view(A) = X instead of A = X
|
|
|
|
*/
|
|
|
|
template<typename RHS>
|
|
|
|
array & operator=(const RHS & X) {
|
2013-08-29 18:02:19 +02:00
|
|
|
static_assert(ImmutableCuboidArray<RHS>::value, "Assignment : RHS not supported");
|
2013-07-17 19:24:07 +02:00
|
|
|
IMPL_TYPE::resize(X.domain());
|
|
|
|
triqs_arrays_assign_delegation(*this,X);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRIQS_DEFINE_COMPOUND_OPERATORS(array);
|
|
|
|
|
|
|
|
};//array class
|
|
|
|
|
|
|
|
#undef IMPL_TYPE
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// how to build the view type ....
|
2013-10-06 22:10:42 +02:00
|
|
|
template < class V, int R, ull_t Opt, ull_t TraversalOrder, bool Borrowed, bool IsConst >
|
|
|
|
struct ISPViewType< V, R, Opt, TraversalOrder,Tag::array_view, Borrowed,IsConst> { typedef array_view<V,R,Opt,TraversalOrder, Borrowed,IsConst> type; };
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
}}//namespace triqs::arrays
|
|
|
|
|
|
|
|
// The std::swap is WRONG for a view because of the copy/move semantics of view.
|
|
|
|
// Use swap instead (the correct one, found by ADL).
|
|
|
|
namespace std {
|
2013-10-06 22:10:42 +02:00
|
|
|
template <typename V, int R, triqs::ull_t Opt, triqs::ull_t To, bool B1, bool B2, bool C1, bool C2>
|
|
|
|
void swap( triqs::arrays::array_view<V,R,Opt,To,B1,C1> & a , triqs::arrays::array_view<V,R,Opt,To,B2,C2> & b)= delete;
|
2013-07-17 19:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "./expression_template/array_algebra.hpp"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|