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.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 #include "map_vector_std_ptr.hpp"
13 
14 #define OBJECT_ADD false
15 #define VECTOR_ADD true
16 
18 template<bool objv, typename vect_dst>
20 {
36  template <typename S, typename M, typename gp, unsigned int impl, unsigned int ...args> inline static void add(const vector<S,M,typename memory_traits_lin<S>::type,memory_traits_lin,gp,impl> & v_src, vect_dst & v_dst)
37  {
38 #ifdef SE_CLASS2
39  check_valid(&v_src,8);
40  check_valid(&v_dst,8);
41 #endif
42  for (size_t i = 0 ; i < v_src.size() ; i++)
44  {
45  // Add a new element
46  v_dst.add();
47 
48  // equal object
49  v_dst.get(v_dst.size()-1) = v_src.get(i);
50  }
51  }
52 };
53 
55 template<typename vect_dst>
56 struct add_prp_impl<OBJECT_ADD,vect_dst>
57 {
73  template <typename S, typename M, typename gp, unsigned int impl, unsigned int ...args> inline static void add(const vector<S,M,typename memory_traits_lin<S>::type,memory_traits_lin,gp,impl> & v_src, vect_dst & v_dst)
74  {
75 #ifdef SE_CLASS2
76  check_valid((void *)&v_dst,8);
77  check_valid((void *)&v_src,8);
78 #endif
79  // Add a new element
80  v_dst.add();
81 
82  // equal object
83  v_dst.get(v_dst.size()-1) = v_src;
84  }
85 };
86 
87 
99 template<typename T>
100 class vector<T,HeapMemory,typename memory_traits_lin<T>::type,memory_traits_lin,grow_policy_double,STD_VECTOR>
101 {
102  // Memory layout
103  typedef typename memory_traits_lin<T>::type layout;
104 // Doeas not work on gcc 4.8.4
105 // template <typename lb> using layout_base = memory_traits_lin<lb>;
106 
108  std::vector<T> base;
109 
111  size_t err_code = 0;
112 
113 public:
114 
116  typedef int yes_i_am_vector;
117 
119  typedef vector_key_iterator iterator_key;
121  typedef T value_type;
122 
123  typedef void base_to_copy;
124 
125  //This file implements a pack and unpack for std vector
126 #include "vector_std_pack_unpack.ipp"
127 
129  inline size_t size() const
130  {
131 #ifdef SE_CLASS2
132  check_valid(this,8);
133 #endif
134  return base.size();
135  }
136 
137 
143  inline void resize(size_t slot)
144  {
145 #ifdef SE_CLASS2
146  check_valid(this,8);
148  // here we have to check if the vector go into reallocation
149  void * ptr_old = &base[0];
150 #endif
151 
152  base.resize(slot);
154 #ifdef SE_CLASS2
155  if (ptr_old != &base[0])
156  {
157  check_delete(ptr_old);
158  check_new(&base[0],slot*sizeof(T),VECTOR_STD_EVENT,1);
159  }
160 #endif
161  }
162 
166  inline void clear()
167  {
168 #ifdef SE_CLASS2
169  check_valid(this,8);
170 #endif
171  base.clear();
172  }
173 
183  inline void add(const T & v)
184  {
185 #ifdef SE_CLASS2
186  check_valid(this,8);
187  void * ptr_old = &base[0];
188 #endif
189 
190  base.push_back(v);
191 
192 #ifdef SE_CLASS2
193 
194  if (ptr_old != &base[0])
195  {
196  check_delete(ptr_old);
197  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
198  }
199 
200 #endif
201  }
212  inline void add(T && v)
213  {
214 #ifdef SE_CLASS2
215  check_valid(this,8);
216  void * ptr_old = &base[0];
217 #endif
218 
219  base.emplace_back(v);
220 
221 #ifdef SE_CLASS2
222 
223  if (ptr_old != &base[0])
224  {
225  check_delete(ptr_old);
226  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
227  }
228 
229 #endif
230  }
231 
235  inline void add()
236  {
237 #ifdef SE_CLASS2
238  check_valid(this,8);
239  void * ptr_old = &base[0];
240 #endif
241 
242  base.emplace_back(T());
243 
244 #ifdef SE_CLASS2
245 
246  if (ptr_old != &base[0])
247  {
248  check_delete(ptr_old);
249  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
250  }
251 
252 #endif
253  }
254 
260  template<typename Mem,typename l,template<typename> class lb,typename gp> inline void add(const openfpm::vector<T,Mem,l,lb,gp> & eles)
261  {
262 
263 #ifdef SE_CLASS2
264  check_valid(this,8);
265  void * ptr_old = &base[0];
266 #endif
267 
268  size_t start = base.size();
269  base.resize(base.size() + eles.size());
270 
271  // copy the elements
272  std::copy(eles.begin(),eles.end(),base.begin()+start);
273 
274 #ifdef SE_CLASS2
275 
276  if (ptr_old != &base[0])
277  {
278  check_delete(ptr_old);
279  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
280  }
281 
282 #endif
283  }
284 
294  template<typename S> inline void add(const S & v)
295  {
296 #ifdef SE_CLASS2
297  check_valid(this,8);
298  void * ptr_old = &base[0];
299 #endif
300 
302 
303 #ifdef SE_CLASS2
304 
305  if (ptr_old != &base[0])
306  {
307  check_delete(ptr_old);
308  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
309  }
310 
311 #endif
312  }
313 
323  template<typename S> inline void add(const S && v)
324  {
325 #ifdef SE_CLASS2
326  check_valid(this,8);
327  void * ptr_old = &base[0];
328 #endif
329 
330  base.push_back(v);
331 
332 #ifdef SE_CLASS2
333 
334  if (ptr_old != &base[0])
335  {
336  check_delete(ptr_old);
337  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
338  }
339 
340 #endif
341  }
342 
357  template <typename S,
358  typename M,
359  typename gp,
360  unsigned int impl,
361  template <typename> class layout_base,
362  unsigned int ...args>
363  void add_prp(const vector<S,M,typename layout_base<S>::type,layout_base,gp,impl> & v)
364  {
365 #ifdef SE_CLASS2
366  check_valid(this,8);
367 #endif
368 
369  add_prp_impl<std::is_same<S,T>::value,typename std::remove_pointer<decltype(*this)>::type>::template add<S,M,gp,impl,args...>(v,*this);
370  }
371 
385  template <typename S,
386  typename M,
387  typename gp,
388  unsigned int impl,
389  template <typename> class layout_base,
390  unsigned int ...args>
391  void add_prp(const T & v)
392  {
393 #ifdef SE_CLASS2
394  check_valid(this,8);
395 #endif
396  add(v);
397  }
398 
405  void erase(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)
406  {
407 #ifdef SE_CLASS2
408  check_valid(this,8);
409 #endif
410 
411  base.erase(start,end);
412  }
413 
419  void remove(size_t key)
420  {
421 #ifdef SE_CLASS2
422  check_valid(this,8);
423 #endif
424 #ifdef SE_CLASS1
425  vector_overflow(key);
426 #endif
427  base.erase(base.begin() + key);
428  }
429 
435  inline auto begin() -> decltype(base.begin())
436  {
437  return base.begin();
438  }
439 
445  inline auto end() -> decltype(base.begin())
446  {
447  return base.end();
448  }
449 
455  inline auto begin() const -> const decltype(base.begin())
456  {
457  return base.begin();
458  }
459 
465  inline auto end() const -> const decltype(base.begin())
466  {
467  return base.end();
468  }
469 
475  inline T & last()
476  {
477 #ifdef SE_CLASS2
478  check_valid(this,8);
479 #endif
480 #ifdef SE_CLASS1
481  if (base.size() == 0)
482  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
483 #endif
484  return base[base.size()-1];
485  }
486 
492  inline const T & last() const
493  {
494 #ifdef SE_CLASS2
495  check_valid(this,8);
496 #endif
497 #ifdef SE_CLASS1
498  if (base.size() == 0)
499  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
500 #endif
501  return base[base.size()-1];
502  }
503 
510  {
511 #ifdef SE_CLASS2
512  check_valid(this,8);
513 #endif
514  return *this;
515  }
516 
522  void swap(std::vector<T> && v)
523  {
524 #ifdef SE_CLASS2
525  check_valid(this,8);
526 #endif
527  base.swap(v);
528  }
529 
535  void unique()
536  {
537 #ifdef SE_CLASS2
538  check_valid(this,8);
539 #endif
540  auto it = std::unique(base.begin(),base.end());
541  base.resize( std::distance(base.begin(),it) );
542  }
543 
549  void sort()
550  {
551 #ifdef SE_CLASS2
552  check_valid(this,8);
553 #endif
554  std::sort(base.begin(), base.end());
555  }
556 
566  template <unsigned int p>inline T& get(size_t id)
567  {
568 #ifdef SE_CLASS2
569  check_valid(this,8);
570 #endif
571 #ifdef SE_CLASS1
572  if (p != 0)
573  {std::cerr << "Error the property does not exist" << "\n";}
574 
575  vector_overflow(id);
576 #endif
577 
578  return base[id];
579  }
580 
590  template <unsigned int p>inline const T& get(size_t id) const
591  {
592 #ifdef SE_CLASS2
593  check_valid(this,8);
594 #endif
595 #ifdef SE_CLASS1
596  if (p != 0)
597  {std::cerr << "Error the property does not exist" << "\n";}
598 
599  vector_overflow(id);
600 #endif
601 
602  return base[id];
603  }
604 
612  inline T & get(size_t id)
613  {
614 #ifdef SE_CLASS2
615  check_valid(this,8);
616 #endif
617 #ifdef SE_CLASS1
618  vector_overflow(id);
619 #endif
620  return base[id];
621  }
622 
630  inline const T & get(size_t id) const
631  {
632 #ifdef SE_CLASS2
633  check_valid(this,8);
634 #endif
635 #ifdef SE_CLASS1
636  vector_overflow(id);
637 #endif
638  return base[id];
639  }
640 
649  inline void fill(unsigned char fl)
650  {
651 #ifdef SE_CLASS2
652  check_valid(this,8);
653 #endif
654  memset(&base[0],fl,base.size() * sizeof(T));
655  }
656 
662  inline void reserve(size_t ns)
663  {
664 #ifdef SE_CLASS2
665  check_valid(this,8);
666 #endif
667  base.reserve(ns);
668  }
669 
671  vector() noexcept
672  :err_code(0)
673  {
674 #ifdef SE_CLASS2
675  check_new(this,8,VECTOR_STD_EVENT,1);
676 #endif
677  }
678 
680  vector(size_t sz) noexcept
681  :base(sz),err_code(0)
682  {
683 #ifdef SE_CLASS2
684  check_new(this,8,VECTOR_STD_EVENT,1);
685  check_new(&base[0],sizeof(T)*sz,VECTOR_STD_EVENT,1);
686 #endif
687  }
688 
690  vector(const vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> & v) noexcept
691  :err_code(0)
692  {
693 #ifdef SE_CLASS2
694  check_new(this,8,VECTOR_STD_EVENT,1);
695  void * ptr_old = &base[0];
696 #endif
697 
698  base = v.base;
699 
700 #ifdef SE_CLASS2
701 
702  if (ptr_old != &base[0])
703  {
704  check_delete(ptr_old);
705  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
706  }
707 
708 #endif
709  }
710 
716  vector(const std::initializer_list<T> & v)
717  :base(v)
718  {
719 #ifdef SE_CLASS2
720  check_new(this,8,VECTOR_STD_EVENT,1);
721  check_new(&base[0],sizeof(T)*v.size(),VECTOR_STD_EVENT,1);
722 #endif
723  }
724 
726  vector(vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> && v) noexcept
727  :err_code(0)
728  {
729 #ifdef SE_CLASS2
730  check_new(this,8,VECTOR_STD_EVENT,1);
731 #endif
732 
733  base.swap(v.base);
734  }
735 
737  ~vector() noexcept
738  {
739 #ifdef SE_CLASS2
740  check_delete(this);
741  check_delete(&base[0]);
742 #endif
743  }
744 
751  {
752 #ifdef SE_CLASS2
753  check_valid(this,8);
754 #endif
755  base.swap(v.base);
756  }
757 
764  {
765 #ifdef SE_CLASS2
766  check_valid(this,8);
767 #endif
768  base.swap(v.base);
769  }
770 
778  vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(const vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> & v)
779  {
780 #ifdef SE_CLASS2
781  check_valid(this,8);
782  void * ptr_old = &base[0];
783 #endif
784 
785  base = v.base;
786 
787 #ifdef SE_CLASS2
788 
789  if (ptr_old != &base[0])
790  {
791  check_delete(ptr_old);
792  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
793  }
794 
795 #endif
796 
797  return *this;
798  }
799 
805  template<typename Mem, typename gp> vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(const vector<T,Mem,layout,memory_traits_lin,gp,STD_VECTOR> & v)
806  {
807 #ifdef SE_CLASS2
808  check_valid(this,8);
809  void * ptr_old = &base[0];
810 #endif
811 
813  decltype(*this),
814  vector<T,Mem,layout,memory_traits_lin,gp,STD_VECTOR> >::copy(*this,v);
815 
816 #ifdef SE_CLASS2
817 
818  if (ptr_old != &base[0])
819  {
820  check_delete(ptr_old);
821  check_new(&base[0],base.size()*sizeof(T),VECTOR_STD_EVENT,1);
822  }
823 
824 #endif
825 
826  return *this;
827  }
828 
836  vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> && v)
837  {
838 #ifdef SE_CLASS2
839  check_valid(this,8);
840 #endif
841  base.swap(v.base);
842 
843  return *this;
844  }
845 
853  template<typename Mem, typename gp> vector<T,HeapMemory,layout,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(vector<T,Mem,layout,memory_traits_lin,gp,STD_VECTOR> && v)
854  {
855 #ifdef SE_CLASS2
856  check_valid(this,8);
857 #endif
858  base.swap(v.base);
859 
860  return *this;
861  }
862 
870  bool operator!=(const vector<T, HeapMemory, layout, memory_traits_lin,grow_policy_double,STD_VECTOR> & v) const
871  {
872  return base != v.base;
873  }
874 
882  bool operator==(const vector<T, HeapMemory, layout, memory_traits_lin,grow_policy_double,STD_VECTOR> & v) const
883  {
884  return base == v.base;
885  }
886 
892  vector_key_iterator getIterator() const
893  {
894 #ifdef SE_CLASS2
895  check_valid(this,8);
896 #endif
897  return vector_key_iterator(base.size());
898  }
899 
907  vector_key_iterator getIteratorTo(size_t k) const
908  {
909 #ifdef SE_CLASS2
910  check_valid(this,8);
911 #endif
912  return vector_key_iterator(k);
913  }
914 
925  template<int ... prp> inline size_t packMem(size_t n, size_t e) const
926  {
927  if (n == 0)
928  return 0;
929  else
930  {
931  packMem_cond<has_packMem<T>::type::value, openfpm::vector<T, HeapMemory, layout, memory_traits_lin, grow_policy_double>, prp...> cm;
932  return cm.packMemory(*this,n,0);
933  }
934  }
935 
946  inline static size_t calculateMemDummy(size_t n, size_t e)
947  {
948  return n*sizeof(T);
949  }
950 
958  inline static size_t calculateNMem(size_t n)
959  {
960 
961  return 1;
962  }
963 
969  void * getPointer()
970  {
971 #ifdef SE_CLASS2
972  check_valid(this,8);
973 #endif
974  return &base[0];
975  }
976 
982  const void * getPointer() const
983  {
984  return &base[0];
985  }
986 
992  static bool noPointers()
993  {
994  return false;
995  }
996 
1002  size_t getLastError()
1003  {
1004 #ifdef SE_CLASS2
1005  check_valid(this,8);
1006 #endif
1007  return err_code;
1008  }
1009 
1015  inline void vector_overflow(size_t v1) const
1016  {
1017  if (v1 >= base.size())
1018  {
1019  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
1020  size_t * err_code_pointer = (size_t *)&this->err_code;\
1021  *err_code_pointer = 2001;\
1022 
1023  ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
1024  }
1025  }
1026 
1027  /* \brief It return the id of structure in the allocation list
1028  *
1029  * \see print_alloc and SE_CLASS2
1030  *
1031  * \return the allocation id of this class
1032  *
1033  */
1034  long int who()
1035  {
1036 #ifdef SE_CLASS2
1037  return check_whoami(this,8);
1038 #else
1039  return -1;
1040 #endif
1041  }
1042 };
1043 
1044 
1045 
1046 #endif /* MAP_VECTOR_STD_HPP_ */
static size_t calculateNMem(size_t n)
How many allocation are required to create n-elements.
bool operator!=(const vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are equal.
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:93
void add_prp(const vector< S, M, typename layout_base< S >::type, layout_base, gp, impl > &v)
It add the element of a source vector to this vector.
size_t packMem(size_t n, size_t e) const
Calculate the memory size required to allocate n elements.
static void add(const vector< S, M, typename memory_traits_lin< S >::type, memory_traits_lin, gp, impl > &v_src, vect_dst &v_dst)
It add the element of a source vector to a destination vector.
struct to merge two vectors
void swap(openfpm::vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &v)
auto begin() -> decltype(base.begin())
Return an std compatible iterator to the first element.
vector_key_iterator getIteratorTo(size_t k) const
Get iterator until a specified element.
auto end() -> decltype(base.begin())
Return an std compatible iterator to the last element.
size_t size()
Stub size.
Definition: map_vector.hpp:70
auto end() const -> const decltype(base.begin())
Return an std compatible iterator to the last element.
auto begin() const -> const decltype(base.begin())
Return an std compatible iterator to the first element.
void add(const S &&v)
It insert a new object on the vector, eventually it reallocate the grid.
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
void swap(openfpm::vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v)
vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v)
Operator= copy the vector into another.
vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(const vector< T, Mem, layout, memory_traits_lin, gp, STD_VECTOR > &v)
Operator= copy the vector into another.
vector_key_iterator getIterator() const
Get an iterator over all the elements of the vector.
vector(const vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) noexcept
Constructor from another vector.
void add(const openfpm::vector< T, Mem, l, lb, gp > &eles)
add elements to the vector
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.
void add(const S &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:201
static void add(const vector< S, M, typename memory_traits_lin< S >::type, memory_traits_lin, gp, impl > &v_src, vect_dst &v_dst)
It add the element of a source vector to this vector.
vector(vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v) noexcept
Constructor from another vector.
bool operator==(const vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are not equal.
This class is a container for the memory interface like HeapMemory CudaMemory.
Definition: memory_c.hpp:31
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(vector< T, Mem, layout, memory_traits_lin, gp, STD_VECTOR > &&v)
Operator= copy the vector into another.
vector< T, HeapMemory, layout, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(const vector< T, HeapMemory, layout, memory_traits_lin, 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.