OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
map_vector_std_ptr.hpp
1 /*
2  * map_vector_std_ptr.hpp
3  *
4  * Created on: Feb 9, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_
9 #define OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_
10 
11 
12 template<typename T,typename gp>
13 class vector<T,PtrMemory,memory_traits_lin,gp,STD_VECTOR>
14 {
17 
19  template <typename lb> using layout_base = memory_traits_lin<lb>;
20 
24  size_t v_size;
25 
28 
30  size_t err_code;
31 
32 public:
33 
35  typedef int yes_i_am_vector;
36 
38  typedef vector_key_iterator iterator_key;
40  typedef T value_type;
41 
43  inline size_t size() const
44  {
45  return v_size;
46  }
47 
48 
54  inline void resize(size_t slot)
55  {
56  // resize is valid only if v_size is 0 and it match the size of PtrMemory
57  if (slot > mem->size()/sizeof(T))
58  std::cerr << __FILE__ << ":" << __LINE__ << " error: this vector cannot be bigger than " << mem->size()/sizeof(T) << " elements\n";
59  v_size = slot;
60  }
61 
65  inline void clear()
66  {
67  v_size = 0;
68  }
69 
79  inline void add(const T & v)
80  {
81  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot add a new element to this vector \n";
82  }
83 
93  inline void add(T && v)
94  {
95  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot add new element to this vector \n";
96  }
97 
102  inline void add()
103  {
104  v_size++;
105 
106  if (v_size > mem->size()/sizeof(T))
107  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot resize a PtrMemory vector over the size stored by PtrMemory";
108  }
109 
116  void erase(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)
117  {
118  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot erase element from this vector \n";
119  }
120 
126  void remove(size_t key)
127  {
128 #ifdef SE_CLASS1
129  vector_overflow(key);
130 #endif
131  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot remove elements from this vector \n";
132  }
133 
139  inline T * begin()
140  {
141  return mem->getPointer();
142  }
143 
149  inline T * end()
150  {
151  return &((T *)mem->getPointer())[v_size];
152  }
153 
159  inline const T * begin() const
160  {
161  return (T *)mem->getPointer();
162  }
163 
169  inline const T * end() const
170  {
171  return &((T *)mem->getPointer())[v_size];
172  }
173 
179  inline T & last()
180  {
181 #ifdef SE_CLASS1
182  if (v_size == 0)
183  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
184 #endif
185  return ((T *)mem->getPointer())[v_size-1];
186  }
187 
193  inline const T & last() const
194  {
195 #ifdef SE_CLASS1
196  if (v_size == 0)
197  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0" << std::endl;
198 #endif
199  return ((T *)mem->getPointer())[v_size-1];
200  }
201 
211  template <unsigned int p>inline T& get(size_t id)
212  {
213 #ifdef SE_CLASS1
214  if (p != 0)
215  {std::cerr << "Error the property does not exist" << "\n";}
216 
217  vector_overflow(id);
218 #endif
219 
220  return ((T *)mem->getPointer())[id];
221  }
222 
223  inline void setMemory(PtrMemory & mem)
224  {
225  this->mem = &mem;
226  }
227 
237  template <unsigned int p>inline const T& get(size_t id) const
238  {
239 #ifdef SE_CLASS1
240  if (p != 0)
241  {std::cerr << "Error the property does not exist" << "\n";}
242 
243  vector_overflow(id);
244 #endif
245 
246  return ((T *)mem->getPointer())[id];
247  }
248 
256  inline T & get(size_t id)
257  {
258 #ifdef SE_CLASS1
259  if (id >= v_size)
260  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << id << "\n";
261 #endif
262  return ((T *)mem->getPointer())[id];
263  }
264 
272  inline const T & get(size_t id) const
273  {
274 #ifdef SE_CLASS1
275  vector_overflow(id);
276 #endif
277  return ((T *)mem->getPointer())[id];
278  }
279 
289  inline void fill(unsigned char fl)
290  {
291  memset(mem->getPointer(),fl,v_size * sizeof(T));
292  }
293 
300  inline void reserve(size_t ns)
301  {
302  }
303 
305  vector() noexcept
306  :v_size(0),mem(NULL),err_code(0)
307  {
308  }
309 
311  vector(size_t sz) noexcept
312  :v_size(sz),err_code(0)
313  {
314  }
315 
317  vector(const vector<T,PtrMemory,layout_base,gp,STD_VECTOR> & v) noexcept
318  :v_size(0),err_code(0)
319  {
320  std::cerr << __FILE__ << ":" << __LINE__ << " error: copy constructor is not supported by this vector \n";
321  }
322 
323 
325  vector(vector<T,PtrMemory,layout_base,gp,STD_VECTOR> && v) noexcept
326  :v_size(0),err_code(0)
327  {
328  std::cerr << __FILE__ << ":" << __LINE__ << " error: copy constructor is not supported by this vector \n";
329  }
330 
332  ~vector() noexcept
333  {
334  }
335 
342  {
343  std::cerr << __FILE__ << ":" << __LINE__ << " error: swap is not supported by this vector \n";
344  }
345 
351  vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> & operator=(const vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> & v)
352  {
353  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator= is not supported by this vector \n";
354 
355  return *this;
356  }
357 
365  vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> & operator=(vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> && v)
366  {
367  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator= is not supported by this vector \n";
368 
369  return *this;
370  }
371 
379  bool operator!=(const vector<T, HeapMemory, layout_base,grow_policy_double,STD_VECTOR> & v) const
380  {
381  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator!= is not supported by this vector \n";
382 
383  return false;
384  }
385 
393  bool operator==(const vector<T, HeapMemory, layout_base,grow_policy_double,STD_VECTOR> & v) const
394  {
395  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator== is not supported by this vector \n";
396 
397  return false;
398  }
399 
405  vector_key_iterator getIterator() const
406  {
407  return vector_key_iterator(v_size);
408  }
409 
410 #ifdef CUDA_GPU
411 
415  template<unsigned int ... prp> void hostToDevice()
416  {}
417 
421  template<unsigned int ... prp> void deviceToHost()
422  {}
423 
428  template<unsigned int ... prp> void deviceToHost(size_t start, size_t stop)
429  {}
430 
431 #endif
432 
438  void * getPointer()
439  {
440  return mem->getPointer();
441  }
442 
448  const void * getPointer() const
449  {
450  return mem->getPointer();
451  }
452 
458  static bool noPointers()
459  {
460  return false;
461  }
462 
468  size_t getLastError()
469  {
470  return err_code;
471  }
472 
478  inline void vector_overflow(size_t v1) const
479  {
480  if (v1 >= v_size)
481  {
482  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
483  size_t * err_code_pointer = (size_t *)&this->err_code;\
484  *err_code_pointer = 2001;\
485  ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
486  }
487  }
488 };
489 
490 
491 #endif /* OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_ */
bool operator!=(const vector< T, HeapMemory, layout_base, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are equal.
size_t size() const
return the size of the vector
vector< T, HeapMemory, layout_base, grow_policy_double, STD_VECTOR > & operator=(const vector< T, HeapMemory, layout_base, grow_policy_double, STD_VECTOR > &v)
Operator= copy the vector into another.
vector_key_iterator getIterator() const
Get iterator.
const T & get(size_t id) const
Get an element of the vector.
Transform the boost::fusion::vector into memory specification (memory_traits)
virtual size_t size() const
the the size of the allocated memory
Definition: PtrMemory.cpp:122
void * getPointer()
Return the pointer to the chunk of memory.
const void * getPointer() const
Return the pointer to the chunk of memory.
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
vector_key_iterator iterator_key
iterator for the vector
vector(size_t sz) noexcept
Constructor, vector of size sz.
const T * end() const
Return an std compatible iterator to the past the last element.
T & get(size_t id)
Get an element of the vector.
vector< T, HeapMemory, layout_base, grow_policy_double, STD_VECTOR > & operator=(vector< T, HeapMemory, layout_base, grow_policy_double, STD_VECTOR > &&v)
Operator= copy the vector into another.
void add(T &&v)
It insert a new object on the vector, eventually it reallocate the grid.
vector(const vector< T, PtrMemory, layout_base, gp, STD_VECTOR > &v) noexcept
Constructor from another vector.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
void reserve(size_t ns)
reserve a memory space in advance to avoid reallocation
bool operator==(const vector< T, HeapMemory, layout_base, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are not equal.
void erase(typename std::vector< T >::iterator start, typename std::vector< T >::iterator end)
Erase the elements from start to end.
T * end()
Return an std compatible iterator to the past the last element.
void remove(size_t key)
Remove one entry from the vector.
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
T & get(size_t id)
Get an element of the vector.
void swap(openfpm::vector< T, PtrMemory, layout_base, gp, STD_VECTOR > &v)
const T & get(size_t id) const
Get an element of the vector.
virtual void * getPointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:156
const T * begin() const
Return an std compatible iterator to the first element.
void fill(unsigned char fl)
it fill all the memory of fl patterns
void clear()
Remove all the element from the vector.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:202
static bool noPointers()
This class has pointer inside.
T value_type
Type of the value the vector is storing.
T * begin()
Return an std compatible iterator to the first element.
vector(vector< T, PtrMemory, layout_base, gp, STD_VECTOR > &&v) noexcept
Constructor from another vector.
memory_traits_lin< T >::type layout
Memory layout.
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:39