5 * Copyright (c) 2019 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
66 typedef std::size_t SizeType;
68 protected: // Construction
71 * @brief Default constructor. Does not allocate space.
79 * Does not release the space. Derived class needs to call Release.
80 * Not virtual as this should not be called directly and we do not want
81 * a vtable for this class as it would unnecessarily increase size.
89 * @brief This method is inlined as it's needed frequently for Vector::End() iterator.
92 * @return The count of elements in this vector
94 SizeType Count() const
99 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
100 items = *(metadata - 1u);
106 * @brief Gets the count of elements in this vector.
108 * @return The count of elements in this vector
110 SizeType Size() const
116 * @brief @ return if the vector is empty.
118 * @return True if the count of elements is empty
122 return Count() == 0u;
126 * @brief Gets the capacity of this vector.
128 * @return The capacity of this vector
130 SizeType Capacity() const;
133 * @brief Releases the data.
135 * Does not call destructors on objects held.
140 protected: // for Derived classes
143 * @brief Helper to set the count.
146 * @param[in] count Number of elements in the vector
148 void SetCount( SizeType count );
151 * @brief Reserves space in the vector.
154 * @param[in] count Count of elements to reserve
155 * @param[in] elementSize Size of a single element
157 void Reserve( SizeType count, SizeType elementSize );
160 * @brief Copy a vector.
163 * @param[in] vector Vector to copy from
164 * @param[in] elementSize Size of a single element
166 void Copy( const VectorBase& vector, SizeType elementSize );
169 * @brief Swaps the contents of two vectors.
172 * @param[in] vector Vector to swap with
174 void Swap( VectorBase& vector );
177 * @brief Erases an element.
179 * Does not change capacity.
181 * @param[in] address Address to erase from
182 * @param[in] elementSize Size to erase
183 * @pre Last element cannot be erased as there is nothing to move.
185 void Erase( char* address, SizeType elementSize );
188 * @brief Erases a range of elements.
190 * Does not change capacity.
192 * @param[in] first Address to the first element to be erased
193 * @param[in] last Address to the last element to be erased
194 * @param[in] elementSize Size of one of the elements to be erased
195 * @return Address pointing to the next element of the last one
197 char* Erase( char* first, char* last, SizeType elementSize );
200 * @brief Copies a number of bytes from \e source to \e destination.
202 * \e source and \e destination must not overlap.
205 * @param[in] destination Pointer to the destination address
206 * @param[in] source Pointer to the source address
207 * @param[in] numberOfBytes The number of bytes to be copied
209 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
213 // not copyable as it does not know the size of elements
214 VectorBase( const VectorBase& ); ///< Undefined @SINCE_1_0.0
215 VectorBase& operator=( const VectorBase& ); ///< Undefined @SINCE_1_0.0
219 void* mData; ///< Pointer to the data.
225 * @brief Vector algorithm variant for trivial types.
227 * Trivial types do not need destructor or copy constructor called.
230 template< bool IsTrivial >
231 class VectorAlgorithms : public VectorBase
233 protected: // API for deriving classes
235 typedef VectorBase::SizeType SizeType;
238 * @brief Empty constructor.
245 * @brief Empty destructor.
252 * @brief Copy vector contents.
255 * @param[in] rhs VectorBase object to copy from
256 * @param[in] elementSize Size of the content
258 void Copy( const VectorBase& rhs, SizeType elementSize )
260 if( rhs.Capacity() > 0u )
262 VectorBase::Copy( rhs, elementSize );
266 VectorBase::Release();
271 * @brief Reserves space in the vector.
274 * @param[in] count Count of elements to reserve
275 * @param[in] elementSize Size of a single element
277 void Reserve( SizeType count, SizeType elementSize )
279 VectorBase::Reserve( count, elementSize );
283 * @brief Resizes the vector. Does not change capacity.
286 * @param[in] count Count to resize to
287 * @param[in] elementSize Size of a single element
289 void Resize( SizeType count, SizeType elementSize )
291 // reserve will copy old elements as well
292 Reserve( count, elementSize );
296 * @brief Clears the contents.
298 * For simple types, nothing to do.
305 VectorBase::SetCount( 0u );
310 * @brief Releases the vector.
315 VectorBase::Release();
319 * @brief Erases an element. Does not change capacity.
322 * @param[in] address Address to erase from
323 * @param[in] elementSize Size to erase
325 void Erase( uint8_t* address, SizeType elementSize )
327 VectorBase::Erase( reinterpret_cast< char* >( address ), elementSize );
331 * @brief Erases a range of elements. Does not change capacity.
334 * @param[in] first Address to the first element to be erased
335 * @param[in] last Address to the last element to be erased
336 * @param[in] elementSize Size of one of the elements to be erased
337 * @return Address pointing to the next element of the last one
339 uint8_t* Erase( uint8_t* first, uint8_t* last, SizeType elementSize )
341 return reinterpret_cast< uint8_t* >( VectorBase::Erase( reinterpret_cast< char* >( first ), reinterpret_cast< char *>( last ), elementSize ) );
345 * @brief Inserts the given elements into the vector.
348 * @param[in] at Address where to insert the elements into the vector
349 * @param[in] from Address to the first element to be inserted
350 * @param[in] to Address to the last element to be inserted
351 * @param[in] elementSize Size of one of the elements to be inserted
353 void Insert( uint8_t* at, uint8_t* from, uint8_t* to, SizeType elementSize )
355 const SizeType size = to - from;
356 const SizeType count = Count();
357 const SizeType newCount = count + size / elementSize;
359 if( newCount > Capacity() )
361 // Calculate the at offset as the pointer is invalid after the Reserve() call.
362 std::size_t offset = at - reinterpret_cast<uint8_t*>( mData );
365 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.
367 // Set the new at pointer.
368 at = reinterpret_cast<uint8_t*>( mData ) + offset;
370 // set new count first as otherwise the debug assert will hit us
371 SetCount( newCount );
373 // Move current items to a new position inside the vector.
374 CopyMemory( reinterpret_cast< char* >( at + size ),
375 reinterpret_cast< const char* >( at ),
376 ( reinterpret_cast<uint8_t*>( mData ) + count * elementSize ) - at );
378 // Copy the given items.
379 CopyMemory( reinterpret_cast< char* >( at ), reinterpret_cast< const char* >( from ), size );
386 * @brief Vector algorithm variant for complex types.
388 * Not yet supported so will lead to compile error
389 * as constructor and destructor are private.
390 * TODO add support for this variant.
394 class VectorAlgorithms< false > : public VectorBase
405 * @brief Vector class with minimum space allocation when it's empty.
408 * @param type The type of the data that the vector holds
410 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
411 class Vector : public VectorAlgorithms< IsTrivialType >
416 * @brief Type definitions.
419 typedef VectorBase::SizeType SizeType; ///< Size type @SINCE_1_0.0
420 typedef T* Iterator; ///< Most simple Iterator is a pointer @SINCE_1_0.0
421 typedef const T* ConstIterator; ///< Const iterator @SINCE_1_0.0
422 typedef T ItemType; ///< Item type @SINCE_1_0.0
425 * @brief Enumeration for BaseType.
430 BaseType = IsTrivialType ///< @SINCE_1_0.0
434 * @brief Default constructor. Does not allocate any space.
441 * @brief Destructor. Releases the allocated space.
450 * @brief Copy constructor.
453 * @param[in] vector Vector to copy from
455 Vector( const Vector& vector )
462 * @brief Assignment operator.
465 * @param[in] vector Vector to assign from
466 * @return Reference to self for chaining
468 Vector& operator=( const Vector& vector )
470 if( this != &vector )
472 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
478 * @brief Iterator to the beginning of the data.
480 * @return Iterator to the beginning of the data
482 Iterator Begin() const
484 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
489 * @brief Iterator to the end of the data (one past last element).
491 * @return Iterator to the end of the data (one past last element)
495 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
496 address += VectorBase::Count();
501 * Support for C++11 Range-based for loop: for( item : container ).
503 * @return The start iterator
505 Iterator begin() const
511 * Support for C++11 Range-based for loop: for( item : container ).
513 * @return The end iterator
521 * @brief Subscript operator.
523 * @param[in] index Index of the element
524 * @return Reference to the element for given index
525 * @pre Index must be in the vector's range.
527 ItemType& operator[]( SizeType index )
529 // reuse the const version
530 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
534 * @brief Subscript operator.
536 * @param[in] index Index of the element
537 * @return Reference to the element for given index
538 * @pre Index must be in the vector's range.
540 const ItemType& operator[]( SizeType index ) const
542 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
543 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
544 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
550 * @brief Pushes back an element to the vector.
552 * The underlying storage may be reallocated to provide space.
553 * If this occurs, all pre-existing pointers into the vector will
557 * @param[in] element Element to be added
559 void PushBack( const ItemType& element )
561 const SizeType count = VectorBase::Count();
562 const SizeType newCount = count + 1u;
563 const SizeType capacity = VectorBase::Capacity();
564 if( newCount > capacity )
567 Reserve( newCount << 1u ); // reserve double the current count
569 // set new count first as otherwise the debug assert will hit us
570 VectorBase::SetCount( newCount );
571 operator[]( count ) = element;
575 * @brief Inserts an element to the vector.
577 * Elements after \e at are moved one position to the right.
579 * The underlying storage may be reallocated to provide space.
580 * If this occurs, all pre-existing pointers into the vector will
584 * @param[in] at Iterator where to insert the elements into the vector
585 * @param[in] element An element to be added
586 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
588 void Insert( Iterator at, const ItemType& element )
590 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
591 const SizeType size = sizeof( ItemType );
592 uint8_t* address = const_cast<uint8_t*>( reinterpret_cast<const uint8_t*>( &element ) );
593 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< uint8_t* >( at ),
600 * @brief Inserts the given elements into the vector.
602 * Elements after \e at are moved the number of given elements positions to the right.
604 * The underlying storage may be reallocated to provide space.
605 * If this occurs, all pre-existing pointers into the vector will
609 * @param[in] at Iterator where to insert the elements into the vector
610 * @param[in] from Iterator to the first element to be inserted
611 * @param[in] to Iterator to the last element to be inserted
612 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
613 * @pre Iterators \e from and \e to must be valid iterators.
614 * @pre Iterator \e from must not be grater than Iterator \e to.
617 void Insert( Iterator at, Iterator from, Iterator to )
619 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
620 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
628 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< uint8_t* >( at ),
629 reinterpret_cast< uint8_t* >( from ),
630 reinterpret_cast< uint8_t* >( to ),
631 sizeof( ItemType ) );
635 * @brief Reserves space in the vector.
637 * Reserving less than current Capacity is a no-op.
639 * @param[in] count Count of elements to reserve
641 void Reserve( SizeType count )
643 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
647 * @brief Resizes the vector. Does not change capacity.
650 * @param[in] count Count to resize to
652 void Resize( SizeType count )
654 ItemType item = ItemType();
659 * @brief Resizes the vector. Does not change capacity.
662 * @param[in] count Count to resize to
663 * @param[in] item An item to insert to the new indices
665 void Resize( SizeType count, const ItemType& item )
667 const SizeType oldCount = VectorBase::Count();
668 if( count <= oldCount )
670 // getting smaller so just set count
671 VectorBase::SetCount( count );
675 // remember how many new items get added
676 SizeType newItems = count - oldCount;
678 for( ; newItems > 0u; --newItems )
686 * @brief Erases an element.
688 * Does not change capacity. Other elements get moved.
691 * @param[in] iterator Iterator pointing to the item to remove
692 * @return Iterator pointing to next element
693 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
696 Iterator Erase( Iterator iterator )
698 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
699 if( iterator < ( End() - 1u ) )
701 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< uint8_t* >( iterator ), sizeof( ItemType ) );
705 // just remove the element
712 * @brief Erases a range of elements.
714 * Does not change capacity. Other elements get moved.
717 * @param[in] first Iterator to the first element to be erased
718 * @param[in] last Iterator to the last element to be erased
720 * @return Iterator pointing to the next element of the last one
721 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
722 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
723 * @pre Iterator \e first must not be grater than Iterator \e last.
726 Iterator Erase( Iterator first, Iterator last )
728 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
729 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
730 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
732 Iterator nextElement;
736 // Erase up to the end.
737 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
742 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< uint8_t* >( first ),
743 reinterpret_cast< uint8_t* >( last ),
744 sizeof( ItemType ) ) );
751 * @brief Removes an element.
753 * Does not maintain order. Swaps the element with end and
754 * decreases size by one. This is much faster than Erase so use
755 * this in case order does not matter. Does not change capacity.
758 * @param[in] iterator Iterator pointing to the item to remove
759 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
762 void Remove( Iterator iterator )
764 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
766 Iterator last = End() - 1u;
767 if( last > iterator )
769 std::swap( *iterator, *last );
771 VectorBase::SetCount( VectorBase::Count() - 1u );
775 * @brief Swaps the contents of two vectors.
778 * @param[in] vector Vector to swap with
780 void Swap( Vector& vector )
782 VectorBase::Swap( vector );
786 * @brief Clears the contents of the vector. Keeps its capacity.
791 VectorAlgorithms<BaseType>::Clear();
795 * @brief Releases the memory that the vector holds.
800 VectorAlgorithms<BaseType>::Release();
809 #endif // DALI_VECTOR_H