24 #include "headcpp.hpp"
25 #include "utilities.hpp"
26 #include "dim_array.hpp"
28 #include "array_iterator.hpp"
35 enum Array_ordering :
bool {
46 template <
typename T>
class Array ;
47 template <
typename T> ostream& operator<< (ostream&,
const Array<T>& ) ;
48 template <
typename T> Array<T> sin(
const Array<T>& ) ;
49 template <
typename T> Array<T> cos(
const Array<T>& ) ;
50 template <
typename T> Array<T> sinh(
const Array<T>& ) ;
51 template <
typename T> Array<T> cosh(
const Array<T>& ) ;
52 template <
typename T> Array<T> operator+ (
const Array<T>&) ;
53 template <
typename T> Array<T> operator- (
const Array<T>&) ;
54 template <
typename T> Array<T> operator+ (
const Array<T>&,
const Array<T>&) ;
55 template <
typename T> Array<T> operator+ (
const Array<T>&, T) ;
56 template <
typename T> Array<T> operator+ (T,
const Array<T>&) ;
57 template <
typename T> Array<T> operator- (
const Array<T>&,
const Array<T>&) ;
58 template <
typename T> Array<T> operator- (
const Array<T>&, T) ;
59 template <
typename T> Array<T> operator- (T,
const Array<T>&) ;
60 template <
typename T> Array<T> operator* (
const Array<T>&,
const Array<T>&) ;
61 template <
typename T> Array<T> operator* (
const Array<T>&, T) ;
62 template <
typename T> Array<T> operator* (T,
const Array<T>&) ;
63 template <
typename T> Array<T> operator/ (
const Array<T>&,
const Array<T>&) ;
64 template <
typename T> Array<T> operator/ (
const Array<T>&, T) ;
65 template <
typename T> Array<T> operator/ (T,
const Array<T>&) ;
66 template <
typename T> Array<T> pow (
const Array<T>&,
int) ;
67 template <
typename T> Array<T> pow (
const Array<T>&,
double) ;
68 template <
typename T> Array<T> sqrt (
const Array<T>&) ;
69 template <
typename T> Array<T> exp (
const Array<T>&) ;
70 template <
typename T> Array<T> log (
const Array<T>&) ;
71 template <
typename T> Array<T> atanh (
const Array<T>&) ;
72 template <
typename T> Array<T> fabs (
const Array<T>&) ;
73 template <
typename T> T scal (
const Array<T>&,
const Array<T>&) ;
74 template <
typename T> T diffmax (
const Array<T>&,
const Array<T>&) ;
75 template <
typename T> T max (
const Array<T>&) ;
76 template <
typename T> T min (
const Array<T>&) ;
77 template <
typename T> T sum (
const Array<T>&) ;
78 template <
typename T> Array<T> atan (
const Array<T>&) ;
86 template <
typename T>
class Array :
public Memory_mapped {
87 static_assert(std::is_arithmetic<T>::value,
"Array<T> implementation won't be efficient if T is not a "
88 " either an integral or floating point built-in type.");
92 Memory_mapped_array<T>
data ;
108 for (
int i=0 ; i<res.
get_ndim() ; i++)
nbr *= res(i) ;
148 explicit Array (FILE* fd,Array_ordering order = last_index) ;
176 void save (FILE* fd, Array_ordering order = last_index)
const ;
191 assert ((pos(i) >=0) && (pos(i)<
dimensions(i))) ;
255 assert ((pos(i) >=0) && (pos(i)<
dimensions(i))) ;
327 CXX_17_ATTRIBUTES(nodiscard)
int get_nbr()
const {
return nbr ;} ;
344 {assert(
nbr == so.
nbr);
for(
int i{0};i<
nbr;i++)
data[i] += so.
data[i];
return *
this;}
347 {assert(
nbr == so.
nbr);
for(
int i{0};i<
nbr;i++)
data[i] -= so.
data[i];
return *
this;}
350 {assert(
nbr == so.
nbr);
for(
int i{0};i<
nbr;i++)
data[i] *= so.
data[i];
return *
this;}
353 {assert(
nbr == so.
nbr);
for(
int i{0};i<
nbr;i++)
data[i] /= so.
data[i];
return *
this;}
379 friend ostream& operator<< <> (ostream&,
const Array<T>& ) ;
445 assert(get_ndim()==1) ;
446 for (
int i = 1 ; i < nbr ; i++){
447 if (data[i]-data[i-1] <= 0){
455 int const dim {dimensions.get_ndim()};
457 Index ret{dimensions};
458 int q=i_first_dim_major;
459 while(q!=0 && k<dim) {
460 auto q_r = div(q,dimensions(k));
462 ret.set(k) = q_r.rem;
463 assert(q==0 || k<dim-1);
468 for(;k<dim-1;k++) ip = ret(k+1) + ip * dimensions(k+1);
473 int const dim {dimensions.get_ndim()};
475 Index ret{dimensions};
476 int q = i_last_dim_major;
477 while(q != 0 && k>=0) {
478 auto q_r = div(q,dimensions(k));
480 ret.set(k) = q_r.rem;
486 for(;k>0;k--) ip = ret(k-1) + dimensions(k-1)*ip;
490 template <
typename T>
Array<T>::Array (FILE* fd,Array_ordering order) : dimensions{fd},nbr{0},data{nullptr} {
491 fread_be(&
nbr,
sizeof(
int), 1, fd) ;
493 if(order == first_index) fread_be(
data.set_data(),
sizeof(T),
nbr, fd) ;
495 Memory_mapped_array<T> lim_data{
nbr};
496 fread_be(lim_data.set_data(),
sizeof(T),
nbr,fd);
502 template <
typename T>
void Array<T>::save (FILE* fd,Array_ordering order)
const {
503 dimensions.save(fd) ;
504 fwrite_be(&nbr,
sizeof(
int), 1, fd) ;
505 if(order == first_index) fwrite_be(data.get_data(),
sizeof(T), nbr, fd) ;
507 Memory_mapped_array<T> lim_data {nbr};
508 for(
int i=0;i<nbr;i++) lim_data[i] = data[to_first_dim_major_index(i)];
509 fwrite_be(lim_data.get_data(),
sizeof(T),nbr,fd);
514 template <
typename T> ostream& operator<< (ostream& o,
const Array<T>& so) {
516 o <<
"Array of " << ndim <<
" dimension(s)" << endl ;
538 o <<
" " << i << endl ;
553 for (
int i=0 ; i<so.
nbr ; i++)
554 o << so.
data[i] <<
" " ;
Version of the Index class optimized for incremental access to Array components.
int position
Corresponding value for 1D indexing .
Template class for arrays.
void swap(Array< T > &so)
Swaps contents between the two arrays (beware when using it with arrays of allocated pointers).
T * pointer
Sylvain's stuff
T & reference
Sylvain's stuff.
reference set(const Index &pos)
Read/write of an element.
T const & const_reference
Sylvain's stuff.
Array(FILE *fd, Array_ordering order=last_index)
Constructor from a file.
int get_nbr() const
Returns the total number of elements.
void delete_data()
Logical destructor (kills the data)
Array & operator=(T xx)
Assigns the same value to all the elements.
reference set(const Array_iterator &pos)
Read/write of an element.
Array(int i, int j)
Constructor for a 2d-array.
pointer set_data()
Direct accessor to the data, read/write version.
Array< T > & operator/=(const Array< T > &so)
Operator /=.
int get_ndim() const
Returns the number of dimensions.
reference set(int i)
Read/write of an element for a 1d-array.
bool is_increasing() const
Checks if a 1D array is increasing.
Array(const Dim_array &res)
Constructor from a Dim_array.
Array(int i, int j, int k)
Constructor for a 3d-array.
int get_size(int i) const
Returns the size of a given dimension.
void save(FILE *fd, Array_ordering order=last_index) const
Save in a file.
const Dim_array & get_dimensions() const
Returns the Dim_array of the Array.
void resize(Dim_array const &new_dim)
Resize the array by reallocating its ressource.
reference set(int i, int j)
Read/write of an element for a 2d-array.
Array< T > & operator*=(const Array< T > &so)
Operator *=.
T operator()(const Index &pos) const
Read only of an element.
Array(int i)
Constructor for a 1d-array.
Dim_array dimensions
Dimensions of the Array.
int nbr
Total number of elements.
int to_first_dim_major_index(int i_last_dim_major) const
Index formulae to change major dimension indexation from last dimension to first.
T const * const_pointer
Sylvain's stuff
const_pointer get_data() const
Direct accessor to the data, read only version.
Array< T > & operator-=(const Array< T > &so)
Operator -=.
T operator()(Array_iterator const &pos) const
Read only of an element.
void resize(int new_size)
Resize overload for the one-dimension array case.
int to_last_dim_major_index(int i_first_dim_major) const
Index formulae to change major dimension indexation from first dimension to the last.
Memory_mapped_array< T > data
Elements of the Array.
Array< T > & operator+=(const Array< T > &so)
Operator +.
reference set(int i, int j, int k)
Read/write of an element for a 3d-array.
Class for storing the dimensions of an array.
int get_ndim() const
Returns the number of dimensions.
int & set(int i)
Read/write of the size of a given dimension.
void swap(Dim_array &so)
Sylvain's stuff.
Class that gives the position inside a multi-dimensional Array.
Dim_array sizes
Sizes of the associated Array.
int & set(int i)
Read/write of the position in a given dimension.