C++ mpi module for stochmagnet_main Package
functions_grid.h
1 #ifndef FUNCTIONS_GRID_H
2 #define FUNCTIONS_GRID_H
3 
4 //types of the code
5 #include "types.h"
6 #include "functions_type.h"
7 
8 //standart library
9 #include <stdlib.h>
10 
11 //for type name
12 #include <cxxabi.h>
13 
14 //include the Template condition
15 #include<type_traits>
16 
17 //regex header for string manipulation
18 #include <regex>
19 
20 //template type verification header
21 #include<type_traits>
22 #include<concepts>
23 
24 //math header isnan
25 #include <math.h>
26 
27 #ifdef DEBUG
28 //define the assert method
29 #include<cassert>
30 #endif
31 
32 namespace functions_grid {
33 
34 
35  //index of element form indices
36  //==============================
37 
38  //level index function I1=a_0+d_. I_2
39  // ...
40  // In=a_{n-1}
41 
48  template<typename S,typename T,typename Q,size_t N,size_t L> requires (L==N)
49  inline S getLevelElementIndex(const T* Dl,const Q *Al) {
50  return (S) (*Al);
51  }
58  template<typename S,typename T,typename Q,size_t N,size_t L> requires (L<N)
59  inline S getLevelElementIndex(const T* Dl,const Q *Al) {
60  return (S) ((*Al)+(*Dl)*getLevelElementIndex<S,T,Q,N,L+1>(Dl+1,Al+1));
61  }
68  template<typename S,typename T,typename Q,size_t N>
69  inline S getElementIndex(const std::array<T,N>& D,const std::array<Q,N>& I) {
70  return getLevelElementIndex<S,T,Q,N,1>(D.data(),I.data());
71  }
78  template<typename S,typename T,size_t N>
79  inline S getElementIndex(const T* D,const T* I) {
80  return getLevelElementIndex<S,T,T,N,1>(D,I);
81  }
88  template<typename S,typename T,typename Q,size_t N,size_t M>
89  inline S getElementIndex(const std::array<T,N>& D,const std::array<Q,M>& I) {
90  return getLevelElementIndex<S,T,Q,M,1>(D.data(),I.data());
91  }
92 
93 
94  //indices of element from it index
95  //=================================
96 
103  template<typename T,typename S,size_t N,size_t i> requires (i==N)
104  inline void getLevelElementIndices(const T* Di,S& index,T *Ai) {
105  (*Ai)=index;
106  }
113  template<typename T,typename S,size_t N,size_t i> requires (i<N)
114  inline void getLevelElementIndices(const T* Di,S& index,T *Ai) {
115  (*Ai)=index%(*Di);
116  index/=(*Di);
117  getLevelElementIndices<T,S,N,i+1>(Di+1,index,Ai+1);
118  }
119 
127  template<typename T,typename S,size_t N> requires functions_type::isArithmeticType<T>
128  inline void getElementIndices(const std::array<T,N>& D,S& index,std::array<T,N>& I) {
129  return getLevelElementIndices<T,S,N,1>(D.data(),index,I.data());
130  }
138  template<typename T,typename S,size_t N,size_t M> requires functions_type::isArithmeticType<T>
139  inline void getElementIndices(const std::array<T,N>& D,S& index,std::array<T,M>& I) {
140  return getLevelElementIndices<T,S,M,1>(D.data(),index,I.data());
141  }
149  template<typename T,typename S,size_t N> requires functions_type::isArithmeticType<T>
150  inline void getElementIndices(const T* D,S& index,T* I) {
151  return getLevelElementIndices<T,S,N,1>(D,index,I);
152  }
153 
154  //next element indices method
155  //============================
156 
162  template<typename T,size_t N,size_t L> requires (L==N)
163  inline void nextLevelElementIndices(const T* Nl,T* Il) {
164  (*Il)++;
165  }
171  template<typename T,size_t N,size_t L> requires (L<N)
172  inline void nextLevelElementIndices(const T* Nl,T* Il) {
173  (*Il)++;
174  if ((*Il)==(*Nl)) {
175  (*Il)=0;
176  nextLevelElementIndices<T,N,L+1>(Nl+1,Il+1);
177  }
178  }
179 
180 
181 
187  template<typename T,size_t N> requires (functions_type::isArithmeticType<T>)
188  inline void nextElementIndices(const std::array<T,N>& size,std::array<T,N>& indices) {
189  return nextLevelElementIndices<T,N,1>(size.data(),indices.data());
190  }
196  template<typename T,size_t N> requires (functions_type::isArithmeticType<T>)
197  inline void nextElementIndices(const T* size,T* indices) {
198  return nextLevelElementIndices<T,N,1>(size,indices);
199  }
200 
201  //has next element indices method
202  //============================
203 
209  template<typename T,size_t N,size_t L> requires (L==N)
210  inline tBoolean hasNextLevelElement(const T* Nl,T* Il) {
211  (*Il)++;
212  return ((*Il)<(*Nl));
213  }
219  template<typename T,size_t N,size_t L> requires (L<N)
220  inline tBoolean hasNextLevelElement(const T* Nl,T* Il) {
221  (*Il)++;
222  if ((*Il)==(*Nl)) {
223  (*Il)=0;
224  return hasNextLevelElement<T,N,L+1>(Nl+1,Il+1);
225  }
226  return true;
227  }
228 
229 
230 
236  template<typename T,size_t N> requires (functions_type::isArithmeticType<T>)
237  inline tBoolean hasNextElement(const std::array<T,N>& size,std::array<T,N>& indices) {
238  return hasNextLevelElement<T,N,1>(size.data(),indices.data());
239  }
240 
241 
242 
243 
244  //next element indices within a block
245  //===================================
246 
255  template<typename T,size_t N,size_t L> requires (L==N)
256  inline tBoolean hasNextLevelElementIndicesWithinBlock(const T* Nl,const T* Sl,const T* El,T* Il) {
257  (*Il)++;
258  return ((*Il)<(*El));
259  }
260 
269  template<typename T,size_t N,size_t L> requires (L<N)
270  inline tBoolean hasNextLevelElementIndicesWithinBlock(const T* Nl,const T* Sl,const T* El,T* Il) {
271  (*Il)++;
272  if ((*Il)==(*El)) {
273  (*Il)=(*Sl);
274  return hasNextLevelElementIndicesWithinBlock<T,N,L+1>(Nl+1,Sl+1,El+1,Il+1);
275  }
276  return true;
277  }
278 
287  template<typename T,size_t N> requires (functions_type::isArithmeticType<T>)
288  inline tBoolean hasNextElementIndicesWithinBlock(const std::array<T,N>& size,
289  const std::array<T,N>& start,
290  const std::array<T,N>& end,
291  std::array<T,N>& indices) {
292  return hasNextLevelElementIndicesWithinBlock<T,N,1>(size.data(),start.data(),end.data(),indices.data());
293  }
294 
295 
301  template<typename T,size_t N,size_t i> requires (i==N)
302  inline tBoolean isLevelIndicesInBound(const T* Di,const T *Bi) {
303  return (*Di)<(*Bi);
304  }
310  template<typename T,size_t N,size_t i> requires (i<N)
311  inline tBoolean isLevelIndicesInBound(const T* Di,const T *Bi) {
312  return ((*Di)<(*Bi)) && isLevelIndicesInBound<T,N,i+1>(Di+1,Bi+1);
313  }
314 
320  template<typename T,size_t N> requires functions_type::isArithmeticType<T>
321  inline tBoolean isIndicesInBound(const T* I,const T* Ns) {
322  return isLevelIndicesInBound<T,N,1>(I,Ns);
323  }
324 
330  template<typename T,size_t N> requires functions_type::isArithmeticType<T>
331  inline tBoolean isIndicesInBound(const std::array<T,N>& I,const std::array<T,N>& Ns) {
332  return isLevelIndicesInBound<T,N,1>(I.data(),Ns.data());
333  }
334 
335 }
336 
337 #endif
338 
339