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.
35 #if defined( DEBUG_ENABLED )
36 #define ENABLE_VECTOR_ASSERTS
39 #if defined( ENABLE_VECTOR_ASSERTS )
40 #define DALI_ASSERT_VECTOR(cond) DALI_ASSERT_ALWAYS(cond)
42 #define DALI_ASSERT_VECTOR(cond)
48 * @addtogroup dali_core_common
53 * @brief Base class to handle the memory of simple vector.
55 * Memory layout is such that it has two std::size_t to hold the count
56 * and capacity of the vector. mData is adjusted so that it points to the
57 * beginning of the first real item so that iterating the items is quick.
59 class DALI_IMPORT_API VectorBase
63 typedef std::size_t SizeType;
65 protected: // Construction
68 * @brief Default constructor. Does not allocate space.
75 * Does not release the space. Derived class needs to call Release.
76 * Not virtual as should not be called directly and we do not want
77 * a vtable for this class as it would unnecessarily increase size.
84 * @brief This method is inlined as its needed frequently for End() iterator.
86 * @return The count of elements in this vector.
88 SizeType Count() const
93 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
94 items = *(metadata - 1u);
100 * @return The count of elements in this vector.
102 SizeType Size() const
108 * @ return If the vector is empty
112 return Count() == 0u;
116 * @return The capacity of this vector.
118 SizeType Capacity() const;
121 * @brief Release the data.
123 * Does not call destructors on objects held.
127 protected: // for Derived classes
130 * @brief Helper to set the count.
132 * @param count Number of elements in the vector.
134 void SetCount( SizeType count );
137 * @brief Reserve space in the vector.
139 * @param count of elements to reserve.
140 * @param elementSize of a single element.
142 void Reserve( SizeType count, SizeType elementSize );
145 * @brief Copy a vector.
147 * @param vector Vector to copy from.
148 * @param elementSize of a single element.
150 void Copy( const VectorBase& vector, SizeType elementSize );
153 * @brief Swap the contents of two vectors.
155 * @param vector Vector to swap with.
157 void Swap( VectorBase& vector );
160 * @brief Erase an element.
162 * Does not change capacity.
163 * @pre last element cannot be erased as there is nothing to move.
164 * @param address to erase from.
165 * @param elementSize to erase.
167 void Erase( char* address, SizeType elementSize );
170 * @brief Erase a range of elements.
172 * Does not change capacity.
173 * @param[in] first Address to the first element to be erased.
174 * @param[in] last Address to the last element to be erased.
175 * @param[in] elementSize Size of one of the elements to be erased.
176 * @return address pointing to the next element of the last one.
178 char* Erase( char* first, char* last, SizeType elementSize );
181 * Copies a number of bytes from \e source to \e destination.
183 * \e source and \e destination must not overlap.
185 * @param[in] destination Pointer to the destination address.
186 * @param[in] source Pointer to the source address.
187 * @param[in] numberOfBytes The number of bytes to be copied.
189 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
193 // not copiable as it does not know the size of elements
194 VectorBase( const VectorBase& ); ///< Undefined
195 VectorBase& operator=( const VectorBase& ); ///< Undefined
199 void* mData; ///< Pointer to the data.
204 * @brief Vector algorithm variant for trivial types.
206 * Trivial types do not need destructor or copy constructor called.
208 template< bool IsTrivial >
209 class VectorAlgorithms : public VectorBase
211 protected: // API for deriving classes
213 typedef VectorBase::SizeType SizeType;
216 * @brief Empty constructor.
222 * @brief Empty destructor.
228 * @brief Copy vector contents.
230 * @param rhs to copy from.
231 * @param elementSize of the content.
233 void Copy( const VectorBase& rhs, SizeType elementSize )
235 if( rhs.Capacity() > 0u )
237 VectorBase::Copy( rhs, elementSize );
241 VectorBase::Release();
246 * @brief Reserve space in the vector.
248 * @param count of elements to reserve.
249 * @param elementSize of a single element.
251 void Reserve( SizeType count, SizeType elementSize )
253 VectorBase::Reserve( count, elementSize );
257 * @brief Resize the vector. Does not change capacity.
259 * @param count to resize to.
260 * @param elementSize of a single element.
262 void Resize( SizeType count, SizeType elementSize )
264 // reserve will copy old elements as well
265 Reserve( count, elementSize );
269 * @brief Clear the contents.
271 * For simple types nothing to do.
277 VectorBase::SetCount( 0u );
282 * @brief Release the vector.
286 VectorBase::Release();
290 * @brief Erase an element. Does not change capacity.
292 * @param address to erase from.
293 * @param elementSize to erase.
295 void Erase( char* address, SizeType elementSize )
297 VectorBase::Erase( address, elementSize );
301 * @brief Erase a range of elements. Does not change capacity.
303 * @param[in] first Address to the first element to be erased.
304 * @param[in] last Address to the last element to be erased.
305 * @param[in] elementSize Size of one of the elements to be erased.
306 * @return address pointing to the next element of the last one.
308 char* Erase( char* first, char* last, SizeType elementSize )
310 return VectorBase::Erase( first, last, elementSize );
314 * @brief Inserts the given elements into the vector.
316 * @param[in] at Address where to insert the elements into the vector.
317 * @param[in] from Address to the first element to be inserted.
318 * @param[in] to Address to the last element to be inserted.
319 * @param[in] elementSize Size of one of the elements to be inserted.
321 void Insert( char* at, char* from, char* to, SizeType elementSize )
323 const SizeType size = to - from;
324 const SizeType count = Count();
325 const SizeType newCount = count + size / elementSize;
327 if( newCount > Capacity() )
329 // Calculate the at offset as the pointer is invalid after the Reserve() call.
330 std::size_t offset = at - reinterpret_cast<char*>( mData );
333 Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
335 // Set the new at pointer.
336 at = reinterpret_cast<char*>( mData ) + offset;
338 // set new count first as otherwise the debug assert will hit us
339 SetCount( newCount );
341 // Move current items to a new position inside the vector.
342 CopyMemory( at + size,
344 ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
346 // Copy the given items.
347 CopyMemory( at, from, size );
352 * @brief Vector algorithm variant for complex types.
354 * Not yet supported so will lead to compile error
355 * as constructor and destructor are private.
356 * TODO add support for this variant.
359 class VectorAlgorithms< false > : public VectorBase
369 * @brief Vector class with minimum space allocation when its empty.
371 * @param type of the data that the vector holds.
373 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
374 class Vector : public VectorAlgorithms< IsTrivialType >
379 * @brief Type definitions.
381 typedef VectorBase::SizeType SizeType;
382 typedef T* Iterator; ///< Most simple Iterator is a pointer
383 typedef const T* ConstIterator;
388 BaseType = IsTrivialType
392 * @brief Default constructor. Does not allocate any space.
398 * @brief Destructor. Releases the allocated space.
406 * @brief Copy constructor.
408 * @param vector Vector to copy from.
410 Vector( const Vector& vector )
417 * @brief Assignment operator.
419 * @param vector Vector to assign from.
420 * @return reference to self for chaining.
422 Vector& operator=( const Vector& vector )
424 if( this != &vector )
426 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
432 * @return Iterator to the beginning of the data.
434 Iterator Begin() const
436 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
441 * @return Iterator to the end of the data (one past last element).
445 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
446 address += VectorBase::Count();
451 * @pre index must be in the vector's range.
452 * @param index of the element.
453 * @return reference to the element for given index.
455 ItemType& operator[]( SizeType index )
457 // reuse the const version
458 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
462 * @pre index must be in the vector's range.
463 * @param index of the element.
464 * @return reference to the element for given index.
466 const ItemType& operator[]( SizeType index ) const
468 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
469 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
470 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
476 * @brief Push back an element to the vector.
478 * The underlying storage may be reallocated to provide space.
479 * If this occurs, all pre-existing pointers into the vector will
482 * @param[in] element to be added.
484 void PushBack( const ItemType& element )
486 const SizeType count = VectorBase::Count();
487 const SizeType newCount = count + 1u;
488 const SizeType capacity = VectorBase::Capacity();
489 if( newCount > capacity )
492 Reserve( newCount << 1u ); // reserve double the current count
494 // set new count first as otherwise the debug assert will hit us
495 VectorBase::SetCount( newCount );
496 operator[]( count ) = element;
500 *@brief Insert an element to the vector.
502 * Elements after \e at are moved one position to the right.
504 * The underlying storage may be reallocated to provide space.
505 * If this occurs, all pre-existing pointers into the vector will
508 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
510 * @param[in] at Iterator where to insert the elements into the vector.
511 * @param[in] element to be added.
513 void Insert( Iterator at, const ItemType& element )
515 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
516 const SizeType size = sizeof( ItemType );
517 char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
518 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
525 * @brief Inserts the given elements into the vector.
527 * Elements after \e at are moved the number of given elements positions to the right.
529 * The underlying storage may be reallocated to provide space.
530 * If this occurs, all pre-existing pointers into the vector will
533 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
534 * @pre Iterators \e from and \e to must be valid iterators.
535 * @pre Iterator \e from must not be grater than Iterator \e to.
537 * @param[in] at Iterator where to insert the elements into the vector.
538 * @param[in] from Iterator to the first element to be inserted.
539 * @param[in] to Iterator to the last element to be inserted.
541 void Insert( Iterator at, Iterator from, Iterator to )
543 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
544 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
552 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
553 reinterpret_cast< char* >( from ),
554 reinterpret_cast< char* >( to ),
555 sizeof( ItemType ) );
559 * @brief Reserve space in the vector.
561 * Reserving less than current Capacity is a no-op.
562 * @param count of elements to reserve.
564 void Reserve( SizeType count )
566 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
570 * @brief Resize the vector. Does not change capacity.
572 * @param count to resize to.
573 * @param item to insert to the new indices.
575 void Resize( SizeType count, ItemType item = ItemType() )
577 const SizeType oldCount = VectorBase::Count();
578 if( count <= oldCount )
580 // getting smaller so just set count
581 VectorBase::SetCount( count );
585 // remember how many new items get added
586 SizeType newItems = count - oldCount;
588 for( ; newItems > 0u; --newItems )
596 * @brief Erase an element.
598 * Does not change capacity. Other elements get moved.
600 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
602 * @param iterator Iterator pointing to item to remove.
603 * @return Iterator pointing to next element.
605 Iterator Erase( Iterator iterator )
607 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
608 if( iterator < ( End() - 1u ) )
610 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
614 // just remove the element
621 * @brief Erase a range of elements.
623 * Does not change capacity. Other elements get moved.
625 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
626 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
627 * @pre Iterator \e first must not be grater than Iterator \e last.
629 * @param[in] first Iterator to the first element to be erased.
630 * @param[in] last Iterator to the last element to be erased.
632 * @return Iterator pointing to the next element of the last one.
634 Iterator Erase( Iterator first, Iterator last )
636 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
637 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
638 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
640 Iterator nextElement;
644 // Erase up to the end.
645 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
650 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
651 reinterpret_cast< char* >( last ),
652 sizeof( ItemType ) ) );
659 * @brief Removes an element.
661 * Does not maintain order. Swaps the element with end and
662 * decreases size by one. This is much faster than Erase so use
663 * this in case order does not matter. Does not change capacity.
665 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
667 * @param iterator Iterator pointing to item to remove.
669 void Remove( Iterator iterator )
671 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
673 Iterator last = End() - 1u;
674 if( last > iterator )
676 std::swap( *iterator, *last );
678 VectorBase::SetCount( VectorBase::Count() - 1u );
682 * @brief Swap the contents of two vectors.
684 * @param vector Vector to swap with.
686 void Swap( Vector& vector )
688 VectorBase::Swap( vector );
692 * @brief Clear the contents of the vector. Keeps its capacity.
696 VectorAlgorithms<BaseType>::Clear();
700 * @brief Release the memory that the vector holds.
704 VectorAlgorithms<BaseType>::Release();
713 #endif /* __DALI_VECTOR_H__ */