OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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,typename memory_traits_lin<T>::type,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 #ifdef SE_CLASS2
46  check_valid(this,8);
47 #endif
48  return v_size;
49  }
50 
51 
57  inline void resize(size_t slot)
58  {
59 #ifdef SE_CLASS2
60  check_valid(this,8);
61 #endif
62  // resize is valid only if v_size is 0 and it match the size of PtrMemory
63  if (slot > mem->size()/sizeof(T))
64  std::cerr << __FILE__ << ":" << __LINE__ << " error: this vector cannot be bigger than " << mem->size()/sizeof(T) << " elements\n";
65  v_size = slot;
66  }
67 
71  inline void clear()
72  {
73 #ifdef SE_CLASS2
74  check_valid(this,8);
75 #endif
76  v_size = 0;
77  }
78 
88  inline void add(const T & v)
89  {
90 #ifdef SE_CLASS2
91  check_valid(this,8);
92 #endif
93  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot add a new element to this vector \n";
94  }
95 
105  inline void add(T && v)
106  {
107 #ifdef SE_CLASS2
108  check_valid(this,8);
109 #endif
110  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot add new element to this vector \n";
111  }
112 
117  inline void add()
118  {
119 #ifdef SE_CLASS2
120  check_valid(this,8);
121 #endif
122  v_size++;
123 
124  if (v_size > mem->size()/sizeof(T))
125  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot resize a PtrMemory vector over the size stored by PtrMemory";
126  }
127 
134  void erase(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)
135  {
136 #ifdef SE_CLASS2
137  check_valid(this,8);
138 #endif
139  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot erase element from this vector \n";
140  }
141 
147  void remove(size_t key)
148  {
149 #ifdef SE_CLASS2
150  check_valid(this,8);
151 #endif
152 #ifdef SE_CLASS1
153  vector_overflow(key);
154 #endif
155  std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot remove elements from this vector \n";
156  }
157 
163  inline T * begin()
164  {
165  return mem->getPointer();
166  }
167 
173  inline T * end()
174  {
175  return &((T *)mem->getPointer())[v_size];
176  }
177 
183  inline const T * begin() const
184  {
185  return (T *)mem->getPointer();
186  }
187 
193  inline const T * end() const
194  {
195  return &((T *)mem->getPointer())[v_size];
196  }
197 
203  inline T & last()
204  {
205 #ifdef SE_CLASS2
206  check_valid(this,8);
207 #endif
208 #ifdef SE_CLASS1
209  if (v_size == 0)
210  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
211 #endif
212  return ((T *)mem->getPointer())[v_size-1];
213  }
214 
220  inline const T & last() const
221  {
222 #ifdef SE_CLASS2
223  check_valid(this,8);
224 #endif
225 #ifdef SE_CLASS1
226  if (v_size == 0)
227  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0" << std::endl;
228 #endif
229  return ((T *)mem->getPointer())[v_size-1];
230  }
231 
241  template <unsigned int p>inline T& get(size_t id)
242  {
243 #ifdef SE_CLASS2
244  check_valid(this,8);
245 #endif
246 #ifdef SE_CLASS1
247  if (p != 0)
248  {std::cerr << "Error the property does not exist" << "\n";}
249 
250  vector_overflow(id);
251 #endif
252 
253  return ((T *)mem->getPointer())[id];
254  }
255 
256  inline void setMemory(PtrMemory & mem)
257  {
258  this->mem = &mem;
259  }
260 
270  template <unsigned int p>inline const T& get(size_t id) const
271  {
272 #ifdef SE_CLASS2
273  check_valid(this,8);
274 #endif
275 #ifdef SE_CLASS1
276  if (p != 0)
277  {std::cerr << "Error the property does not exist" << "\n";}
278 
279  vector_overflow(id);
280 #endif
281 
282  return ((T *)mem->getPointer())[id];
283  }
284 
292  inline T & get(size_t id)
293  {
294 #ifdef SE_CLASS2
295  check_valid(this,8);
296 #endif
297 #ifdef SE_CLASS1
298  if (id >= v_size)
299  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << id << "\n";
300 #endif
301  return ((T *)mem->getPointer())[id];
302  }
303 
311  inline const T & get(size_t id) const
312  {
313 #ifdef SE_CLASS2
314  check_valid(this,8);
315 #endif
316 #ifdef SE_CLASS1
317  vector_overflow(id);
318 #endif
319  return ((T *)mem->getPointer())[id];
320  }
321 
331  inline void fill(unsigned char fl)
332  {
333 #ifdef SE_CLASS2
334  check_valid(this,8);
335 #endif
336  memset(mem->getPointer(),fl,v_size * sizeof(T));
337  }
338 
345  inline void reserve(size_t ns)
346  {
347 #ifdef SE_CLASS2
348  check_valid(this,8);
349 #endif
350  }
351 
353  vector() noexcept
354  :v_size(0),mem(NULL),err_code(0)
355  {
356 #ifdef SE_CLASS2
357  check_new(this,8,VECTOR_STD_EVENT,1);
358 #endif
359  }
360 
362  vector(size_t sz) noexcept
363  :v_size(sz),err_code(0)
364  {
365 #ifdef SE_CLASS2
366  check_new(this,8,VECTOR_STD_EVENT,1);
367 #endif
368  }
369 
371  vector(const vector<T,PtrMemory,layout,layout_base,gp,STD_VECTOR> & v) noexcept
372  :v_size(0),err_code(0)
373  {
374 #ifdef SE_CLASS2
375  check_new(this,8,VECTOR_STD_EVENT,1);
376 #endif
377 
378  std::cerr << __FILE__ << ":" << __LINE__ << " error: copy constructor is not supported by this vector \n";
379  }
380 
381 
383  vector(vector<T,PtrMemory,layout,layout_base,gp,STD_VECTOR> && v) noexcept
384  :v_size(0),err_code(0)
385  {
386 #ifdef SE_CLASS2
387  check_new(this,8,VECTOR_STD_EVENT,1);
388 #endif
389 
390  std::cerr << __FILE__ << ":" << __LINE__ << " error: copy constructor is not supported by this vector \n";
391  }
392 
394  ~vector() noexcept
395  {
396 #ifdef SE_CLASS2
397  check_delete(this);
398 #endif
399  }
400 
407  {
408 #ifdef SE_CLASS2
409  check_valid(this,8);
410 #endif
411  std::cerr << __FILE__ << ":" << __LINE__ << " error: swap is not supported by this vector \n";
412  }
413 
419  vector<T,HeapMemory,layout,layout_base,grow_policy_double,STD_VECTOR> & operator=(const vector<T,HeapMemory,layout,layout_base,grow_policy_double,STD_VECTOR> & v)
420  {
421 #ifdef SE_CLASS2
422  check_valid(this,8);
423 #endif
424  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator= is not supported by this vector \n";
425 
426  return *this;
427  }
428 
436  vector<T,HeapMemory,layout,layout_base,grow_policy_double,STD_VECTOR> & operator=(vector<T,HeapMemory,layout,layout_base,grow_policy_double,STD_VECTOR> && v)
437  {
438 #ifdef SE_CLASS2
439  check_valid(this,8);
440 #endif
441  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator= is not supported by this vector \n";
442 
443  return *this;
444  }
445 
453  bool operator!=(const vector<T, HeapMemory, layout, layout_base,grow_policy_double,STD_VECTOR> & v) const
454  {
455  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator!= is not supported by this vector \n";
456 
457  return false;
458  }
459 
467  bool operator==(const vector<T, HeapMemory, layout, layout_base,grow_policy_double,STD_VECTOR> & v) const
468  {
469  std::cerr << __FILE__ << ":" << __LINE__ << " error: operator== is not supported by this vector \n";
470 
471  return false;
472  }
473 
479  vector_key_iterator getIterator() const
480  {
481 #ifdef SE_CLASS2
482  check_valid(this,8);
483 #endif
484  return vector_key_iterator(v_size);
485  }
486 
487 
493  void * getPointer()
494  {
495 #ifdef SE_CLASS2
496  check_valid(this,8);
497 #endif
498  return mem->getPointer();
499  }
500 
506  const void * getPointer() const
507  {
508  return mem->getPointer();
509  }
510 
516  static bool noPointers()
517  {
518  return false;
519  }
520 
526  size_t getLastError()
527  {
528 #ifdef SE_CLASS2
529  check_valid(this,8);
530 #endif
531  return err_code;
532  }
533 
539  inline void vector_overflow(size_t v1) const
540  {
541  if (v1 >= v_size)
542  {
543  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
544  size_t * err_code_pointer = (size_t *)&this->err_code;\
545  *err_code_pointer = 2001;\
546  ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
547  }
548  }
549 
550  /* \brief It return the id of structure in the allocation list
551  *
552  * \see print_alloc and SE_CLASS2
553  *
554  * \return the allocation id
555  *
556  */
557  long int who()
558  {
559 #ifdef SE_CLASS2
560  return check_whoami(this,8);
561 #else
562  return -1;
563 #endif
564  }
565 };
566 
567 
568 #endif /* OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_ */
T * end()
Return an std compatible iterator to the past the last element.
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:93
const T * begin() const
Return an std compatible iterator to the first element.
vector< T, HeapMemory, layout, layout_base, grow_policy_double, STD_VECTOR > & operator=(const vector< T, HeapMemory, layout, layout_base, grow_policy_double, STD_VECTOR > &v)
Operator= copy the vector into another.
bool operator==(const vector< T, HeapMemory, layout, layout_base, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are not equal.
const T * end() const
Return an std compatible iterator to the past the last element.
void erase(typename std::vector< T >::iterator start, typename std::vector< T >::iterator end)
Erase the elements from start to end.
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
vector(const vector< T, PtrMemory, layout, layout_base, gp, STD_VECTOR > &v) noexcept
Constructor from another vector.
void add(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
vector(vector< T, PtrMemory, layout, layout_base, gp, STD_VECTOR > &&v) noexcept
Constructor from another vector.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
bool operator!=(const vector< T, HeapMemory, layout, layout_base, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are equal.
void swap(openfpm::vector< T, PtrMemory, layout, layout_base, gp, STD_VECTOR > &v)
This class is a container for the memory interface like HeapMemory CudaMemory.
Definition: memory_c.hpp:31
vector< T, HeapMemory, layout, layout_base, grow_policy_double, STD_VECTOR > & operator=(vector< T, HeapMemory, layout, layout_base, grow_policy_double, STD_VECTOR > &&v)
Operator= copy the vector into another.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:42