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)
49 * @brief Base class to handle the memory of simple vector.
51 * Memory layout is such that it has two std::size_t to hold the count
52 * and capacity of the vector. mData is adjusted so that it points to the
53 * beginning of the first real item so that iterating the items is quick.
55 class DALI_IMPORT_API VectorBase
59 typedef std::size_t SizeType;
61 protected: // Construction
64 * @brief Default constructor. Does not allocate space.
71 * Does not release the space. Derived class needs to call Release.
72 * Not virtual as should not be called directly and we do not want
73 * a vtable for this class as it would unnecessarily increase size.
80 * @brief This method is inlined as its needed frequently for End() iterator.
82 * @return The count of elements in this vector.
84 SizeType Count() const
89 SizeType* metadata = reinterpret_cast< SizeType* >( mData );
90 items = *(metadata - 1u);
96 * @return The count of elements in this vector.
104 * @return The capacity of this vector.
106 SizeType Capacity() const;
109 * @brief Release the data.
111 * Does not call destructors on objects held.
115 protected: // for Derived classes
118 * @brief Helper to set the count.
120 * @param count Number of elements in the vector.
122 void SetCount( SizeType count );
125 * @brief Reserve space in the vector.
127 * @param count of elements to reserve.
128 * @param elementSize of a single element.
130 void Reserve( SizeType count, SizeType elementSize );
133 * @brief Copy a vector.
135 * @param vector Vector to copy from.
136 * @param elementSize of a single element.
138 void Copy( const VectorBase& vector, SizeType elementSize );
141 * @brief Swap the contents of two vectors.
143 * @param vector Vector to swap with.
145 void Swap( VectorBase& vector );
148 * @brief Erase an element.
150 * Does not change capacity.
151 * @pre last element cannot be erased as there is nothing to move.
152 * @param address to erase from.
153 * @param elementSize to erase.
155 void Erase( char* address, SizeType elementSize );
158 * @brief Erase a range of elements.
160 * Does not change capacity.
161 * @param[in] first Address to the first element to be erased.
162 * @param[in] last Address to the last element to be erased.
163 * @param[in] elementSize Size of one of the elements to be erased.
164 * @return address pointing to the next element of the last one.
166 char* Erase( char* first, char* last, SizeType elementSize );
169 * Copies a number of bytes from \e source to \e destination.
171 * \e source and \e destination must not overlap.
173 * @param[in] destination Pointer to the destination address.
174 * @param[in] source Pointer to the source address.
175 * @param[in] numberOfBytes The number of bytes to be copied.
177 void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
181 // not copiable as it does not know the size of elements
182 VectorBase( const VectorBase& ); ///< Undefined
183 VectorBase& operator=( const VectorBase& ); ///< Undefined
187 void* mData; ///< Pointer to the data.
192 * @brief Vector algorithm variant for trivial types.
194 * Trivial types do not need destructor or copy constructor called.
196 template< bool IsTrivial >
197 class VectorAlgorithms : public VectorBase
199 protected: // API for deriving classes
201 typedef VectorBase::SizeType SizeType;
204 * @brief Empty constructor.
210 * @brief Empty destructor.
216 * @brief Copy vector contents.
218 * @param rhs to copy from.
219 * @param elementSize of the content.
221 void Copy( const VectorBase& rhs, SizeType elementSize )
223 if( rhs.Capacity() > 0u )
225 VectorBase::Copy( rhs, elementSize );
229 VectorBase::Release();
234 * @brief Reserve space in the vector.
236 * @param count of elements to reserve.
237 * @param elementSize of a single element.
239 void Reserve( SizeType count, SizeType elementSize )
241 VectorBase::Reserve( count, elementSize );
245 * @brief Resize the vector. Does not change capacity.
247 * @param count to resize to.
248 * @param elementSize of a single element.
250 void Resize( SizeType count, SizeType elementSize )
252 // reserve will copy old elements as well
253 Reserve( count, elementSize );
257 * @brief Clear the contents.
259 * For simple types nothing to do.
265 VectorBase::SetCount( 0u );
270 * @brief Release the vector.
274 VectorBase::Release();
278 * @brief Erase an element. Does not change capacity.
280 * @param address to erase from.
281 * @param elementSize to erase.
283 void Erase( char* address, SizeType elementSize )
285 VectorBase::Erase( address, elementSize );
289 * @brief Erase a range of elements. Does not change capacity.
291 * @param[in] first Address to the first element to be erased.
292 * @param[in] last Address to the last element to be erased.
293 * @param[in] elementSize Size of one of the elements to be erased.
294 * @return address pointing to the next element of the last one.
296 char* Erase( char* first, char* last, SizeType elementSize )
298 return VectorBase::Erase( first, last, elementSize );
302 * @brief Inserts the given elements into the vector.
304 * @param[in] at Address where to insert the elements into the vector.
305 * @param[in] from Address to the first element to be inserted.
306 * @param[in] to Address to the last element to be inserted.
307 * @param[in] elementSize Size of one of the elements to be inserted.
309 void Insert( char* at, char* from, char* to, SizeType elementSize )
311 const SizeType size = to - from;
312 const SizeType count = Count();
313 const SizeType newCount = count + size / elementSize;
315 if( newCount > Capacity() )
317 // Calculate the at offset as the pointer is invalid after the Reserve() call.
318 std::size_t offset = at - reinterpret_cast<char*>( mData );
321 Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
323 // Set the new at pointer.
324 at = reinterpret_cast<char*>( mData ) + offset;
326 // set new count first as otherwise the debug assert will hit us
327 SetCount( newCount );
329 // Move current items to a new position inside the vector.
330 CopyMemory( at + size,
332 ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
334 // Copy the given items.
335 CopyMemory( at, from, size );
340 * @brief Vector algorithm variant for complex types.
342 * Not yet supported so will lead to compile error
343 * as constructor and destructor are private.
344 * TODO add support for this variant.
347 class VectorAlgorithms< false > : public VectorBase
357 * @brief Vector class with minimum space allocation when its empty.
359 * @param type of the data that the vector holds.
361 template< class T, bool IsTrivialType = TypeTraits<T>::IS_TRIVIAL_TYPE == true >
362 class Vector : public VectorAlgorithms< IsTrivialType >
367 * @brief Type definitions.
369 typedef VectorBase::SizeType SizeType;
370 typedef T* Iterator; ///< Most simple Iterator is a pointer
371 typedef const T* ConstIterator;
376 BaseType = IsTrivialType
380 * @brief Default constructor. Does not allocate any space.
386 * @brief Destructor. Releases the allocated space.
394 * @brief Copy constructor.
396 * @param vector Vector to copy from.
398 Vector( const Vector& vector )
405 * @brief Assignment operator.
407 * @param vector Vector to assign from.
408 * @return reference to self for chaining.
410 Vector& operator=( const Vector& vector )
412 if( this != &vector )
414 VectorAlgorithms<BaseType>::Copy( vector, sizeof( ItemType ) );
420 * @return Iterator to the beginning of the data.
422 Iterator Begin() const
424 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
429 * @return Iterator to the end of the data (one past last element).
433 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
434 address += VectorBase::Count();
439 * @pre index must be in the vector's range.
440 * @param index of the element.
441 * @return reference to the element for given index.
443 ItemType& operator[]( SizeType index )
445 // reuse the const version
446 return const_cast< ItemType& >( const_cast< const Vector< ItemType >& >( *this )[ index ] );
450 * @pre index must be in the vector's range.
451 * @param index of the element.
452 * @return reference to the element for given index.
454 const ItemType& operator[]( SizeType index ) const
456 DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
457 DALI_ASSERT_VECTOR( index < VectorBase::Count() && "Index out of bounds" );
458 ItemType* address = reinterpret_cast<ItemType*>( VectorBase::mData );
464 * @brief Push back an element to the vector.
466 * The underlying storage may be reallocated to provide space.
467 * If this occurs, all pre-existing pointers into the vector will
470 * @param[in] element to be added.
472 void PushBack( const ItemType& element )
474 const SizeType count = VectorBase::Count();
475 const SizeType newCount = count + 1u;
476 const SizeType capacity = VectorBase::Capacity();
477 if( newCount > capacity )
480 Reserve( newCount << 1u ); // reserve double the current count
482 // set new count first as otherwise the debug assert will hit us
483 VectorBase::SetCount( newCount );
484 operator[]( count ) = element;
488 *@brief Insert an element to the vector.
490 * Elements after \e at are moved one position to the right.
492 * The underlying storage may be reallocated to provide space.
493 * If this occurs, all pre-existing pointers into the vector will
496 * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
498 * @param[in] at Iterator where to insert the elements into the vector.
499 * @param[in] element to be added.
501 void Insert( Iterator at, const ItemType& element )
503 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
504 const SizeType size = sizeof( ItemType );
505 char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
506 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
513 * @brief Inserts the given elements into the vector.
515 * Elements after \e at are moved the number of given elements positions to the right.
517 * The underlying storage may be reallocated to provide space.
518 * If this occurs, all pre-existing pointers into the vector will
521 * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
522 * @pre Iterators \e from and \e to must be valid iterators.
523 * @pre Iterator \e from must not be grater than Iterator \e to.
525 * @param[in] at Iterator where to insert the elements into the vector.
526 * @param[in] from Iterator to the first element to be inserted.
527 * @param[in] to Iterator to the last element to be inserted.
529 void Insert( Iterator at, Iterator from, Iterator to )
531 DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
532 DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
540 VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
541 reinterpret_cast< char* >( from ),
542 reinterpret_cast< char* >( to ),
543 sizeof( ItemType ) );
547 * @brief Reserve space in the vector.
549 * Reserving less than current Capacity is a no-op.
550 * @param count of elements to reserve.
552 void Reserve( SizeType count )
554 VectorAlgorithms<BaseType>::Reserve( count, sizeof( ItemType ) );
558 * @brief Resize the vector. Does not change capacity.
560 * @param count to resize to.
561 * @param item to insert to the new indices.
563 void Resize( SizeType count, ItemType item = ItemType() )
565 const SizeType oldCount = VectorBase::Count();
566 if( count <= oldCount )
568 // getting smaller so just set count
569 VectorBase::SetCount( count );
573 // remember how many new items get added
574 SizeType newItems = count - oldCount;
576 for( ; newItems > 0u; --newItems )
584 * @brief Erase an element.
586 * Does not change capacity. Other elements get moved.
588 * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
590 * @param iterator Iterator pointing to item to remove.
591 * @return Iterator pointing to next element.
593 Iterator Erase( Iterator iterator )
595 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
596 if( iterator < ( End() - 1u ) )
598 VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
602 // just remove the element
609 * @brief Erase a range of elements.
611 * Does not change capacity. Other elements get moved.
613 * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
614 * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
615 * @pre Iterator \e first must not be grater than Iterator \e last.
617 * @param[in] first Iterator to the first element to be erased.
618 * @param[in] last Iterator to the last element to be erased.
620 * @return Iterator pointing to the next element of the last one.
622 Iterator Erase( Iterator first, Iterator last )
624 DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
625 DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
626 DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
628 Iterator nextElement;
632 // Erase up to the end.
633 VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
638 nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
639 reinterpret_cast< char* >( last ),
640 sizeof( ItemType ) ) );
647 * @brief Removes an element.
649 * Does not maintain order. Swaps the element with end and
650 * decreases size by one. This is much faster than Erase so use
651 * this in case order does not matter. Does not change capacity.
653 * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
655 * @param iterator Iterator pointing to item to remove.
657 void Remove( Iterator iterator )
659 DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
661 Iterator last = End() - 1u;
662 if( last > iterator )
664 std::swap( *iterator, *last );
666 VectorBase::SetCount( VectorBase::Count() - 1u );
670 * @brief Swap the contents of two vectors.
672 * @param vector Vector to swap with.
674 void Swap( Vector& vector )
676 VectorBase::Swap( vector );
680 * @brief Clear the contents of the vector. Keeps its capacity.
684 VectorAlgorithms<BaseType>::Clear();
688 * @brief Release the memory that the vector holds.
692 VectorAlgorithms<BaseType>::Release();
698 #endif /* __DALI_VECTOR_H__ */