OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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
18template<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
52template<typename vect_dst>
53struct 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
92template<typename T, typename grow_p>
93class 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;
102
104 size_t err_code = 0;
105
106public:
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 template< class InputIt >
649 vector(InputIt first, InputIt last)
650 {
651 while(first != last) {
652 add(*first);
653 ++first;
654 }
655 }
656
658 ~vector() noexcept
659 {
660 }
661
668 {
669 base.swap(v.base);
670 }
671
678 {
679 base.swap(v.base);
680 }
681
689 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)
690 {
691 base = v.base;
692
693 return *this;
694 }
695
701 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)
702 {
704 decltype(*this),
705 vector<T,Mem,memory_traits_lin,gp,STD_VECTOR> >::copy(*this,v);
706
707 return *this;
708 }
709
717 vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> & operator=(vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> && v)
718 {
719 base.swap(v.base);
720
721 return *this;
722 }
723
731 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)
732 {
733 base.swap(v.base);
734
735 return *this;
736 }
737
745 bool operator!=(const vector<T, HeapMemory, memory_traits_lin,grow_policy_double,STD_VECTOR> & v) const
746 {
747 return base != v.base;
748 }
749
757 bool operator==(const vector<T, HeapMemory, memory_traits_lin,grow_policy_double,STD_VECTOR> & v) const
758 {
759 return base == v.base;
760 }
761
767 vector_key_iterator getIterator() const
768 {
769 return vector_key_iterator(base.size());
770 }
771
779 vector_key_iterator getIteratorTo(size_t k) const
780 {
781 return vector_key_iterator(k);
782 }
783
794 template<int ... prp> inline size_t packMem(size_t n, size_t e) const
795 {
796 if (n == 0)
797 return 0;
798 else
799 {
800 packMem_cond<has_packMem<T>::type::value, openfpm::vector<T, HeapMemory, memory_traits_lin, grow_policy_double>, prp...> cm;
801 return cm.packMemory(*this,n,0);
802 }
803 }
804
815 inline static size_t calculateMemDummy(size_t n, size_t e)
816 {
817 return n*sizeof(T);
818 }
819
827 inline static size_t calculateNMem(size_t n)
828 {
829
830 return 1;
831 }
832
838 void * getPointer()
839 {
840 return &base[0];
841 }
842
846 template<unsigned int ... prp> void hostToDevice()
847 {}
848
852 template<unsigned int ... prp> void deviceToHost()
853 {}
854
859 template<unsigned int ... prp> void deviceToHost(size_t start, size_t stop)
860 {}
861
866 template<unsigned int ... prp> void hostToDevice(size_t start, size_t stop)
867 {}
868
874 const void * getPointer() const
875 {
876 return &base[0];
877 }
878
884 static bool noPointers()
885 {
886 return false;
887 }
888
895 {
896 return err_code;
897 }
898
904 inline void vector_overflow(size_t v1) const
905 {
906 if (v1 >= base.size())
907 {
908 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
909 size_t * err_code_pointer = (size_t *)&this->err_code;\
910 *err_code_pointer = 2001;\
911
912 ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
913 }
914 }
915};
916
929template<typename T>
931:private vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR>
932{
933 typedef vector<T,HeapMemory,memory_traits_lin,grow_policy_double,STD_VECTOR> base_type;
934
936 size_t v_size = 0;
937
938public:
939
945 size_t size()
946 {
947 return v_size;
948 }
949
955 size_t size_base()
956 {
957 return base_type::size();
958 }
959
965 void resize(size_t sz)
966 {
967 if (sz <= base_type::size())
968 {
969 v_size = sz;
970 return;
971 }
972
973 base_type::resize(sz);
974
975 v_size = sz;
976 }
977
982 void clear()
983 {
984 resize(0);
985 }
986
991 void add()
992 {
993 resize(v_size+1);
994 }
995
1001 void resize_base(size_t sz)
1002 {
1003 base_type::resize(sz);
1004 v_size = sz;
1005 }
1006
1014 inline T & get(size_t id)
1015 {
1016 return base_type::get(id);
1017 }
1018
1026 inline T & operator[](size_t id)
1027 {
1028 return base_type::get(id);
1029 }
1030
1038 inline T & last()
1039 {
1040 return base_type::get(size()-1);
1041 }
1042
1048 void swap(openfpm::vector_fr<T> & v)
1049 {
1050 size_t v_size_tmp = v.v_size;
1051 v.v_size = v_size;
1052 v_size = v_size_tmp;
1053
1054 base_type::swap(v);
1055 }
1056};
1057
1058#endif /* MAP_VECTOR_STD_HPP_ */
This class allocate, and destroy CPU memory.
Implementation of 1-D std::vector like structure.
size_t size()
Stub size.
vector(vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v) noexcept
Constructor from another vector.
const T & get(size_t id) const
Get an element of the vector.
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 add(const S &v)
It insert a new object on the vector, eventually it reallocate the object.
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
auto end() -> decltype(base.begin())
Return an std compatible iterator to the last element.
void fill(unsigned char fl)
it fill all the memory of fl patterns
vector(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) noexcept
Constructor from another vector.
auto begin() -> decltype(base.begin())
Return an std compatible iterator to the first element.
const void * getPointer() const
Return the pointer to the chunk of memory.
bool operator!=(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are equal.
void clear()
Remove all the element from the vector.
void * getPointer()
Return the pointer to the chunk of memory.
void add_prp_device(const vector< S, M, layout_base, gp, impl > &v)
It add the element of a source vector to this vector.
vector_key_iterator getIteratorTo(size_t k) const
Get iterator until a specified element.
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.
size_t packMem(size_t n, size_t e) const
Calculate the memory size required to allocate n elements.
vector_key_iterator getIterator() const
Get an iterator over all the elements of the vector.
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 swap(openfpm::vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &&v)
void remove(size_t key)
Remove one entry from the vector.
grow_policy_double grow_policy
growing policy of this vector
void reserve(size_t ns)
reserve a memory space in advance to avoid reallocation
void deviceToHost(size_t start, size_t stop)
Do nothing.
void remove(openfpm::vector< size_t > &keys, size_t start=0)
Remove several entries from the vector.
void add(T &&v)
It insert a new object on the vector, eventually it reallocate the grid.
bool operator==(const vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v) const
Check that two vectors are not equal.
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 openfpm::vector< T, Mem, lb, gp > &eles)
add elements to the vector
vector(InputIt first, InputIt last)
Constructor from iterators.
vector(const std::initializer_list< T > &v)
Initializer from constructor.
void add_prp(const vector< S, M, layout_base, gp, impl > &v)
It add the element of a source vector to this vector.
void swap(std::vector< T > &&v)
swap the memory between the two vector
static size_t calculateMemDummy(size_t n, size_t e)
Calculate the memory size required to allocate n elements.
vector(size_t sz) noexcept
Constructor, vector of size sz.
openfpm::vector< T > duplicate() const
Duplicate the vector.
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.
void add(const S &&v)
It insert a new object on the vector, eventually it reallocate the grid.
void swap(openfpm::vector< T, HeapMemory, memory_traits_lin, grow_policy_double, STD_VECTOR > &v)
static bool noPointers()
This class has pointer inside.
auto begin() const -> const decltype(base.begin())
Return an std compatible iterator to the first element.
static size_t calculateNMem(size_t n)
How many allocation are required to create 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.
void erase(typename std::vector< T >::iterator start, typename std::vector< T >::iterator end)
Erase the elements from start to end.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
auto end() const -> const 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.
Implementation of 1-D std::vector like structure.
void add()
Add another element.
T & get(size_t id)
Get an element of the vector.
size_t size_base()
return the base size of the vector
T & operator[](size_t id)
Get an element of the vector.
void clear()
Eliminate all elements.
void swap(openfpm::vector_fr< T > &v)
size_t size()
return the size of the vector
T & last()
Get an element of the vector.
void resize_base(size_t sz)
resize the base vector (this kill the objects)
void resize(size_t sz)
resize the vector retaining the objects
size_t v_size
size of the vector
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.
struct to merge two vectors
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.
Transform the boost::fusion::vector into memory specification (memory_traits)