1 #ifndef __DALI_VECTOR_H__
2 #define __DALI_VECTOR_H__
5 * Copyright (c) 2014 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.
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/math/math-utils.h>
29 * @brief For DALi internal use asserts are enabled in debug builds.
31 * For Application use asserts can be enabled manually.
33 #if defined( DEBUG_ENABLED )
34 #define ENABLE_VECTOR_ASSERTS
37 #if defined( ENABLE_VECTOR_ASSERTS )
38 #define DALI_ASSERT_VECTOR(cond) DALI_ASSERT_ALWAYS(cond)
40 #define DALI_ASSERT_VECTOR(cond)
43 namespace Dali DALI_IMPORT_API
47 * @brief Base class to handle the memory of simple vector.
49 * Memory layout is such that it has two std::size_t to hold the count
50 * and capacity of the vector. mData is adjusted so that it points to the
51 * beginning of the first real item so that iterating the items is quick.
57 typedef std::size_t SizeType;
59 protected: // Construction
62 * @brief Default constructor. Does not allocate space.
69 * Does not release the space. Derived class needs to call Release.
70 * Not virtual as should not be called directly and we do not want
71 * a vtable for this class as it would unnecessarily increase size.
78 * @brief This method is inlined as its needed frequently for End() iterator.
80 * @return The count of elements in this vector.
82 SizeType Count() const
87 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
88 items = *(metadata - 1u);
94 * @return The count of elements in this vector.
102 * @return The capacity of this vector.
104 SizeType Capacity() const;
107 * @brief Release the data.
109 * Does not call destructors on objects held.
113 protected: // for Derived classes
116 * @brief Helper to set the count.
118 * @param count Number of elements in the vector.
120 void SetCount( SizeType count );
123 * @brief Reserve space in the vector.
125 * @param count of elements to reserve.
126 * @param elementSize of a single element.
128 void Reserve( SizeType count, SizeType elementSize );
131 * @brief Copy a vector.
133 * @param vector Vector to copy from.
134 * @param elementSize of a single element.
136 void Copy( const VectorBase& vector, SizeType elementSize );
139 * @brief Swap the contents of two vectors.
141 * @param vector Vector to swap with.
143 void Swap( VectorBase& vector );
146 * @brief Erase an element.
148 * Does not change capacity.
149 * @pre last element cannot be erased as there is nothing to move.
150 * @param address to erase from.
151 * @param elementSize to erase.
153 void Erase( char* address, SizeType elementSize );
156 * @brief Erase a range of elements.
158 * Does not change capacity.
159 * @param[in] first Address to the first element to be erased.
160 * @param[in] last Address to the last element to be erased.
161 * @param[in] elementSize Size of one of the elements to be erased.
162 * @return address pointing to the next element of the last one.
164 char* Erase( char* first, char* last, SizeType elementSize );
167 * Copies a number of bytes from \e source to \e destination.
169 * \e source and \e destination must not overlap.
171 * @param[in] destination Pointer to the destination address.
172 * @param[in] source Pointer to the source address.
173 * @param[in] numberOfBytes The number of bytes to be copied.
175 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
179 // not copiable as it does not know the size of elements
180 VectorBase( const VectorBase& ); ///< Undefined
181 VectorBase& operator=( const VectorBase& ); ///< Undefined
185 void* mData; ///< Pointer to the data.
190 * @brief Vector algorithm variant for trivial types.
192 * Trivial types do not need destructor or copy constructor called.
194 template< bool IsTrivial >
195 class VectorAlgorithms : public VectorBase
197 protected: // API for deriving classes
199 typedef VectorBase::SizeType SizeType;
202 * @brief Empty constructor.
208 * @brief Empty destructor.
214 * @brief Copy vector contents.
216 * @param rhs to copy from.
217 * @param elementSize of the content.
219 void Copy( const VectorBase& rhs, SizeType elementSize )
221 if( rhs.Capacity() > 0u )
223 VectorBase::Copy( rhs, elementSize );
227 VectorBase::Release();
232 * @brief Reserve space in the vector.
234 * @param count of elements to reserve.
235 * @param elementSize of a single element.
237 void Reserve( SizeType count, SizeType elementSize )
239 VectorBase::Reserve( count, elementSize );
243 * @brief Resize the vector. Does not change capacity.
245 * @param count to resize to.
246 * @param elementSize of a single element.
248 void Resize( SizeType count, SizeType elementSize )
250 // reserve will copy old elements as well
251 Reserve( count, elementSize );
255 * @brief Clear the contents.
257 * For simple types nothing to do.
263 VectorBase::SetCount( 0u );
268 * @brief Release the vector.
272 VectorBase::Release();
276 * @brief Erase an element. Does not change capacity.
278 * @param address to erase from.
279 * @param elementSize to erase.
281 void Erase( char* address, SizeType elementSize )
283 VectorBase::Erase( address, elementSize );
287 * @brief Erase a range of elements. Does not change capacity.
289 * @param[in] first Address to the first element to be erased.
290 * @param[in] last Address to the last element to be erased.
291 * @param[in] elementSize Size of one of the elements to be erased.
292 * @return address pointing to the next element of the last one.
294 char* Erase( char* first, char* last, SizeType elementSize )
296 return VectorBase::Erase( first, last, elementSize );
300 * @brief Inserts the given elements into the vector.
302 * @param[in] at Address where to insert the elements into the vector.
303 * @param[in] from Address to the first element to be inserted.
304 * @param[in] to Address to the last element to be inserted.
305 * @param[in] elementSize Size of one of the elements to be inserted.
307 void Insert( char* at, char* from, char* to, SizeType elementSize )
309 const SizeType size = to - from;
310 const SizeType count = Count();
311 const SizeType newCount = count + size / elementSize;
313 if( newCount > Capacity() )
315 // Calculate the at offset as the pointer is invalid after the Reserve() call.
316 std::size_t offset = at - reinterpret_cast<char*>( mData );
319 Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
321 // Set the new at pointer.
322 at = reinterpret_cast<char*>( mData ) + offset;
324 // set new count first as otherwise the debug assert will hit us
325 SetCount( newCount );
327 // Move current items to a new position inside the vector.
328 CopyMemory( at + size,
330 ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
332 // Copy the given items.
333 CopyMemory( at, from, size );
338 * @brief Vector algorithm variant for complex types.
340 * Not yet supported so will lead to compile error
341 * as constructor and destructor are private.
342 * TODO add support for this variant.
345 class VectorAlgorithms< false > : public VectorBase
355 * @brief Vector class with minimum space allocation when its empty.
357 * @param type of the data that the vector holds.
359 template< class T, bool IsTrivialType = __has_trivial_destructor(T) && __has_trivial_copy(T) >
360 class Vector : public VectorAlgorithms< IsTrivialType >
365 * @brief Type definitions.
367 typedef VectorBase::SizeType SizeType;
368 typedef T* Iterator; ///< Most simple Iterator is a pointer
369 typedef const T* ConstIterator;
374 BaseType = IsTrivialType
378 * @brief Default constructor. Does not allocate any space.
384 * @brief Destructor. Releases the allocated space.
392 * @brief Copy constructor.
394 * @param vector Vector to copy from.
396 Vector( const Vector& vector )
403 * @brief Assignment operator.
405 * @param vector Vector to assign from.
406 * @return reference to self for chaining.
408 Vector& operator=( const Vector& vector )
410 if( this != &vector )
412 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
418 * @return Iterator to the beginning of the data.
420 Iterator Begin() const
422 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
427 * @return Iterator to the end of the data (one past last element).
431 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
432 address += VectorBase::Count();
437 * @pre index must be in the vector's range.
438 * @param index of the element.
439 * @return reference to the element for given index.
441 ItemType& operator[]( SizeType index )
443 // reuse the const version
444 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
448 * @pre index must be in the vector's range.
449 * @param index of the element.
450 * @return reference to the element for given index.
452 const ItemType& operator[]( SizeType index ) const
454 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
455 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
456 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
462 * @brief Push back an element to the vector.
464 * The underlying storage may be reallocated to provide space.
465 * If this occurs, all pre-existing pointers into the vector will
468 * @param[in] element to be added.
470 void PushBack( const ItemType& element )
472 const SizeType count = VectorBase::Count();
473 const SizeType newCount = count + 1u;
474 const SizeType capacity = VectorBase::Capacity();
475 if( newCount > capacity )
478 Reserve( newCount << 1u ); // reserve double the current count
480 // set new count first as otherwise the debug assert will hit us
481 VectorBase::SetCount( newCount );
482 operator[]( count ) = element;
486 *@brief Insert an element to the vector.
488 * Elements after \e at are moved one position to the right.
490 * The underlying storage may be reallocated to provide space.
491 * If this occurs, all pre-existing pointers into the vector will
494 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
496 * @param[in] at Iterator where to insert the elements into the vector.
497 * @param[in] element to be added.
499 void Insert( Iterator at, const ItemType& element )
501 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
502 const SizeType size = sizeof( ItemType );
503 char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
504 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
511 * @brief Inserts the given elements into the vector.
513 * Elements after \e at are moved the number of given elements positions to the right.
515 * The underlying storage may be reallocated to provide space.
516 * If this occurs, all pre-existing pointers into the vector will
519 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
520 * @pre Iterators \e from and \e to must be valid iterators.
521 * @pre Iterator \e from must not be grater than Iterator \e to.
523 * @param[in] at Iterator where to insert the elements into the vector.
524 * @param[in] from Iterator to the first element to be inserted.
525 * @param[in] to Iterator to the last element to be inserted.
527 void Insert( Iterator at, Iterator from, Iterator to )
529 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
530 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
538 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
539 reinterpret_cast< char* >( from ),
540 reinterpret_cast< char* >( to ),
541 sizeof( ItemType ) );
545 * @brief Reserve space in the vector.
547 * Reserving less than current Capacity is a no-op.
548 * @param count of elements to reserve.
550 void Reserve( SizeType count )
552 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
556 * @brief Resize the vector. Does not change capacity.
558 * @param count to resize to.
559 * @param item to insert to the new indices.
561 void Resize( SizeType count, ItemType item = ItemType() )
563 const SizeType oldCount = VectorBase::Count();
564 if( count <= oldCount )
566 // getting smaller so just set count
567 VectorBase::SetCount( count );
571 // remember how many new items get added
572 SizeType newItems = count - oldCount;
574 for( ; newItems > 0u; --newItems )
582 * @brief Erase an element.
584 * Does not change capacity. Other elements get moved.
586 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
588 * @param iterator Iterator pointing to item to remove.
589 * @return Iterator pointing to next element.
591 Iterator Erase( Iterator iterator )
593 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
594 if( iterator < ( End() - 1u ) )
596 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
600 // just remove the element
607 * @brief Erase a range of elements.
609 * Does not change capacity. Other elements get moved.
611 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
612 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
613 * @pre Iterator \e first must not be grater than Iterator \e last.
615 * @param[in] first Iterator to the first element to be erased.
616 * @param[in] last Iterator to the last element to be erased.
618 * @return Iterator pointing to the next element of the last one.
620 Iterator Erase( Iterator first, Iterator last )
622 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
623 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
624 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
626 Iterator nextElement;
630 // Erase up to the end.
631 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
636 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
637 reinterpret_cast< char* >( last ),
638 sizeof( ItemType ) ) );
645 * @brief Removes an element.
647 * Does not maintain order. Swaps the element with end and
648 * decreases size by one. This is much faster than Erase so use
649 * this in case order does not matter. Does not change capacity.
651 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
653 * @param iterator Iterator pointing to item to remove.
655 void Remove( Iterator iterator )
657 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
659 Iterator last = End() - 1u;
660 if( last > iterator )
662 std::swap( *iterator, *last );
664 VectorBase::SetCount( VectorBase::Count() - 1u );
668 * @brief Swap the contents of two vectors.
670 * @param vector Vector to swap with.
672 void Swap( Vector& vector )
674 VectorBase::Swap( vector );
678 * @brief Clear the contents of the vector. Keeps its capacity.
682 VectorAlgorithms<BaseType>::Clear();
686 * @brief Release the memory that the vector holds.
690 VectorAlgorithms<BaseType>::Release();
696 #endif /* __DALI_VECTOR_H__ */