OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
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>
37  inline static void add(const vector<S,M,memory_traits_lin,gp,impl> & v_src, vect_dst & v_dst)
38  {
40  for (size_t i = 0 ; i < v_src.size() ; i++)
41  {
42  // Add a new element
43  v_dst.add();
44 
45  // equal object
46  v_dst.get(v_dst.size()-1) = v_src.get(i);
47  }
48  }
49 };
50 
52 template<typename vect_dst>
53 struct add_prp_impl<OBJECT_ADD,vect_dst>
54 {
70  template <typename S, typename M, typename gp, unsigned int impl, unsigned int ...args> inline static void add(const vector<S,M,memory_traits_lin,gp,impl> & v_src, vect_dst & v_dst)
71  {
72  // Add a new element
73  v_dst.add();
74 
75  // equal object
76  v_dst.get(v_dst.size()-1) = v_src;
77  }
78 };
79 
80 
92 template<typename T, typename grow_p>
93 class vector<T,HeapMemory,memory_traits_lin,grow_p,STD_VECTOR>
94 {
95  // Memory layout
96  typedef typename memory_traits_lin<T>::type layout;
97 // Doeas not work on gcc 4.8.4
98 // template <typename lb> using layout_base = memory_traits_lin<lb>;
99 
101  std::vector<T> base;
104  size_t err_code = 0;
105 
106 public:
107 
109  typedef int yes_i_am_vector;
110 
112 
114  typedef vector_key_iterator iterator_key;
116  typedef T value_type;
117 
118  typedef void base_to_copy;
119 
121  typedef grow_policy_double grow_policy;
122 
123  template<typename Tobj>
124  struct layout_base__
125  {
127  };
128 
129  //This file implements a pack and unpack for std vector
130 #include "vector_std_pack_unpack.ipp"
131 
133  inline size_t size() const
134  {
135  return base.size();
136  }
137 
138 
144  inline void resize(size_t slot)
145  {
146  base.resize(slot);
147  }
148 
152  inline void clear()
153  {
154  base.clear();
155  }
156 
160  inline void shrink_to_fit()
161  {
162  base.shrink_to_fit();
163  }
164 
174  inline void add(const T & v)
175  {
176  if (std::is_same<grow_p,openfpm::grow_policy_identity>::value == true)
177  {
178  // we reserve just one space more to avoid the capacity to increase by two
179  base.reserve(base.size()+1);
180  }
181 
182  base.push_back(v);
183  }
184 
194  inline void add(T && v)
195  {
196  if (std::is_same<grow_p,openfpm::grow_policy_identity>::value == true)
197  {
198  // we reserve just one space more to avoid the capacity to increase by two
199  base.reserve(base.size()+1);
200  }
201 
202  base.emplace_back(v);
203  }
204 
208  inline void add()
209  {
210  base.emplace_back(T());
211  }
212 
218  template<typename Mem,template<typename> class lb,typename gp> inline void add(const openfpm::vector<T,Mem,lb,gp> & eles)
219  {
220  if (std::is_same<grow_p,openfpm::grow_policy_identity>::value == true)
221  {
222  // we reserve just one space more to avoid the capacity to increase by two
223  base.reserve(base.size() + eles.size());
224  }
225 
226  size_t start = base.size();
227  base.resize(base.size() + eles.size());
228 
229  // copy the elements
230  std::copy(eles.begin(),eles.end(),base.begin()+start);
231  }
232 
242  template<typename S> inline void add(const S & v)
243  {
245  }
246 
256  template<typename S> inline void add(const S && v)
257  {
258  if (std::is_same<grow_p,openfpm::grow_policy_identity>::value == true)
259  {
260  // we reserve just one space more to avoid the capacity to increase by two
261  base.reserve(base.size() + 1);
262  }
263 
264  base.push_back(v);
265  }
266 
281  template <typename S,
282  typename M,
283  typename gp,
284  unsigned int impl,
285  template <typename> class layout_base,
286  unsigned int ...args>
287  void add_prp(const vector<S,M,layout_base,gp,impl> & v)
288  {
289  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);
290  }
291 
306  template <typename S,
307  typename M,
308  typename gp,
309  unsigned int impl,
310  template <typename> class layout_base,
311  unsigned int ...args>
312  void add_prp_device(const vector<S,M,layout_base,gp,impl> & v)
313  {
314  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);
315  }
316 
330  template <typename S,
331  typename M,
332  typename gp,
333  unsigned int impl,
334  template <typename> class layout_base,
335  unsigned int ...args>
336  void add_prp(const T & v)
337  {
338  add(v);
339  }
340 
347  void erase(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)
348  {
349  base.erase(start,end);
350  }
351 
357  void remove(size_t key)
358  {
359 #ifdef SE_CLASS1
360  vector_overflow(key);
361 #endif
362  base.erase(base.begin() + key);
363  }
364 
373  void remove(openfpm::vector<size_t> & keys, size_t start = 0)
374  {
375  // Nothing to remove return
376  if (keys.size() <= start )
377  return;
378 
379  size_t a_key = start;
380  size_t d_k = keys.get(a_key);
381  size_t s_k = keys.get(a_key) + 1;
382 
383  // keys
384  while (s_k < size())
385  {
386  // s_k should always point to a key that is not going to be deleted
387  while (a_key+1 < keys.size() && s_k == keys.get(a_key+1))
388  {
389  a_key++;
390  s_k = keys.get(a_key) + 1;
391  }
392 
393  // In case of overflow
394  if (s_k >= size())
395  break;
396 
397  base[d_k] = base[s_k];
398  d_k++;
399  s_k++;
400  }
401 
402  // re-calculate the vector size
403 
404  base.resize(base.size() - (keys.size() - start));
405  }
406 
412  inline auto begin() -> decltype(base.begin())
413  {
414  return base.begin();
415  }
416 
422  inline auto end() -> decltype(base.begin())
423  {
424  return base.end();
425  }
426 
432  inline auto begin() const -> const decltype(base.begin())
433  {
434  return base.begin();
435  }
436 
442  inline auto end() const -> const decltype(base.begin())
443  {
444  return base.end();
445  }
446 
452  inline T & last()
453  {
454 #ifdef SE_CLASS1
455  if (base.size() == 0)
456  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
457 #endif
458  return base[base.size()-1];
459  }
460 
466  inline const T & last() const
467  {
468 #ifdef SE_CLASS1
469  if (base.size() == 0)
470  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
471 #endif
472  return base[base.size()-1];
473  }
474 
481  {
482  return *this;
483  }
484 
490  void swap(std::vector<T> && v)
491  {
492  base.swap(v);
493  }
494 
500  void unique()
501  {
502  auto it = std::unique(base.begin(),base.end());
503  base.resize( std::distance(base.begin(),it) );
504  }
505 
511  void sort()
512  {
513  std::sort(base.begin(), base.end());
514  }
515 
525  template <unsigned int p>inline T& get(size_t id)
526  {
527 #ifdef SE_CLASS1
528  if (p != 0)
529  {std::cerr << "Error the property does not exist" << "\n";}
530 
531  vector_overflow(id);
532 #endif
533 
534  return base[id];
535  }
536 
546  template <unsigned int p>inline const T& get(size_t id) const
547  {
548 #ifdef SE_CLASS1
549  if (p != 0)
550  {std::cerr << "Error the property does not exist" << "\n";}
551 
552  vector_overflow(id);
553 #endif
554 
555  return base[id];
556  }
557 
565  inline T & get(size_t id)
566  {
567 #ifdef SE_CLASS1
568  vector_overflow(id);
569 #endif
570  return base[id];
571  }
572 
580  inline const T & get(size_t id) const
581  {
582 #ifdef SE_CLASS1
583  vector_overflow(id);
584 #endif
585  return base[id];
586  }
587 
596  inline void fill(unsigned char fl)
597  {
598  memset(&base[0],fl,base.size() * sizeof(T));
599  }
600 
606  inline void reserve(size_t ns)
607  {
608  base.reserve(ns);
609  }
610 
612  vector() noexcept
613  :err_code(0)
614  {
615  }
616 
618  vector(size_t sz) noexcept
619  :base(sz),err_code(0)
620  {
621  }
622 
624  vector(const vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & v) noexcept
625  :err_code(0)
626  {
627  base = v.base;
628  }
629 
635  vector(const std::initializer_list<T> & v)
636  :base(v)
637  {
638  }
639 
641  vector(vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> && v) noexcept
642  :err_code(0)
643  {
644  base.swap(v.base);
645  }
646 
648  ~vector() noexcept
649  {
650  }
651 
658  {
659  base.swap(v.base);
660  }
661 
668  {
669  base.swap(v.base);
670  }
671 
679  vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(const vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & v)
680  {
681  base = v.base;
682 
683  return *this;
684  }
685 
691  template<typename Mem, typename gp> vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(const vector<T,Mem,memory_traits_lin,gp,STD_VECTOR> & v)
692  {
694  decltype(*this),
695  vector<T,Mem,memory_traits_lin,gp,STD_VECTOR> >::copy(*this,v);
696 
697  return *this;
698  }
699 
707  vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> && v)
708  {
709  base.swap(v.base);
710 
711  return *this;
712  }
713 
721  template<typename Mem, typename gp> vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(vector<T,Mem,memory_traits_lin,gp,STD_VECTOR> && v)
722  {
723  base.swap(v.base);
724 
725  return *this;
726  }
727 
735  bool operator!=(const vector<T, HeapMemory, memory_traits_lin,grow_policy_double,STD_VECTOR> & v) const
736  {
737  return base != v.base;
738  }
739 
747  bool operator==(const vector<T, HeapMemory, memory_traits_lin,grow_policy_double,STD_VECTOR> & v) const
748  {
749  return base == v.base;
750  }
751 
757  vector_key_iterator getIterator() const
758  {
759  return vector_key_iterator(base.size());
760  }
761 
769  vector_key_iterator getIteratorTo(size_t k) const
770  {
771  return vector_key_iterator(k);
772  }
773 
784  template<int ... prp> inline size_t packMem(size_t n, size_t e) const
785  {
786  if (n == 0)
787  return 0;
788  else
789  {
790  packMem_cond<has_packMem<T>::type::value, openfpm::vector<T, HeapMemory, memory_traits_lin, grow_policy_double>, prp...> cm;
791  return cm.packMemory(*this,n,0);
792  }
793  }
794 
805  inline static size_t calculateMemDummy(size_t n, size_t e)
806  {
807  return n*sizeof(T);
808  }
809 
817  inline static size_t calculateNMem(size_t n)
818  {
819 
820  return 1;
821  }
822 
828  void * getPointer()
829  {
830  return &base[0];
831  }
832 
836  template<unsigned int ... prp> void hostToDevice()
837  {}
838 
842  template<unsigned int ... prp> void deviceToHost()
843  {}
844 
849  template<unsigned int ... prp> void deviceToHost(size_t start, size_t stop)
850  {}
851 
856  template<unsigned int ... prp> void hostToDevice(size_t start, size_t stop)
857  {}
858 
864  const void * getPointer() const
865  {
866  return &base[0];
867  }
868 
874  static bool noPointers()
875  {
876  return false;
877  }
878 
884  size_t getLastError()
885  {
886  return err_code;
887  }
888 
894  inline void vector_overflow(size_t v1) const
895  {
896  if (v1 >= base.size())
897  {
898  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
899  size_t * err_code_pointer = (size_t *)&this->err_code;\
900  *err_code_pointer = 2001;\
901 
902  ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
903  }
904  }
905 };
906 
919 template<typename T>
921 :private vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR>
922 {
923  typedef vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> base_type;
924 
926  size_t v_size = 0;
927 
928 public:
929 
935  size_t size()
936  {
937  return v_size;
938  }
939 
945  size_t size_base()
946  {
947  return base_type::size();
948  }
949 
955  void resize(size_t sz)
956  {
957  if (sz <= base_type::size())
958  {
959  v_size = sz;
960  return;
961  }
962 
963  base_type::resize(sz);
964 
965  v_size = sz;
966  }
967 
972  void clear()
973  {
974  resize(0);
975  }
976 
981  void add()
982  {
983  resize(v_size+1);
984  }
985 
991  void resize_base(size_t sz)
992  {
993  base_type::resize(sz);
994  v_size = sz;
995  }
996 
1004  inline T & get(size_t id)
1005  {
1006  return base_type::get(id);
1007  }
1008 
1016  inline T & operator[](size_t id)
1017  {
1018  return base_type::get(id);
1019  }
1020 
1028  inline T & last()
1029  {
1030  return base_type::get(size()-1);
1031  }
1032 
1039  {
1040  size_t v_size_tmp = v.v_size;
1041  v.v_size = v_size;
1042  v_size = v_size_tmp;
1043 
1044  base_type::swap(v);
1045  }
1046 };
1047 
1048 #endif /* MAP_VECTOR_STD_HPP_ */
vector(vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v) noexcept
Constructor from another vector.
void swap(openfpm::vector_fr< T > &v)
void * getPointer()
Return the pointer to the chunk of memory.
void clear()
Remove all the element from the vector.
static size_t calculateMemDummy(size_t n, size_t e)
Calculate the memory size required to allocate n elements.
vector_key_iterator iterator_key
iterator for the vector
void add_prp(const T &v)
It add the element it is equivalent to add.
Transform the boost::fusion::vector into memory specification (memory_traits)
static void add(const vector< S, M, memory_traits_lin, gp, impl > &v_src, vect_dst &v_dst)
It add the element of a source vector to a destination vector.
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< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v)
Operator= copy the vector into another.
void swap(openfpm::vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v)
size_t size()
return the size of the vector
struct to merge two vectors
openfpm::vector< T > duplicate() const
Duplicate the vector.
Implementation of 1-D std::vector like structure.
size_t packMem(size_t n, size_t e) const
Calculate the memory size required to allocate n elements.
void add_prp(const vector< S, M, layout_base, gp, impl > &v)
It add the element of a source vector to this vector.
T & last()
Get an element of the vector.
size_t size() const
return the size of the vector
vector_key_iterator getIterator() const
Get an iterator over all the elements of the vector.
vector(size_t sz) noexcept
Constructor, vector of size sz.
static bool noPointers()
This class has pointer inside.
size_t size()
Stub size.
Definition: map_vector.hpp:211
T value_type
Type of the value the vector is storing.
const T & get(size_t id) const
Get an element of the vector.
void shrink_to_fit()
Fit the memory to the size of the vector.
bool operator==(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are not equal.
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
static void add(const vector< S, M, memory_traits_lin, gp, impl > &v_src, vect_dst &v_dst)
It add the element of a source vector to this vector.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:920
void remove(size_t key)
Remove one entry from the vector.
T & operator[](size_t id)
Get an element of the vector.
bool operator!=(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are equal.
vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v)
Operator= copy the vector into another.
void reserve(size_t ns)
reserve a memory space in advance to avoid reallocation
void clear()
Eliminate all elements.
void swap(std::vector< T > &&v)
swap the memory between the two vector
auto begin() const -> const decltype(base.begin())
Return an std compatible iterator to the first element.
auto end() const -> const decltype(base.begin())
Return an std compatible iterator to the last element.
void remove(openfpm::vector< size_t > &keys, size_t start=0)
Remove several entries from the vector.
void add(const S &v)
It insert a new object on the vector, eventually it reallocate the object.
vector(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) noexcept
Constructor from another vector.
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
static size_t calculateNMem(size_t n)
How many allocation are required to create n-elements.
T & get(size_t id)
Get an element of the vector.
const void * getPointer() const
Return the pointer to the chunk of memory.
void fill(unsigned char fl)
it fill all the memory of fl patterns
size_t size_base()
return the base size of the vector
size_t v_size
size of the vector
void add_prp_device(const vector< S, M, layout_base, gp, impl > &v)
It add the element of a source vector to this vector.
void add()
Add another element.
vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(vector< T, Mem, memory_traits_lin, gp, STD_VECTOR > &&v)
Operator= copy the vector into another.
void hostToDevice(size_t start, size_t stop)
Do nothing.
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > & operator=(const vector< T, Mem, memory_traits_lin, gp, STD_VECTOR > &v)
Operator= copy the vector into another.
void add(const S &&v)
It insert a new object on the vector, eventually it reallocate the grid.
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.
void resize(size_t sz)
resize the vector retaining the objects
void deviceToHost(size_t start, size_t stop)
Do nothing.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
void resize_base(size_t sz)
resize the base vector (this kill the objects)
grow_policy_double grow_policy
growing policy of this vector
void swap(openfpm::vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v)
vector(const std::initializer_list< T > &v)
Initializer from constructor.
const T & get(size_t id) const
Get an element of the vector.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:202
void add(T &&v)
It insert a new object on the vector, eventually it reallocate the grid.
void add(const openfpm::vector< T, Mem, lb, gp > &eles)
add elements to the vector