2020-02-23 12:21:28 +01:00
//*************************************************************************************************
//*************************************************************************************************
//
// XObjArray
//
// Developed by jief666, from 1997.
//
//*************************************************************************************************
//*************************************************************************************************
# if !defined(__XOBJARRAY_H__)
# define __XOBJARRAY_H__
2020-04-24 08:36:29 +02:00
# include <XToolsConf.h>
2020-04-26 12:12:05 +02:00
# include "XToolsCommon.h"
2020-02-23 12:21:28 +01:00
# if 1
2020-04-24 11:30:09 +02:00
# define XObjArray_DBG(...) printf__VA_ARGS__)
2020-02-23 12:21:28 +01:00
# else
# define XObjArray_DBG(...)
# endif
template < class TYPE >
class XObjArrayEntry
{
public :
TYPE * Object ;
bool FreeIt ;
} ;
template < class TYPE >
class XObjArrayNC
{
public :
XObjArrayEntry < TYPE > * _Data ;
2020-04-26 12:12:05 +02:00
size_t _Len ;
size_t m_allocatedSize ;
2020-02-23 12:21:28 +01:00
public :
void Init ( ) ;
XObjArrayNC ( ) { Init ( ) ; }
virtual ~ XObjArrayNC ( ) ;
protected :
2020-04-24 11:30:09 +02:00
// XObjArrayNC(const XObjArrayNC<TYPE> &anObjArrayNC) { (void)anObjArrayNC; panic("Intentionally not defined"); }
// const XObjArrayNC<TYPE> &operator =(const XObjArrayNC<TYPE> &anObjArrayNC) { (void)anObjArrayNC; panic("Intentionally not defined"); }
XObjArrayNC ( const XObjArrayNC < TYPE > & anObjArrayNC ) = delete ;
const XObjArrayNC < TYPE > & operator = ( const XObjArrayNC < TYPE > & anObjArrayNC ) = delete ;
2020-02-23 12:21:28 +01:00
xsize _getLen ( ) const { return _Len ; }
public :
2020-03-09 02:12:24 +01:00
xsize AllocatedSize ( ) const { return m_allocatedSize ; }
2020-02-27 15:34:29 +01:00
xsize size ( ) const { return _Len ; }
xsize length ( ) const { return _Len ; }
2020-02-23 12:21:28 +01:00
2020-02-27 15:34:29 +01:00
bool NotNull ( ) const { return size ( ) > 0 ; }
bool IsNull ( ) const { return size ( ) = = 0 ; }
2020-02-23 12:21:28 +01:00
2020-04-26 12:12:05 +02:00
template < typename IntegralType , enable_if ( is_integral ( IntegralType ) ) >
const TYPE & ElementAt ( IntegralType nIndex ) const
{
if ( nIndex < 0 ) {
panic ( " XObjArrayNC::ElementAt() : i < 0. System halted \n " ) ;
}
if ( ( unsigned_type ( IntegralType ) ) nIndex > = _Len ) {
panic ( " XObjArrayNC::ElementAt() -> operator [] - index (%zu) greater than length (%zu) \n " , ( size_t ) nIndex , _Len ) ;
}
return * ( ( TYPE * ) ( _Data [ nIndex ] . Object ) ) ;
}
template < typename IntegralType , enable_if ( is_integral ( IntegralType ) ) >
TYPE & ElementAt ( IntegralType nIndex )
{
if ( nIndex < 0 ) {
panic ( " XObjArrayNC::ElementAt() : i < 0. System halted \n " ) ;
}
if ( ( unsigned_type ( IntegralType ) ) nIndex > = _Len ) {
panic ( " XObjArrayNC::ElementAt() const -> operator [] - index (%zu) greater than length (%zu) \n " , ( size_t ) nIndex , _Len ) ;
}
return * ( ( TYPE * ) ( _Data [ nIndex ] . Object ) ) ;
}
2020-02-23 12:21:28 +01:00
// This was useful for realtime debugging with a debugger that do not recognise references. That was years and years ago. Probably not needed anymore.
# ifdef _DEBUG_iufasdfsfk
const TYPE * DbgAt ( int i ) const { if ( i > = 0 & & ( xsize ) i < _Len ) return & ElementAt ( ( xsize ) i ) ; else return NULL ; }
# endif
2020-04-26 12:12:05 +02:00
template < typename IntegralType , enable_if ( is_integral ( IntegralType ) ) >
const TYPE & operator [ ] ( IntegralType nIndex ) const { return ElementAt ( nIndex ) ; }
template < typename IntegralType , enable_if ( is_integral ( IntegralType ) ) >
TYPE & operator [ ] ( IntegralType nIndex ) { return ElementAt ( nIndex ) ; }
2020-02-23 12:21:28 +01:00
xsize AddReference ( TYPE * newElement , bool FreeIt ) ;
// xsize InsertRef(TYPE *newElement, xsize pos, bool FreeIt = false);
xsize InsertRef ( TYPE * newElement , xsize pos , bool FreeIt ) ;
void SetFreeIt ( xsize nIndex , bool Flag ) ;
void SetFreeIt ( const TYPE * Element , bool Flag ) ;
void Remove ( const TYPE * Element ) ;
void RemoveWithoutFreeing ( const TYPE * Element ) ;
void Remove ( const TYPE & Element ) ;
void RemoveAtIndex ( xsize nIndex ) ;
void RemoveAtIndex ( int nIndex ) ;
void RemoveWithoutFreeing ( xsize nIndex ) ; // If you use this, there might be a design problem somewhere ???
//void Remove(int nIndex);
void RemoveAllBut ( const TYPE * Element ) ;
void Empty ( ) ;
public :
void CheckSize ( xsize nNewSize , xsize nGrowBy = XArrayGrowByDefault ) ;
} ;
template < class TYPE >
class XObjArray : public XObjArrayNC < TYPE >
{
public :
XObjArray ( ) : XObjArrayNC < TYPE > ( ) { }
XObjArray ( const XObjArray < TYPE > & anObjArray ) ;
const XObjArray < TYPE > & operator = ( const XObjArray < TYPE > & anObjArray ) ;
xsize AddCopy ( const TYPE & newElement , bool FreeIt = true ) ;
xsize AddCopies ( const TYPE & n1 , bool FreeIt = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , const TYPE & n12 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , const TYPE & n12 , const TYPE & n13 , bool FreeThem = true ) ;
xsize AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , const TYPE & n12 , const TYPE & n13 , const TYPE & n14 , bool FreeThem = true ) ;
//TYPE & AddNew(bool FreeIt = true);
xsize InsertCopy ( const TYPE & newElement , xsize pos ) ;
} ;
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//
// XObjArray
//
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/* Constructeur */
template < class TYPE >
void XObjArrayNC < TYPE > : : Init ( )
{
2020-02-23 21:23:31 +01:00
_Data = nullptr ;
2020-03-09 02:12:24 +01:00
m_allocatedSize = 0 ;
2020-02-23 12:21:28 +01:00
_Len = 0 ;
// THis was useful for realtime debugging with a debugger that do not recognise references.
# ifdef _DEBUG_iufasdfsfk
{
const TYPE * tmp ;
tmp = DbgAt ( 0 ) ;
}
# endif
}
/* Constructeur */
template < class TYPE >
2020-04-09 19:07:32 +02:00
XObjArray < TYPE > : : XObjArray ( const XObjArray < TYPE > & anObjArray ) : XObjArrayNC < TYPE > ( )
2020-02-23 12:21:28 +01:00
{
xsize ui ;
XObjArrayNC < TYPE > : : Init ( ) ;
2020-02-27 15:34:29 +01:00
this - > CheckSize ( anObjArray . size ( ) , ( xsize ) 0 ) ;
for ( ui = 0 ; ui < anObjArray . size ( ) ; ui + = 1 ) AddCopy ( anObjArray . ElementAt ( ui ) ) ;
2020-02-23 12:21:28 +01:00
}
/* operator = */
template < class TYPE >
const XObjArray < TYPE > & XObjArray < TYPE > : : operator = ( const XObjArray < TYPE > & anObjArray )
{
xsize ui ;
XObjArrayNC < TYPE > : : Empty ( ) ;
2020-04-26 12:12:05 +02:00
this - > CheckSize ( anObjArray . length ( ) , 0 ) ;
2020-02-27 15:34:29 +01:00
for ( ui = 0 ; ui < anObjArray . size ( ) ; ui + = 1 ) AddCopy ( anObjArray . ElementAt ( ui ) ) ;
2020-02-23 12:21:28 +01:00
return * this ;
}
/* Destructeur */
template < class TYPE >
XObjArrayNC < TYPE > : : ~ XObjArrayNC ( )
{
//printf("XObjArray Destructor\n");
Empty ( ) ;
2020-03-21 14:12:26 +01:00
if ( _Data ) free ( _Data ) ;
2020-02-23 12:21:28 +01:00
}
/* CheckSize() */
template < class TYPE >
void XObjArrayNC < TYPE > : : CheckSize ( xsize nNewSize , xsize nGrowBy )
{
2020-03-09 02:12:24 +01:00
if ( m_allocatedSize < nNewSize ) {
2020-02-23 12:21:28 +01:00
nNewSize + = nGrowBy + 1 ;
2020-04-24 08:36:29 +02:00
_Data = ( XObjArrayEntry < TYPE > * ) Xrealloc ( ( void * ) _Data , sizeof ( XObjArrayEntry < TYPE > ) * nNewSize , sizeof ( XObjArrayEntry < TYPE > ) * m_allocatedSize ) ;
2020-02-23 12:21:28 +01:00
if ( ! _Data ) {
2020-04-24 08:36:29 +02:00
panic ( " XObjArrayNC<TYPE>::CheckSize(nNewSize=%zu, nGrowBy=%zu) : Xrealloc(%zu, %zu, % " PRIuPTR " ) returned NULL. System halted \n " , nNewSize , nGrowBy , m_allocatedSize , sizeof ( XObjArrayEntry < TYPE > ) * nNewSize , ( uintptr_t ) _Data ) ;
2020-02-23 12:21:28 +01:00
}
2020-03-09 02:12:24 +01:00
// memset(&_Data[m_allocatedSize], 0, (nNewSize-m_allocatedSize) * sizeof(XObjArrayEntry<TYPE>));
m_allocatedSize = nNewSize ;
2020-02-23 12:21:28 +01:00
}
}
///* Add() */
//template<class TYPE>
//TYPE &XObjArray<TYPE>::AddNew(bool FreeIt)
//{
// XObjArrayNC<TYPE>::CheckSize(XObjArray<TYPE>::_getLen()+1);
// XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len].Object = new TYPE;
// XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len].FreeIt = FreeIt;
// XObjArray<TYPE>::_Len += 1;
// return *((TYPE *)(XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len-1].Object));
//}
/* Add(TYPE &, xsize) */
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopy ( const TYPE & newElement , bool FreeIt )
{
XObjArrayNC < TYPE > : : CheckSize ( XObjArray < TYPE > : : _Len + 1 ) ;
XObjArray < TYPE > : : _Data [ XObjArray < TYPE > : : _Len ] . Object = new TYPE ( newElement ) ;
XObjArray < TYPE > : : _Data [ XObjArray < TYPE > : : _Len ] . FreeIt = FreeIt ;
XObjArray < TYPE > : : _Len + = 1 ;
return XObjArray < TYPE > : : _Len - 1 ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , bool FreeIt )
{
return AddCopy ( n1 , FreeIt ) ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , FreeThem ) ;
AddCopy ( n2 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , FreeThem ) ;
AddCopy ( n3 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , FreeThem ) ;
AddCopy ( n4 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , FreeThem ) ;
AddCopy ( n5 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , FreeThem ) ;
AddCopy ( n6 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , FreeThem ) ;
AddCopy ( n7 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , FreeThem ) ;
AddCopy ( n8 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , FreeThem ) ;
AddCopy ( n9 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 , FreeThem ) ;
AddCopy ( n10 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 , n10 , FreeThem ) ;
AddCopy ( n11 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , const TYPE & n12 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 , n10 , n11 , FreeThem ) ;
AddCopy ( n12 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , const TYPE & n12 , const TYPE & n13 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 , n10 , n11 , n12 , FreeThem ) ;
AddCopy ( n13 , FreeThem ) ;
return ui ;
}
template < class TYPE >
xsize XObjArray < TYPE > : : AddCopies ( const TYPE & n1 , const TYPE & n2 , const TYPE & n3 , const TYPE & n4 , const TYPE & n5 , const TYPE & n6 , const TYPE & n7 , const TYPE & n8 , const TYPE & n9 , const TYPE & n10 , const TYPE & n11 , const TYPE & n12 , const TYPE & n13 , const TYPE & n14 , bool FreeThem )
{
xsize ui = AddCopies ( n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 , n10 , n11 , n12 , n13 , FreeThem ) ;
AddCopy ( n14 , FreeThem ) ;
return ui ;
}
/* Add(TYPE *, xsize) */
template < class TYPE >
xsize XObjArrayNC < TYPE > : : AddReference ( TYPE * newElement , bool FreeIt )
{
XObjArrayNC < TYPE > : : CheckSize ( XObjArrayNC < TYPE > : : _Len + 1 ) ;
XObjArrayNC < TYPE > : : _Data [ XObjArrayNC < TYPE > : : _Len ] . Object = newElement ;
XObjArrayNC < TYPE > : : _Data [ XObjArrayNC < TYPE > : : _Len ] . FreeIt = FreeIt ;
XObjArrayNC < TYPE > : : _Len + = 1 ;
return XObjArrayNC < TYPE > : : _Len - 1 ;
}
/* Insert(TYPE &, xsize) */
template < class TYPE >
xsize XObjArray < TYPE > : : InsertCopy ( const TYPE & newElement , xsize pos )
{
if ( pos < XObjArray < TYPE > : : _Len ) {
XObjArrayNC < TYPE > : : CheckSize ( XObjArray < TYPE > : : _Len + 1 ) ;
memmove ( & XObjArray < TYPE > : : _Data [ pos + 1 ] , & XObjArray < TYPE > : : _Data [ pos ] , ( XObjArray < TYPE > : : _Len - pos ) * sizeof ( XObjArrayEntry < TYPE > ) ) ;
XObjArray < TYPE > : : _Data [ pos ] . Object = new TYPE ( newElement ) ;
XObjArray < TYPE > : : _Data [ pos ] . FreeIt = true ;
XObjArray < TYPE > : : _Len + = 1 ;
return pos ;
} else {
return AddCopy ( newElement ) ;
}
}
/* Insert(TYPE &, xsize) */
template < class TYPE >
xsize XObjArrayNC < TYPE > : : InsertRef ( TYPE * newElement , xsize pos , bool FreeIt )
{
if ( pos < XObjArrayNC < TYPE > : : _Len ) {
CheckSize ( XObjArrayNC < TYPE > : : _Len + 1 ) ;
memmove ( & XObjArrayNC < TYPE > : : _Data [ pos + 1 ] , & XObjArrayNC < TYPE > : : _Data [ pos ] , ( XObjArrayNC < TYPE > : : _Len - pos ) * sizeof ( XObjArrayEntry < TYPE > ) ) ;
_Data [ pos ] . Object = newElement ;
_Data [ pos ] . FreeIt = FreeIt ;
XObjArrayNC < TYPE > : : _Len + = 1 ;
return pos ;
} else {
return AddRef ( newElement , FreeIt ) ;
}
}
/* SetFreeIt(xsize, bool) */
template < class TYPE >
void XObjArrayNC < TYPE > : : SetFreeIt ( xsize nIndex , bool Flag )
{
if ( nIndex < XObjArrayNC < TYPE > : : _Len )
{
XObjArrayNC < TYPE > : : _Data [ nIndex ] . FreeIt = Flag ;
}
else {
# if defined(_DEBUG)
throw " XObjArray::SetFreeIt(xsize) -> nIndex >= _Len \n " ;
# endif
}
}
/* SetFreeIt(const TYPE *Element, bool) */
template < class TYPE >
void XObjArrayNC < TYPE > : : SetFreeIt ( const TYPE * Element , bool Flag )
{
xsize i ;
for ( i = 0 ; i < XObjArrayNC < TYPE > : : _Len ; i + = 1 ) {
if ( ( ( TYPE * ) XObjArrayNC < TYPE > : : _Data [ i ] . Object ) = = Element ) {
SetFreeIt ( i , Flag ) ;
return ;
}
}
# if defined(_DEBUG)
throw " XObjArray::SetFreeIt(const TYPE *) -> nIndex >= _Len \n " ;
# endif
}
/* Remove(xsize) */
template < class TYPE >
void XObjArrayNC < TYPE > : : RemoveAtIndex ( xsize nIndex )
{
if ( nIndex < XObjArrayNC < TYPE > : : _Len )
{
if ( nIndex > = XObjArrayNC < TYPE > : : _Len ) {
2020-04-24 11:30:09 +02:00
panic ( " void XObjArrayNC<TYPE>::RemoveAtIndex(xsize nIndex) : BUG nIndex (%zu) is > length(). System halted \n " , nIndex ) ;
2020-02-23 12:21:28 +01:00
}
}
if ( _Data [ nIndex ] . FreeIt )
{
TYPE * TmpObject ; // BCB 4 oblige me to use a tmp var for doing the delete.
TmpObject = ( TYPE * ) ( _Data [ nIndex ] . Object ) ;
delete TmpObject ;
}
2020-03-21 14:12:26 +01:00
if ( nIndex < XObjArrayNC < TYPE > : : _Len - 1 ) memmove ( & _Data [ nIndex ] , & _Data [ nIndex + 1 ] , ( _Len - nIndex - 1 ) * sizeof ( XObjArrayEntry < TYPE > ) ) ;
2020-02-23 12:21:28 +01:00
_Len - = 1 ;
return ;
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* RemoveWithoutFreeing(xsize) */
template < class TYPE >
void XObjArrayNC < TYPE > : : RemoveWithoutFreeing ( xsize nIndex )
{
if ( nIndex < _Len )
{
if ( nIndex < _Len - 1 ) memcpy ( & _Data [ nIndex ] , & _Data [ nIndex + 1 ] , ( _Len - nIndex - 1 ) * sizeof ( XObjArrayEntry < TYPE > ) ) ;
_Len - = 1 ;
return ;
}
# if defined(_DEBUG)
throw " XObjArray::RemoveWithoutFreeing(xsize) -> nIndex > _Len \n " ;
# endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* Remove(int) */
template < class TYPE >
void XObjArrayNC < TYPE > : : RemoveAtIndex ( int nIndex )
{
2020-04-24 08:36:29 +02:00
# if defined(__XTOOLS_CHECK_OVERFLOW__)
2020-02-23 12:21:28 +01:00
if ( nIndex < 0 ) {
2020-04-24 11:30:09 +02:00
panic ( " XArray<TYPE>::RemoveAtIndex(int nIndex) : BUG nIndex (%d) is < 0. System halted \n " , nIndex ) ;
}
2020-02-23 12:21:28 +01:00
# endif
RemoveAtIndex ( ( xsize ) nIndex ) ; // Remove(xsize) will check that index is < _Len
}
/* Remove(const TYPE &) */
template < class TYPE >
void XObjArrayNC < TYPE > : : Remove ( const TYPE & Element )
{
xsize i ;
for ( i = 0 ; i < _Len ; i + = 1 ) {
if ( * ( ( TYPE * ) ( _Data [ i ] . Object ) ) = = Element ) {
2020-04-05 14:25:39 +02:00
RemoveAtIndex ( i ) ;
2020-02-23 12:21:28 +01:00
return ;
}
}
# if defined(_DEBUG)
2020-04-24 11:30:09 +02:00
printf ( " XObjArray::Remove(TYPE &) -> Not found \n " ) ;
2020-04-05 14:25:39 +02:00
panic ( ) ;
2020-02-23 12:21:28 +01:00
# endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* Remove(const TYPE *) */
template < class TYPE >
void XObjArrayNC < TYPE > : : Remove ( const TYPE * Element )
{
xsize i ;
for ( i = 0 ; i < _Len ; i + = 1 ) {
if ( ( ( TYPE * ) _Data [ i ] . Object ) = = Element ) {
Remove ( i ) ;
return ;
}
}
# if defined(_DEBUG)
throw " XObjArray::Remove(TYPE *) -> not found \n " ;
# endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* RemoveWithoutFreeing(const TYPE *) */
template < class TYPE >
void XObjArrayNC < TYPE > : : RemoveWithoutFreeing ( const TYPE * Element )
{
xsize i ;
for ( i = 0 ; i < _Len ; i + = 1 ) {
if ( ( ( TYPE * ) _Data [ i ] . Object ) = = Element ) {
RemoveWithoutFreeing ( i ) ;
return ;
}
}
# if defined(_DEBUG)
throw " XObjArray::RemoveWithoutFreeing(TYPE *) -> not found \n " ;
# endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
template < class TYPE >
void XObjArrayNC < TYPE > : : RemoveAllBut ( const TYPE * Element )
{
xsize i ;
for ( i = _Len ; i - - ; ) {
if ( ( ( TYPE * ) _Data [ i ] . Object ) ! = Element ) {
Remove ( i ) ;
}
}
}
/* Empty() */
template < class TYPE >
void XObjArrayNC < TYPE > : : Empty ( )
{
xsize i ;
if ( _Len > 0 ) {
for ( i = 0 ; i < _Len ; i + = 1 ) {
if ( _Data [ i ] . FreeIt )
{
TYPE * TmpObject ; // BCB 4 oblige me to use a tmp var for doing the delete.
TmpObject = ( TYPE * ) ( _Data [ i ] . Object ) ;
delete TmpObject ;
}
}
_Len = 0 ;
}
}
# endif