17 #if !defined(ON_ARRAY_DEFS_INC_)
18 #define ON_ARRAY_DEFS_INC_
20 #if defined(ON_COMPILER_MSC)
32 #pragma warning(disable:4100)
39 #pragma warning(disable:4211)
61 return (T*)onrealloc(ptr,capacity*
sizeof(T));
110 memcpy( m_a, src.
m_a, m_count*
sizeof(T) );
138 return ((
unsigned int)m_count);
150 return ((
unsigned int)(m_capacity*
sizeof(T)));
156 return ((
unsigned int)(
sizeof(T)));
163 return ON_CRC32(current_remainder,m_count*
sizeof(m_a[0]),m_a);
169 #if defined(ON_DEBUG)
170 if ( i < 0 || i > m_capacity )
172 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
181 #if defined(ON_DEBUG)
182 if ( i > (
unsigned int)m_capacity )
184 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
194 #if defined(ON_DEBUG)
195 if ( i < 0 || i > (ON__INT64)m_capacity )
197 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
206 #if defined(ON_DEBUG)
207 if ( i > (ON__UINT64)m_capacity )
209 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
218 #if defined(ON_DEBUG)
219 if ( i < 0 || i > m_capacity )
221 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
230 #if defined(ON_DEBUG)
231 if ( i > (
unsigned int)m_capacity )
233 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
243 #if defined(ON_DEBUG)
244 if ( i < 0 || i > ((ON__INT64)m_capacity) )
246 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
255 #if defined(ON_DEBUG)
256 if ( i > (ON__UINT64)m_capacity )
258 ON_ERROR(
"ON_SimpleArray[i]: i out of range.");
268 return (m_count > 0) ? m_a : 0;
274 return (m_count > 0) ? m_a : 0;
302 if ( m_a && m_a != p )
310 if ( m_a && m_a != p )
314 m_capacity = capacity;
320 return (m_count > 0) ? m_a : 0;
326 return (m_count > 0) ? m_a : 0;
332 return (i >= 0 && i < m_count) ? m_a+i : 0;
338 return (i < (
unsigned int)m_count) ? m_a+i : 0;
344 return (i >= 0 && i < m_count) ? m_a+i : 0;
350 return (i < (
unsigned int)m_count) ? m_a+i : 0;
356 return (i >= 0 && i < (ON__INT64)m_count) ? m_a+i : 0;
362 return (i < (ON__UINT64)m_count) ? m_a+i : 0;
368 return (i >= 0 && i < (ON__INT64)m_count) ? m_a+i : 0;
374 return (i < (ON__UINT64)m_count) ? m_a+i : 0;
380 return (m_count > 0) ? m_a+(m_count-1) : 0;
386 return (m_count > 0) ? m_a+(m_count-1) : 0;
396 if ( ele_cnt <= 0 || src_i < 0 || dest_i < 0 || src_i == dest_i ||
397 src_i + ele_cnt > m_count || dest_i > m_count )
400 int capacity = dest_i + ele_cnt;
401 if ( capacity > m_capacity ) {
402 if ( capacity < 2*m_capacity )
403 capacity = 2*m_capacity;
404 SetCapacity( capacity );
407 memmove( &m_a[dest_i], &m_a[src_i], ele_cnt*
sizeof(T) );
413 if ( m_count == m_capacity )
415 int new_capacity = NewCapacity();
416 Reserve( new_capacity );
418 memset( &m_a[m_count], 0,
sizeof(T) );
419 return m_a[m_count++];
425 if ( m_count == m_capacity )
427 const int newcapacity = NewCapacity();
430 const int s = (int)(&x - m_a);
431 if ( s >= 0 && s < m_capacity )
439 Reserve( newcapacity );
440 m_a[m_count++] = temp;
444 Reserve(newcapacity);
452 if ( count > 0 && p )
454 if ( count + m_count > m_capacity )
456 int newcapacity = NewCapacity();
457 if ( newcapacity < count + m_count )
458 newcapacity = count + m_count;
459 Reserve( newcapacity );
461 memcpy( m_a + m_count, p, count*
sizeof(T) );
469 if( i >= 0 && i <= m_count )
471 if ( m_count == m_capacity )
473 int newcapacity = NewCapacity();
474 Reserve( newcapacity );
477 Move( i+1, i,
static_cast<unsigned int>(m_count)-1-i );
491 if ( i >= 0 && i < m_count ) {
492 Move( i, i+1, m_count-1-i );
494 memset( &m_a[m_count], 0,
sizeof(T) );
502 memset( m_a, 0, m_capacity*
sizeof(T) );
515 for ( ; i < j; i++, j-- ) {
536 for (
int i = 0; i < m_count; i++ ) {
537 if (!memcmp(p,m_a+i,
sizeof(T)))
546 for (
int i = 0; i < m_count; i++ ) {
547 if (!compar(key,m_a+i))
556 const T* found = (key&&m_a&&m_count>0)
557 ? (
const T*)bsearch( key, m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar )
567 #if defined(ON_COMPILER_MSC1300)
568 rc = ((int)(found - m_a));
569 #elif 8 == ON_SIZEOF_POINTER
573 const ON__UINT64 fptr = (ON__UINT64)found;
574 const ON__UINT64 aptr = (ON__UINT64)m_a;
575 const ON__UINT64 sz = (ON__UINT64)
sizeof(T);
576 const ON__UINT64 i = (fptr - aptr)/sz;
582 const ON__UINT32 fptr = (ON__UINT32)found;
583 const ON__UINT32 aptr = (ON__UINT32)m_a;
584 const ON__UINT32 sz = (ON__UINT32)
sizeof(T);
585 const ON__UINT32 i = (fptr - aptr)/sz;
602 if ( count > m_count )
606 const T* found = (key&&m_a&&m_count>0)
607 ? (
const T*)bsearch( key, m_a, count,
sizeof(T), (
int(*)(
const void*,
const void*))compar )
617 #if defined(ON_COMPILER_MSC1300)
618 rc = ((int)(found - m_a));
619 #elif 8 == ON_SIZEOF_POINTER
623 const ON__UINT64 fptr = (ON__UINT64)found;
624 const ON__UINT64 aptr = (ON__UINT64)m_a;
625 const ON__UINT64 sz = (ON__UINT64)
sizeof(T);
626 const ON__UINT64 i = (fptr - aptr)/sz;
632 const ON__UINT32 fptr = (ON__UINT32)found;
633 const ON__UINT32 aptr = (ON__UINT32)m_a;
634 const ON__UINT32 sz = (ON__UINT32)
sizeof(T);
635 const ON__UINT32 i = (fptr - aptr)/sz;
653 if ( m_a && m_count > 0 && compar ) {
655 ON_hsort( m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
665 if ( m_a && m_count > 0 && compar ) {
667 ON_qsort( m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
677 if ( m_a && m_count > 0 && compar && index ) {
679 ON_Sort(sa, index, m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
680 else if ( m_count == 1 )
691 if ( m_a && m_count > 0 && compar && index ) {
693 ON_Sort(sa, index, m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*,
void*))compar, p );
694 else if ( m_count == 1 )
705 if ( m_a && m_count > 0 && index ) {
707 T* buffer = (T*)onmalloc(m_count*
sizeof(buffer[0]));
708 memcpy( buffer, m_a, m_count*
sizeof(T) );
709 for (i = 0; i < m_count; i++ )
710 memcpy( m_a+i, buffer+index[i],
sizeof(T) );
720 if ( m_a && m_capacity > 0 ) {
721 memset( m_a, 0, m_capacity*
sizeof(T) );
728 if ( m_a && m_capacity > 0 ) {
729 memset( m_a, value, m_capacity*
sizeof(T) );
738 if( m_capacity < newcap )
739 SetCapacity( newcap );
745 SetCapacity( m_count );
759 if ( count >= 0 && count <= m_capacity )
767 if ( capacity != m_capacity ) {
769 if ( m_count > capacity )
772 m_a = Realloc( m_a, capacity );
774 if ( capacity > m_capacity ) {
776 memset( m_a + m_capacity, 0, (capacity-m_capacity)*
sizeof(T) );
778 m_capacity = capacity;
782 m_count = m_capacity = 0;
788 m_count = m_capacity = 0;
813 const std::size_t cap_size = 32*
sizeof(
void*)*1024*1024;
814 if (m_count*
sizeof(T) <= cap_size || m_count < 8)
815 return ((m_count <= 2) ? 4 : 2*m_count);
819 int delta_count = 8 + cap_size/
sizeof(T);
820 if ( delta_count > m_count )
821 delta_count = m_count;
822 return (m_count + delta_count);
845 constexpr std::size_t cap_size = 32*
sizeof(
void*)*1024*1024;
846 if (m_count*
sizeof(T) <= cap_size || m_count < 8)
847 return ((m_count <= 2) ? 4 : 2*m_count);
851 int delta_count = 8 + cap_size/
sizeof(T);
852 if ( delta_count > m_count )
853 delta_count = m_count;
854 return (m_count + delta_count);
896 T* reptr = (T*)onrealloc(ptr,capacity*
sizeof(T));
897 if ( ptr && reptr && reptr != ptr )
902 for ( i = 0; i < this->m_count; i++ )
904 reptr[i].MemoryRelocate();
920 return (T*)onrealloc(ptr,capacity*
sizeof(T));
929 for ( i = 0; i < this->m_count; i++ )
931 current_remainder = this->m_a[i].DataCRC(current_remainder);
933 return current_remainder;
978 if ( m_capacity < src.
m_count ) {
983 for ( i = 0; i < m_count; i++ ) {
1013 return ((
unsigned int)m_count);
1025 return ((
unsigned int)(m_capacity*
sizeof(T)));
1031 return ((
unsigned int)(
sizeof(T)));
1037 #if defined(ON_DEBUG)
1038 if ( i < 0 || i > m_capacity )
1040 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1050 #if defined(ON_DEBUG)
1051 if ( i < 0 || i > (ON__INT64)m_capacity )
1053 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1062 #if defined(ON_DEBUG)
1063 if ( i > (
unsigned int)m_capacity )
1065 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1074 #if defined(ON_DEBUG)
1075 if ( i > (ON__UINT64)m_capacity )
1077 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1086 #if defined(ON_DEBUG)
1087 if ( i < 0 || i > m_capacity )
1089 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1098 #if defined(ON_DEBUG)
1099 if ( i < 0 || i > (ON__INT64)m_capacity )
1101 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1110 #if defined(ON_DEBUG)
1111 if ( i > (
unsigned int)m_capacity )
1113 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1122 #if defined(ON_DEBUG)
1123 if ( i > (ON__UINT64)m_capacity )
1125 ON_ERROR(
"ON_ClassArray[i]: i out of range.");
1134 return (m_count > 0) ? m_a : 0;
1140 return (m_count > 0) ? m_a : 0;
1168 if ( m_a && m_a != p )
1176 if ( m_a && m_a != p )
1180 m_capacity = capacity;
1186 return (m_count > 0) ? m_a : 0;
1192 return (m_count > 0) ? m_a : 0;
1198 return (i >= 0 && i < m_count) ? m_a+i : 0;
1204 return (i < (
unsigned int)m_count) ? m_a+i : 0;
1210 return (i >= 0 && i < m_count) ? m_a+i : 0;
1216 return (i < (
unsigned int)m_count) ? m_a+i : 0;
1223 return (i >= 0 && i < (ON__INT64)m_count) ? m_a+i : 0;
1229 return (i < (ON__UINT64)m_count) ? m_a+i : 0;
1235 return (i >= 0 && i < (ON__INT64)m_count) ? m_a+i : 0;
1241 return (i < (ON__UINT64)m_count) ? m_a+i : 0;
1248 return (m_count > 0) ? m_a+(m_count-1) : 0;
1254 return (m_count > 0) ? m_a+(m_count-1) : 0;
1265 if ( ele_cnt <= 0 || src_i < 0 || dest_i < 0 || src_i == dest_i ||
1266 src_i + ele_cnt > m_count || dest_i > m_count )
1269 int capacity = dest_i + ele_cnt;
1270 if ( capacity > m_capacity ) {
1271 if ( capacity < 2*m_capacity )
1272 capacity = 2*m_capacity;
1273 SetCapacity( capacity );
1280 memmove( (
void*)(&m_a[dest_i]), (
const void*)(&m_a[src_i]), ele_cnt*
sizeof(T) );
1300 if ( m_count == m_capacity )
1302 int newcapacity = NewCapacity();
1303 Reserve( newcapacity );
1308 DestroyElement(m_a[m_count]);
1310 ConstructDefaultElement(&m_a[m_count]);
1312 return m_a[m_count++];
1318 if ( m_count == m_capacity )
1320 const int newcapacity = NewCapacity();
1323 const int s = (int)(&x - m_a);
1324 if ( s >= 0 && s < m_capacity )
1332 Reserve( newcapacity );
1333 m_a[m_count++] = temp;
1337 Reserve(newcapacity);
1346 if ( count > 0 && p )
1348 if ( count + m_count > m_capacity )
1350 int newcapacity = NewCapacity();
1351 if ( newcapacity < count + m_count )
1352 newcapacity = count + m_count;
1353 Reserve( newcapacity );
1355 for ( i = 0; i < count; i++ ) {
1356 m_a[m_count++] = p[i];
1365 if( i >= 0 && i <= m_count )
1367 if ( m_count == m_capacity )
1369 int newcapacity = NewCapacity();
1370 Reserve( newcapacity );
1372 DestroyElement( m_a[m_count] );
1374 if ( i < m_count-1 ) {
1375 Move( i+1, i,
static_cast<unsigned int>(m_count)-1-i );
1378 memset( (
void*)(&m_a[i]), 0,
sizeof(T) );
1379 ConstructDefaultElement( &m_a[i] );
1382 ConstructDefaultElement( &m_a[m_count-1] );
1397 if ( i >= 0 && i < m_count )
1399 DestroyElement( m_a[i] );
1402 memset( (
void*)(&m_a[i]), 0,
sizeof(T) );
1403 Move( i, i+1, m_count-1-i );
1406 memset( (
void*)(&m_a[m_count-1]), 0,
sizeof(T) );
1407 ConstructDefaultElement(&m_a[m_count-1]);
1416 for ( i = m_count-1; i >= 0; i-- ) {
1417 DestroyElement( m_a[i] );
1420 memset( (
void*)(&m_a[i]), 0,
sizeof(T) );
1421 ConstructDefaultElement( &m_a[i] );
1435 for ( ; i < j; i++, j-- ) {
1436 memcpy( t, &m_a[i],
sizeof(T) );
1437 memcpy( &m_a[i], &m_a[j],
sizeof(T) );
1438 memcpy( &m_a[j], t,
sizeof(T) );
1445 if ( i != j && i >= 0 && j >= 0 && i < m_count && j < m_count ) {
1447 memcpy( t, &m_a[i],
sizeof(T) );
1448 memcpy( &m_a[i], &m_a[j],
sizeof(T) );
1449 memcpy( &m_a[j], t,
sizeof(T) );
1456 for (
int i = 0; i < m_count; i++ )
1458 if (!compar(key,m_a+i))
1467 const T* found = (key&&m_a&&m_count>0) ? (
const T*)bsearch( key, m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar ) : 0;
1468 #if defined(ON_COMPILER_MSC1300)
1470 return found ? ((int)(found - m_a)) : -1;
1473 return found ? ((int)((((ON__UINT64)found) - ((ON__UINT64)m_a))/
sizeof(T))) : -1;
1480 if ( count > m_count )
1484 const T* found = (key&&m_a&&m_count>0) ? (
const T*)bsearch( key, m_a, count,
sizeof(T), (
int(*)(
const void*,
const void*))compar ) : 0;
1485 #if defined(ON_COMPILER_MSC1300)
1487 return found ? ((int)(found - m_a)) : -1;
1490 return found ? ((int)((((ON__UINT64)found) - ((ON__UINT64)m_a))/
sizeof(T))) : -1;
1498 if ( m_a && m_count > 0 && compar )
1501 ON_hsort( m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
1511 if ( m_a && m_count > 0 && compar )
1514 ON_qsort( m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
1528 if ( this->m_a && this->m_count > 0 && compar )
1530 if ( this->m_count > 1 )
1532 ON_hsort( this->m_a, this->m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
1537 for ( i = 0; i < this->m_count; i++ )
1539 this->m_a[i].MemoryRelocate();
1553 if ( this->m_a && this->m_count > 0 && compar )
1555 if ( this->m_count > 1 )
1557 ON_qsort( this->m_a, this->m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
1562 for ( i = 0; i < this->m_count; i++ )
1564 this->m_a[i].MemoryRelocate();
1577 if ( m_a && m_count > 0 && compar && index )
1580 ON_Sort(sa, index, m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*))compar );
1581 else if ( m_count == 1 )
1592 if ( m_a && m_count > 0 && compar && index )
1595 ON_Sort(sa, index, m_a, m_count,
sizeof(T), (
int(*)(
const void*,
const void*,
void*))compar, p );
1596 else if ( m_count == 1 )
1607 if ( m_a && m_count > 0 && index )
1610 T* buffer = (T*)onmalloc(m_count*
sizeof(buffer[0]));
1611 memcpy( buffer, m_a, m_count*
sizeof(T) );
1612 for (i = 0; i < m_count; i++ )
1613 memcpy( m_a+i, buffer+index[i],
sizeof(T) );
1624 if ( m_a && m_capacity > 0 ) {
1625 for ( i = m_capacity-1; i >= 0; i-- ) {
1626 DestroyElement(m_a[i]);
1629 memset( (
void*)(&m_a[i]), 0,
sizeof(T) );
1630 ConstructDefaultElement(&m_a[i]);
1640 if( m_capacity < newcap )
1641 SetCapacity( newcap );
1647 SetCapacity( m_count );
1661 if ( count >= 0 && count <= m_capacity )
1670 if ( capacity < 1 ) {
1672 for ( i = m_capacity-1; i >= 0; i-- ) {
1673 DestroyElement(m_a[i]);
1681 else if ( m_capacity < capacity ) {
1683 m_a = Realloc( m_a, capacity );
1691 memset( (
void*)(m_a + m_capacity), 0, (capacity-m_capacity)*
sizeof(T) );
1692 for ( i = m_capacity; i < capacity; i++ ) {
1693 ConstructDefaultElement(&m_a[i]);
1695 m_capacity = capacity;
1704 else if ( m_capacity > capacity ) {
1706 for ( i = m_capacity-1; i >= capacity; i-- ) {
1707 DestroyElement(m_a[i]);
1709 if ( m_count > capacity )
1711 m_capacity = capacity;
1712 m_a = Realloc( m_a, capacity );
1728 int ON_CompareIncreasing(
const T* a,
const T* b)
1739 int ON_CompareDecreasing(
const T* a,
const T* b)
1748 #if defined(ON_COMPILER_MSC)
1749 #pragma warning(pop)
unsigned int SizeOfArray() const
bool Permute(const int *)
virtual T * Realloc(T *, int)
unsigned int UnsignedCount() const
int BinarySearch(const T *, int(*)(const T *, const T *)) const
int Search(const T *, int(*)(const T *, const T *)) const
void ConstructDefaultElement(T *)
virtual bool QuickSort(int(*)(const T *, const T *))
ON_ClassArray< T > & operator=(const ON_ClassArray< T > &)
bool Sort(ON::sort_algorithm sort_algorithm, int *, int(*)(const T *, const T *)) const
unsigned int SizeOfElement() const
virtual bool HeapSort(int(*)(const T *, const T *))
void EmergencyDestroy(void)
void Insert(int, const T &)
bool HeapSort(int(*)(const T *, const T *))
bool QuickSort(int(*)(const T *, const T *))
ON__UINT32 DataCRC(ON__UINT32 current_remainder) const
ON_ObjectArray< T > & operator=(const ON_ObjectArray< T > &)
bool Sort(ON::sort_algorithm sort_algorithm, int *, int(*)(const T *, const T *)) const
void Insert(int, const T &)
unsigned int UnsignedCount() const
unsigned int SizeOfArray() const
bool HeapSort(int(*)(const T *, const T *))
void EmergencyDestroy(void)
bool QuickSort(int(*)(const T *, const T *))
void MemSet(unsigned char)
ON__UINT32 DataCRC(ON__UINT32 current_remainder) const
int Search(const T &) const
virtual T * Realloc(T *, int)
bool Permute(const int *)
virtual ON_SimpleArray< T > & operator=(const ON_SimpleArray< T > &)
int BinarySearch(const T *, int(*)(const T *, const T *)) const
unsigned int SizeOfElement() const
virtual ~ON_SimpleArray()