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.hpp
1 /*
2  * map_vector.hpp
3  *
4  * Created on: Aug 30, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef MAP_VECTOR_HPP
9 #define MAP_VECTOR_HPP
10 
11 #include <iostream>
12 #include <typeinfo>
13 #include "util/common.hpp"
14 #include "memory/PtrMemory.hpp"
15 #include "util/object_util.hpp"
16 #include "Grid/util.hpp"
17 #include "Vector/util.hpp"
18 #include "Vector/map_vector_grow_p.hpp"
19 #include "memory/ExtPreAlloc.hpp"
20 #include "util/util_debug.hpp"
21 #include "util/Pack_stat.hpp"
22 #include "Grid/map_grid.hpp"
23 #include "memory/HeapMemory.hpp"
24 #include "vect_isel.hpp"
25 #include "util/object_s_di.hpp"
26 #include "util.hpp"
27 #ifdef HAVE_MPI
28 #include <mpi.h>
29 #endif
30 #include "util/Pack_stat.hpp"
31 #include "memory/ExtPreAlloc.hpp"
32 #include <string.h>
33 #include "vect_isel.hpp"
34 #include "Packer_Unpacker/Unpacker.hpp"
35 #include "Packer_Unpacker/Packer.hpp"
36 #include <fstream>
37 
38 namespace openfpm
39 {
45  {
47  size_t end;
48 
49  protected:
50 
52  size_t gk;
53 
54  public:
55 
60  vector_key_iterator(size_t end, size_t start = 0)
61  : end(end),gk(start)
62  {}
63 
64 
73  {
75 
76  gk++;
77 
78  return *this;
79  }
80 
87  void set(int d, size_t sz)
88  {
89  // set the counter dim to sz
90 
91  gk = sz;
92  }
93 
101  bool isNext()
102  {
103  if (gk < end)
104  {
106 
107  return true;
108  }
109 
111  return false;
112  }
121  size_t get()
122  {
123  return gk;
124  }
125  };
126 
143  template<typename T, typename Memory, typename grow_p, unsigned int impl>
144  class vector
145  {
146  };
147 
148  #include "map_vector_std.hpp"
149 
165  template<typename T,typename Memory, typename grow_p>
166  class vector<T,Memory,grow_p,OPENFPM_NATIVE>
167  {
170 
174  size_t v_size;
175 
180  size_t err_code;
181 
191  std::vector<size_t> getV(size_t sz)
192  {
193  std::vector<size_t> tmp;
194 
195  tmp.push_back(sz);
196 
197  return tmp;
198  }
199 
206  void non_zero_one(size_t sz[1], size_t arg)
207  {
208  if (arg == 0)
209  {sz[0] = 1;}
210  else
211  {sz[0] = arg;}
212  }
213 
214  public:
215 
217  typedef int yes_i_am_vector;
218 
220  typedef typename grid_cpu<1,T>::memory_conf memory_conf;
221 
224 
226  // you can access all the properties of T
228 
230  typedef T value_type;
231 
232  // Implementation of packer and unpacker for vector
233 #include "vector_pack_unpack.ipp"
234 
241  size_t size() const
242  {
243 #ifdef SE_CLASS2
244  check_valid(this,8);
245 #endif
246  return v_size;
247  }
257  void reserve(size_t sp)
258  {
259 #ifdef SE_CLASS2
260  check_valid(this,8);
261 #endif
262  if (sp > base.size())
263  {
265  size_t sz[1] = {sp};
266  base.resize(sz);
267  }
268  }
269 
275  void clear()
276  {
277 #ifdef SE_CLASS2
278  check_valid(this,8);
279 #endif
280  resize(0);
281  }
282 
290  void resize(size_t slot)
291  {
292 #ifdef SE_CLASS2
293  check_valid(this,8);
294 #endif
295  // If we need more space than what we allocated, allocate new memory
296 
297  if (slot > base.size())
298  {
299  size_t gr = grow_p::grow(base.size(),slot);
300 
302  size_t sz[1] = {gr};
303  base.resize(sz);
304  }
305 
306  // update the vector size
307  v_size = slot;
308  }
309 
311  typedef size_t access_key;
312 
319  void add()
320  {
321 #ifdef SE_CLASS2
322  check_valid(this,8);
323 #endif
324 
326  if (v_size >= base.size())
327  {
329  size_t sz[1];
330  non_zero_one(sz,2*base.size());
331  base.resize(sz);
332  }
333 
335  v_size++;
336  }
337 
346  void add(const T & v)
347  {
348 #ifdef SE_CLASS2
349  check_valid(this,8);
350 #endif
351 
353  if (v_size >= base.size())
354  {
356  size_t sz[1];
357  non_zero_one(sz,2*base.size());
358  base.resize(sz);
359  }
360 
362  base.set(v_size,v);
363 
365  v_size++;
366  }
367 
377  void add(const typename grid_cpu<1,T>::container & v)
378  {
379 #ifdef SE_CLASS2
380  check_valid(this,8);
381 #endif
382 
384  if (v_size >= base.size())
385  {
387  size_t sz[1];
388  non_zero_one(sz,2*base.size());
389  base.resize(sz);
390  }
391 
393  base.set(v_size,v);
394 
396  v_size++;
397  }
398 
404  template <typename M, typename gp> void add(const vector<T, M,gp,OPENFPM_NATIVE> & v)
405  {
406 #ifdef SE_CLASS2
407  check_valid(this,8);
408 #endif
409  for (size_t i = 0 ; i < v.size() ; i++)
411  add(v.get(i));
412  }
413 
428  template <typename S, typename M, typename gp, unsigned int ...args> void add_prp(const vector<S, M,gp,OPENFPM_NATIVE> & v)
429  {
430 #ifdef SE_CLASS2
431  check_valid(this,8);
432 #endif
433  for (size_t i = 0 ; i < v.size() ; i++)
435  {
436  // Add a new element
437  add();
438 
439  // write the object in the last element
440  object_s_di<decltype(v.get(i)),decltype(get(size()-1)),OBJ_ENCAP,args...>(v.get(i),get(size()-1));
441  }
442  }
443 
449  void remove(size_t key)
450  {
451 #ifdef SE_CLASS2
452  check_valid(this,8);
453 #endif
454  size_t d_k = key;
455  size_t s_k = key + 1;
456 
457  // keys
458  while (s_k < size())
459  {
460  set(d_k,get(s_k));
461  d_k++;
462  s_k++;
463  }
464 
465  // re-calculate the vector size
466 
467  v_size--;
468  }
469 
476  void remove(openfpm::vector<size_t> & keys, size_t start = 0)
477  {
478 #ifdef SE_CLASS2
479  check_valid(this,8);
480 #endif
481  // Nothing to remove return
482  if (keys.size() <= start )
483  return;
484 
485  size_t a_key = start;
486  size_t d_k = keys.get(a_key);
487  size_t s_k = keys.get(a_key) + 1;
488 
489  // keys
490  while (s_k < size())
491  {
492  // s_k should always point to a key that is not going to be deleted
493  while (a_key+1 < keys.size() && s_k == keys.get(a_key+1))
494  {
495  a_key++;
496  s_k = keys.get(a_key) + 1;
497  }
498 
499  // In case of overflow
500  if (s_k >= size())
501  break;
502 
503  set(d_k,get(s_k));
504  d_k++;
505  s_k++;
506  }
507 
508  // re-calculate the vector size
509 
510  v_size -= keys.size() - start;
511  }
512 
524  template <unsigned int p>inline const typename type_cpu_prop<p,memory_lin>::type & get(size_t id) const
525  {
526 #ifdef SE_CLASS2
527  check_valid(this,8);
528 #endif
529 #ifdef SE_CLASS1
530  VECTOR_OVERFLOW_NATIVE(id)
531 #endif
532  grid_key_dx<1> key(id);
533 
534  return base.template get<p>(key);
535  }
536 
547  inline const typename grid_cpu<1,T>::container get(size_t id) const
548  {
549 #ifdef SE_CLASS2
550  check_valid(this,8);
551 #endif
552 #ifdef SE_CLASS1
553  VECTOR_OVERFLOW_NATIVE(id)
554 #endif
555  grid_key_dx<1> key(id);
556 
557  return base.get_o(key);
558  }
559 
566  inline const typename grid_cpu<1,T>::container last() const
567  {
568 #ifdef SE_CLASS2
569  check_valid(this,8);
570 #endif
571  grid_key_dx<1> key(size()-1);
572 
573  return base.get_o(key);
574  }
587  template <unsigned int p>inline typename type_cpu_prop<p,memory_lin>::type & get(size_t id)
588  {
589 #ifdef SE_CLASS2
590  check_valid(this,8);
591 #endif
592 #ifdef SE_CLASS1
593  VECTOR_OVERFLOW_NATIVE(id)
594 #endif
595  grid_key_dx<1> key(id);
596 
597  return base.template get<p>(key);
598  }
599 
610  inline typename grid_cpu<1,T>::container get(size_t id)
611  {
612 #ifdef SE_CLASS2
613  check_valid(this,8);
614 #endif
615 #ifdef SE_CLASS1
616  VECTOR_OVERFLOW_NATIVE(id)
617 #endif
618  grid_key_dx<1> key(id);
620  return base.get_o(key);
621  }
622 
629  inline typename grid_cpu<1,T>::container last()
630  {
631 #ifdef SE_CLASS2
632  check_valid(this,8);
633 #endif
634  grid_key_dx<1> key(size()-1);
635 
636  return base.get_o(key);
637  }
638 
640  ~vector() THROW
641  {
642  // Eliminate the pointer
643  #ifdef SE_CLASS2
644  check_delete(this);
645  #endif
646  }
647 
654  {
655 #ifdef SE_CLASS2
656  check_valid(this,8);
657 #endif
659 
660  dup.v_size = v_size;
661  dup.base.swap(base.duplicate());
662 
663  return dup;
664  }
665 
672  :v_size(0),err_code(0)
673  {
674  // Add this pointer
675 #ifdef SE_CLASS2
676  check_new(this,8,VECTOR_EVENT,1);
677 #endif
678  swap(v);
679  }
680 
687  :v_size(0),err_code(0)
688  {
689 #ifdef SE_CLASS2
690  check_new(this,8,VECTOR_EVENT,1);
691 #endif
692  swap(v.duplicate());
693  }
694 
696  vector() THROW
697  :v_size(0),base(getV(0)),err_code(0)
698  {
699 #ifdef SE_CLASS2
700  check_new(this,8,VECTOR_EVENT,1);
701 #endif
702  base.setMemory();
703  }
704 
706  vector(size_t sz) THROW
707  :v_size(sz),base(getV(sz)),err_code(0)
708  {
709 #ifdef SE_CLASS2
710  check_new(this,8,VECTOR_EVENT,1);
711 #endif
712  base.setMemory();
713  }
714 
721  void set(size_t id, const typename grid_cpu<1,T>::container & obj)
722  {
723 #ifdef SE_CLASS2
724  check_valid(this,8);
725 #endif
726 #ifdef SE_CLASS1
727  VECTOR_OVERFLOW_NATIVE(id)
728 #endif
729  base.set(id,obj);
731  }
732 
739  void set(size_t id, const T & obj)
740  {
741 #ifdef SE_CLASS2
742  check_valid(this,8);
743 #endif
744 #ifdef SE_CLASS1
745  VECTOR_OVERFLOW_NATIVE(id)
746 #endif
747  base.set(id,obj);
749  }
750 
758  void set(size_t id, vector<T,Memory,grow_p,OPENFPM_NATIVE> & v, size_t src)
759  {
760 #ifdef SE_CLASS2
761  check_valid(this,8);
762 #endif
763 #ifdef SE_CLASS1
764  VECTOR_OVERFLOW_NATIVE(id)
765 #endif
766  base.set(id,v.base,src);
767  }
768 
779  {
780 #ifdef SE_CLASS2
781  check_valid(this,8);
782 #endif
783  v_size = mv.v_size;
784  base.swap(mv.base);
785 
786  return *this;
787  }
788 
799  {
800 #ifdef SE_CLASS2
801  check_valid(this,8);
802 #endif
803  v_size = mv.v_size;
804  size_t rsz[1] = {v_size};
805  base.resize(rsz);
806 
807  // copy the object
808  for (size_t i = 0 ; i < v_size ; i++ )
809  {
810  grid_key_dx<1> key(i);
811  base.set(key,mv.base,key);
812  }
813 
814  return *this;
815  }
816 
823  {
824  return !this->operator==(v);
825  }
826 
833  {
834  if (v_size != v.v_size)
835  return false;
836 
837  // check object by object
838  for (size_t i = 0 ; i < v_size ; i++ )
839  {
840  grid_key_dx<1> key(i);
841 
842  if (base.get_o(key) != v.base.get_o(key))
843  return false;
844  }
845 
846  return true;
847  }
848 
855  {
856 #ifdef SE_CLASS2
857  check_valid(this,8);
858 #endif
859  size_t sz_sp = v_size;
860 
861  // swap the v_size
862  v_size = v.v_size;
863 
864  base.swap(v.base);
865  v.v_size = sz_sp;
866  }
867 
874  {
875 #ifdef SE_CLASS2
876  check_valid(this,8);
877 #endif
878  size_t sz_sp = v_size;
879 
880  // swap the v_size
881  v_size = v.v_size;
882 
883  base.swap(v.base);
884  v.v_size = sz_sp;
885  }
886 
893  {
894 #ifdef SE_CLASS2
895  check_valid(this,8);
896 #endif
897  return vector_key_iterator(v_size,mark);
898  }
899 
908  {
909 #ifdef SE_CLASS2
910  check_valid(this,8);
911 #endif
912  return vector_key_iterator(v_size);
913  }
914 
921  size_t packObjectSize()
922  {
923 #ifdef SE_CLASS2
924  check_valid(this,8);
925 #endif
926  return base.packObjectSize();
927  }
928 
936  size_t packObject(void * mem)
937  {
938 #ifdef SE_CLASS2
939  check_valid(this,8);
940 #endif
941  return base.packObject(mem);
942  }
943 
954  template<int ... prp> static inline size_t calculateMem(size_t n, size_t e)
955  {
956  if (n == 0)
957  {
958  return 0;
959  }
960  else
961  {
962  if (sizeof...(prp) == 0)
963  return grow_p::grow(0,n) * sizeof(typename T::type);
964 
965  typedef object<typename object_creator<typename T::type,prp...>::type> prp_object;
966 #ifdef DEBUG
967  std::cout << "Inside calculateMem() (map_vector)" << std::endl;
968 #endif
969  return grow_p::grow(0,n) * sizeof(prp_object);
970  }
971  }
972 
983  template<int ... prp> static inline size_t packMem(size_t n, size_t e)
984  {
985  if (sizeof...(prp) == 0)
986  return n * sizeof(typename T::type);
987 
988  typedef object<typename object_creator<typename T::type,prp...>::type> prp_object;
989 #ifdef DEBUG
990  std::cout << "Inside packMem() (map_vector)" << std::endl;
991 #endif
992  return n * sizeof(prp_object);
993  }
994 
1002  inline static size_t calculateNMem(size_t n)
1003  {
1004  return 1;
1005  }
1006 
1012  void setMemory(Memory & mem)
1013  {
1014 #ifdef SE_CLASS2
1015  check_valid(this,8);
1016 #endif
1017  base.setMemory(mem);
1018  }
1019 
1025  void * getPointer()
1026  {
1027 #ifdef SE_CLASS2
1028  check_valid(this,8);
1029 #endif
1030  return base.getPointer();
1031  }
1032 
1038  const void * getPointer() const
1039  {
1040 #ifdef SE_CLASS2
1041  check_valid(this,8);
1042 #endif
1043  return base.getPointer();
1044  }
1045 
1049  size_t getLastError()
1050  {
1051 #ifdef SE_CLASS2
1052  check_valid(this,8);
1053 #endif
1054  return err_code;
1055  }
1056 
1062  static bool noPointers()
1063  {
1064  return false;
1065  }
1066 
1067  /* \brief It return the id of structure in the allocation list
1068  *
1069  * \see print_alloc and SE_CLASS2
1070  *
1071  */
1072  long int who()
1073  {
1074 #ifdef SE_CLASS2
1075  return check_whoami(this,8);
1076 #else
1077  return -1;
1078 #endif
1079  }
1080  };
1081 
1082  template <typename T> using vector_std = vector<T, HeapMemory, openfpm::grow_policy_double, STD_VECTOR>;
1083 
1084 }
1085 
1086 #endif
static size_t packMem(size_t n, size_t e)
Calculate the memory size required to pack n elements.
Definition: map_vector.hpp:983
vector_key_iterator getIterator() const
Get the vector elements iterator.
Definition: map_vector.hpp:907
It copy the properties from one object to another.
std::vector< size_t > getV(size_t sz)
Create a 1D vector that contain the vector size.
Definition: map_vector.hpp:191
vector< T, Memory, grow_p, OPENFPM_NATIVE > & operator=(const vector< T, Memory, grow_p, OPENFPM_NATIVE > &mv)
Assignment operator.
Definition: map_vector.hpp:798
void reserve(size_t sp)
Reserve slots in the vector to avoid reallocation.
Definition: map_vector.hpp:257
vector() THROW
Constructor, vector of size 0.
Definition: map_vector.hpp:696
const void * getPointer() const
Return the pointer that store the data.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
grid_cpu< 1, T >::memory_conf memory_conf
Type of the encapsulation memory parameter.
Definition: map_vector.hpp:220
vector_key_iterator operator++()
Get the next element.
Definition: map_vector.hpp:72
vector(vector< T, Memory, grow_p, OPENFPM_NATIVE > &&v)
Constructor from another temporal vector.
Definition: map_vector.hpp:671
void setMemory(Memory &mem)
Set the memory of the base structure using an object.
void add(const typename grid_cpu< 1, T >::container &v)
It insert a new object on the vector, eventually it reallocate the vector.
Definition: map_vector.hpp:377
size_t end
Linearized end element.
Definition: map_vector.hpp:47
size_t packObject(void *mem)
Pack the object into the given pointer.
Definition: map_vector.hpp:936
void swap(openfpm::vector< T, Memory, grow_p, OPENFPM_NATIVE > &&v)
Swap the memory with another vector.
Definition: map_vector.hpp:873
vector_key_iterator getIteratorFrom(size_t mark) const
Get iterator over the particles from a particular index.
Definition: map_vector.hpp:892
vector< T, Memory, grow_p, OPENFPM_NATIVE > & operator=(vector< T, Memory, grow_p, OPENFPM_NATIVE > &&mv)
Assignment operator.
Definition: map_vector.hpp:778
vector_key_iterator(size_t end, size_t start=0)
Constructor require the size of the vector.
Definition: map_vector.hpp:60
void set(int d, size_t sz)
Set the dimension.
Definition: map_vector.hpp:87
vector(const vector< T, Memory, grow_p, OPENFPM_NATIVE > &v) THROW
Constructor from another constant vector.
Definition: map_vector.hpp:686
size_t access_key
Access key for the vector.
Definition: map_vector.hpp:311
int yes_i_am_vector
it define that it is a vector
Definition: map_vector.hpp:217
vector(size_t sz) THROW
Constructor, vector of size sz.
Definition: map_vector.hpp:706
bool isNext()
Check if there is the next element.
Definition: map_vector.hpp:101
encapc< dim, T, Mem > get_o(const grid_key_dx< dim > &v1)
Get the of the selected element as a boost::fusion::vector.
Definition: map_grid.hpp:858
static size_t calculateMem(size_t n, size_t e)
Calculate the memory size required to allocate n elements.
Definition: map_vector.hpp:954
bool operator!=(const vector< T, Memory, grow_p, OPENFPM_NATIVE > &v) const
Check that two vectors are equal.
Definition: map_vector.hpp:822
grid_cpu< 1, T, Memory > base
1-D static grid
Definition: map_vector.hpp:177
grid_cpu< 1, T >::container container
Object container for T, it is the return type of get_o it return a object type trough.
Definition: map_vector.hpp:227
size_t packObjectSize()
Return the size of the message needed to pack this object.
Definition: map_vector.hpp:921
size_t size() const
Return the size of the vector.
Definition: map_vector.hpp:241
void swap(openfpm::vector< T, Memory, grow_p, OPENFPM_NATIVE > &v)
Swap the memory with another vector.
Definition: map_vector.hpp:854
size_t gk
Actual key.
Definition: map_vector.hpp:52
void add(const vector< T, M, gp, OPENFPM_NATIVE > &v)
It add the element of another vector to this vector.
Definition: map_vector.hpp:404
static bool noPointers()
This class has pointer inside.
void * getPointer()
Return the pointer that store the data.
void add_prp(const vector< S, M, gp, OPENFPM_NATIVE > &v)
It add the element of a source vector to this vector.
Definition: map_vector.hpp:428
void set(size_t id, vector< T, Memory, grow_p, OPENFPM_NATIVE > &v, size_t src)
Set the element of the vector v from another element of another vector.
Definition: map_vector.hpp:758
static size_t calculateNMem(size_t n)
How many allocation are required to create n-elements.
T value_type
Type of the value the vector is storing.
Definition: map_vector.hpp:230
this structure encapsulate an object of the grid
Definition: Encap.hpp:209
void set(size_t id, const typename grid_cpu< 1, T >::container &obj)
Set the object id to obj.
Definition: map_vector.hpp:721
bool operator==(const vector< T, Memory, grow_p, OPENFPM_NATIVE > &v) const
Check that two vectors are not equal.
Definition: map_vector.hpp:832
void swap(grid_cpu< dim, T, S, Mem > &grid)
It move the allocated object from one grid to another.
Definition: map_grid.hpp:1006
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:202
grid_cpu< 1, T, Memory >::memory_lin memory_lin
This structure use this layout.
Definition: map_vector.hpp:169
vector< T, Memory, grow_p, OPENFPM_NATIVE > duplicate() const
It duplicate the vector.
Definition: map_vector.hpp:653
void non_zero_one(size_t sz[1], size_t arg)
If the argument is zero return 1 otherwise return the argument.
Definition: map_vector.hpp:206
vector_key_iterator iterator_key
iterator for the vector
Definition: map_vector.hpp:223
It create a boost::fusion vector with the selected properties.
void set(size_t id, const T &obj)
Set the object id to obj.
Definition: map_vector.hpp:739
grid_cpu< 1, T >::container last()
Get the last element of the vector.
Definition: map_vector.hpp:629
boost::fusion::result_of::at< vtype, boost::mpl::int_< p > >::type type
return a memory_c<...>
Definition: Encap.hpp:170
const grid_cpu< 1, T >::container last() const
Get the last element of the vector.
Definition: map_vector.hpp:566
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
Definition: map_vector.hpp:346
Implementation of 1-D std::vector like structure.
Definition: map_grid.hpp:94
void resize(size_t slot)
Resize the vector.
Definition: map_vector.hpp:290
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:166
void add()
It insert a new emtpy object on the vector, eventually it reallocate the grid.
Definition: map_vector.hpp:319