OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
MemFast.hpp
1 /*
2  * MemFast.hpp
3  *
4  * Created on: Mar 22, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef MEMFAST_HPP_
9 #define MEMFAST_HPP_
10 
11 #include "Space/SpaceBox.hpp"
12 #include "util/mathutil.hpp"
13 #include "Space/Shape/HyperCube.hpp"
14 #include "NN/CellList/CellListIterator.hpp"
15 #include <unordered_map>
16 #include "util/common.hpp"
17 #include "Vector/map_vector.hpp"
18 
29 template <typename local_index = size_t>
30 class Mem_fast
31 {
33  local_index slot;
34 
37 
40 
44 
49  inline void realloc()
50  {
51  // we do not have enough slots reallocate the basic structure with more
52  // slots
53  base cl_base_(2*slot * cl_n.size());
54 
55  // copy cl_base
56  for (size_t i = 0 ; i < cl_n.size() ; i++)
57  {
58  for (size_t j = 0 ; j < cl_n.get(i) ; j++)
59  cl_base_.get(2*i*slot + j) = cl_base.get(slot * i + j);
60  }
61 
62  // Double the number of slots
63  slot *= 2;
64 
65  // swap the memory
66  cl_base.swap(cl_base_);
67  }
68 
69 
70 public:
71 
77  inline size_t size()
78  {
79  return cl_n.size();
80  }
81 
88  inline void init_to_zero(local_index slot, local_index tot_n_cell)
89  {
90  this->slot = slot;
91 
92  // create the array that store the number of particle on each cell and se it to 0
93 
94  cl_n.resize(tot_n_cell);
95  cl_n.fill(0);
96 
97  // create the array that store the cell id
98 
99  cl_base.resize(tot_n_cell * slot);
100  }
101 
107  inline void operator=(const Mem_fast<local_index> & mem)
108  {
109  slot = mem.slot;
110 
111  cl_n = mem.cl_n;
112  cl_base = mem.cl_base;
113  }
114 
120  inline void operator=(Mem_fast<local_index> && mem)
121  {
122  this->swap(mem);
123  }
124 
131  inline void addCell(local_index cell_id, typename base::value_type ele)
132  {
133  // Get the number of element the cell is storing
134 
135  size_t nl = getNelements(cell_id);
136 
137  if (nl + 1 >= slot)
138  {
139  realloc();
140  }
141 
142  // we have enough slot to store another neighbor element
143 
144  cl_base.get(slot * cell_id + cl_n.get(cell_id)) = ele;
145  cl_n.get(cell_id)++;
146  }
147 
154  inline void add(local_index cell_id, typename base::value_type ele)
155  {
156  // add the element to the cell
157 
158  this->addCell(cell_id,ele);
159  }
160 
169  inline auto get(local_index cell, local_index ele) -> decltype(cl_base.get(cell * slot + ele)) &
170  {
171  return cl_base.get(cell * slot + ele);
172  }
173 
174 
183  inline auto get(local_index cell, local_index ele) const -> decltype(cl_base.get(cell * slot + ele)) &
184  {
185  return cl_base.get(cell * slot + ele);
186  }
187 
188 
195  inline void remove(local_index cell, local_index ele)
196  {
197  cl_n.get(cell)--;
198  }
199 
207  inline size_t getNelements(const local_index cell_id) const
208  {
209  return cl_n.get(cell_id);
210  }
211 
212 
218  inline void swap(Mem_fast<local_index> & mem)
219  {
220  cl_n.swap(mem.cl_n);
221  cl_base.swap(mem.cl_base);
222 
223  size_t cl_slot_tmp = mem.slot;
224  mem.slot = slot;
225  slot = cl_slot_tmp;
226  }
227 
233  inline void swap(Mem_fast<local_index> && mem)
234  {
235  slot = mem.slot;
236 
237  cl_n.swap(mem.cl_n);
238  cl_base.swap(mem.cl_base);
239  }
240 
246  inline void clear()
247  {
248  for (size_t i = 0 ; i < cl_n.size() ; i++)
249  cl_n.get(i) = 0;
250  }
251 
259  inline const local_index & getStartId(local_index cell_id) const
260  {
261  return cl_base.get(cell_id*slot);
262  }
263 
271  inline const local_index & getStopId(local_index cell_id) const
272  {
273  return cl_base.get(cell_id*slot+cl_n.get(cell_id));
274  }
275 
283  inline const local_index & get_lin(const local_index * part_id) const
284  {
285  return *part_id;
286  }
287 
288 public:
289 
291  typedef local_index loc_index;
292 
298  inline Mem_fast(local_index slot)
299  :slot(slot)
300  {}
301 
307  inline void set_slot(local_index slot)
308  {
309  this->slot = slot;
310  }
311 
312 };
313 
314 
315 #endif /* CELLLISTSTANDARD_HPP_ */
local_index slot
Number of slot for each cell.
Definition: MemFast.hpp:33
const local_index & getStartId(local_index cell_id) const
Get the first element of a cell (as reference)
Definition: MemFast.hpp:259
openfpm::vector< local_index > base
base that store the data
Definition: MemFast.hpp:39
void operator=(Mem_fast< local_index > &&mem)
copy an object Mem_fast
Definition: MemFast.hpp:120
It is a class that work like a vector of vector.
Definition: MemFast.hpp:30
size_t size()
Stub size.
Definition: map_vector.hpp:70
void clear()
Delete all the elements in the Cell-list.
Definition: MemFast.hpp:246
base cl_base
Definition: MemFast.hpp:43
void set_slot(local_index slot)
Set the number of slot for each cell.
Definition: MemFast.hpp:307
void swap(Mem_fast< local_index > &&mem)
swap to Mem_fast object
Definition: MemFast.hpp:233
void operator=(const Mem_fast< local_index > &mem)
copy an object Mem_fast
Definition: MemFast.hpp:107
void init_to_zero(local_index slot, local_index tot_n_cell)
Initialize the data to zero.
Definition: MemFast.hpp:88
local_index loc_index
expose the type of the local index
Definition: MemFast.hpp:291
size_t size()
return the number of elements
Definition: MemFast.hpp:77
void addCell(local_index cell_id, typename base::value_type ele)
Add an element to the cell.
Definition: MemFast.hpp:131
void realloc()
realloc the data structures
Definition: MemFast.hpp:49
const local_index & get_lin(const local_index *part_id) const
Just return the value pointed by part_id.
Definition: MemFast.hpp:283
openfpm::vector< local_index > cl_n
number of particle in each cell list
Definition: MemFast.hpp:36
void swap(Mem_fast< local_index > &mem)
swap to Mem_fast object
Definition: MemFast.hpp:218
const local_index & getStopId(local_index cell_id) const
Get the last element of a cell (as reference)
Definition: MemFast.hpp:271
size_t getNelements(const local_index cell_id) const
Get the number of elements in the cell.
Definition: MemFast.hpp:207
void add(local_index cell_id, typename base::value_type ele)
Add an element to the cell.
Definition: MemFast.hpp:154
Mem_fast(local_index slot)
Constructor.
Definition: MemFast.hpp:298