OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
NNc_array.hpp
1 /*
2  * NNc_array.hpp
3  *
4  * Created on: Feb 6, 2018
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_NN_CELLLIST_NNC_ARRAY_HPP_
9 #define OPENFPM_DATA_SRC_NN_CELLLIST_NNC_ARRAY_HPP_
10 
11 #include "Grid/grid_sm.hpp"
12 #include <boost/mpl/bool.hpp>
13 
19 template<unsigned int dim>
21 {
23  typedef boost::mpl::bool_< dim < 10 > type;
24 };
25 
26 /* \brief NNc_array
27  *
28  * \param size
29  *
30  */
31 template<unsigned int dim, unsigned int size, bool thr = as_array_nnc<dim>::type::value>
32 class NNc_array
33 {
35  long int NNc_arr[size];
36 
39 
40 public:
41 
47  void set_size(const size_t (& sz)[dim])
48  {
49  gs.setDimensions(sz);
50  }
51 
57  inline const long int & operator[](size_t i) const
58  {
59  return NNc_arr[i];
60  }
61 
67  inline long int & operator[](size_t i)
68  {
69  return NNc_arr[i];
70  }
71 
76  void init_full()
77  {
78  typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
79  typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
80  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
81 
82  // Generate the sub-grid iterator
83 
84  grid_key_dx_iterator_sub<dim> gr_sub3(gs,NNzero::data,NNtwo::data);
85 
86  // Calculate the NNc array
87 
88  size_t middle = gs.LinId(NNone::data);
89  size_t i = 0;
90  while (gr_sub3.isNext())
91  {
92  NNc_arr[i] = (long int)gs.LinId(gr_sub3.get()) - middle;
93 
94  ++gr_sub3;
95  i++;
96  }
97  }
98 
99 
104  void init_sym()
105  {
106  // compile-time array {0,0,0,....} {2,2,2,...} {1,1,1,...}
107 
108  typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
109  typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
110  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
111 
112  size_t middle = gs.LinId(NNone::data);
113 
114  // Generate the sub-grid iterator
115 
116  grid_key_dx_iterator_sub<dim> gr_sub3(gs,NNzero::data,NNtwo::data);
117 
118  // Calculate the NNc_sym array
119 
120  size_t i = 0;
121  while (gr_sub3.isNext())
122  {
123  auto key = gr_sub3.get();
124 
125  size_t lin = gs.LinId(key);
126 
127  // Only the first half is considered
128  if (lin < middle)
129  {
130  ++gr_sub3;
131  continue;
132  }
133 
134  NNc_arr[i] = lin - middle;
135 
136  ++gr_sub3;
137  i++;
138  }
139  }
140 
146  const long int * getPointer() const
147  {
148  return NNc_arr;
149  }
150 
157  {
158  std::copy(&nnc.NNc_arr[0],&nnc.NNc_arr[size],&NNc_arr[0]);
159  gs = nnc.gs;
160 
161  return *this;
162  }
163 
170  {
171  gs.swap(nnc.gs);
172 
173  long int NNc_full_tmp[openfpm::math::pow(3,dim)];
174 
175  std::copy(&nnc.NNc_arr[0],&nnc.NNc_arr[size],&NNc_full_tmp[0]);
176  std::copy(&NNc_arr[0],&NNc_arr[size],&nnc.NNc_arr[0]);
177  std::copy(&NNc_full_tmp[0],&NNc_full_tmp[size],&NNc_arr[0]);
178  }
179 };
180 
181 /* \brief NNc_array
182  *
183  * It encapsulate a 3^{dim} array containing the neighborhood cells-id
184  *
185  * \tparam dim dimensionality
186  * \tparam size total number of neighborhood cells
187  *
188  */
189 template<unsigned int dim, unsigned int size>
190 class NNc_array<dim,size,false>
191 {
195 
198 
200  size_t sub_off;
201  size_t sym_mid;
202 
203  bool full_or_sym;
204 
205 public:
206 
212  void set_size(const size_t (& sz)[dim])
213  {
214  typedef typename generate_array<size_t,dim, Fill_three>::result NNthree;
215 
216  gs.setDimensions(sz);
217  gs_base.setDimensions(NNthree::data);
218 
219  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
220  sub_off = gs.LinId(NNone::data);
221  sym_mid = gs_base.LinId(NNone::data);
222  }
223 
229  long int operator[](size_t i) const
230  {
231  if (full_or_sym == true)
232  {
233  grid_key_dx<dim> key = gs_base.InvLinId(i);
234  return gs.LinId(key) - sub_off;
235  }
236 
237  grid_key_dx<dim> key = gs_base.InvLinId(i + sym_mid);
238  return gs.LinId(key) - sub_off;
239  }
240 
241  void init_full()
242  {
243  full_or_sym = true;
244  }
245 
246  void init_sym()
247  {
248  full_or_sym = false;
249  }
250 
256  const long int * getPointer() const
257  {
258  std::cerr << __FILE__ << ":" << __LINE__ << " error dimension is too high to use this type of neighborhood" << std::endl;
259  return NULL;
260  }
261 
268  {
269  gs = nnc.gs;
270  gs_base = nnc.gs_base;
271  sub_off = nnc.sub_off;
272  sym_mid = nnc.sym_mid;
273 
274  bool full_or_sym;
275  }
276 
283  {
284  gs.swap(nnc.gs);
285  gs_base.swap(nnc.gs_base);
286 
287  size_t sub_off_tmp = sub_off;
288  sub_off = nnc.sub_off;
289  nnc.sub_off = sub_off_tmp;
290 
291  size_t sym_mid_tmp = sym_mid;
292  sym_mid = nnc.sym_mid;
293  nnc.sym_mid = sym_mid_tmp;
294 
295  bool full_or_sym_tmp = full_or_sym;
296  full_or_sym = nnc.full_or_sym;
297  nnc.full_or_sym = full_or_sym_tmp;
298  }
299 };
300 
301 
302 #endif /* OPENFPM_DATA_SRC_NN_CELLLIST_NNC_ARRAY_HPP_ */
grid_sm< dim, void > gs
size of the cell array on each dimension
Definition: NNc_array.hpp:38
grid_sm< dim, void > gs
Definition: NNc_array.hpp:194
mem_id LinId(const grid_key_dx< N > &gk, const char sum_id[N]) const
Linearization of the grid_key_dx with a specified shift.
Definition: grid_sm.hpp:337
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
const long int * getPointer() const
return the pointer to the array
Definition: NNc_array.hpp:146
long int NNc_arr[size]
NNc_array.
Definition: NNc_array.hpp:35
long int operator[](size_t i) const
return the element i
Definition: NNc_array.hpp:229
const long int * getPointer() const
return the pointer to the array
Definition: NNc_array.hpp:256
void swap(NNc_array< dim, size, false > &nnc)
swap the NNc_array
Definition: NNc_array.hpp:282
void set_size(const size_t(&sz)[dim])
Set the size in each.
Definition: NNc_array.hpp:47
const long int & operator[](size_t i) const
return the element i
Definition: NNc_array.hpp:57
Set a dimension threshold.
Definition: NNc_array.hpp:20
void swap(grid_sm< N, T > &g)
swap the grid_sm informations
Definition: grid_sm.hpp:700
void init_sym()
Initialize the NNc array with symmetric neighborhood cells indexes.
Definition: NNc_array.hpp:104
grid_key_dx< dim > get() const
Return the actual grid key iterator.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
NNc_array< dim, size, thr > & operator=(const NNc_array< dim, size, thr > &nnc)
Copy the NNc_array.
Definition: NNc_array.hpp:156
long int & operator[](size_t i)
return the element i
Definition: NNc_array.hpp:67
NNc_array< dim, size, false > & operator=(const NNc_array< dim, size, false > &nnc)
Copy the NNc_array.
Definition: NNc_array.hpp:267
void swap(NNc_array< dim, size, thr > &nnc)
swap NNc_array
Definition: NNc_array.hpp:169
void set_size(const size_t(&sz)[dim])
set the size of the cell grid
Definition: NNc_array.hpp:212
grid_sm< dim, void > gs_base
Information about the grid in the reduced space.
Definition: NNc_array.hpp:197
void init_full()
Initialize the NNc array with full neighborhood cells indexes.
Definition: NNc_array.hpp:76
bool isNext()
Check if there is the next element.
void setDimensions(const size_t(&dims)[N])
Reset the dimension of the grid.
Definition: grid_sm.hpp:229