OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
map_vector_std.hpp
1 /*
2  * map_vector_std.hpp
3  *
4  * Created on: Mar 8, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef MAP_VECTOR_STD_HPP_
9 #define MAP_VECTOR_STD_HPP_
10 
11 #include "se_vector.hpp"
12 
24 template<typename T>
25 class vector<T,HeapMemory,grow_policy_double,STD_VECTOR>
26 {
30  size_t v_size;
31 
33  std::vector<T> base;
34 
36  size_t err_code;
37 
38 public:
39 
41  typedef int yes_i_am_vector;
42 
44  typedef vector_key_iterator iterator_key;
46  typedef T value_type;
47 
48  //This file implements a pack and unpack for std vector
49 #include "vector_std_pack_unpack.ipp"
50 
52  inline size_t size() const
53  {
54 #ifdef SE_CLASS2
55  check_valid(this,8);
56 #endif
57  return base.size();
58  }
59 
60 
66  inline void resize(size_t slot)
67  {
68 #ifdef SE_CLASS2
69  check_valid(this,8);
70 #endif
71  v_size = slot;
72 
73  base.resize(slot);
74  }
75 
79  inline void clear()
80  {
81 #ifdef SE_CLASS2
82  check_valid(this,8);
83 #endif
84  base.clear();
85  }
86 
96  inline void add(const T & v)
97  {
98 #ifdef SE_CLASS2
99  check_valid(this,8);
100 #endif
101  base.push_back(v);
102  }
103 
113  inline void add(T && v)
114  {
115 #ifdef SE_CLASS2
116  check_valid(this,8);
117 #endif
118  base.emplace_back(v);
119  }
120 
125  inline void add()
126  {
127 #ifdef SE_CLASS2
128  check_valid(this,8);
129 #endif
130  base.emplace_back(T());
131  }
139  void erase(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)
140  {
141 #ifdef SE_CLASS2
142  check_valid(this,8);
143 #endif
144  base.erase(start,end);
145  }
146 
152  void remove(size_t key)
153  {
154 #ifdef SE_CLASS2
155  check_valid(this,8);
156 #endif
157 #ifdef SE_CLASS1
158  vector_overflow(key);
159 #endif
160  base.erase(base.begin() + key);
161  }
162 
168  inline auto begin() -> decltype(base.begin())
169  {
170  return base.begin();
171  }
172 
178  inline auto end() -> decltype(base.begin())
179  {
180  return base.end();
181  }
182 
188  inline T & last()
189  {
190 #ifdef SE_CLASS2
191  check_valid(this,8);
192 #endif
193 #ifdef SE_CLASS1
194  if (base.size() == 0)
195  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
196 #endif
197  return base[base.size()-1];
198  }
199 
205  inline const T & last() const
206  {
207 #ifdef SE_CLASS2
208  check_valid(this,8);
209 #endif
210 #ifdef SE_CLASS1
211  if (base.size() == 0)
212  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
213 #endif
214  return base[base.size()-1];
215  }
216 
222  std::vector<T> duplicate()
223  {
224 #ifdef SE_CLASS2
225  check_valid(this,8);
226 #endif
227  return base;
228  }
229 
235  void swap(std::vector<T> && v)
236  {
237 #ifdef SE_CLASS2
238  check_valid(this,8);
239 #endif
240  base.swap(v);
241  }
242 
248  void unique()
249  {
250 #ifdef SE_CLASS2
251  check_valid(this,8);
252 #endif
253  auto it = std::unique(base.begin(),base.end());
254  base.resize( std::distance(base.begin(),it) );
255  }
256 
262  void sort()
263  {
264 #ifdef SE_CLASS2
265  check_valid(this,8);
266 #endif
267  std::sort(base.begin(), base.end());
268  }
269 
279  template <unsigned int p>inline T& get(size_t id)
280  {
281 #ifdef SE_CLASS2
282  check_valid(this,8);
283 #endif
284 #ifdef SE_CLASS1
285  if (p != 0)
286  {std::cerr << "Error the property does not exist" << "\n";}
287 
288  vector_overflow(id);
289 #endif
290 
291  return base[id];
292  }
293 
303  template <unsigned int p>inline const T& get(size_t id) const
304  {
305 #ifdef SE_CLASS2
306  check_valid(this,8);
307 #endif
308 #ifdef SE_CLASS1
309  if (p != 0)
310  {std::cerr << "Error the property does not exist" << "\n";}
311 
312  vector_overflow(id);
313 #endif
314 
315  return base[id];
316  }
317 
325  inline T & get(size_t id)
326  {
327 #ifdef SE_CLASS2
328  check_valid(this,8);
329 #endif
330 #ifdef SE_CLASS1
331  if (id >= base.size())
332  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << id << "\n";
333 #endif
334  return base[id];
335  }
336 
344  inline const T & get(size_t id) const
345  {
346 #ifdef SE_CLASS2
347  check_valid(this,8);
348 #endif
349 #ifdef SE_CLASS1
350  vector_overflow(id);
351 #endif
352  return base[id];
353  }
354 
364  inline void fill(unsigned char fl)
365  {
366 #ifdef SE_CLASS2
367  check_valid(this,8);
368 #endif
369  memset(&base[0],fl,base.size() * sizeof(T));
370  }
371 
378  inline void reserve(size_t ns)
379  {
380 #ifdef SE_CLASS2
381  check_valid(this,8);
382 #endif
383  base.reserve(ns);
384  }
385 
387  vector() noexcept
388  :v_size(0),err_code(0)
389  {
390 #ifdef SE_CLASS2
391  check_new(this,8,VECTOR_STD_EVENT,1);
392 #endif
393  }
394 
396  vector(size_t sz) noexcept
397  :v_size(0),base(sz),err_code(0)
398  {
399 #ifdef SE_CLASS2
400  check_new(this,8,VECTOR_STD_EVENT,1);
401 #endif
402  }
403 
406  :v_size(0),err_code(0)
407  {
408 #ifdef SE_CLASS2
409  check_new(this,8,VECTOR_STD_EVENT,1);
410 #endif
411 
412  base = v.base;
413  }
414 
420  vector(const std::initializer_list<T> & v)
421  :base(v)
422  {}
423 
426  :v_size(0),err_code(0)
427  {
428 #ifdef SE_CLASS2
429  check_new(this,8,VECTOR_STD_EVENT,1);
430 #endif
431 
432  base.swap(v.base);
433  }
434 
436  ~vector() noexcept
437  {
438 #ifdef SE_CLASS2
439  check_delete(this);
440 #endif
441  }
442 
449  {
450 #ifdef SE_CLASS2
451  check_valid(this,8);
452 #endif
453  base.swap(v.base);
454  }
455 
462  {
463 #ifdef SE_CLASS2
464  check_valid(this,8);
465 #endif
466  base = v.base;
467 
468  return *this;
469  }
470 
477  {
478 #ifdef SE_CLASS2
479  check_valid(this,8);
480 #endif
481  base.swap(v.base);
482 
483  return *this;
484  }
485 
492  {
493  return base != v.base;
494  }
495 
502  {
503  return base == v.base;
504  }
505 
511  vector_key_iterator getIterator() const
512  {
513 #ifdef SE_CLASS2
514  check_valid(this,8);
515 #endif
516  return vector_key_iterator(base.size());
517  }
518 
529  template<int ... prp> inline size_t packMem(size_t n, size_t e)
530  {
531  if (n == 0)
532  return 0;
533  else {
534 #ifdef DEBUG
535  std::cout << "Inside map_vector_std.hpp packMem()" << std::endl;
536 #endif
537  packMem_cond<has_packMem<T>::type::value, openfpm::vector<T, HeapMemory, grow_policy_double>, prp...> cm;
538  return cm.packMemory(*this,n,0);
539  }
540  }
541 
552  inline static size_t calculateMemDummy(size_t n, size_t e)
553  {
554  return n*sizeof(T);
555  }
556 
564  inline static size_t calculateNMem(size_t n)
565  {
566 
567  return 1;
568  }
569 
575  void * getPointer()
576  {
577 #ifdef SE_CLASS2
578  check_valid(this,8);
579 #endif
580  return &base[0];
581  }
582 
588  const void * getPointer() const
589  {
590  return &base[0];
591  }
592 
598  static bool noPointers()
599  {
600  return false;
601  }
602 
606  size_t getLastError()
607  {
608 #ifdef SE_CLASS2
609  check_valid(this,8);
610 #endif
611  return err_code;
612  }
613 
619  inline void vector_overflow(size_t v1) const
620  {
621  if (v1 >= base.size())
622  {
623  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
624  size_t * err_code_pointer = (size_t *)&this->err_code;\
625  *err_code_pointer = 2001;\
626  ACTION_ON_ERROR(VECTOR_ERROR);\
627  }
628  }
629 
630  /* \brief It return the id of structure in the allocation list
631  *
632  * \see print_alloc and SE_CLASS2
633  *
634  */
635  long int who()
636  {
637 #ifdef SE_CLASS2
638  return check_whoami(this,8);
639 #else
640  return -1;
641 #endif
642  }
643 };
644 
645 
646 #endif /* MAP_VECTOR_STD_HPP_ */
std::vector< T > duplicate()
Duplicate the vector.
size_t size() const
return the size of the vector
void swap(openfpm::vector< T, HeapMemory, grow_policy_double, STD_VECTOR > &v)
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:25
vector_key_iterator iterator_key
iterator for the vector
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
vector< T, HeapMemory, grow_policy_double, STD_VECTOR > & operator=(vector< T, HeapMemory, grow_policy_double, STD_VECTOR > &&v)
Operator= copy the vector into another.
static bool noPointers()
This class has pointer inside.
vector_key_iterator getIterator() const
Get iterator.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
vector(const vector< T, HeapMemory, grow_policy_double, STD_VECTOR > &v) noexcept
Constructor from another vector.
const T & last() const
Get the last element.
static size_t calculateMemDummy(size_t n, size_t e)
Calculate the memory size required to allocate n elements.
void erase(typename std::vector< T >::iterator start, typename std::vector< T >::iterator end)
Erase the elements from start to end.
auto end() -> decltype(base.begin())
Return an std compatible iterator to the last element.
vector(vector< T, HeapMemory, grow_policy_double, 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.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:202
size_t packMem(size_t n, size_t e)
Calculate the memory size required to allocate n elements.
void clear()
Remove all the element from the vector.
bool operator==(const vector< T, HeapMemory, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are not equal.
vector(size_t sz) noexcept
Constructor, vector of size sz.
void fill(unsigned char fl)
it fill all the memory of fl patterns
bool operator!=(const vector< T, HeapMemory, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are equal.
vector< T, HeapMemory, grow_policy_double, STD_VECTOR > & operator=(const vector< T, HeapMemory, grow_policy_double, STD_VECTOR > &v)
Operator= copy the vector into another.
static size_t calculateNMem(size_t n)
How many allocation are required to create n-elements.
Implementation of 1-D std::vector like structure.
void swap(std::vector< T > &&v)
swap the memory between the two vector
auto begin() -> decltype(base.begin())
Return an std compatible iterator to the first element.
void * getPointer()
Return the pointer to the chunk of memory.
void reserve(size_t ns)
reserve a memory space in advance to avoid reallocation
vector(const std::initializer_list< T > &v)
Initializer from constructor.
vector() noexcept
Constructor, vector of size 0.
Implementation of 1-D std::vector like structure.
Definition: map_grid.hpp:94
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
const void * getPointer() const
Return the pointer to the chunk of memory.
T value_type
Type of the value the vector is storing.