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.
192 * This template section does not need to be exported, it should use the visibility of the code its used in
198 * @brief Vector algorithm variant for trivial types.
200 * Trivial types do not need destructor or copy constructor called.
202 template< bool IsTrivial >
203 class VectorAlgorithms : public VectorBase
205 protected: // API for deriving classes
207 typedef VectorBase::SizeType SizeType;
210 * @brief Empty constructor.
216 * @brief Empty destructor.
222 * @brief Copy vector contents.
224 * @param rhs to copy from.
225 * @param elementSize of the content.
227 void Copy( const VectorBase& rhs, SizeType elementSize )
229 if( rhs.Capacity() > 0u )
231 VectorBase::Copy( rhs, elementSize );
235 VectorBase::Release();
240 * @brief Reserve space in the vector.
242 * @param count of elements to reserve.
243 * @param elementSize of a single element.
245 void Reserve( SizeType count, SizeType elementSize )
247 VectorBase::Reserve( count, elementSize );
251 * @brief Resize the vector. Does not change capacity.
253 * @param count to resize to.
254 * @param elementSize of a single element.
256 void Resize( SizeType count, SizeType elementSize )
258 // reserve will copy old elements as well
259 Reserve( count, elementSize );
263 * @brief Clear the contents.
265 * For simple types nothing to do.
271 VectorBase::SetCount( 0u );
276 * @brief Release the vector.
280 VectorBase::Release();
284 * @brief Erase an element. Does not change capacity.
286 * @param address to erase from.
287 * @param elementSize to erase.
289 void Erase( char* address, SizeType elementSize )
291 VectorBase::Erase( address, elementSize );
295 * @brief Erase a range of elements. Does not change capacity.
297 * @param[in] first Address to the first element to be erased.
298 * @param[in] last Address to the last element to be erased.
299 * @param[in] elementSize Size of one of the elements to be erased.
300 * @return address pointing to the next element of the last one.
302 char* Erase( char* first, char* last, SizeType elementSize )
304 return VectorBase::Erase( first, last, elementSize );
308 * @brief Inserts the given elements into the vector.
310 * @param[in] at Address where to insert the elements into the vector.
311 * @param[in] from Address to the first element to be inserted.
312 * @param[in] to Address to the last element to be inserted.
313 * @param[in] elementSize Size of one of the elements to be inserted.
315 void Insert( char* at, char* from, char* to, SizeType elementSize )
317 const SizeType size = to - from;
318 const SizeType count = Count();
319 const SizeType newCount = count + size / elementSize;
321 if( newCount > Capacity() )
323 // Calculate the at offset as the pointer is invalid after the Reserve() call.
324 std::size_t offset = at - reinterpret_cast<char*>( mData );
327 Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
329 // Set the new at pointer.
330 at = reinterpret_cast<char*>( mData ) + offset;
332 // set new count first as otherwise the debug assert will hit us
333 SetCount( newCount );
335 // Move current items to a new position inside the vector.
336 CopyMemory( at + size,
338 ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
340 // Copy the given items.
341 CopyMemory( at, from, size );
346 * @brief Vector algorithm variant for complex types.
348 * Not yet supported so will lead to compile error
349 * as constructor and destructor are private.
350 * TODO add support for this variant.
353 class VectorAlgorithms< false > : public VectorBase
363 * @brief Vector class with minimum space allocation when its empty.
365 * @param type of the data that the vector holds.
367 template< class T, bool IsTrivialType = __has_trivial_destructor(T) && __has_trivial_copy(T) >
368 class Vector : public VectorAlgorithms< IsTrivialType >
373 * @brief Type definitions.
375 typedef VectorBase::SizeType SizeType;
376 typedef T* Iterator; ///< Most simple Iterator is a pointer
377 typedef const T* ConstIterator;
382 BaseType = IsTrivialType
386 * @brief Default constructor. Does not allocate any space.
392 * @brief Destructor. Releases the allocated space.
400 * @brief Copy constructor.
402 * @param vector Vector to copy from.
404 Vector( const Vector& vector )
411 * @brief Assignment operator.
413 * @param vector Vector to assign from.
414 * @return reference to self for chaining.
416 Vector& operator=( const Vector& vector )
418 if( this != &vector )
420 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
426 * @return Iterator to the beginning of the data.
428 Iterator Begin() const
430 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
435 * @return Iterator to the end of the data (one past last element).
439 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
440 address += VectorBase::Count();
445 * @pre index must be in the vector's range.
446 * @param index of the element.
447 * @return reference to the element for given index.
449 ItemType& operator[]( SizeType index )
451 // reuse the const version
452 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
456 * @pre index must be in the vector's range.
457 * @param index of the element.
458 * @return reference to the element for given index.
460 const ItemType& operator[]( SizeType index ) const
462 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
463 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
464 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
470 * @brief Push back an element to the vector.
472 * The underlying storage may be reallocated to provide space.
473 * If this occurs, all pre-existing pointers into the vector will
476 * @param[in] element to be added.
478 void PushBack( const ItemType& element )
480 const SizeType count = VectorBase::Count();
481 const SizeType newCount = count + 1u;
482 const SizeType capacity = VectorBase::Capacity();
483 if( newCount > capacity )
486 Reserve( newCount << 1u ); // reserve double the current count
488 // set new count first as otherwise the debug assert will hit us
489 VectorBase::SetCount( newCount );
490 operator[]( count ) = element;
494 *@brief Insert an element to the vector.
496 * Elements after \e at are moved one position to the right.
498 * The underlying storage may be reallocated to provide space.
499 * If this occurs, all pre-existing pointers into the vector will
502 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
504 * @param[in] at Iterator where to insert the elements into the vector.
505 * @param[in] element to be added.
507 void Insert( Iterator at, const ItemType& element )
509 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
510 const SizeType size = sizeof( ItemType );
511 char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
512 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
519 * @brief Inserts the given elements into the vector.
521 * Elements after \e at are moved the number of given elements positions to the right.
523 * The underlying storage may be reallocated to provide space.
524 * If this occurs, all pre-existing pointers into the vector will
527 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
528 * @pre Iterators \e from and \e to must be valid iterators.
529 * @pre Iterator \e from must not be grater than Iterator \e to.
531 * @param[in] at Iterator where to insert the elements into the vector.
532 * @param[in] from Iterator to the first element to be inserted.
533 * @param[in] to Iterator to the last element to be inserted.
535 void Insert( Iterator at, Iterator from, Iterator to )
537 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
538 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
546 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
547 reinterpret_cast< char* >( from ),
548 reinterpret_cast< char* >( to ),
549 sizeof( ItemType ) );
553 * @brief Reserve space in the vector.
555 * Reserving less than current Capacity is a no-op.
556 * @param count of elements to reserve.
558 void Reserve( SizeType count )
560 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
564 * @brief Resize the vector. Does not change capacity.
566 * @param count to resize to.
567 * @param item to insert to the new indices.
569 void Resize( SizeType count, ItemType item = ItemType() )
571 const SizeType oldCount = VectorBase::Count();
572 if( count <= oldCount )
574 // getting smaller so just set count
575 VectorBase::SetCount( count );
579 // remember how many new items get added
580 SizeType newItems = count - oldCount;
582 for( ; newItems > 0u; --newItems )
590 * @brief Erase an element.
592 * Does not change capacity. Other elements get moved.
594 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
596 * @param iterator Iterator pointing to item to remove.
597 * @return Iterator pointing to next element.
599 Iterator Erase( Iterator iterator )
601 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
602 if( iterator < ( End() - 1u ) )
604 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
608 // just remove the element
615 * @brief Erase a range of elements.
617 * Does not change capacity. Other elements get moved.
619 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
620 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
621 * @pre Iterator \e first must not be grater than Iterator \e last.
623 * @param[in] first Iterator to the first element to be erased.
624 * @param[in] last Iterator to the last element to be erased.
626 * @return Iterator pointing to the next element of the last one.
628 Iterator Erase( Iterator first, Iterator last )
630 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
631 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
632 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
634 Iterator nextElement;
638 // Erase up to the end.
639 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
644 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
645 reinterpret_cast< char* >( last ),
646 sizeof( ItemType ) ) );
653 * @brief Removes an element.
655 * Does not maintain order. Swaps the element with end and
656 * decreases size by one. This is much faster than Erase so use
657 * this in case order does not matter. Does not change capacity.
659 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
661 * @param iterator Iterator pointing to item to remove.
663 void Remove( Iterator iterator )
665 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
667 Iterator last = End() - 1u;
668 if( last > iterator )
670 std::swap( *iterator, *last );
672 VectorBase::SetCount( VectorBase::Count() - 1u );
676 * @brief Swap the contents of two vectors.
678 * @param vector Vector to swap with.
680 void Swap( Vector& vector )
682 VectorBase::Swap( vector );
686 * @brief Clear the contents of the vector. Keeps its capacity.
690 VectorAlgorithms<BaseType>::Clear();
694 * @brief Release the memory that the vector holds.
698 VectorAlgorithms<BaseType>::Release();
704 #endif /* __DALI_VECTOR_H__ */