5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <cstdint> // uint32_t
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>
31 #define DALI_ASSERT_VECTOR(cond) DALI_ASSERT_ALWAYS(cond)
36 * @addtogroup dali_core_common
41 * @brief Base class to handle the memory of simple vector.
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.
48 class DALI_CORE_API VectorBase
51 using SizeType = std::size_t;
53 protected: // Construction
55 * @brief Default constructor. Does not allocate space.
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.
72 * @brief This method is inlined as it's needed frequently for Vector::End() iterator.
75 * @return The count of elements in this vector
77 SizeType Count() const
82 SizeType* metadata = reinterpret_cast<SizeType*>(mData);
83 items = *(metadata - 1u);
89 * @brief Gets the count of elements in this vector.
91 * @return The count of elements in this vector
99 * @brief @ return if the vector is empty.
101 * @return True if the count of elements is empty
105 return Count() == 0u;
109 * @brief Gets the capacity of this vector.
111 * @return The capacity of this vector
113 SizeType Capacity() const;
116 * @brief Releases the data.
118 * Does not call destructors on objects held.
123 protected: // for Derived classes
125 * @brief Helper to set the count.
128 * @param[in] count Number of elements in the vector
130 void SetCount(SizeType count);
133 * @brief Reserves space in the vector.
136 * @param[in] count Count of elements to reserve
137 * @param[in] elementSize Size of a single element
139 void Reserve(SizeType count, SizeType elementSize);
142 * @brief Copy a vector.
145 * @param[in] vector Vector to copy from
146 * @param[in] elementSize Size of a single element
148 void Copy(const VectorBase& vector, SizeType elementSize);
151 * @brief Swaps the contents of two vectors.
154 * @param[in] vector Vector to swap with
156 void Swap(VectorBase& vector);
159 * @brief Erases an element.
161 * Does not change capacity.
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.
167 void Erase(char* address, SizeType elementSize);
170 * @brief Erases a range of elements.
172 * Does not change capacity.
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
179 char* Erase(char* first, char* last, SizeType elementSize);
182 * @brief Copies a number of bytes from \e source to \e destination.
184 * \e source and \e destination must not overlap.
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
191 void CopyMemory(char* destination, const char* source, size_t numberOfBytes);
194 // not copyable as it does not know the size of elements
195 VectorBase(const VectorBase&) = delete; ///< Deleted copy constructor. @SINCE_1_0.0
196 VectorBase& operator=(const VectorBase&) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
198 // not movable as this is handled by deriving classes
199 VectorBase(VectorBase&&) = delete; ///< Deleted move constructor. @SINCE_1_9.25
200 VectorBase& operator=(VectorBase&&) = delete; ///< Deleted copy assignment operator. @SINCE_1_9.25
203 void* mData; ///< Pointer to the data.
208 * @brief Vector algorithm variant for trivial types.
210 * Trivial types do not need destructor or copy constructor called.
213 template<bool IsTrivial>
214 class VectorAlgorithms : public VectorBase
216 protected: // API for deriving classes
217 using SizeType = VectorBase::SizeType;
220 * @brief Empty constructor.
223 VectorAlgorithms() = default;
226 * @brief Empty destructor.
229 ~VectorAlgorithms() = default;
232 * @brief Copy vector contents.
235 * @param[in] rhs VectorBase object to copy from
236 * @param[in] elementSize Size of the content
238 void Copy(const VectorBase& rhs, SizeType elementSize)
240 if(rhs.Capacity() > 0u)
242 VectorBase::Copy(rhs, elementSize);
246 VectorBase::Release();
251 * @brief Reserves space in the vector.
254 * @param[in] count Count of elements to reserve
255 * @param[in] elementSize Size of a single element
257 void Reserve(SizeType count, SizeType elementSize)
259 VectorBase::Reserve(count, elementSize);
263 * @brief Resizes the vector. Does not change capacity.
266 * @param[in] count Count to resize to
267 * @param[in] elementSize Size of a single element
269 void Resize(SizeType count, SizeType elementSize)
271 // reserve will copy old elements as well
272 Reserve(count, elementSize);
276 * @brief Clears the contents.
278 * For simple types, nothing to do.
285 VectorBase::SetCount(0u);
290 * @brief Releases the vector.
295 VectorBase::Release();
299 * @brief Erases an element. Does not change capacity.
302 * @param[in] address Address to erase from
303 * @param[in] elementSize Size to erase
305 void Erase(uint8_t* address, SizeType elementSize)
307 VectorBase::Erase(reinterpret_cast<char*>(address), elementSize);
311 * @brief Erases a range of elements. Does not change capacity.
314 * @param[in] first Address to the first element to be erased
315 * @param[in] last Address to the last element to be erased
316 * @param[in] elementSize Size of one of the elements to be erased
317 * @return Address pointing to the next element of the last one
319 uint8_t* Erase(uint8_t* first, uint8_t* last, SizeType elementSize)
321 return reinterpret_cast<uint8_t*>(VectorBase::Erase(reinterpret_cast<char*>(first), reinterpret_cast<char*>(last), elementSize));
325 * @brief Inserts the given elements into the vector.
328 * @param[in] at Address where to insert the elements into the vector
329 * @param[in] from Address to the first element to be inserted
330 * @param[in] to Address to the last element to be inserted
331 * @param[in] elementSize Size of one of the elements to be inserted
333 void Insert(uint8_t* at, uint8_t* from, uint8_t* to, SizeType elementSize)
335 const SizeType size = to - from;
336 const SizeType count = Count();
337 const SizeType newCount = count + size / elementSize;
339 if(newCount > Capacity())
341 // Calculate the at offset as the pointer is invalid after the Reserve() call.
342 std::size_t offset = at - reinterpret_cast<uint8_t*>(mData);
345 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.
347 // Set the new at pointer.
348 at = reinterpret_cast<uint8_t*>(mData) + offset;
350 // set new count first as otherwise the debug assert will hit us
353 // Move current items to a new position inside the vector.
354 CopyMemory(reinterpret_cast<char*>(at + size),
355 reinterpret_cast<const char*>(at),
356 (reinterpret_cast<uint8_t*>(mData) + count * elementSize) - at);
358 // Copy the given items.
359 CopyMemory(reinterpret_cast<char*>(at), reinterpret_cast<const char*>(from), size);
366 * @brief Vector algorithm variant for complex types.
368 * Not yet supported so will lead to compile error
369 * as constructor and destructor are private.
370 * TODO add support for this variant.
374 class VectorAlgorithms<false> : public VectorBase
377 VectorAlgorithms() = default;
378 ~VectorAlgorithms() = default;
383 * @brief Vector class with minimum space allocation when it's empty.
386 * @param type The type of the data that the vector holds
388 template<class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true>
389 class Vector : public VectorAlgorithms<IsTrivialType>
393 * @brief Type definitions.
396 using SizeType = VectorBase::SizeType; ///< Size type @SINCE_1_0.0
397 using Iterator = T*; ///< Most simple Iterator is a pointer @SINCE_1_0.0
398 using ConstIterator = const T*; ///< Const iterator @SINCE_1_0.0
399 using ItemType = T; ///< Item type @SINCE_1_0.0
402 * @brief Enumeration for BaseType.
407 BaseType = IsTrivialType ///< @SINCE_1_0.0
411 * @brief Default constructor. Does not allocate any space.
417 * @brief Destructor. Releases the allocated space.
426 * @brief Copy constructor.
429 * @param[in] vector Vector to copy from
431 Vector(const Vector& vector)
433 // reuse copy assignment
438 * @brief Default move constructor.
441 * @param[in] vector Vector to move
443 Vector(Vector&& vector)
445 // reuse move assignment
446 operator=(std::move(vector));
450 * @brief Assignment operator.
453 * @param[in] vector Vector to assign from
454 * @return Reference to self for chaining
456 Vector& operator=(const Vector& vector)
460 VectorAlgorithms<BaseType>::Copy(vector, sizeof(ItemType));
466 * @brief Default move assignment operator.
469 * @param[in] vector Vector to move
471 Vector& operator=(Vector&& vector)
475 if(VectorBase::mData)
479 VectorBase::mData = vector.mData;
480 vector.mData = nullptr;
486 * @brief Iterator to the beginning of the data.
488 * @return Iterator to the beginning of the data
490 Iterator Begin() const
492 ItemType* address = reinterpret_cast<ItemType*>(VectorBase::mData);
497 * @brief Iterator to the end of the data (one past last element).
499 * @return Iterator to the end of the data (one past last element)
503 ItemType* address = reinterpret_cast<ItemType*>(VectorBase::mData);
504 address += VectorBase::Count();
509 * Support for C++11 Range-based for loop: for( item : container ).
511 * @return The start iterator
513 Iterator begin() const
519 * Support for C++11 Range-based for loop: for( item : container ).
521 * @return The end iterator
529 * @brief Subscript operator.
531 * @param[in] index Index of the element
532 * @return Reference to the element for given index
533 * @pre Index must be in the vector's range.
535 ItemType& operator[](SizeType index)
537 // reuse the const version
538 return const_cast<ItemType&>(const_cast<const Vector<ItemType>&>(*this)[index]);
542 * @brief Subscript operator.
544 * @param[in] index Index of the element
545 * @return Reference to the element for given index
546 * @pre Index must be in the vector's range.
548 const ItemType& operator[](SizeType index) const
550 DALI_ASSERT_VECTOR(VectorBase::mData && "Vector is empty");
551 DALI_ASSERT_VECTOR(index < VectorBase::Count() && "Index out of bounds");
552 ItemType* address = reinterpret_cast<ItemType*>(VectorBase::mData);
558 * @brief Pushes back an element to the vector.
560 * The underlying storage may be reallocated to provide space.
561 * If this occurs, all pre-existing pointers into the vector will
565 * @param[in] element Element to be added
567 void PushBack(const ItemType& element)
569 const SizeType count = VectorBase::Count();
570 const SizeType newCount = count + 1u;
571 const SizeType capacity = VectorBase::Capacity();
572 if(newCount > capacity)
575 Reserve(newCount << 1u); // reserve double the current count
577 // set new count first as otherwise the debug assert will hit us
578 VectorBase::SetCount(newCount);
579 operator[](count) = element;
583 * @brief Inserts an element to the vector.
585 * Elements after \e at are moved one position to the right.
587 * The underlying storage may be reallocated to provide space.
588 * If this occurs, all pre-existing pointers into the vector will
592 * @param[in] at Iterator where to insert the elements into the vector
593 * @param[in] element An element to be added
594 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
596 void Insert(Iterator at, const ItemType& element)
598 DALI_ASSERT_VECTOR((at <= End()) && (at >= Begin()) && "Iterator not inside vector");
599 const SizeType size = sizeof(ItemType);
600 uint8_t* address = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&element));
601 VectorAlgorithms<BaseType>::Insert(reinterpret_cast<uint8_t*>(at),
608 * @brief Inserts the given elements into the vector.
610 * Elements after \e at are moved the number of given elements positions to the right.
612 * The underlying storage may be reallocated to provide space.
613 * If this occurs, all pre-existing pointers into the vector will
617 * @param[in] at Iterator where to insert the elements into the vector
618 * @param[in] from Iterator to the first element to be inserted
619 * @param[in] to Iterator to the last element to be inserted
620 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
621 * @pre Iterators \e from and \e to must be valid iterators.
622 * @pre Iterator \e from must not be grater than Iterator \e to.
625 void Insert(Iterator at, Iterator from, Iterator to)
627 DALI_ASSERT_VECTOR((at <= End()) && (at >= Begin()) && "Iterator not inside vector");
628 DALI_ASSERT_VECTOR((from <= to) && "from address can't be greater than to");
636 VectorAlgorithms<BaseType>::Insert(reinterpret_cast<uint8_t*>(at),
637 reinterpret_cast<uint8_t*>(from),
638 reinterpret_cast<uint8_t*>(to),
643 * @brief Reserves space in the vector.
645 * Reserving less than current Capacity is a no-op.
647 * @param[in] count Count of elements to reserve
649 void Reserve(SizeType count)
651 VectorAlgorithms<BaseType>::Reserve(count, sizeof(ItemType));
655 * @brief Resizes the vector. Does not change capacity.
658 * @param[in] count Count to resize to
660 void Resize(SizeType count)
662 ItemType item = ItemType();
667 * @brief Resizes the vector without initializing the data.
669 * Can be used as a data container for reading whole file content.
671 * @param[in] count Count to resize to
673 void ResizeUninitialized(SizeType count)
676 VectorBase::SetCount(count);
680 * @brief Resizes the vector. Does not change capacity.
683 * @param[in] count Count to resize to
684 * @param[in] item An item to insert to the new indices
686 void Resize(SizeType count, const ItemType& item)
688 const SizeType oldCount = VectorBase::Count();
689 if(count <= oldCount)
691 // getting smaller so just set count
692 VectorBase::SetCount(count);
696 // remember how many new items get added
697 SizeType newItems = count - oldCount;
699 for(; newItems > 0u; --newItems)
707 * @brief Erases an element.
709 * Does not change capacity. Other elements get moved.
712 * @param[in] iterator Iterator pointing to the item to remove
713 * @return Iterator pointing to next element
714 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
717 Iterator Erase(Iterator iterator)
719 DALI_ASSERT_VECTOR((iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector");
720 if(iterator < (End() - 1u))
722 VectorAlgorithms<BaseType>::Erase(reinterpret_cast<uint8_t*>(iterator), sizeof(ItemType));
726 // just remove the element
733 * @brief Erases a range of elements.
735 * Does not change capacity. Other elements get moved.
738 * @param[in] first Iterator to the first element to be erased
739 * @param[in] last Iterator to the last element to be erased
741 * @return Iterator pointing to the next element of the last one
742 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
743 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
744 * @pre Iterator \e first must not be grater than Iterator \e last.
747 Iterator Erase(Iterator first, Iterator last)
749 DALI_ASSERT_VECTOR((first <= End()) && (first >= Begin()) && "Iterator not inside vector");
750 DALI_ASSERT_VECTOR((last <= End()) && (last >= Begin()) && "Iterator not inside vector");
751 DALI_ASSERT_VECTOR((first <= last) && "first iterator greater than last");
753 Iterator nextElement;
757 // Erase up to the end.
758 VectorBase::SetCount(VectorBase::Count() - (last - first));
763 nextElement = reinterpret_cast<Iterator>(VectorAlgorithms<BaseType>::Erase(reinterpret_cast<uint8_t*>(first),
764 reinterpret_cast<uint8_t*>(last),
772 * @brief Removes an element.
774 * Does not maintain order. Swaps the element with end and
775 * decreases size by one. This is much faster than Erase so use
776 * this in case order does not matter. Does not change capacity.
779 * @param[in] iterator Iterator pointing to the item to remove
780 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
783 void Remove(Iterator iterator)
785 DALI_ASSERT_VECTOR((iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector");
787 Iterator last = End() - 1u;
790 std::swap(*iterator, *last);
792 VectorBase::SetCount(VectorBase::Count() - 1u);
796 * @brief Swaps the contents of two vectors.
799 * @param[in] vector Vector to swap with
801 void Swap(Vector& vector)
803 VectorBase::Swap(vector);
807 * @brief Clears the contents of the vector. Keeps its capacity.
812 VectorAlgorithms<BaseType>::Clear();
816 * @brief Releases the memory that the vector holds.
821 VectorAlgorithms<BaseType>::Release();
826 * @brief Erases all elements that compare equal to value from the vector.
829 * @param[in] vector The vector
830 * @param[in] value The value to be removed.
832 template<class T, class U>
833 inline void Erase(Dali::Vector<T>& vector, const U& value)
835 auto begin = vector.Begin();
836 auto end = vector.End();
838 vector.Erase(std::remove(begin, end, value), end);
842 * @brief Erases all elements that satisfy the predicate from the vector.
845 * @param[in] vector The vector
846 * @param[in] predicate The predicate
848 template<class T, class Predicate>
849 inline void EraseIf(Dali::Vector<T>& vector, Predicate predicate)
851 auto begin = vector.Begin();
852 auto end = vector.End();
854 vector.Erase(std::remove_if(begin, end, predicate), end);
862 #endif // DALI_VECTOR_H