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.
224 * @brief Vector algorithm variant for trivial types.
226 * Trivial types do not need destructor or copy constructor called.
229 template< bool IsTrivial >
230 class VectorAlgorithms : public VectorBase
232 protected: // API for deriving classes
234 typedef VectorBase::SizeType SizeType;
237 * @brief Empty constructor.
244 * @brief Empty destructor.
251 * @brief Copy vector contents.
254 * @param[in] rhs VectorBase object to copy from
255 * @param[in] elementSize Size of the content
257 void Copy( const VectorBase& rhs, SizeType elementSize )
259 if( rhs.Capacity() > 0u )
261 VectorBase::Copy( rhs, elementSize );
265 VectorBase::Release();
270 * @brief Reserves space in the vector.
273 * @param[in] count Count of elements to reserve
274 * @param[in] elementSize Size of a single element
276 void Reserve( SizeType count, SizeType elementSize )
278 VectorBase::Reserve( count, elementSize );
282 * @brief Resizes the vector. Does not change capacity.
285 * @param[in] count Count to resize to
286 * @param[in] elementSize Size of a single element
288 void Resize( SizeType count, SizeType elementSize )
290 // reserve will copy old elements as well
291 Reserve( count, elementSize );
295 * @brief Clears the contents.
297 * For simple types, nothing to do.
304 VectorBase::SetCount( 0u );
309 * @brief Releases the vector.
314 VectorBase::Release();
318 * @brief Erases an element. Does not change capacity.
321 * @param[in] address Address to erase from
322 * @param[in] elementSize Size to erase
324 void Erase( uint8_t* address, SizeType elementSize )
326 VectorBase::Erase( reinterpret_cast< char* >( address ), elementSize );
330 * @brief Erases a range of elements. Does not change capacity.
333 * @param[in] first Address to the first element to be erased
334 * @param[in] last Address to the last element to be erased
335 * @param[in] elementSize Size of one of the elements to be erased
336 * @return Address pointing to the next element of the last one
338 uint8_t* Erase( uint8_t* first, uint8_t* last, SizeType elementSize )
340 return reinterpret_cast< uint8_t* >( VectorBase::Erase( reinterpret_cast< char* >( first ), reinterpret_cast< char *>( last ), elementSize ) );
344 * @brief Inserts the given elements into the vector.
347 * @param[in] at Address where to insert the elements into the vector
348 * @param[in] from Address to the first element to be inserted
349 * @param[in] to Address to the last element to be inserted
350 * @param[in] elementSize Size of one of the elements to be inserted
352 void Insert( uint8_t* at, uint8_t* from, uint8_t* to, SizeType elementSize )
354 const SizeType size = to - from;
355 const SizeType count = Count();
356 const SizeType newCount = count + size / elementSize;
358 if( newCount > Capacity() )
360 // Calculate the at offset as the pointer is invalid after the Reserve() call.
361 std::size_t offset = at - reinterpret_cast<uint8_t*>( mData );
364 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.
366 // Set the new at pointer.
367 at = reinterpret_cast<uint8_t*>( mData ) + offset;
369 // set new count first as otherwise the debug assert will hit us
370 SetCount( newCount );
372 // Move current items to a new position inside the vector.
373 CopyMemory( reinterpret_cast< char* >( at + size ),
374 reinterpret_cast< const char* >( at ),
375 ( reinterpret_cast<uint8_t*>( mData ) + count * elementSize ) - at );
377 // Copy the given items.
378 CopyMemory( reinterpret_cast< char* >( at ), reinterpret_cast< const char* >( from ), size );
383 * @brief Vector algorithm variant for complex types.
385 * Not yet supported so will lead to compile error
386 * as constructor and destructor are private.
387 * TODO add support for this variant.
391 class VectorAlgorithms< false > : public VectorBase
401 * @brief Vector class with minimum space allocation when it's empty.
404 * @param type The type of the data that the vector holds
406 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
407 class Vector : public VectorAlgorithms< IsTrivialType >
412 * @brief Type definitions.
415 typedef VectorBase::SizeType SizeType; ///< Size type @SINCE_1_0.0
416 typedef T* Iterator; ///< Most simple Iterator is a pointer @SINCE_1_0.0
417 typedef const T* ConstIterator; ///< Const iterator @SINCE_1_0.0
418 typedef T ItemType; ///< Item type @SINCE_1_0.0
421 * @brief Enumeration for BaseType.
426 BaseType = IsTrivialType ///< @SINCE_1_0.0
430 * @brief Default constructor. Does not allocate any space.
437 * @brief Destructor. Releases the allocated space.
446 * @brief Copy constructor.
449 * @param[in] vector Vector to copy from
451 Vector( const Vector& vector )
458 * @brief Assignment operator.
461 * @param[in] vector Vector to assign from
462 * @return Reference to self for chaining
464 Vector& operator=( const Vector& vector )
466 if( this != &vector )
468 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
474 * @brief Iterator to the beginning of the data.
476 * @return Iterator to the beginning of the data
478 Iterator Begin() const
480 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
485 * @brief Iterator to the end of the data (one past last element).
487 * @return Iterator to the end of the data (one past last element)
491 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
492 address += VectorBase::Count();
497 * Support for C++11 Range-based for loop: for( item : container ).
499 * @return The start iterator
501 Iterator begin() const
507 * Support for C++11 Range-based for loop: for( item : container ).
509 * @return The end iterator
517 * @brief Subscript operator.
519 * @param[in] index Index of the element
520 * @return Reference to the element for given index
521 * @pre Index must be in the vector's range.
523 ItemType& operator[]( SizeType index )
525 // reuse the const version
526 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
530 * @brief Subscript operator.
532 * @param[in] index Index of the element
533 * @return Reference to the element for given index
534 * @pre Index must be in the vector's range.
536 const ItemType& operator[]( SizeType index ) const
538 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
539 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
540 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
546 * @brief Pushes back an element to the vector.
548 * The underlying storage may be reallocated to provide space.
549 * If this occurs, all pre-existing pointers into the vector will
553 * @param[in] element Element to be added
555 void PushBack( const ItemType& element )
557 const SizeType count = VectorBase::Count();
558 const SizeType newCount = count + 1u;
559 const SizeType capacity = VectorBase::Capacity();
560 if( newCount > capacity )
563 Reserve( newCount << 1u ); // reserve double the current count
565 // set new count first as otherwise the debug assert will hit us
566 VectorBase::SetCount( newCount );
567 operator[]( count ) = element;
571 * @brief Inserts an element to the vector.
573 * Elements after \e at are moved one position to the right.
575 * The underlying storage may be reallocated to provide space.
576 * If this occurs, all pre-existing pointers into the vector will
580 * @param[in] at Iterator where to insert the elements into the vector
581 * @param[in] element An element to be added
582 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
584 void Insert( Iterator at, const ItemType& element )
586 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
587 const SizeType size = sizeof( ItemType );
588 uint8_t* address = const_cast<uint8_t*>( reinterpret_cast<const uint8_t*>( &element ) );
589 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< uint8_t* >( at ),
596 * @brief Inserts the given elements into the vector.
598 * Elements after \e at are moved the number of given elements positions to the right.
600 * The underlying storage may be reallocated to provide space.
601 * If this occurs, all pre-existing pointers into the vector will
605 * @param[in] at Iterator where to insert the elements into the vector
606 * @param[in] from Iterator to the first element to be inserted
607 * @param[in] to Iterator to the last element to be inserted
608 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
609 * @pre Iterators \e from and \e to must be valid iterators.
610 * @pre Iterator \e from must not be grater than Iterator \e to.
613 void Insert( Iterator at, Iterator from, Iterator to )
615 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
616 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
624 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< uint8_t* >( at ),
625 reinterpret_cast< uint8_t* >( from ),
626 reinterpret_cast< uint8_t* >( to ),
627 sizeof( ItemType ) );
631 * @brief Reserves space in the vector.
633 * Reserving less than current Capacity is a no-op.
635 * @param[in] count Count of elements to reserve
637 void Reserve( SizeType count )
639 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
643 * @brief Resizes the vector. Does not change capacity.
646 * @param[in] count Count to resize to
648 void Resize( SizeType count )
650 ItemType item = ItemType();
655 * @brief Resizes the vector. Does not change capacity.
658 * @param[in] count Count to resize to
659 * @param[in] item An item to insert to the new indices
661 void Resize( SizeType count, const ItemType& item )
663 const SizeType oldCount = VectorBase::Count();
664 if( count <= oldCount )
666 // getting smaller so just set count
667 VectorBase::SetCount( count );
671 // remember how many new items get added
672 SizeType newItems = count - oldCount;
674 for( ; newItems > 0u; --newItems )
682 * @brief Erases an element.
684 * Does not change capacity. Other elements get moved.
687 * @param[in] iterator Iterator pointing to the item to remove
688 * @return Iterator pointing to next element
689 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
692 Iterator Erase( Iterator iterator )
694 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
695 if( iterator < ( End() - 1u ) )
697 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< uint8_t* >( iterator ), sizeof( ItemType ) );
701 // just remove the element
708 * @brief Erases a range of elements.
710 * Does not change capacity. Other elements get moved.
713 * @param[in] first Iterator to the first element to be erased
714 * @param[in] last Iterator to the last element to be erased
716 * @return Iterator pointing to the next element of the last one
717 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
718 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
719 * @pre Iterator \e first must not be grater than Iterator \e last.
722 Iterator Erase( Iterator first, Iterator last )
724 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
725 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
726 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
728 Iterator nextElement;
732 // Erase up to the end.
733 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
738 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< uint8_t* >( first ),
739 reinterpret_cast< uint8_t* >( last ),
740 sizeof( ItemType ) ) );
747 * @brief Removes an element.
749 * Does not maintain order. Swaps the element with end and
750 * decreases size by one. This is much faster than Erase so use
751 * this in case order does not matter. Does not change capacity.
754 * @param[in] iterator Iterator pointing to the item to remove
755 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
758 void Remove( Iterator iterator )
760 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
762 Iterator last = End() - 1u;
763 if( last > iterator )
765 std::swap( *iterator, *last );
767 VectorBase::SetCount( VectorBase::Count() - 1u );
771 * @brief Swaps the contents of two vectors.
774 * @param[in] vector Vector to swap with
776 void Swap( Vector& vector )
778 VectorBase::Swap( vector );
782 * @brief Clears the contents of the vector. Keeps its capacity.
787 VectorAlgorithms<BaseType>::Clear();
791 * @brief Releases the memory that the vector holds.
796 VectorAlgorithms<BaseType>::Release();
805 #endif // DALI_VECTOR_H