5 * Copyright (c) 2020 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>
32 * @brief For DALi internal use, asserts are enabled in debug builds.
34 * For Application use, asserts can be enabled manually.
37 #if defined( DEBUG_ENABLED )
38 #define ENABLE_VECTOR_ASSERTS
41 #if defined( ENABLE_VECTOR_ASSERTS )
42 #define DALI_ASSERT_VECTOR(cond) DALI_ASSERT_ALWAYS(cond)
44 #define DALI_ASSERT_VECTOR(cond)
50 * @addtogroup dali_core_common
55 * @brief Base class to handle the memory of simple vector.
57 * Memory layout is such that it has two std::size_t to hold the count
58 * and capacity of the vector. VectorBase::mData is adjusted so that it points to the
59 * beginning of the first real item so that iterating the items is quick.
62 class DALI_CORE_API VectorBase
65 using SizeType = std::size_t;
67 protected: // Construction
70 * @brief Default constructor. Does not allocate space.
78 * Does not release the space. Derived class needs to call Release.
79 * Not virtual as this should not be called directly and we do not want
80 * a vtable for this class as it would unnecessarily increase size.
88 * @brief This method is inlined as it's needed frequently for Vector::End() iterator.
91 * @return The count of elements in this vector
93 SizeType Count() const
98 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
99 items = *(metadata - 1u);
105 * @brief Gets the count of elements in this vector.
107 * @return The count of elements in this vector
109 SizeType Size() const
115 * @brief @ return if the vector is empty.
117 * @return True if the count of elements is empty
121 return Count() == 0u;
125 * @brief Gets the capacity of this vector.
127 * @return The capacity of this vector
129 SizeType Capacity() const;
132 * @brief Releases the data.
134 * Does not call destructors on objects held.
139 protected: // for Derived classes
142 * @brief Helper to set the count.
145 * @param[in] count Number of elements in the vector
147 void SetCount( SizeType count );
150 * @brief Reserves space in the vector.
153 * @param[in] count Count of elements to reserve
154 * @param[in] elementSize Size of a single element
156 void Reserve( SizeType count, SizeType elementSize );
159 * @brief Copy a vector.
162 * @param[in] vector Vector to copy from
163 * @param[in] elementSize Size of a single element
165 void Copy( const VectorBase& vector, SizeType elementSize );
168 * @brief Swaps the contents of two vectors.
171 * @param[in] vector Vector to swap with
173 void Swap( VectorBase& vector );
176 * @brief Erases an element.
178 * Does not change capacity.
180 * @param[in] address Address to erase from
181 * @param[in] elementSize Size to erase
182 * @pre Last element cannot be erased as there is nothing to move.
184 void Erase( char* address, SizeType elementSize );
187 * @brief Erases a range of elements.
189 * Does not change capacity.
191 * @param[in] first Address to the first element to be erased
192 * @param[in] last Address to the last element to be erased
193 * @param[in] elementSize Size of one of the elements to be erased
194 * @return Address pointing to the next element of the last one
196 char* Erase( char* first, char* last, SizeType elementSize );
199 * @brief Copies a number of bytes from \e source to \e destination.
201 * \e source and \e destination must not overlap.
204 * @param[in] destination Pointer to the destination address
205 * @param[in] source Pointer to the source address
206 * @param[in] numberOfBytes The number of bytes to be copied
208 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
212 // not copyable as it does not know the size of elements
213 VectorBase( const VectorBase& ) = delete; ///< Deleted copy constructor. @SINCE_1_0.0
214 VectorBase& operator=( const VectorBase& ) = delete; ///< Deleted copy assignment operator. @SINCE_1_0.0
216 // not movable as this is handled by deriving classes
217 VectorBase( VectorBase&& ) = delete; ///< Deleted move constructor. @SINCE_1_9.25
218 VectorBase& operator=( VectorBase&& ) = delete; ///< Deleted copy assignment operator. @SINCE_1_9.25
222 void* mData; ///< Pointer to the data.
228 * @brief Vector algorithm variant for trivial types.
230 * Trivial types do not need destructor or copy constructor called.
233 template< bool IsTrivial >
234 class VectorAlgorithms : public VectorBase
236 protected: // API for deriving classes
237 using SizeType = VectorBase::SizeType;
240 * @brief Empty constructor.
247 * @brief Empty destructor.
254 * @brief Copy vector contents.
257 * @param[in] rhs VectorBase object to copy from
258 * @param[in] elementSize Size of the content
260 void Copy( const VectorBase& rhs, SizeType elementSize )
262 if( rhs.Capacity() > 0u )
264 VectorBase::Copy( rhs, elementSize );
268 VectorBase::Release();
273 * @brief Reserves space in the vector.
276 * @param[in] count Count of elements to reserve
277 * @param[in] elementSize Size of a single element
279 void Reserve( SizeType count, SizeType elementSize )
281 VectorBase::Reserve( count, elementSize );
285 * @brief Resizes the vector. Does not change capacity.
288 * @param[in] count Count to resize to
289 * @param[in] elementSize Size of a single element
291 void Resize( SizeType count, SizeType elementSize )
293 // reserve will copy old elements as well
294 Reserve( count, elementSize );
298 * @brief Clears the contents.
300 * For simple types, nothing to do.
307 VectorBase::SetCount( 0u );
312 * @brief Releases the vector.
317 VectorBase::Release();
321 * @brief Erases an element. Does not change capacity.
324 * @param[in] address Address to erase from
325 * @param[in] elementSize Size to erase
327 void Erase( uint8_t* address, SizeType elementSize )
329 VectorBase::Erase( reinterpret_cast< char* >( address ), elementSize );
333 * @brief Erases a range of elements. Does not change capacity.
336 * @param[in] first Address to the first element to be erased
337 * @param[in] last Address to the last element to be erased
338 * @param[in] elementSize Size of one of the elements to be erased
339 * @return Address pointing to the next element of the last one
341 uint8_t* Erase( uint8_t* first, uint8_t* last, SizeType elementSize )
343 return reinterpret_cast< uint8_t* >( VectorBase::Erase( reinterpret_cast< char* >( first ), reinterpret_cast< char *>( last ), elementSize ) );
347 * @brief Inserts the given elements into the vector.
350 * @param[in] at Address where to insert the elements into the vector
351 * @param[in] from Address to the first element to be inserted
352 * @param[in] to Address to the last element to be inserted
353 * @param[in] elementSize Size of one of the elements to be inserted
355 void Insert( uint8_t* at, uint8_t* from, uint8_t* to, SizeType elementSize )
357 const SizeType size = to - from;
358 const SizeType count = Count();
359 const SizeType newCount = count + size / elementSize;
361 if( newCount > Capacity() )
363 // Calculate the at offset as the pointer is invalid after the Reserve() call.
364 std::size_t offset = at - reinterpret_cast<uint8_t*>( mData );
367 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.
369 // Set the new at pointer.
370 at = reinterpret_cast<uint8_t*>( mData ) + offset;
372 // set new count first as otherwise the debug assert will hit us
373 SetCount( newCount );
375 // Move current items to a new position inside the vector.
376 CopyMemory( reinterpret_cast< char* >( at + size ),
377 reinterpret_cast< const char* >( at ),
378 ( reinterpret_cast<uint8_t*>( mData ) + count * elementSize ) - at );
380 // Copy the given items.
381 CopyMemory( reinterpret_cast< char* >( at ), reinterpret_cast< const char* >( from ), size );
388 * @brief Vector algorithm variant for complex types.
390 * Not yet supported so will lead to compile error
391 * as constructor and destructor are private.
392 * TODO add support for this variant.
396 class VectorAlgorithms< false > : public VectorBase
407 * @brief Vector class with minimum space allocation when it's empty.
410 * @param type The type of the data that the vector holds
412 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
413 class Vector : public VectorAlgorithms< IsTrivialType >
418 * @brief Type definitions.
421 using SizeType = VectorBase::SizeType; ///< Size type @SINCE_1_0.0
422 using Iterator = T*; ///< Most simple Iterator is a pointer @SINCE_1_0.0
423 using ConstIterator = const T*; ///< Const iterator @SINCE_1_0.0
424 using ItemType = T; ///< Item type @SINCE_1_0.0
427 * @brief Enumeration for BaseType.
432 BaseType = IsTrivialType ///< @SINCE_1_0.0
436 * @brief Default constructor. Does not allocate any space.
443 * @brief Destructor. Releases the allocated space.
452 * @brief Copy constructor.
455 * @param[in] vector Vector to copy from
457 Vector( const Vector& vector )
459 // reuse copy assignment
464 * @brief Default move constructor.
467 * @param[in] vector Vector to move
469 Vector( Vector&& vector )
471 // reuse move assignment
472 operator=( std::move( vector ) );
476 * @brief Assignment operator.
479 * @param[in] vector Vector to assign from
480 * @return Reference to self for chaining
482 Vector& operator=( const Vector& vector )
484 if( this != &vector )
486 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
492 * @brief Default move assignment operator.
495 * @param[in] vector Vector to move
497 Vector& operator=( Vector&& vector )
499 if( this != &vector )
501 if( VectorBase::mData )
505 VectorBase::mData = vector.mData;
506 vector.mData = nullptr;
512 * @brief Iterator to the beginning of the data.
514 * @return Iterator to the beginning of the data
516 Iterator Begin() const
518 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
523 * @brief Iterator to the end of the data (one past last element).
525 * @return Iterator to the end of the data (one past last element)
529 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
530 address += VectorBase::Count();
535 * Support for C++11 Range-based for loop: for( item : container ).
537 * @return The start iterator
539 Iterator begin() const
545 * Support for C++11 Range-based for loop: for( item : container ).
547 * @return The end iterator
555 * @brief Subscript operator.
557 * @param[in] index Index of the element
558 * @return Reference to the element for given index
559 * @pre Index must be in the vector's range.
561 ItemType& operator[]( SizeType index )
563 // reuse the const version
564 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
568 * @brief Subscript operator.
570 * @param[in] index Index of the element
571 * @return Reference to the element for given index
572 * @pre Index must be in the vector's range.
574 const ItemType& operator[]( SizeType index ) const
576 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
577 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
578 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
584 * @brief Pushes back an element to the vector.
586 * The underlying storage may be reallocated to provide space.
587 * If this occurs, all pre-existing pointers into the vector will
591 * @param[in] element Element to be added
593 void PushBack( const ItemType& element )
595 const SizeType count = VectorBase::Count();
596 const SizeType newCount = count + 1u;
597 const SizeType capacity = VectorBase::Capacity();
598 if( newCount > capacity )
601 Reserve( newCount << 1u ); // reserve double the current count
603 // set new count first as otherwise the debug assert will hit us
604 VectorBase::SetCount( newCount );
605 operator[]( count ) = element;
609 * @brief Inserts an element to the vector.
611 * Elements after \e at are moved one position to the right.
613 * The underlying storage may be reallocated to provide space.
614 * If this occurs, all pre-existing pointers into the vector will
618 * @param[in] at Iterator where to insert the elements into the vector
619 * @param[in] element An element to be added
620 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
622 void Insert( Iterator at, const ItemType& element )
624 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
625 const SizeType size = sizeof( ItemType );
626 uint8_t* address = const_cast<uint8_t*>( reinterpret_cast<const uint8_t*>( &element ) );
627 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< uint8_t* >( at ),
634 * @brief Inserts the given elements into the vector.
636 * Elements after \e at are moved the number of given elements positions to the right.
638 * The underlying storage may be reallocated to provide space.
639 * If this occurs, all pre-existing pointers into the vector will
643 * @param[in] at Iterator where to insert the elements into the vector
644 * @param[in] from Iterator to the first element to be inserted
645 * @param[in] to Iterator to the last element to be inserted
646 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
647 * @pre Iterators \e from and \e to must be valid iterators.
648 * @pre Iterator \e from must not be grater than Iterator \e to.
651 void Insert( Iterator at, Iterator from, Iterator to )
653 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
654 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
662 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< uint8_t* >( at ),
663 reinterpret_cast< uint8_t* >( from ),
664 reinterpret_cast< uint8_t* >( to ),
665 sizeof( ItemType ) );
669 * @brief Reserves space in the vector.
671 * Reserving less than current Capacity is a no-op.
673 * @param[in] count Count of elements to reserve
675 void Reserve( SizeType count )
677 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
681 * @brief Resizes the vector. Does not change capacity.
684 * @param[in] count Count to resize to
686 void Resize( SizeType count )
688 ItemType item = ItemType();
693 * @brief Resizes the vector without initializing the data.
695 * Can be used as a data container for reading whole file content.
697 * @param[in] count Count to resize to
699 void ResizeUninitialized( SizeType count )
702 VectorBase::SetCount( count );
706 * @brief Resizes the vector. Does not change capacity.
709 * @param[in] count Count to resize to
710 * @param[in] item An item to insert to the new indices
712 void Resize( SizeType count, const ItemType& item )
714 const SizeType oldCount = VectorBase::Count();
715 if( count <= oldCount )
717 // getting smaller so just set count
718 VectorBase::SetCount( count );
722 // remember how many new items get added
723 SizeType newItems = count - oldCount;
725 for( ; newItems > 0u; --newItems )
733 * @brief Erases an element.
735 * Does not change capacity. Other elements get moved.
738 * @param[in] iterator Iterator pointing to the item to remove
739 * @return Iterator pointing to next element
740 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
743 Iterator Erase( Iterator iterator )
745 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
746 if( iterator < ( End() - 1u ) )
748 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< uint8_t* >( iterator ), sizeof( ItemType ) );
752 // just remove the element
759 * @brief Erases a range of elements.
761 * Does not change capacity. Other elements get moved.
764 * @param[in] first Iterator to the first element to be erased
765 * @param[in] last Iterator to the last element to be erased
767 * @return Iterator pointing to the next element of the last one
768 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
769 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
770 * @pre Iterator \e first must not be grater than Iterator \e last.
773 Iterator Erase( Iterator first, Iterator last )
775 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
776 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
777 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
779 Iterator nextElement;
783 // Erase up to the end.
784 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
789 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< uint8_t* >( first ),
790 reinterpret_cast< uint8_t* >( last ),
791 sizeof( ItemType ) ) );
798 * @brief Removes an element.
800 * Does not maintain order. Swaps the element with end and
801 * decreases size by one. This is much faster than Erase so use
802 * this in case order does not matter. Does not change capacity.
805 * @param[in] iterator Iterator pointing to the item to remove
806 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
809 void Remove( Iterator iterator )
811 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
813 Iterator last = End() - 1u;
814 if( last > iterator )
816 std::swap( *iterator, *last );
818 VectorBase::SetCount( VectorBase::Count() - 1u );
822 * @brief Swaps the contents of two vectors.
825 * @param[in] vector Vector to swap with
827 void Swap( Vector& vector )
829 VectorBase::Swap( vector );
833 * @brief Clears the contents of the vector. Keeps its capacity.
838 VectorAlgorithms<BaseType>::Clear();
842 * @brief Releases the memory that the vector holds.
847 VectorAlgorithms<BaseType>::Release();
856 #endif // DALI_VECTOR_H