Revert "[Tizen] Prevent accessing invalid index"
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-vector.h
1 #ifndef DALI_VECTOR_H
2 #define DALI_VECTOR_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <cstddef>
24 #include <cstdint> // uint32_t
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/common/type-traits.h>
29 #include <dali/public-api/math/math-utils.h>
30
31 #define DALI_ASSERT_VECTOR(cond) DALI_ASSERT_ALWAYS(cond)
32
33 namespace Dali
34 {
35 /**
36  * @addtogroup dali_core_common
37  * @{
38  */
39
40 /**
41  * @brief Base class to handle the memory of simple vector.
42  *
43  * Memory layout is such that it has two std::size_t to hold the count
44  * and capacity of the vector. VectorBase::mData is adjusted so that it points to the
45  * beginning of the first real item so that iterating the items is quick.
46  * @SINCE_1_0.0
47  */
48 class DALI_CORE_API VectorBase
49 {
50 public: // Typedefs
51   using SizeType = std::size_t;
52
53 protected: // Construction
54   /**
55    * @brief Default constructor. Does not allocate space.
56    * @SINCE_1_0.0
57    */
58   VectorBase();
59
60   /**
61    * @brief Destructor.
62    *
63    * Does not release the space. Derived class needs to call Release.
64    * Not virtual as this should not be called directly and we do not want
65    * a vtable for this class as it would unnecessarily increase size.
66    * @SINCE_1_0.0
67    */
68   ~VectorBase();
69
70 public: // API
71   /**
72    * @brief This method is inlined as it's needed frequently for Vector::End() iterator.
73    *
74    * @SINCE_1_0.0
75    * @return The count of elements in this vector
76    */
77   SizeType Count() const
78   {
79     SizeType items = 0u;
80     if(mData)
81     {
82       SizeType* metadata = reinterpret_cast<SizeType*>(mData);
83       items              = *(metadata - 1u);
84     }
85     return items;
86   }
87
88   /**
89    * @brief Gets the count of elements in this vector.
90    * @SINCE_1_0.0
91    * @return The count of elements in this vector
92    */
93   SizeType Size() const
94   {
95     return Count();
96   }
97
98   /**
99    * @brief @ return if the vector is empty.
100    * @SINCE_1_0.0
101    * @return True if the count of elements is empty
102    */
103   bool Empty() const
104   {
105     return Count() == 0u;
106   }
107
108   /**
109    * @brief Gets the capacity of this vector.
110    * @SINCE_1_0.0
111    * @return The capacity of this vector
112    */
113   SizeType Capacity() const;
114
115   /**
116    * @brief Releases the data.
117    *
118    * Does not call destructors on objects held.
119    * @SINCE_1_0.0
120    */
121   void Release();
122
123 protected: // for Derived classes
124   /**
125    * @brief Helper to set the count.
126    *
127    * @SINCE_1_0.0
128    * @param[in] count Number of elements in the vector
129    */
130   void SetCount(SizeType count);
131
132   /**
133    * @brief Reserves space in the vector.
134    *
135    * @SINCE_1_0.0
136    * @param[in] count Count of elements to reserve
137    * @param[in] elementSize Size of a single element
138    */
139   void Reserve(SizeType count, SizeType elementSize);
140
141   /**
142    * @brief Copy a vector.
143    *
144    * @SINCE_1_0.0
145    * @param[in] vector Vector to copy from
146    * @param[in] elementSize Size of a single element
147    */
148   void Copy(const VectorBase& vector, SizeType elementSize);
149
150   /**
151    * @brief Swaps the contents of two vectors.
152    *
153    * @SINCE_1_0.0
154    * @param[in] vector Vector to swap with
155    */
156   void Swap(VectorBase& vector);
157
158   /**
159    * @brief Erases an element.
160    *
161    * Does not change capacity.
162    * @SINCE_1_0.0
163    * @param[in] address Address to erase from
164    * @param[in] elementSize Size to erase
165    * @pre Last element cannot be erased as there is nothing to move.
166    */
167   void Erase(char* address, SizeType elementSize);
168
169   /**
170    * @brief Erases a range of elements.
171    *
172    * Does not change capacity.
173    * @SINCE_1_0.0
174    * @param[in] first Address to the first element to be erased
175    * @param[in] last Address to the last element to be erased
176    * @param[in] elementSize Size of one of the elements to be erased
177    * @return Address pointing to the next element of the last one
178    */
179   char* Erase(char* first, char* last, SizeType elementSize);
180
181   /**
182    * @brief Copies a number of bytes from \e source to \e destination.
183    *
184    * \e source and \e destination must not overlap.
185    *
186    * @SINCE_1_0.0
187    * @param[in] destination Pointer to the destination address
188    * @param[in] source Pointer to the source address
189    * @param[in] numberOfBytes The number of bytes to be copied
190    */
191   void CopyMemory(char* destination, const char* source, size_t numberOfBytes);
192
193   /**
194    * @brief Replace the data as new data address.
195    * After replace, release the old data.
196    *
197    * It will be used when we want to keep the mData integrity.
198    *
199    * Does not call destructors on objects held.
200    * @param[in] newData new data address to be replaced
201    */
202   void Replace(void* newData);
203
204 private:
205   // not copyable as it does not know the size of elements
206   VectorBase(const VectorBase&) = delete;            ///< Deleted copy constructor. @SINCE_1_0.0
207   VectorBase& operator=(const VectorBase&) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
208
209   // not movable as this is handled by deriving classes
210   VectorBase(VectorBase&&) = delete;            ///< Deleted move constructor. @SINCE_1_9.25
211   VectorBase& operator=(VectorBase&&) = delete; ///< Deleted copy assignment operator. @SINCE_1_9.25
212
213 protected:     // Data
214   void* mData; ///< Pointer to the data.
215 };
216
217 /// @cond internal
218 /**
219  * @brief Vector algorithm variant for trivial types.
220  *
221  * Trivial types do not need destructor or copy constructor called.
222  * @SINCE_1_0.0
223  */
224 template<bool IsTrivial>
225 class VectorAlgorithms : public VectorBase
226 {
227 protected: // API for deriving classes
228   using SizeType = VectorBase::SizeType;
229
230   /**
231    * @brief Empty constructor.
232    * @SINCE_1_0.0
233    */
234   VectorAlgorithms() = default;
235
236   /**
237    * @brief Empty destructor.
238    * @SINCE_1_0.0
239    */
240   ~VectorAlgorithms() = default;
241
242   /**
243    * @brief Copy vector contents.
244    *
245    * @SINCE_1_0.0
246    * @param[in] rhs VectorBase object to copy from
247    * @param[in] elementSize Size of the content
248    */
249   void Copy(const VectorBase& rhs, SizeType elementSize)
250   {
251     if(rhs.Capacity() > 0u)
252     {
253       VectorBase::Copy(rhs, elementSize);
254     }
255     else
256     {
257       VectorBase::Release();
258     }
259   }
260
261   /**
262    * @brief Reserves space in the vector.
263    *
264    * @SINCE_1_0.0
265    * @param[in] count Count of elements to reserve
266    * @param[in] elementSize Size of a single element
267    */
268   void Reserve(SizeType count, SizeType elementSize)
269   {
270     VectorBase::Reserve(count, elementSize);
271   }
272
273   /**
274    * @brief Resizes the vector. Does not change capacity.
275    *
276    * @SINCE_1_0.0
277    * @param[in] count Count to resize to
278    * @param[in] elementSize Size of a single element
279    */
280   void Resize(SizeType count, SizeType elementSize)
281   {
282     // reserve will copy old elements as well
283     Reserve(count, elementSize);
284   }
285
286   /**
287    * @brief Clears the contents.
288    *
289    * For simple types, nothing to do.
290    * @SINCE_1_0.0
291    */
292   void Clear()
293   {
294     if(mData)
295     {
296       VectorBase::SetCount(0u);
297     }
298   }
299
300   /**
301    * @brief Releases the vector.
302    * @SINCE_1_0.0
303    */
304   void Release()
305   {
306     VectorBase::Release();
307   }
308
309   /**
310    * @brief Erases an element. Does not change capacity.
311    *
312    * @SINCE_1_0.0
313    * @param[in] address Address to erase from
314    * @param[in] elementSize Size to erase
315    */
316   void Erase(uint8_t* address, SizeType elementSize)
317   {
318     VectorBase::Erase(reinterpret_cast<char*>(address), elementSize);
319   }
320
321   /**
322    * @brief Erases a range of elements. Does not change capacity.
323    *
324    * @SINCE_1_0.0
325    * @param[in] first Address to the first element to be erased
326    * @param[in] last Address to the last element to be erased
327    * @param[in] elementSize Size of one of the elements to be erased
328    * @return Address pointing to the next element of the last one
329    */
330   uint8_t* Erase(uint8_t* first, uint8_t* last, SizeType elementSize)
331   {
332     return reinterpret_cast<uint8_t*>(VectorBase::Erase(reinterpret_cast<char*>(first), reinterpret_cast<char*>(last), elementSize));
333   }
334
335   /**
336    * @brief Inserts the given elements into the vector.
337    *
338    * @SINCE_1_0.0
339    * @param[in] at Address where to insert the elements into the vector
340    * @param[in] from Address to the first element to be inserted
341    * @param[in] to Address to the last element to be inserted
342    * @param[in] elementSize Size of one of the elements to be inserted
343    */
344   void Insert(uint8_t* at, uint8_t* from, uint8_t* to, SizeType elementSize)
345   {
346     const SizeType size     = to - from;
347     const SizeType count    = Count();
348     const SizeType newCount = count + size / elementSize;
349
350     if(newCount > Capacity())
351     {
352       // Calculate the at offset as the pointer is invalid after the Reserve() call.
353       std::size_t offset = at - reinterpret_cast<uint8_t*>(mData);
354
355       // need more space
356       Reserve(NextPowerOfTwo(static_cast<uint32_t>(newCount)), elementSize); // reserve enough space to store at least the next power of two elements of the new required size.
357
358       // Set the new at pointer.
359       at = reinterpret_cast<uint8_t*>(mData) + offset;
360     }
361     // set new count first as otherwise the debug assert will hit us
362     SetCount(newCount);
363
364     // Move current items to a new position inside the vector.
365     CopyMemory(reinterpret_cast<char*>(at + size),
366                reinterpret_cast<const char*>(at),
367                (reinterpret_cast<uint8_t*>(mData) + count * elementSize) - at);
368
369     // Copy the given items.
370     CopyMemory(reinterpret_cast<char*>(at), reinterpret_cast<const char*>(from), size);
371   }
372 };
373 /// @endcond
374
375 /// @cond internal
376 /**
377  * @brief Vector algorithm variant for complex types.
378  *
379  * Not yet supported so will lead to compile error
380  * as constructor and destructor are private.
381  * TODO add support for this variant.
382  * @SINCE_1_0.0
383  */
384 template<>
385 class VectorAlgorithms<false> : public VectorBase
386 {
387 private:
388   VectorAlgorithms()  = default;
389   ~VectorAlgorithms() = default;
390 };
391 /// @endcond
392
393 /**
394  * @brief Vector class with minimum space allocation when it's empty.
395  *
396  * @SINCE_1_0.0
397  * @param type The type of the data that the vector holds
398  */
399 template<class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true>
400 class Vector : public VectorAlgorithms<IsTrivialType>
401 {
402 public: // API
403   /**
404    * @brief Type definitions.
405    * @SINCE_1_0.0
406    */
407   using SizeType      = VectorBase::SizeType; ///< Size type @SINCE_1_0.0
408   using Iterator      = T*;                   ///< Most simple Iterator is a pointer @SINCE_1_0.0
409   using ConstIterator = const T*;             ///< Const iterator @SINCE_1_0.0
410   using ItemType      = T;                    ///< Item type @SINCE_1_0.0
411
412   /**
413    * @brief Enumeration for BaseType.
414    * @SINCE_1_0.0
415    */
416   enum
417   {
418     BaseType = IsTrivialType ///< @SINCE_1_0.0
419   };
420
421   /**
422    * @brief Default constructor. Does not allocate any space.
423    * @SINCE_1_0.0
424    */
425   Vector() = default;
426
427   /**
428    * @brief Destructor. Releases the allocated space.
429    * @SINCE_1_0.0
430    */
431   ~Vector()
432   {
433     Release();
434   }
435
436   /**
437    * @brief Copy constructor.
438    *
439    * @SINCE_1_0.0
440    * @param[in] vector Vector to copy from
441    */
442   Vector(const Vector& vector)
443   {
444     // reuse copy assignment
445     operator=(vector);
446   }
447
448   /**
449    * @brief Default move constructor.
450    *
451    * @SINCE_1_9.25
452    * @param[in] vector Vector to move
453    */
454   Vector(Vector&& vector)
455   {
456     // reuse move assignment
457     operator=(std::move(vector));
458   }
459
460   /**
461    * @brief Assignment operator.
462    *
463    * @SINCE_1_0.0
464    * @param[in] vector Vector to assign from
465    * @return Reference to self for chaining
466    */
467   Vector& operator=(const Vector& vector)
468   {
469     if(this != &vector)
470     {
471       VectorAlgorithms<BaseType>::Copy(vector, sizeof(ItemType));
472     }
473     return *this;
474   }
475
476   /**
477    * @brief Default move assignment operator.
478    *
479    * @SINCE_1_9.25
480    * @param[in] vector Vector to move
481    */
482   Vector& operator=(Vector&& vector)
483   {
484     if(this != &vector)
485     {
486       VectorAlgorithms<BaseType>::Replace(vector.mData);
487       vector.mData = nullptr;
488     }
489     return *this;
490   }
491
492   /**
493    * @brief Iterator to the beginning of the data.
494    * @SINCE_1_0.0
495    * @return Iterator to the beginning of the data
496    */
497   Iterator Begin() const
498   {
499     ItemType* address = reinterpret_cast<ItemType*>(VectorBase::mData);
500     return address;
501   }
502
503   /**
504    * @brief Iterator to the end of the data (one past last element).
505    * @SINCE_1_0.0
506    * @return Iterator to the end of the data (one past last element)
507    */
508   Iterator End() const
509   {
510     ItemType* address = reinterpret_cast<ItemType*>(VectorBase::mData);
511     address += VectorBase::Count();
512     return address;
513   }
514
515   /**
516    * Support for C++11 Range-based for loop: for( item : container ).
517    * @SINCE_1_2.60
518    * @return The start iterator
519    */
520   Iterator begin() const
521   {
522     return Begin();
523   }
524
525   /**
526    * Support for C++11 Range-based for loop: for( item : container ).
527    * @SINCE_1_2.60
528    * @return The end iterator
529    */
530   Iterator end() const
531   {
532     return End();
533   }
534
535   /**
536    * @brief Subscript operator.
537    * @SINCE_1_0.0
538    * @param[in] index Index of the element
539    * @return Reference to the element for given index
540    * @pre Index must be in the vector's range.
541    */
542   ItemType& operator[](SizeType index)
543   {
544     // reuse the const version
545     return const_cast<ItemType&>(const_cast<const Vector<ItemType>&>(*this)[index]);
546   }
547
548   /**
549    * @brief Subscript operator.
550    * @SINCE_1_0.0
551    * @param[in] index Index of the element
552    * @return Reference to the element for given index
553    * @pre Index must be in the vector's range.
554    */
555   const ItemType& operator[](SizeType index) const
556   {
557     DALI_ASSERT_VECTOR(VectorBase::mData && "Vector is empty");
558     DALI_ASSERT_VECTOR(index < VectorBase::Count() && "Index out of bounds");
559     ItemType* address = reinterpret_cast<ItemType*>(VectorBase::mData);
560     address += index;
561     return *address;
562   }
563
564   /**
565    * @brief Pushes back an element to the vector.
566    *
567    * The underlying storage may be reallocated to provide space.
568    * If this occurs, all pre-existing pointers into the vector will
569    * become invalid.
570    *
571    * @SINCE_1_0.0
572    * @param[in] element Element to be added
573    */
574   void PushBack(const ItemType& element)
575   {
576     const SizeType count    = VectorBase::Count();
577     const SizeType newCount = count + 1u;
578     const SizeType capacity = VectorBase::Capacity();
579     if(newCount > capacity)
580     {
581       // need more space
582       Reserve(newCount << 1u); // reserve double the current count
583     }
584     // set new count first as otherwise the debug assert will hit us
585     VectorBase::SetCount(newCount);
586     operator[](count) = element;
587   }
588
589   /**
590    * @brief Inserts an element to the vector.
591    *
592    * Elements after \e at are moved one position to the right.
593    *
594    * The underlying storage may be reallocated to provide space.
595    * If this occurs, all pre-existing pointers into the vector will
596    * become invalid.
597    *
598    * @SINCE_1_0.0
599    * @param[in] at Iterator where to insert the elements into the vector
600    * @param[in] element An element to be added
601    * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
602    */
603   void Insert(Iterator at, const ItemType& element)
604   {
605     DALI_ASSERT_VECTOR((at <= End()) && (at >= Begin()) && "Iterator not inside vector");
606     const SizeType size    = sizeof(ItemType);
607     uint8_t*       address = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&element));
608     VectorAlgorithms<BaseType>::Insert(reinterpret_cast<uint8_t*>(at),
609                                        address,
610                                        address + size,
611                                        size);
612   }
613
614   /**
615    * @brief Inserts the given elements into the vector.
616    *
617    * Elements after \e at are moved the number of given elements positions to the right.
618    *
619    * The underlying storage may be reallocated to provide space.
620    * If this occurs, all pre-existing pointers into the vector will
621    * become invalid.
622    *
623    * @SINCE_1_0.0
624    * @param[in] at Iterator where to insert the elements into the vector
625    * @param[in] from Iterator to the first element to be inserted
626    * @param[in] to Iterator to the last element to be inserted
627    * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
628    * @pre Iterators \e from and \e to must be valid iterators.
629    * @pre Iterator \e from must not be grater than Iterator \e to.
630    *
631    */
632   void Insert(Iterator at, Iterator from, Iterator to)
633   {
634     DALI_ASSERT_VECTOR((at <= End()) && (at >= Begin()) && "Iterator not inside vector");
635     DALI_ASSERT_VECTOR((from <= to) && "from address can't be greater than to");
636
637     if(from == to)
638     {
639       // nothing to copy.
640       return;
641     }
642
643     VectorAlgorithms<BaseType>::Insert(reinterpret_cast<uint8_t*>(at),
644                                        reinterpret_cast<uint8_t*>(from),
645                                        reinterpret_cast<uint8_t*>(to),
646                                        sizeof(ItemType));
647   }
648
649   /**
650    * @brief Reserves space in the vector.
651    *
652    * Reserving less than current Capacity is a no-op.
653    * @SINCE_1_0.0
654    * @param[in] count Count of elements to reserve
655    */
656   void Reserve(SizeType count)
657   {
658     VectorAlgorithms<BaseType>::Reserve(count, sizeof(ItemType));
659   }
660
661   /**
662    * @brief Resizes the vector. Does not change capacity.
663    *
664    * @SINCE_1_0.0
665    * @param[in] count Count to resize to
666    */
667   void Resize(SizeType count)
668   {
669     ItemType item = ItemType();
670     Resize(count, item);
671   }
672
673   /**
674    * @brief Resizes the vector without initializing the data.
675    *
676    * Can be used as a data container for reading whole file content.
677    * @SINCE_1_9.27
678    * @param[in] count Count to resize to
679    */
680   void ResizeUninitialized(SizeType count)
681   {
682     Reserve(count);
683     VectorBase::SetCount(count);
684   }
685
686   /**
687    * @brief Resizes the vector. Does not change capacity.
688    *
689    * @SINCE_1_0.0
690    * @param[in] count Count to resize to
691    * @param[in] item An item to insert to the new indices
692    */
693   void Resize(SizeType count, const ItemType& item)
694   {
695     const SizeType oldCount = VectorBase::Count();
696     if(count <= oldCount)
697     {
698       // getting smaller so just set count
699       VectorBase::SetCount(count);
700     }
701     else
702     {
703       // remember how many new items get added
704       SizeType newItems = count - oldCount;
705       Reserve(count);
706       for(; newItems > 0u; --newItems)
707       {
708         PushBack(item);
709       }
710     }
711   }
712
713   /**
714    * @brief Erases an element.
715    *
716    * Does not change capacity. Other elements get moved.
717    *
718    * @SINCE_1_0.0
719    * @param[in] iterator Iterator pointing to the item to remove
720    * @return Iterator pointing to next element
721    * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
722    *
723    */
724   Iterator Erase(Iterator iterator)
725   {
726     DALI_ASSERT_VECTOR((iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector");
727     if(iterator < (End() - 1u))
728     {
729       VectorAlgorithms<BaseType>::Erase(reinterpret_cast<uint8_t*>(iterator), sizeof(ItemType));
730     }
731     else
732     {
733       // just remove the element
734       Remove(iterator);
735     }
736     return iterator;
737   }
738
739   /**
740    * @brief Erases a range of elements.
741    *
742    * Does not change capacity. Other elements get moved.
743    *
744    * @SINCE_1_0.0
745    * @param[in] first Iterator to the first element to be erased
746    * @param[in] last Iterator to the last element to be erased
747    *
748    * @return Iterator pointing to the next element of the last one
749    * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
750    * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
751    * @pre Iterator \e first must not be grater than Iterator \e last.
752    *
753    */
754   Iterator Erase(Iterator first, Iterator last)
755   {
756     DALI_ASSERT_VECTOR((first <= End()) && (first >= Begin()) && "Iterator not inside vector");
757     DALI_ASSERT_VECTOR((last <= End()) && (last >= Begin()) && "Iterator not inside vector");
758     DALI_ASSERT_VECTOR((first <= last) && "first iterator greater than last");
759
760     Iterator nextElement;
761
762     if(last == End())
763     {
764       // Erase up to the end.
765       VectorBase::SetCount(VectorBase::Count() - (last - first));
766       nextElement = End();
767     }
768     else
769     {
770       nextElement = reinterpret_cast<Iterator>(VectorAlgorithms<BaseType>::Erase(reinterpret_cast<uint8_t*>(first),
771                                                                                  reinterpret_cast<uint8_t*>(last),
772                                                                                  sizeof(ItemType)));
773     }
774
775     return nextElement;
776   }
777
778   /**
779    * @brief Removes an element.
780    *
781    * Does not maintain order. Swaps the element with end and
782    * decreases size by one.  This is much faster than Erase so use
783    * this in case order does not matter. Does not change capacity.
784    *
785    * @SINCE_1_0.0
786    * @param[in] iterator Iterator pointing to the item to remove
787    * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
788    *
789    */
790   void Remove(Iterator iterator)
791   {
792     DALI_ASSERT_VECTOR((iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector");
793
794     Iterator last = End() - 1u;
795     if(last > iterator)
796     {
797       std::swap(*iterator, *last);
798     }
799     VectorBase::SetCount(VectorBase::Count() - 1u);
800   }
801
802   /**
803    * @brief Swaps the contents of two vectors.
804    *
805    * @SINCE_1_0.0
806    * @param[in] vector Vector to swap with
807    */
808   void Swap(Vector& vector)
809   {
810     VectorBase::Swap(vector);
811   }
812
813   /**
814    * @brief Clears the contents of the vector. Keeps its capacity.
815    * @SINCE_1_0.0
816    */
817   void Clear()
818   {
819     VectorAlgorithms<BaseType>::Clear();
820   }
821
822   /**
823    * @brief Releases the memory that the vector holds.
824    * @SINCE_1_0.0
825    */
826   void Release()
827   {
828     VectorAlgorithms<BaseType>::Release();
829   }
830 };
831
832 /**
833  * @brief Erases all elements that compare equal to value from the vector.
834  *
835  * @SINCE_1_9.33
836  * @param[in] vector The vector
837  * @param[in] value The value to be removed.
838  */
839 template<class T, class U>
840 inline void Erase(Dali::Vector<T>& vector, const U& value)
841 {
842   auto begin = vector.Begin();
843   auto end   = vector.End();
844
845   vector.Erase(std::remove(begin, end, value), end);
846 }
847
848 /**
849  * @brief Erases all elements that satisfy the predicate from the vector.
850  *
851  * @SINCE_1_9.33
852  * @param[in] vector The vector
853  * @param[in] predicate The predicate
854  */
855 template<class T, class Predicate>
856 inline void EraseIf(Dali::Vector<T>& vector, Predicate predicate)
857 {
858   auto begin = vector.Begin();
859   auto end   = vector.End();
860
861   vector.Erase(std::remove_if(begin, end, predicate), end);
862 }
863
864 /**
865  * @}
866  */
867 } // namespace Dali
868
869 #endif // DALI_VECTOR_H