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 )
66template<
class T,
bool Obsolete>
class DynamicArray;
67template<
class T,
bool Obsolete,
class U>
unsigned FindIndexOf(
const DynamicArray<T, Obsolete>& array,
const U&
object );
68template<
class T,
bool Obsolete>
void DeleteEachAndClear( DynamicArray<T, Obsolete>& array );
77template<
class T,
bool Obsolete = true>
90 if( initialCapacity != 0 )
96#if defined( DISTI_HAS_CPP11 )
107 const auto capacity =
static_cast<unsigned>( list.size() );
111 for(
auto&& item : list )
126 DeepCopyFrom( other );
136 DeepCopyFrom( other );
146 DeepCopyFrom( other );
158 DeepCopyFrom( other );
163#if defined( DISTI_HAS_RVAL_REFS )
167 : _capacity( other._capacity )
168 , _count( other._count )
169 , _objects( other._objects )
174 other._objects = NULL;
180 : _capacity( other._capacity )
181 , _count( other._count )
182 , _objects( other._objects )
187 other._objects = NULL;
195 StealFrom( std::move( other ) );
204 StealFrom( std::move( other ) );
218 unsigned Count()
const {
return _count; }
223 void Count(
const unsigned newCount )
225 if( newCount > _capacity )
230 if( newCount > _count )
234 for(
unsigned i = _count; i < newCount; ++i )
236 new( _objects + i ) T();
244 Destroy( newCount, _count );
268 if( newCapacity == _capacity )
273 else if( 0 == newCapacity )
275 Destroy( 0, _count );
276 std::free( _objects );
289 T*
const newObjects =
reinterpret_cast<T*
>( std::malloc(
sizeof( T ) * newCapacity ) );
292 throw std::bad_alloc();
296 if( newCapacity < _count )
298 Destroy( newCapacity, _count );
299 _count = newCapacity;
306 for(
unsigned i = 0; i < _count; i++ )
312 std::free( _objects );
316 _objects = newObjects;
317 _capacity = newCapacity;
331 unsigned Insert(
const T&
object,
unsigned loc )
333 return InsertImpl(
object, loc );
342 InsertImpl(
object, _count );
360 return _objects[ _count - 1 ];
368 return _objects[ _count - 1 ];
377 InsertImpl(
object, 0 );
381#if defined( DISTI_HAS_RVAL_REFS )
388 unsigned Insert( T&&
object,
unsigned loc )
390 return InsertImpl( std::move(
object ), loc );
398 return InsertImpl( std::move(
object ), _count );
407 InsertImpl( std::move(
object ), 0 );
429 if( index >= _count )
435 Destroy( _objects[ index ] );
438 for(
unsigned j = index; j < _count - 1u; j++ )
455 if( index >= _capacity )
459 else if( index >= _count )
461 for(
unsigned i = _count; i <= index; ++i )
463 Assign( _objects[ i ], T() );
469 return _objects[ index ];
478 GLS_ASSERT( index < _capacity && index < _count );
479 return _objects[ index ];
499 return ( _count == 0 );
518 void
Size(
unsigned newCapacity )
534 return Insert(
object, loc );
557 return Insert(
object, loc + 1 );
565 DISTI_DEPRECATED(
"Renamed to PushFront(), but prefer PushBack() instead to avoid inefficiencies." )
572#if defined( DISTI_HAS_RVAL_REFS )
583 Insert( std::move(
object ), loc );
594 return PushBack( std::move(
object ) );
605 Insert( std::move(
object ), loc + 1 );
613 DISTI_DEPRECATED(
"Renamed to PushFront(), but prefer PushBack() instead to avoid inefficiencies." )
628 return Erase(
object );
644 DISTI_DEPRECATED(
"Use the non-member function DeleteEachAndClear( dynamicArray ) instead." )
662 DISTI_DEPRECATED(
"Use helper function FindIndexOf() instead, but note that it returns unsigned." )
665 const unsigned index =
FindIndexOf( *
this,
object );
678 void GrowAround(
const unsigned loc )
680 const unsigned newCapacity = _capacity == 0 ? 2 : _capacity * 2;
681 T*
const newObjects =
reinterpret_cast<T*
>( std::malloc(
sizeof( T ) * newCapacity ) );
684 throw std::bad_alloc();
688 _capacity = newCapacity;
694 for(
unsigned i = 0; i < loc; i++ )
699 for(
unsigned i = loc; i < _count; i++ )
705 std::free( _objects );
708 _objects = newObjects;
714 unsigned InsertImpl(
DISTI_UREF( U )
object,
unsigned loc )
719 if( loc >= _count + 1 )
725 if( _count + 1 > _capacity )
732 for(
unsigned i = _count; i > loc; i-- )
744 template<
bool OtherObsolete>
745 void DeepCopyFrom(
const DynamicArray<T, OtherObsolete>& other )
748 for(
unsigned i = 0; i < other.Count(); i++ )
750 Assign( _objects[ i ], other._objects[ i ] );
752 _count = other.Count();
755#if defined( DISTI_HAS_RVAL_REFS )
756 template<
bool OtherObsolete>
757 void StealFrom( DynamicArray<T, OtherObsolete>&& other )
765 _capacity = other._capacity;
766 _count = other._count;
767 _objects = other._objects;
772 other._objects = NULL;
779 static void Assign( T& dest,
DISTI_UREF( U ) src )
788 static void Destroy( T&
object )
804 void Destroy(
unsigned i,
const unsigned end )
809 Destroy( _objects[ i++ ] );
817template<
class T,
bool Obsolete>
826template<
class T,
bool Obsolete>
835template<
class T,
bool Obsolete>
844template<
class T,
bool Obsolete>
854template<
class T,
bool Obsolete,
class U>
857 for(
unsigned i = 0, count = array.
Count(); i < count; ++i )
859 if(
object == array[ i ] )
870template<
class T,
bool Obsolete>
873 for(
unsigned i = 0; i < array.
Count(); ++i )
884template<
class T,
bool Obsolete>
887 for(
unsigned i = 0; i < array.
Count(); ++i )
Definition: dynamic_array.h:79
DynamicArray(std::initializer_list< T > list)
Definition: dynamic_array.h:102
bool Erase(const T &object)
Definition: dynamic_array.h:414
void Count(const unsigned newCount)
Definition: dynamic_array.h:223
void PushFront(const T &object)
Definition: dynamic_array.h:375
int Position(const T &object) const
Definition: dynamic_array.h:663
unsigned Capacity() const
Definition: dynamic_array.h:260
T * InternalArray()
Definition: dynamic_array.h:484
unsigned Insert(const T &object, unsigned loc)
Definition: dynamic_array.h:331
unsigned PushBack(T &&object)
Definition: dynamic_array.h:396
void ClearList()
Definition: dynamic_array.h:653
DynamicArray(const DynamicArray< T, true > &other)
Definition: dynamic_array.h:121
T ElementType
Typedef for the type of object contained in this array.
Definition: dynamic_array.h:81
const T & Back() const
Definition: dynamic_array.h:365
unsigned Count() const
Definition: dynamic_array.h:218
DynamicArray & operator=(const DynamicArray< T, false > &other)
Definition: dynamic_array.h:154
T & operator[](const unsigned index)
Definition: dynamic_array.h:452
~DynamicArray()
DynamicArray destructor. Clears the array but does not delete the objects being pointed to by the arr...
Definition: dynamic_array.h:210
T & Back()
Definition: dynamic_array.h:357
unsigned Insert(T &&object, unsigned loc)
Definition: dynamic_array.h:388
const T * InternalArray() const
Definition: dynamic_array.h:491
bool EraseAt(const unsigned index)
Definition: dynamic_array.h:427
bool DeleteObjectAtIndex(const unsigned index)
Definition: dynamic_array.h:637
DynamicArray(DynamicArray< T, true > &&other)
Definition: dynamic_array.h:166
void PushObject(const T &object)
Definition: dynamic_array.h:566
unsigned InsertObjectAfter(const T &object, unsigned loc)
Definition: dynamic_array.h:555
DynamicArray(DynamicArray< T, false > &&other)
Definition: dynamic_array.h:179
bool IsEmpty() const
Definition: dynamic_array.h:497
void Clear()
Definition: dynamic_array.h:253
void PopBack()
Removes the last item in the array. If the array is empty, it has no effect.
Definition: dynamic_array.h:347
void EmptyList()
Definition: dynamic_array.h:645
const T & operator[](const unsigned index) const
Definition: dynamic_array.h:476
DynamicArray(const DynamicArray< T, false > &other)
Definition: dynamic_array.h:131
unsigned InsertObject(const T &object, unsigned loc)
Definition: dynamic_array.h:532
void PushFront(T &&object)
Definition: dynamic_array.h:405
unsigned PushBack(const T &object)
Definition: dynamic_array.h:340
unsigned Size() const
Definition: dynamic_array.h:511
void Capacity(const unsigned newCapacity)
Definition: dynamic_array.h:265
void StripCapacity()
Definition: dynamic_array.h:322
DynamicArray & operator=(DynamicArray< T, true > &&other)
Definition: dynamic_array.h:193
DynamicArray(unsigned initialCapacity=0)
Definition: dynamic_array.h:85
DynamicArray & operator=(const DynamicArray< T, true > &other)
Definition: dynamic_array.h:142
DynamicArray & operator=(DynamicArray< T, false > &&other)
Definition: dynamic_array.h:202
bool DeleteObject(const T &object)
Definition: dynamic_array.h:626
Contains the DistiAssert macro.
#define GLS_ASSERT(exp)
Definition: disti_assert.h:150
#define GLS_VERIFY(exp)
Definition: disti_assert.h:170
Macros and helper code to determine what subset of C++11/14/17 is available.
#define DISTI_UREF_FORWARD(T, x)
Macro to wrap std::forward, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:195
#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:267
#define DISTI_RVAL_MOVE(x)
Macro to wrap std::move, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:193
#define DISTI_TT_FALSE_TYPE
Macro wrapping std::false_type, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:271
#define DISTI_DEPRECATED(msg)
Defines whether this compiler supports the C++14 deprecated attribute.
Definition: gls_cpp_lang_support.h:457
#define DISTI_UREF(T)
Macro to wrap r-value reference, removed on playforms that don't support it.
Definition: gls_cpp_lang_support.h:197
#define DISTI_TT_TRUE_TYPE
Macro wrapping std::true_type, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:269
#define DISTI_FINAL
Macro to wrap the final keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:216
A file for all GL Studio files to include.
Force inclusion of the DirectShow library.
Definition: bmpimage.h:47
DistiAttribDict::const_iterator begin(const DistiAttribDict &dict)
Definition: disti_metadata.h:954
void DeleteArraysAndClear(DynamicArray< T, Obsolete > &array)
Definition: dynamic_array.h:885
DistiAttribDict::const_iterator end(const DistiAttribDict &dict)
Definition: disti_metadata.h:959
unsigned FindIndexOf(const DynamicArray< T, Obsolete > &array, const U &object)
Definition: dynamic_array.h:855
const unsigned g_itemNotFound
Constant returned by FindIndexOf() if the item is not found.
Definition: dynamic_array.h:72
void DeleteEachAndClear(DynamicArray< T, Obsolete > &array)
Definition: dynamic_array.h:871