1 #ifndef __DALI_VECTOR_H__
2 #define __DALI_VECTOR_H__
5 * Copyright (c) 2015 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.
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/type-traits.h>
28 #include <dali/public-api/math/math-utils.h>
31 * @brief For DALi internal use asserts are enabled in debug builds.
33 * For Application use asserts can be enabled manually.
36 #if defined( DEBUG_ENABLED )
37 #define ENABLE_VECTOR_ASSERTS
40 #if defined( ENABLE_VECTOR_ASSERTS )
41 #define DALI_ASSERT_VECTOR(cond) DALI_ASSERT_ALWAYS(cond)
43 #define DALI_ASSERT_VECTOR(cond)
49 * @addtogroup dali_core_common
54 * @brief Base class to handle the memory of simple vector.
56 * Memory layout is such that it has two std::size_t to hold the count
57 * and capacity of the vector. VectorBase::mData is adjusted so that it points to the
58 * beginning of the first real item so that iterating the items is quick.
61 class DALI_IMPORT_API VectorBase
65 typedef std::size_t SizeType; ///< Size type @since_tizen 2.4
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 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 its 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 Gets the capacity of this vector.
117 * @return The capacity of this vector.
119 SizeType Capacity() const;
122 * @brief Release the data.
124 * Does not call destructors on objects held.
129 protected: // for Derived classes
132 * @brief Helper to set the count.
135 * @param[in] count Number of elements in the vector.
137 void SetCount( SizeType count );
140 * @brief Reserve space in the vector.
143 * @param[in] count Count of elements to reserve.
144 * @param[in] elementSize Size of a single element.
146 void Reserve( SizeType count, SizeType elementSize );
149 * @brief Copy a vector.
152 * @param[in] vector Vector to copy from.
153 * @param[in] elementSize Size of a single element.
155 void Copy( const VectorBase& vector, SizeType elementSize );
158 * @brief Swap the contents of two vectors.
161 * @param[in] vector Vector to swap with.
163 void Swap( VectorBase& vector );
166 * @brief Erase an element.
168 * Does not change capacity.
170 * @param[in] address Adress to erase from.
171 * @param[in] elementSize Size to erase.
172 * @pre Last element cannot be erased as there is nothing to move.
174 void Erase( char* address, SizeType elementSize );
177 * @brief Erase a range of elements.
179 * Does not change capacity.
181 * @param[in] first Address to the first element to be erased.
182 * @param[in] last Address to the last element to be erased.
183 * @param[in] elementSize Size of one of the elements to be erased.
184 * @return Address pointing to the next element of the last one.
186 char* Erase( char* first, char* last, SizeType elementSize );
189 * @brief Copies a number of bytes from \e source to \e destination.
191 * \e source and \e destination must not overlap.
194 * @param[in] destination Pointer to the destination address.
195 * @param[in] source Pointer to the source address.
196 * @param[in] numberOfBytes The number of bytes to be copied.
198 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
202 // not copiable as it does not know the size of elements
203 VectorBase( const VectorBase& ); ///< Undefined @since_tizen 2.4
204 VectorBase& operator=( const VectorBase& ); ///< Undefined @since_tizen 2.4
208 void* mData; ///< Pointer to the data.
213 * @brief Vector algorithm variant for trivial types.
215 * Trivial types do not need destructor or copy constructor called.
218 template< bool IsTrivial >
219 class VectorAlgorithms : public VectorBase
221 protected: // API for deriving classes
223 typedef VectorBase::SizeType SizeType; ///< Size type @since_tizen 2.4
226 * @brief Empty constructor.
233 * @brief Empty destructor.
240 * @brief Copy vector contents.
243 * @param[in] rhs VectorBase object to copy from.
244 * @param[in] elementSize Size of the content.
246 void Copy( const VectorBase& rhs, SizeType elementSize )
248 if( rhs.Capacity() > 0u )
250 VectorBase::Copy( rhs, elementSize );
254 VectorBase::Release();
259 * @brief Reserve space in the vector.
262 * @param[in] count Count of elements to reserve.
263 * @param[in] elementSize Size of a single element.
265 void Reserve( SizeType count, SizeType elementSize )
267 VectorBase::Reserve( count, elementSize );
271 * @brief Resize the vector. Does not change capacity.
274 * @param[in] count Count to resize to.
275 * @param[in] elementSize Size of a single element.
277 void Resize( SizeType count, SizeType elementSize )
279 // reserve will copy old elements as well
280 Reserve( count, elementSize );
284 * @brief Clear the contents.
286 * For simple types nothing to do.
293 VectorBase::SetCount( 0u );
298 * @brief Release the vector.
303 VectorBase::Release();
307 * @brief Erase an element. Does not change capacity.
310 * @param[in] address Address to erase from.
311 * @param[in] elementSize Size to erase.
313 void Erase( char* address, SizeType elementSize )
315 VectorBase::Erase( address, elementSize );
319 * @brief Erase a range of elements. Does not change capacity.
322 * @param[in] first Address to the first element to be erased.
323 * @param[in] last Address to the last element to be erased.
324 * @param[in] elementSize Size of one of the elements to be erased.
325 * @return Address pointing to the next element of the last one.
327 char* Erase( char* first, char* last, SizeType elementSize )
329 return VectorBase::Erase( first, last, elementSize );
333 * @brief Inserts the given elements into the vector.
336 * @param[in] at Address where to insert the elements into the vector.
337 * @param[in] from Address to the first element to be inserted.
338 * @param[in] to Address to the last element to be inserted.
339 * @param[in] elementSize Size of one of the elements to be inserted.
341 void Insert( char* at, char* from, char* to, SizeType elementSize )
343 const SizeType size = to - from;
344 const SizeType count = Count();
345 const SizeType newCount = count + size / elementSize;
347 if( newCount > Capacity() )
349 // Calculate the at offset as the pointer is invalid after the Reserve() call.
350 std::size_t offset = at - reinterpret_cast<char*>( mData );
353 Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
355 // Set the new at pointer.
356 at = reinterpret_cast<char*>( mData ) + offset;
358 // set new count first as otherwise the debug assert will hit us
359 SetCount( newCount );
361 // Move current items to a new position inside the vector.
362 CopyMemory( at + size,
364 ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
366 // Copy the given items.
367 CopyMemory( at, from, size );
372 * @brief Vector algorithm variant for complex types.
374 * Not yet supported so will lead to compile error
375 * as constructor and destructor are private.
376 * TODO add support for this variant.
380 class VectorAlgorithms< false > : public VectorBase
390 * @brief Vector class with minimum space allocation when its empty.
393 * @param[in] type of the data that the vector holds.
395 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
396 class Vector : public VectorAlgorithms< IsTrivialType >
401 * @brief Type definitions.
404 typedef VectorBase::SizeType SizeType; ///< Size type @since_tizen 2.4
405 typedef T* Iterator; ///< Most simple Iterator is a pointer @since_tizen 2.4
406 typedef const T* ConstIterator; ///< Const iterator @since_tizen 2.4
407 typedef T ItemType; ///< Item type @since_tizen 2.4
411 BaseType = IsTrivialType
415 * @brief Default constructor. Does not allocate any space.
422 * @brief Destructor. Releases the allocated space.
431 * @brief Copy constructor.
434 * @param[in] vector Vector to copy from.
436 Vector( const Vector& vector )
443 * @brief Assignment operator.
446 * @param[in] vector Vector to assign from.
447 * @return Reference to self for chaining.
449 Vector& operator=( const Vector& vector )
451 if( this != &vector )
453 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
459 * @brief Iterator to the beginning of the data.
461 * @return Iterator to the beginning of the data.
463 Iterator Begin() const
465 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
470 * @brief Iterator to the end of the data (one past last element).
472 * @return Iterator to the end of the data (one past last element).
476 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
477 address += VectorBase::Count();
482 * @brief Subscript operator.
484 * @param[in] index Index of the element.
485 * @return Reference to the element for given index.
486 * @pre Index must be in the vector's range.
488 ItemType& operator[]( SizeType index )
490 // reuse the const version
491 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
495 * @brief Subscript operator.
497 * @param[in] index of the element.
498 * @return reference to the element for given index.
499 * @pre index must be in the vector's range.
501 const ItemType& operator[]( SizeType index ) const
503 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
504 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
505 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
511 * @brief Push back an element to the vector.
513 * The underlying storage may be reallocated to provide space.
514 * If this occurs, all pre-existing pointers into the vector will
518 * @param[in] element Element to be added.
520 void PushBack( const ItemType& element )
522 const SizeType count = VectorBase::Count();
523 const SizeType newCount = count + 1u;
524 const SizeType capacity = VectorBase::Capacity();
525 if( newCount > capacity )
528 Reserve( newCount << 1u ); // reserve double the current count
530 // set new count first as otherwise the debug assert will hit us
531 VectorBase::SetCount( newCount );
532 operator[]( count ) = element;
536 *@brief Insert an element to the vector.
538 * Elements after \e at are moved one position to the right.
540 * The underlying storage may be reallocated to provide space.
541 * If this occurs, all pre-existing pointers into the vector will
544 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
546 * @param[in] at Iterator where to insert the elements into the vector.
547 * @param[in] element An element to be added.
550 void Insert( Iterator at, const ItemType& element )
552 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
553 const SizeType size = sizeof( ItemType );
554 char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
555 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
562 * @brief Inserts the given elements into the vector.
564 * Elements after \e at are moved the number of given elements positions to the right.
566 * The underlying storage may be reallocated to provide space.
567 * If this occurs, all pre-existing pointers into the vector will
571 * @param[in] at Iterator where to insert the elements into the vector.
572 * @param[in] from Iterator to the first element to be inserted.
573 * @param[in] to Iterator to the last element to be inserted.
574 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
575 * @pre Iterators \e from and \e to must be valid iterators.
576 * @pre Iterator \e from must not be grater than Iterator \e to.
579 void Insert( Iterator at, Iterator from, Iterator to )
581 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
582 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
590 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
591 reinterpret_cast< char* >( from ),
592 reinterpret_cast< char* >( to ),
593 sizeof( ItemType ) );
597 * @brief Reserve space in the vector.
599 * Reserving less than current Capacity is a no-op.
601 * @param[in] count Count of elements to reserve.
603 void Reserve( SizeType count )
605 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
609 * @brief Resize the vector. Does not change capacity.
612 * @param[in] count Count to resize to.
613 * @param[in] item An item to insert to the new indices.
615 void Resize( SizeType count, ItemType item = ItemType() )
617 const SizeType oldCount = VectorBase::Count();
618 if( count <= oldCount )
620 // getting smaller so just set count
621 VectorBase::SetCount( count );
625 // remember how many new items get added
626 SizeType newItems = count - oldCount;
628 for( ; newItems > 0u; --newItems )
636 * @brief Erase an element.
638 * Does not change capacity. Other elements get moved.
641 * @param[in] iterator Iterator pointing to item to remove.
642 * @return Iterator pointing to next element.
643 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
646 Iterator Erase( Iterator iterator )
648 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
649 if( iterator < ( End() - 1u ) )
651 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
655 // just remove the element
662 * @brief Erase a range of elements.
664 * Does not change capacity. Other elements get moved.
667 * @param[in] first Iterator to the first element to be erased.
668 * @param[in] last Iterator to the last element to be erased.
670 * @return Iterator pointing to the next element of the last one.
671 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
672 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
673 * @pre Iterator \e first must not be grater than Iterator \e last.
676 Iterator Erase( Iterator first, Iterator last )
678 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
679 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
680 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
682 Iterator nextElement;
686 // Erase up to the end.
687 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
692 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
693 reinterpret_cast< char* >( last ),
694 sizeof( ItemType ) ) );
701 * @brief Removes an element.
703 * Does not maintain order. Swaps the element with end and
704 * decreases size by one. This is much faster than Erase so use
705 * this in case order does not matter. Does not change capacity.
708 * @param[in] iterator Iterator pointing to item to remove.
709 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
712 void Remove( Iterator iterator )
714 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
716 Iterator last = End() - 1u;
717 if( last > iterator )
719 std::swap( *iterator, *last );
721 VectorBase::SetCount( VectorBase::Count() - 1u );
725 * @brief Swap the contents of two vectors.
728 * @param[in] vector Vector to swap with.
730 void Swap( Vector& vector )
732 VectorBase::Swap( vector );
736 * @brief Clear the contents of the vector. Keeps its capacity.
741 VectorAlgorithms<BaseType>::Clear();
745 * @brief Release the memory that the vector holds.
750 VectorAlgorithms<BaseType>::Release();
759 #endif /* __DALI_VECTOR_H__ */