1/*
2 * map_vector_std_ptr.hpp
3 *
4 * Created on: Feb 9, 2016
5 * Author: i-bird
6 */
7
8#ifndef OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_
9#define OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_
10
11
12template<typename T,typename gp>
13class vector<T,PtrMemory,memory_traits_lin,gp,STD_VECTOR>
14{
15 //! Memory layout
16 typedef typename memory_traits_lin<T>::type layout;
17
18 //! function for memory layout
19 template <typename lb> using layout_base = memory_traits_lin<lb>;
20
21 //! Actual size of the vector, warning: it is not the space allocated in grid
22 //! grid size increase by a fixed amount every time we need a vector bigger than
23 //! the actually allocated space
24 size_t v_size;
25
26 //! 1-D static grid
27 PtrMemory * mem;
28
29 //! Error code
30 size_t err_code;
31
32public:
33
34 //! it define that it is a vector
35 typedef int yes_i_am_vector;
36
37 //! iterator for the vector
38 typedef vector_key_iterator iterator_key;
39 //! Type of the value the vector is storing
40 typedef T value_type;
41
42 //! return the size of the vector
43 inline size_t size() const
44 {
45 return v_size;
46 }
47
48
49 /*! \ brief Resize the vector to contain n elements
50 *
51 * \param slot number of elements
52 *
53 */
54 inline void resize(size_t slot)
55 {
56 // resize is valid only if v_size is 0 and it match the size of PtrMemory
57 if (slot > mem->size()/sizeof(T))
58 std::cerr << __FILE__ << ":" << __LINE__ << " error: this vector cannot be bigger than " << mem->size()/sizeof(T) << " elements\n";
59 v_size = slot;
60 }
61
62 /*! \brief Remove all the element from the vector
63 *
64 */
65 inline void clear()
66 {
67 v_size = 0;
68 }
69
70 /*! \brief It insert a new object on the vector, eventually it reallocate the grid
71 *
72 * \param v element to add
73 *
74 * \warning It is not thread safe should not be used in multi-thread environment
75 * reallocation, work only on cpu
76 *
77 *vector_isel<T>::value
78 */
79 inline void add(const T & v)
80 {
81 std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot add a new element to this vector \n";
82 }
83
84 /*! \brief It insert a new object on the vector, eventually it reallocate the grid
85 *
86 * \param v element to add
87 *
88 * \warning It is not thread safe should not be used in multi-thread environment
89 * reallocation, work only on cpu
90 *
91 *vector_isel<T>::value
92 */
93 inline void add(T && v)
94 {
95 std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot add new element to this vector \n";
96 }
97
98 /*! \brief Add an empty object (it call the default constructor () ) at the end of the vector
99 *
100 */
101
102 inline void add()
103 {
104 v_size++;
105
106 if (v_size > mem->size()/sizeof(T))
107 std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot resize a PtrMemory vector over the size stored by PtrMemory";
108 }
109
110 /*! \brief Erase the elements from start to end
111 *
112 * \param start element
113 * \param end element
114 *
115 */
116 void erase(typename std::vector<T>::iterator start, typename std::vector<T>::iterator end)
117 {
118 std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot erase element from this vector \n";
119 }
120
121 /*! \brief Remove one entry from the vector
122 *
123 * \param key element to remove
124 *
125 */
126 void remove(size_t key)
127 {
128#ifdef SE_CLASS1
129 vector_overflow(key);
130#endif
131 std::cerr << __FILE__ << ":" << __LINE__ << " error: you cannot remove elements from this vector \n";
132 }
133
134 /*! \brief Return an std compatible iterator to the first element
135 *
136 * \return an iterator to the first element
137 *
138 */
139 inline T * begin()
140 {
141 return mem->getPointer();
142 }
143
144 /*! \brief Return an std compatible iterator to the past the last element
145 *
146 * \return an iterator to the last element
147 *
148 */
149 inline T * end()
150 {
151 return &((T *)mem->getPointer())[v_size];
152 }
153
154 /*! \brief Return an std compatible iterator to the first element
155 *
156 * \return an iterator to the first element
157 *
158 */
159 inline const T * begin() const
160 {
161 return (T *)mem->getPointer();
162 }
163
164 /*! \brief Return an std compatible iterator to the past the last element
165 *
166 * \return an iterator to the last element
167 *
168 */
169 inline const T * end() const
170 {
171 return &((T *)mem->getPointer())[v_size];
172 }
173
174 /*! \brief Get the last element
175 *
176 * \return the last element as reference
177 *
178 */
179 inline T & last()
180 {
181#ifdef SE_CLASS1
182 if (v_size == 0)
183 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
184#endif
185 return ((T *)mem->getPointer())[v_size-1];
186 }
187
188 /*! \brief Get the last element
189 *
190 * \return the last element as reference
191 *
192 */
193 inline const T & last() const
194 {
195#ifdef SE_CLASS1
196 if (v_size == 0)
197 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0" << std::endl;
198#endif
199 return ((T *)mem->getPointer())[v_size-1];
200 }
201
202 /*! \brief Get an element of the vector
203 *
204 * \tparam p must be 0
205 *
206 * \param id element to get
207 *
208 * \return the reference to the element
209 *
210 */
211 template <unsigned int p>inline T& get(size_t id)
212 {
213#ifdef SE_CLASS1
214 if (p != 0)
215 {std::cerr << "Error the property does not exist" << "\n";}
216
217 vector_overflow(id);
218#endif
219
220 return ((T *)mem->getPointer())[id];
221 }
222
223 inline void setMemory(PtrMemory & mem)
224 {
225 this->mem = &mem;
226 }
227
228 /*! \brief Get an element of the vector
229 *
230 * \tparam p must be 0
231 *
232 * \param id element to get
233 *
234 * \return the reference to the element
235 *
236 */
237 template <unsigned int p>inline const T& get(size_t id) const
238 {
239#ifdef SE_CLASS1
240 if (p != 0)
241 {std::cerr << "Error the property does not exist" << "\n";}
242
243 vector_overflow(id);
244#endif
245
246 return ((T *)mem->getPointer())[id];
247 }
248
249 /*! \brief Get an element of the vector
250 *
251 * \param id element to get
252 *
253 * \return the element reference
254 *
255 */
256 inline T & get(size_t id)
257 {
258#ifdef SE_CLASS1
259 if (id >= v_size)
260 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << id << "\n";
261#endif
262 return ((T *)mem->getPointer())[id];
263 }
264
265 /*! \brief Get an element of the vector
266 *
267 * \param id element to get
268 *
269 * \return the element value
270 *
271 */
272 inline const T & get(size_t id) const
273 {
274#ifdef SE_CLASS1
275 vector_overflow(id);
276#endif
277 return ((T *)mem->getPointer())[id];
278 }
279
280 /*! \brief it fill all the memory of fl patterns
281 *
282 * WARNING does not assign a value to each element but it fill the memory
283 * Useful to fast set the memory to zero
284 *
285 * \param fl byte to fill
286 *
287 */
288
289 inline void fill(unsigned char fl)
290 {
291 memset(mem->getPointer(),fl,v_size * sizeof(T));
292 }
293
294 /*! \brief reserve a memory space in advance to avoid reallocation
295 *
296 * \param ns number of element the memory has to store
297 *
298 */
299
300 inline void reserve(size_t ns)
301 {
302 }
303
304 //! Constructor, vector of size 0
305 vector() noexcept
306 :v_size(0),mem(NULL),err_code(0)
307 {
308 }
309
310 //! Constructor, vector of size sz
311 vector(size_t sz) noexcept
312 :v_size(sz),err_code(0)
313 {
314 }
315
316 //! Constructor from another vector
317 vector(const vector<T,PtrMemory,layout_base,gp,STD_VECTOR> & v) noexcept
318 :v_size(0),err_code(0)
319 {
320 std::cerr << __FILE__ << ":" << __LINE__ << " error: copy constructor is not supported by this vector \n";
321 }
322
323
324 //! Constructor from another vector
325 vector(vector<T,PtrMemory,layout_base,gp,STD_VECTOR> && v) noexcept
326 :v_size(0),err_code(0)
327 {
328 std::cerr << __FILE__ << ":" << __LINE__ << " error: copy constructor is not supported by this vector \n";
329 }
330
331 //! destructor
332 ~vector() noexcept
333 {
334 }
335
336 /*! swap the content of the vector
337 *
338 * \param v vector to be swapped with
339 *
340 */
341 void swap(openfpm::vector<T,PtrMemory,layout_base,gp,STD_VECTOR> & v)
342 {
343 std::cerr << __FILE__ << ":" << __LINE__ << " error: swap is not supported by this vector \n";
344 }
345
346 /*! \brief Operator= copy the vector into another
347 *
348 * \return itself
349 *
350 */
351 vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> & operator=(const vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> & v)
352 {
353 std::cerr << __FILE__ << ":" << __LINE__ << " error: operator= is not supported by this vector \n";
354
355 return *this;
356 }
357
358 /*! \brief Operator= copy the vector into another
359 *
360 * \param v vector to copy
361 *
362 * \return itself
363 *
364 */
365 vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> & operator=(vector<T,HeapMemory,layout_base,grow_policy_double,STD_VECTOR> && v)
366 {
367 std::cerr << __FILE__ << ":" << __LINE__ << " error: operator= is not supported by this vector \n";
368
369 return *this;
370 }
371
372 /*! \brief Check that two vectors are equal
373 *
374 * \param vector to compare
375 *
376 * \return true if the two vector match
377 *
378 */
379 bool operator!=(const vector<T, HeapMemory, layout_base,grow_policy_double,STD_VECTOR> & v) const
380 {
381 std::cerr << __FILE__ << ":" << __LINE__ << " error: operator!= is not supported by this vector \n";
382
383 return false;
384 }
385
386 /*! \brief Check that two vectors are not equal
387 *
388 * \param vector to compare
389 *
390 * \return true if the vector match
391 *
392 */
393 bool operator==(const vector<T, HeapMemory, layout_base,grow_policy_double,STD_VECTOR> & v) const
394 {
395 std::cerr << __FILE__ << ":" << __LINE__ << " error: operator== is not supported by this vector \n";
396
397 return false;
398 }
399
400 /*! \brief Get iterator
401 *
402 * \return an iterator
403 *
404 */
405 vector_key_iterator getIterator() const
406 {
407 return vector_key_iterator(v_size);
408 }
409
410#ifdef CUDA_GPU
411
412 /*! \brief Do nothing
413 *
414 */
415 template<unsigned int ... prp> void hostToDevice()
416 {}
417
418 /*! \brief Do nothing
419 *
420 */
421 template<unsigned int ... prp> void deviceToHost()
422 {}
423
424 /*! \brief Do nothing
425 *
426 *
427 */
428 template<unsigned int ... prp> void deviceToHost(size_t start, size_t stop)
429 {}
430
431#endif
432
433 /*! \brief Return the pointer to the chunk of memory
434 *
435 * \return the pointer to the chunk of memory
436 *
437 */
438 void * getPointer()
439 {
440 return mem->getPointer();
441 }
442
443 /*! \brief Return the pointer to the chunk of memory
444 *
445 * \return the pointer to the chunk of memory
446 *
447 */
448 const void * getPointer() const
449 {
450 return mem->getPointer();
451 }
452
453 /*! \brief This class has pointer inside
454 *
455 * \return false
456 *
457 */
458 static bool noPointers()
459 {
460 return false;
461 }
462
463 /*! \brief Return the last error
464 *
465 * \return the last error
466 *
467 */
468 size_t getLastError()
469 {
470 return err_code;
471 }
472
473 /*! \brief check that the id does not overflow the buffer
474 *
475 * \param v1 to check
476 *
477 */
478 inline void vector_overflow(size_t v1) const
479 {
480 if (v1 >= v_size)
481 {
482 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
483 size_t * err_code_pointer = (size_t *)&this->err_code;\
484 *err_code_pointer = 2001;\
485 ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
486 }
487 }
488};
489
490
491#endif /* OPENFPM_DATA_SRC_VECTOR_MAP_VECTOR_STD_PTR_HPP_ */
492