2013-07-17 19:24:07 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
|
|
*
|
2014-09-07 14:34:10 +02:00
|
|
|
* Copyright (C) 2011-2014 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_MINI_VECTOR_H
|
|
|
|
#define TRIQS_ARRAYS_MINI_VECTOR_H
|
|
|
|
#include "./first_include.hpp"
|
|
|
|
#include <iostream>
|
2014-09-07 14:34:10 +02:00
|
|
|
#include "./macros.hpp"
|
|
|
|
#include "./c14.hpp"
|
2013-07-17 19:24:07 +02:00
|
|
|
#include "./compiler_details.hpp"
|
|
|
|
#include "./exceptions.hpp"
|
|
|
|
#include <boost/serialization/utility.hpp>
|
2014-05-02 16:00:12 +02:00
|
|
|
#include <boost/preprocessor/repetition/repeat.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/enum_params.hpp>
|
2013-07-17 19:24:07 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define TRIQS_MINI_VECTOR_NRANK_MAX 10
|
|
|
|
|
2014-05-02 16:00:12 +02:00
|
|
|
#define TRIQS_MAKE_NVP(NAME,X) X
|
|
|
|
//#define TRIQS_MAKE_NVP(NAME,X) boost::serialization::make_nvp(NAME,X)
|
2013-07-17 19:24:07 +02:00
|
|
|
namespace triqs { namespace utility {
|
|
|
|
|
2014-09-07 14:34:10 +02:00
|
|
|
struct no_init_tag{};
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
template <typename T, int Rank>
|
|
|
|
class mini_vector {
|
2014-09-05 13:26:21 +02:00
|
|
|
T _data[(Rank!=0 ? Rank : 1)];
|
2013-07-17 19:24:07 +02:00
|
|
|
friend class boost::serialization::access;
|
2014-05-02 16:00:12 +02:00
|
|
|
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & TRIQS_MAKE_NVP("_data",_data); }
|
2013-07-17 19:24:07 +02:00
|
|
|
void init() { for (int i=0;i<Rank; ++i) _data[i] = 0;}
|
|
|
|
public :
|
|
|
|
|
|
|
|
typedef T value_type;
|
2014-09-07 14:34:10 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
mini_vector(){init();}
|
2014-09-07 14:34:10 +02:00
|
|
|
|
|
|
|
mini_vector(no_init_tag){}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-09-07 14:34:10 +02:00
|
|
|
mini_vector(T x_0) {
|
|
|
|
static_assert(Rank == 1, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1) {
|
|
|
|
static_assert(Rank == 2, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2) {
|
|
|
|
static_assert(Rank == 3, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3) {
|
|
|
|
static_assert(Rank == 4, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3, T x_4) {
|
|
|
|
static_assert(Rank == 5, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
_data[4] = x_4;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3, T x_4, T x_5) {
|
|
|
|
static_assert(Rank == 6, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
_data[4] = x_4;
|
|
|
|
_data[5] = x_5;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3, T x_4, T x_5, T x_6) {
|
|
|
|
static_assert(Rank == 7, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
_data[4] = x_4;
|
|
|
|
_data[5] = x_5;
|
|
|
|
_data[6] = x_6;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3, T x_4, T x_5, T x_6, T x_7) {
|
|
|
|
static_assert(Rank == 8, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
_data[4] = x_4;
|
|
|
|
_data[5] = x_5;
|
|
|
|
_data[6] = x_6;
|
|
|
|
_data[7] = x_7;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3, T x_4, T x_5, T x_6, T x_7, T x_8) {
|
|
|
|
static_assert(Rank == 9, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
_data[4] = x_4;
|
|
|
|
_data[5] = x_5;
|
|
|
|
_data[6] = x_6;
|
|
|
|
_data[7] = x_7;
|
|
|
|
_data[8] = x_8;
|
|
|
|
}
|
|
|
|
mini_vector(T x_0, T x_1, T x_2, T x_3, T x_4, T x_5, T x_6, T x_7, T x_8, T x_9) {
|
|
|
|
static_assert(Rank == 10, "mini_vector : incorrect number of variables in constructor");
|
|
|
|
_data[0] = x_0;
|
|
|
|
_data[1] = x_1;
|
|
|
|
_data[2] = x_2;
|
|
|
|
_data[3] = x_3;
|
|
|
|
_data[4] = x_4;
|
|
|
|
_data[5] = x_5;
|
|
|
|
_data[6] = x_6;
|
|
|
|
_data[7] = x_7;
|
|
|
|
_data[8] = x_8;
|
|
|
|
_data[9] = x_9;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define AUX(z,p,unused) _data[p] = x_##p;
|
|
|
|
#define IMPL(z, NN, unused) \
|
|
|
|
mini_vector (BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(NN), T x_)){ \
|
|
|
|
static_assert(Rank-1==NN,"mini_vector : incorrect number of variables in constructor");\
|
|
|
|
BOOST_PP_REPEAT(BOOST_PP_INC(NN),AUX,nil) }
|
|
|
|
BOOST_PP_REPEAT(TRIQS_MINI_VECTOR_NRANK_MAX , IMPL, nil);
|
|
|
|
#undef IMPL
|
|
|
|
#undef AUX
|
|
|
|
*/
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
mini_vector(const mini_vector & x){ *this = x; }
|
|
|
|
mini_vector(mini_vector && x){ *this = std::move(x); }
|
|
|
|
|
|
|
|
template<typename T2> mini_vector(const mini_vector<T2,Rank> & x){ *this = x; }
|
|
|
|
|
|
|
|
mini_vector(const std::vector<T> & v){
|
|
|
|
if (v.size()!=Rank)
|
|
|
|
TRIQS_RUNTIME_ERROR<< "mini_vector construction : vector size incorrect : expected "<<Rank<<" got : "<< v.size();
|
|
|
|
for (int i=0;i<Rank; ++i) _data[i] = v[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
mini_vector & operator=(const mini_vector & x){ for (int i=0;i<Rank; ++i) _data[i] = x._data[i]; return *this;}
|
2013-09-28 21:33:19 +02:00
|
|
|
mini_vector & operator=(mini_vector && x) = default;
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
friend void swap(mini_vector & a, mini_vector & b) { std::swap(a._data, b._data);}
|
|
|
|
|
2014-02-13 17:28:55 +01:00
|
|
|
int size() const { return Rank;}
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
template<typename T2>
|
|
|
|
mini_vector & operator=(const mini_vector<T2,Rank> & x){ for (int i=0;i<Rank; ++i) _data[i] = x[i]; return *this;}
|
2014-09-07 14:34:10 +02:00
|
|
|
|
|
|
|
mini_vector &operator=(T x) {
|
|
|
|
for (int i = 0; i < Rank; ++i) _data[i] = x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
T & operator[](size_t i) { return _data[i];}
|
|
|
|
const T & operator[](size_t i) const { return _data[i];}
|
|
|
|
|
|
|
|
std::vector<T> to_vector () const { std::vector<T> V(Rank); for (int i=0;i<Rank; ++i) V[i] = _data[i]; return V; }
|
|
|
|
|
|
|
|
T product_of_elements () const { T res=1; for (int i=0;i<Rank; ++i) res *= _data[i]; return res; }
|
|
|
|
|
|
|
|
T * restrict ptr() { return _data;}
|
|
|
|
const T * restrict ptr() const { return _data;}
|
|
|
|
|
|
|
|
std::string to_string() const {
|
|
|
|
std::stringstream fs;
|
|
|
|
fs <<"(";
|
|
|
|
for (int i=0;i<Rank; ++i) fs<<(i==0? "" : " ") << (*this)[i];
|
|
|
|
fs<<")";
|
|
|
|
return fs.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
mini_vector<T, Rank+1> append (T const & x) const {
|
|
|
|
mini_vector<T, Rank+1> res;
|
|
|
|
for (int i=0;i<Rank; ++i) res[i]=_data[i];
|
|
|
|
res[Rank] = x;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
mini_vector<T, Rank+1> front_append (T const & x) const {
|
|
|
|
mini_vector<T, Rank+1> res;
|
|
|
|
for (int i=0;i<Rank; ++i) res[i+1]=_data[i];
|
|
|
|
res[0] = x;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
mini_vector<T, Rank-1> pop () const {
|
|
|
|
mini_vector<T, Rank-1> res;
|
|
|
|
for (int i=0;i<Rank-1; ++i) res[i]=_data[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
mini_vector<T, Rank-1> front_pop () const {
|
|
|
|
mini_vector<T, Rank-1> res;
|
|
|
|
for (int i=1;i<Rank; ++i) res[i-1]=_data[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream & operator << ( std::ostream & out, mini_vector const & v ) {return out<<v.to_string();}
|
|
|
|
friend std::stringstream & operator << ( std::stringstream & out, mini_vector const & v ) { out<<v.to_string(); return out;}
|
|
|
|
}; // class mini_vector
|
|
|
|
|
2014-09-05 13:26:21 +02:00
|
|
|
// ------------ specialization for size 0 -------------
|
|
|
|
template <typename T> class mini_vector<T, 0> {
|
|
|
|
T _data[1];
|
|
|
|
public:
|
2014-09-07 14:34:10 +02:00
|
|
|
mini_vector(){}
|
|
|
|
mini_vector(no_init_tag){}
|
|
|
|
|
2014-09-05 13:26:21 +02:00
|
|
|
T & operator[](size_t i) { return _data[i];}
|
|
|
|
const T & operator[](size_t i) const { return _data[i];}
|
2014-09-07 14:34:10 +02:00
|
|
|
T * restrict ptr() { return _data;}
|
|
|
|
const T * restrict ptr() const { return _data;}
|
|
|
|
friend std::ostream & operator << ( std::ostream & out, mini_vector const & v ) {return out<<"[]";}
|
|
|
|
};
|
2014-09-05 13:26:21 +02:00
|
|
|
|
|
|
|
// ------------- Comparison --------------------------------------
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
template <typename T, int R>
|
|
|
|
bool operator ==(mini_vector<T,R> const & v1, mini_vector<T,R> const & v2) {
|
|
|
|
for (int i=0;i<R; ++i) { if (v1[i]!=v2[i]) return false;}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, int R> bool operator !=(mini_vector<T,R> const & v1, mini_vector<T,R> const & v2) { return (!(v1==v2));}
|
|
|
|
|
2014-09-05 13:26:21 +02:00
|
|
|
// ------------- join --------------------------------------
|
|
|
|
template <typename T1, typename T2, int R1, int R2>
|
|
|
|
mini_vector<T1, R1+R2> join (mini_vector<T1,R1> const & v1, mini_vector<T2,R2> const & v2) {
|
|
|
|
mini_vector<T1, R1+R2> res;
|
2013-07-17 19:24:07 +02:00
|
|
|
for (int i=0;i<R1; ++i) res[i]=v1[i];
|
|
|
|
for (int i=0;i<R2; ++i) res[R1+i]=v2[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-09-05 13:26:21 +02:00
|
|
|
// ------------- dot --------------------------------------
|
2013-07-17 19:24:07 +02:00
|
|
|
template <typename T1, typename T2, int Rank>
|
|
|
|
T1 dot_product(mini_vector<T1,Rank> const & v1, mini_vector<T2,Rank> const & v2) {
|
|
|
|
T1 res=0;
|
|
|
|
for (int i=0;i<Rank; ++i) res += v1[i]*v2[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-09-07 14:34:10 +02:00
|
|
|
//#ifndef TRIQS_C11
|
|
|
|
// // ------------- transform a tuple into a minivector --------------------------------------
|
|
|
|
//
|
|
|
|
// template <typename T, typename TU> mini_vector<T, std::tuple_size<TU>::value> tuple_to_mini_vector(TU const &t) {
|
|
|
|
// return tuple::apply_construct_parenthesis<mini_vector<T, std::tuple_size<TU>::value>>(t);
|
|
|
|
// }
|
|
|
|
//#endif
|
2013-07-19 13:25:31 +02:00
|
|
|
|
|
|
|
}}//namespace triqs::arrays
|
2014-09-05 13:26:21 +02:00
|
|
|
|
|
|
|
namespace std { // overload std::get to work with it
|
|
|
|
|
|
|
|
template <int i, typename T, int R> AUTO_DECL get(triqs::utility::mini_vector<T, R> &v) RETURN(v[i]);
|
|
|
|
template <int i, typename T, int R> AUTO_DECL get(triqs::utility::mini_vector<T, R> const &v) RETURN(v[i]);
|
2014-09-07 14:34:10 +02:00
|
|
|
template<typename T, int R> struct tuple_size<triqs::utility::mini_vector<T, R>>: std::integral_constant<size_t,R>{};
|
|
|
|
|
2014-09-05 13:26:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
#endif
|
|
|
|
|