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 The capacity of this vector.
110 SizeType Capacity() const;
113 * @brief Release the data.
115 * Does not call destructors on objects held.
119 protected: // for Derived classes
122 * @brief Helper to set the count.
124 * @param count Number of elements in the vector.
126 void SetCount( SizeType count );
129 * @brief Reserve space in the vector.
131 * @param count of elements to reserve.
132 * @param elementSize of a single element.
134 void Reserve( SizeType count, SizeType elementSize );
137 * @brief Copy a vector.
139 * @param vector Vector to copy from.
140 * @param elementSize of a single element.
142 void Copy( const VectorBase& vector, SizeType elementSize );
145 * @brief Swap the contents of two vectors.
147 * @param vector Vector to swap with.
149 void Swap( VectorBase& vector );
152 * @brief Erase an element.
154 * Does not change capacity.
155 * @pre last element cannot be erased as there is nothing to move.
156 * @param address to erase from.
157 * @param elementSize to erase.
159 void Erase( char* address, SizeType elementSize );
162 * @brief Erase a range of elements.
164 * Does not change capacity.
165 * @param[in] first Address to the first element to be erased.
166 * @param[in] last Address to the last element to be erased.
167 * @param[in] elementSize Size of one of the elements to be erased.
168 * @return address pointing to the next element of the last one.
170 char* Erase( char* first, char* last, SizeType elementSize );
173 * Copies a number of bytes from \e source to \e destination.
175 * \e source and \e destination must not overlap.
177 * @param[in] destination Pointer to the destination address.
178 * @param[in] source Pointer to the source address.
179 * @param[in] numberOfBytes The number of bytes to be copied.
181 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
185 // not copiable as it does not know the size of elements
186 VectorBase( const VectorBase& ); ///< Undefined
187 VectorBase& operator=( const VectorBase& ); ///< Undefined
191 void* mData; ///< Pointer to the data.
196 * @brief Vector algorithm variant for trivial types.
198 * Trivial types do not need destructor or copy constructor called.
200 template< bool IsTrivial >
201 class VectorAlgorithms : public VectorBase
203 protected: // API for deriving classes
205 typedef VectorBase::SizeType SizeType;
208 * @brief Empty constructor.
214 * @brief Empty destructor.
220 * @brief Copy vector contents.
222 * @param rhs to copy from.
223 * @param elementSize of the content.
225 void Copy( const VectorBase& rhs, SizeType elementSize )
227 if( rhs.Capacity() > 0u )
229 VectorBase::Copy( rhs, elementSize );
233 VectorBase::Release();
238 * @brief Reserve space in the vector.
240 * @param count of elements to reserve.
241 * @param elementSize of a single element.
243 void Reserve( SizeType count, SizeType elementSize )
245 VectorBase::Reserve( count, elementSize );
249 * @brief Resize the vector. Does not change capacity.
251 * @param count to resize to.
252 * @param elementSize of a single element.
254 void Resize( SizeType count, SizeType elementSize )
256 // reserve will copy old elements as well
257 Reserve( count, elementSize );
261 * @brief Clear the contents.
263 * For simple types nothing to do.
269 VectorBase::SetCount( 0u );
274 * @brief Release the vector.
278 VectorBase::Release();
282 * @brief Erase an element. Does not change capacity.
284 * @param address to erase from.
285 * @param elementSize to erase.
287 void Erase( char* address, SizeType elementSize )
289 VectorBase::Erase( address, elementSize );
293 * @brief Erase a range of elements. Does not change capacity.
295 * @param[in] first Address to the first element to be erased.
296 * @param[in] last Address to the last element to be erased.
297 * @param[in] elementSize Size of one of the elements to be erased.
298 * @return address pointing to the next element of the last one.
300 char* Erase( char* first, char* last, SizeType elementSize )
302 return VectorBase::Erase( first, last, elementSize );
306 * @brief Inserts the given elements into the vector.
308 * @param[in] at Address where to insert the elements into the vector.
309 * @param[in] from Address to the first element to be inserted.
310 * @param[in] to Address to the last element to be inserted.
311 * @param[in] elementSize Size of one of the elements to be inserted.
313 void Insert( char* at, char* from, char* to, SizeType elementSize )
315 const SizeType size = to - from;
316 const SizeType count = Count();
317 const SizeType newCount = count + size / elementSize;
319 if( newCount > Capacity() )
321 // Calculate the at offset as the pointer is invalid after the Reserve() call.
322 std::size_t offset = at - reinterpret_cast<char*>( mData );
325 Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
327 // Set the new at pointer.
328 at = reinterpret_cast<char*>( mData ) + offset;
330 // set new count first as otherwise the debug assert will hit us
331 SetCount( newCount );
333 // Move current items to a new position inside the vector.
334 CopyMemory( at + size,
336 ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
338 // Copy the given items.
339 CopyMemory( at, from, size );
344 * @brief Vector algorithm variant for complex types.
346 * Not yet supported so will lead to compile error
347 * as constructor and destructor are private.
348 * TODO add support for this variant.
351 class VectorAlgorithms< false > : public VectorBase
361 * @brief Vector class with minimum space allocation when its empty.
363 * @param type of the data that the vector holds.
365 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
366 class Vector : public VectorAlgorithms< IsTrivialType >
371 * @brief Type definitions.
373 typedef VectorBase::SizeType SizeType;
374 typedef T* Iterator; ///< Most simple Iterator is a pointer
375 typedef const T* ConstIterator;
380 BaseType = IsTrivialType
384 * @brief Default constructor. Does not allocate any space.
390 * @brief Destructor. Releases the allocated space.
398 * @brief Copy constructor.
400 * @param vector Vector to copy from.
402 Vector( const Vector& vector )
409 * @brief Assignment operator.
411 * @param vector Vector to assign from.
412 * @return reference to self for chaining.
414 Vector& operator=( const Vector& vector )
416 if( this != &vector )
418 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
424 * @return Iterator to the beginning of the data.
426 Iterator Begin() const
428 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
433 * @return Iterator to the end of the data (one past last element).
437 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
438 address += VectorBase::Count();
443 * @pre index must be in the vector's range.
444 * @param index of the element.
445 * @return reference to the element for given index.
447 ItemType& operator[]( SizeType index )
449 // reuse the const version
450 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
454 * @pre index must be in the vector's range.
455 * @param index of the element.
456 * @return reference to the element for given index.
458 const ItemType& operator[]( SizeType index ) const
460 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
461 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
462 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
468 * @brief Push back an element to the vector.
470 * The underlying storage may be reallocated to provide space.
471 * If this occurs, all pre-existing pointers into the vector will
474 * @param[in] element to be added.
476 void PushBack( const ItemType& element )
478 const SizeType count = VectorBase::Count();
479 const SizeType newCount = count + 1u;
480 const SizeType capacity = VectorBase::Capacity();
481 if( newCount > capacity )
484 Reserve( newCount << 1u ); // reserve double the current count
486 // set new count first as otherwise the debug assert will hit us
487 VectorBase::SetCount( newCount );
488 operator[]( count ) = element;
492 *@brief Insert an element to the vector.
494 * Elements after \e at are moved one position to the right.
496 * The underlying storage may be reallocated to provide space.
497 * If this occurs, all pre-existing pointers into the vector will
500 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
502 * @param[in] at Iterator where to insert the elements into the vector.
503 * @param[in] element to be added.
505 void Insert( Iterator at, const ItemType& element )
507 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
508 const SizeType size = sizeof( ItemType );
509 char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
510 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
517 * @brief Inserts the given elements into the vector.
519 * Elements after \e at are moved the number of given elements positions to the right.
521 * The underlying storage may be reallocated to provide space.
522 * If this occurs, all pre-existing pointers into the vector will
525 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
526 * @pre Iterators \e from and \e to must be valid iterators.
527 * @pre Iterator \e from must not be grater than Iterator \e to.
529 * @param[in] at Iterator where to insert the elements into the vector.
530 * @param[in] from Iterator to the first element to be inserted.
531 * @param[in] to Iterator to the last element to be inserted.
533 void Insert( Iterator at, Iterator from, Iterator to )
535 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
536 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
544 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
545 reinterpret_cast< char* >( from ),
546 reinterpret_cast< char* >( to ),
547 sizeof( ItemType ) );
551 * @brief Reserve space in the vector.
553 * Reserving less than current Capacity is a no-op.
554 * @param count of elements to reserve.
556 void Reserve( SizeType count )
558 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
562 * @brief Resize the vector. Does not change capacity.
564 * @param count to resize to.
565 * @param item to insert to the new indices.
567 void Resize( SizeType count, ItemType item = ItemType() )
569 const SizeType oldCount = VectorBase::Count();
570 if( count <= oldCount )
572 // getting smaller so just set count
573 VectorBase::SetCount( count );
577 // remember how many new items get added
578 SizeType newItems = count - oldCount;
580 for( ; newItems > 0u; --newItems )
588 * @brief Erase an element.
590 * Does not change capacity. Other elements get moved.
592 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
594 * @param iterator Iterator pointing to item to remove.
595 * @return Iterator pointing to next element.
597 Iterator Erase( Iterator iterator )
599 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
600 if( iterator < ( End() - 1u ) )
602 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
606 // just remove the element
613 * @brief Erase a range of elements.
615 * Does not change capacity. Other elements get moved.
617 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
618 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
619 * @pre Iterator \e first must not be grater than Iterator \e last.
621 * @param[in] first Iterator to the first element to be erased.
622 * @param[in] last Iterator to the last element to be erased.
624 * @return Iterator pointing to the next element of the last one.
626 Iterator Erase( Iterator first, Iterator last )
628 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
629 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
630 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
632 Iterator nextElement;
636 // Erase up to the end.
637 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
642 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
643 reinterpret_cast< char* >( last ),
644 sizeof( ItemType ) ) );
651 * @brief Removes an element.
653 * Does not maintain order. Swaps the element with end and
654 * decreases size by one. This is much faster than Erase so use
655 * this in case order does not matter. Does not change capacity.
657 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
659 * @param iterator Iterator pointing to item to remove.
661 void Remove( Iterator iterator )
663 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
665 Iterator last = End() - 1u;
666 if( last > iterator )
668 std::swap( *iterator, *last );
670 VectorBase::SetCount( VectorBase::Count() - 1u );
674 * @brief Swap the contents of two vectors.
676 * @param vector Vector to swap with.
678 void Swap( Vector& vector )
680 VectorBase::Swap( vector );
684 * @brief Clear the contents of the vector. Keeps its capacity.
688 VectorAlgorithms<BaseType>::Clear();
692 * @brief Release the memory that the vector holds.
696 VectorAlgorithms<BaseType>::Release();
705 #endif /* __DALI_VECTOR_H__ */