39 #ifndef INCLUDED_DISTI_DYNAMIC_ARRAY_H
40 #define INCLUDED_DISTI_DYNAMIC_ARRAY_H
51 #if defined( DISTI_HAS_CPP11 )
52 # include <initializer_list>
55 #if defined( DISTI_HAS_TYPE_TRAITS )
56 # include <type_traits>
58 #if defined( DISTI_HAS_RVAL_REFS )
77 template<
class T,
bool Obsolete = true>
81 typedef T ElementType;
90 if( initialCapacity != 0 )
96 #if defined( DISTI_HAS_CPP11 )
106 const auto capacity =
static_cast<unsigned>( list.size() );
110 for(
auto&& item : list )
124 DeepCopyFrom( other );
133 DeepCopyFrom( other );
141 DeepCopyFrom( other );
151 DeepCopyFrom( other );
156 #if defined( DISTI_HAS_RVAL_REFS )
159 : _capacity( other._capacity )
160 , _count( other._count )
161 , _objects( other._objects )
166 other._objects = NULL;
171 : _capacity( other._capacity )
172 , _count( other._count )
173 , _objects( other._objects )
178 other._objects = NULL;
184 StealFrom( std::move( other ) );
190 StealFrom( std::move( other ) );
204 unsigned Count()
const {
return _count; }
208 void Count(
const unsigned newCount )
210 if( newCount > _capacity )
215 if( newCount > _count )
219 for(
unsigned i = _count; i < newCount; ++i )
221 new( _objects + i ) T();
229 Destroy( newCount, _count );
253 if( newCapacity == _capacity )
258 else if( 0 == newCapacity )
260 Destroy( 0, _count );
261 std::free( _objects );
274 T*
const newObjects =
reinterpret_cast<T*
>( std::malloc(
sizeof( T ) * newCapacity ) );
277 throw std::bad_alloc();
281 if( newCapacity < _count )
283 Destroy( newCapacity, _count );
284 _count = newCapacity;
291 for(
unsigned i = 0; i < _count; i++ )
293 Assign( newObjects[ i ], DISTI_RVAL_MOVE( _objects[ i ] ) );
297 std::free( _objects );
301 _objects = newObjects;
302 _capacity = newCapacity;
316 unsigned Insert(
const T&
object,
unsigned loc )
318 return InsertImpl(
object, loc );
327 InsertImpl(
object, _count );
345 return _objects[ _count - 1 ];
353 return _objects[ _count - 1 ];
361 InsertImpl(
object, 0 );
365 #if defined( DISTI_HAS_RVAL_REFS )
372 unsigned Insert( T&&
object,
unsigned loc )
374 return InsertImpl( std::move(
object ), loc );
381 return InsertImpl( std::move(
object ), _count );
389 InsertImpl( std::move(
object ), 0 );
399 if( loc != g_itemNotFound )
411 if( index >= _count )
417 Destroy( _objects[ index ] );
420 for(
unsigned j = index; j < _count - 1u; j++ )
422 Assign( _objects[ j ], DISTI_RVAL_MOVE( _objects[ j + 1u ] ) );
435 if( index >= _capacity )
439 else if( index >= _count )
441 for(
unsigned i = _count; i <= index; ++i )
443 Assign( _objects[ i ], T() );
449 return _objects[ index ];
456 GLS_ASSERT( index < _capacity && index < _count );
457 return _objects[ index ];
477 return ( _count == 0 );
497 void
Size(
unsigned newCapacity )
513 return Insert(
object, loc );
535 return Insert(
object, loc + 1 );
542 DISTI_DEPRECATED(
"Renamed to PushFront(), but prefer PushBack() instead to avoid inefficiencies." )
549 #if defined( DISTI_HAS_RVAL_REFS )
561 Insert( std::move(
object ), loc );
571 return PushBack( std::move(
object ) );
583 Insert( std::move(
object ), loc + 1 );
590 DISTI_DEPRECATED(
"Renamed to PushFront(), but prefer PushBack() instead to avoid inefficiencies." )
605 return Erase(
object );
621 DISTI_DEPRECATED(
"Use the non-member function DeleteEachAndClear( dynamicArray ) instead." )
638 DISTI_DEPRECATED(
"Use helper function FindIndexOf() instead, but note that it returns unsigned." )
641 const unsigned index =
FindIndexOf( *
this,
object );
642 return index == g_itemNotFound ? -1 :
static_cast<int>( index );
654 void GrowAround(
const unsigned loc )
656 const unsigned newCapacity = _capacity == 0 ? 2 : _capacity * 2;
657 T*
const newObjects =
reinterpret_cast<T*
>( std::malloc(
sizeof( T ) * newCapacity ) );
660 throw std::bad_alloc();
664 _capacity = newCapacity;
670 for(
unsigned i = 0; i < loc; i++ )
672 Assign( newObjects[ i ], DISTI_RVAL_MOVE( _objects[ i ] ) );
675 for(
unsigned i = loc; i < _count; i++ )
677 Assign( newObjects[ i + 1 ], DISTI_RVAL_MOVE( _objects[ i ] ) );
681 std::free( _objects );
684 _objects = newObjects;
690 unsigned InsertImpl( DISTI_UREF( U )
object,
unsigned loc )
695 if( loc >= _count + 1 )
701 if( _count + 1 > _capacity )
708 for(
unsigned i = _count; i > loc; i-- )
710 Assign( _objects[ i ], DISTI_RVAL_MOVE( _objects[ i - 1 ] ) );
715 Assign( _objects[ loc ], DISTI_UREF_FORWARD( U,
object ) );
720 template<
bool OtherObsolete>
721 void DeepCopyFrom(
const DynamicArray<T, OtherObsolete>& other )
724 for(
unsigned i = 0; i < other.Count(); i++ )
726 Assign( _objects[ i ], other._objects[ i ] );
728 _count = other.Count();
731 #if defined( DISTI_HAS_RVAL_REFS )
732 template<
bool OtherObsolete>
733 void StealFrom( DynamicArray<T, OtherObsolete>&& other )
741 _capacity = other._capacity;
742 _count = other._count;
743 _objects = other._objects;
748 other._objects = NULL;
755 static void Assign( T& dest, DISTI_UREF( U ) src )
760 new( &dest ) T( DISTI_UREF_FORWARD( U, src ) );
764 static void Destroy( T&
object )
770 static void DestroyImpl( T&
object, DISTI_TT_FALSE_TYPE )
776 static void DestroyImpl( T& , DISTI_TT_TRUE_TYPE ) {}
780 void Destroy(
unsigned i,
const unsigned end )
785 Destroy( _objects[ i++ ] );
791 template<
class T,
bool Obsolete>
798 template<
class T,
bool Obsolete>
805 template<
class T,
bool Obsolete>
812 template<
class T,
bool Obsolete>
820 template<
class T,
bool Obsolete,
class U>
823 for(
unsigned i = 0, count = array.
Count(); i < count; ++i )
825 if(
object == array[ i ] )
835 template<
class T,
bool Obsolete>
838 for(
unsigned i = 0; i < array.
Count(); ++i )
848 template<
class T,
bool Obsolete>
851 for(
unsigned i = 0; i < array.
Count(); ++i )
const T & operator[](const unsigned index) const
Definition: dynamic_array.h:454
unsigned Size() const
Definition: dynamic_array.h:489
unsigned Insert(const T &object, unsigned loc)
Definition: dynamic_array.h:316
unsigned PushBack(T &&object)
Definition: dynamic_array.h:379
void Capacity(const unsigned newCapacity)
Definition: dynamic_array.h:250
#define DISTI_DEPRECATED(msg)
Defines whether this compiler supports the C++14 deprecated attribute.
Definition: gls_cpp_lang_support.h:436
unsigned Insert(T &&object, unsigned loc)
Definition: dynamic_array.h:372
DynamicArray & operator=(const DynamicArray< T, true > &other)
Assign this array by deep-copying other.
Definition: dynamic_array.h:137
DynamicArray(DynamicArray< T, true > &&other)
Construct this array by taking ownership of other's contents.
Definition: dynamic_array.h:158
unsigned Count() const
Definition: dynamic_array.h:204
Definition: dynamic_array.h:66
DynamicArray & operator=(const DynamicArray< T, false > &other)
Assign this array by deep-copying other.
Definition: dynamic_array.h:147
void Count(const unsigned newCount)
Definition: dynamic_array.h:208
void PushFront(T &&object)
Definition: dynamic_array.h:387
DynamicArray(unsigned initialCapacity=0)
Definition: dynamic_array.h:85
const T * end(const DynamicArray< T, Obsolete > &array)
Overload end() so that range-for loops and other constructs work with DynamicArray.
Definition: dynamic_array.h:813
void DeleteEachAndClear(DynamicArray< T, Obsolete > &array)
Definition: dynamic_array.h:836
void EmptyList()
Definition: dynamic_array.h:622
int Position(const T &object) const
Definition: dynamic_array.h:639
~DynamicArray()
DynamicArray destructor. Clears the array but does not delete the objects being pointed to by the arr...
Definition: dynamic_array.h:196
void PushObject(const T &object)
Definition: dynamic_array.h:543
A file for all GL Studio files to include.
unsigned FindIndexOf(const DynamicArray< T, Obsolete > &array, const U &object)
Definition: dynamic_array.h:821
DynamicArray & operator=(DynamicArray< T, true > &&other)
Assign this array by clearing any current contents and then taking ownership of other's contents...
Definition: dynamic_array.h:182
unsigned Capacity() const
Definition: dynamic_array.h:245
#define GLS_ASSERT(exp)
Definition: disti_assert.h:135
bool Erase(const T &object)
Definition: dynamic_array.h:396
bool DeleteObjectAtIndex(const unsigned index)
Definition: dynamic_array.h:614
T & operator[](const unsigned index)
Definition: dynamic_array.h:432
#define GLS_VERIFY(exp)
Definition: disti_assert.h:155
const T & Back() const
Definition: dynamic_array.h:350
unsigned InsertObject(const T &object, unsigned loc)
Definition: dynamic_array.h:511
DynamicArray(DynamicArray< T, false > &&other)
Construct this array by taking ownership of other's contents.
Definition: dynamic_array.h:170
#define DISTI_IS_TRIVIALLY_DESTRUCTIBLE(T)
Determines if a class T is trivially destructible (i.e., has no side effects)
Definition: gls_cpp_lang_support.h:250
T * InternalArray()
Definition: dynamic_array.h:462
DistiAttribDict::const_iterator begin(const DistiAttribDict &dict)
Definition: disti_metadata.h:860
T & Back()
Definition: dynamic_array.h:342
const unsigned g_itemNotFound
Constant returned by FindIndexOf() if the item is not found.
Definition: dynamic_array.h:72
DynamicArray(std::initializer_list< T > list)
Definition: dynamic_array.h:101
DynamicArray(const DynamicArray< T, true > &other)
Initialize this array by deep-copying other.
Definition: dynamic_array.h:119
bool IsEmpty() const
Definition: dynamic_array.h:475
const T * InternalArray() const
Definition: dynamic_array.h:469
unsigned PushBack(const T &object)
Definition: dynamic_array.h:325
bool EraseAt(const unsigned index)
Definition: dynamic_array.h:409
Contains the DistiAssert macro.
void Clear()
Definition: dynamic_array.h:238
void PushFront(const T &object)
Definition: dynamic_array.h:359
Macros and helper code to determine what subset of C++11/14/17 is available.
void DeleteArraysAndClear(DynamicArray< T, Obsolete > &array)
Definition: dynamic_array.h:849
Definition: bmpimage.h:46
void PopBack()
Removes the last item in the array. If the array is empty, it has no effect.
Definition: dynamic_array.h:332
unsigned InsertObjectAfter(const T &object, unsigned loc)
Definition: dynamic_array.h:533
DynamicArray(const DynamicArray< T, false > &other)
Initialize this array by deep-copying other.
Definition: dynamic_array.h:128
void StripCapacity()
Definition: dynamic_array.h:307
void ClearList()
Definition: dynamic_array.h:630
bool DeleteObject(const T &object)
Definition: dynamic_array.h:603