opennurbs_array.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(ON_ARRAY_INC_)
18 #define ON_ARRAY_INC_
19 
20 
21 
22 ////////////////////////////////////////////////////////////////
23 //
24 // The ON_SimpleArray<> template is more efficient than the
25 // ON_ClassArray<> template, but ON_SimpleArray<> should not
26 // be used for arrays of classes that require explicit
27 // construction, destruction, or copy operators.
28 //
29 // Elements returned by AppendNew() are memset to zero.
30 //
31 // By default, ON_SimpleArray<> uses onrealloc() to manage
32 // the dynamic array memory. If you want to use something
33 // besides onrealloc() to manage the array memory, then override
34 // ON_SimpleArray::Realloc().
35 
36 template <class T> class ON_SimpleArray
37 {
38 public:
39  // construction ////////////////////////////////////////////////////////
40 
41  // These constructors create an array that uses onrealloc() to manage
42  // the array memory.
43  ON_SimpleArray() ON_NOEXCEPT;
44 
45  virtual
47 
48  // Copy constructor
50 
51  ////// Assignment operator
52  ////// Making a virtual operator= was a mistake.
53  ////// One reason might have been that the operator is virtual
54  ////// so ON_UuidList::operator= will be called when one is
55  ////// passed as an ON_SimpleArray<ON_UUID>& to a function?
56  ////virtual
58 
59 #if defined(ON_HAS_RVALUEREF)
60  // Clone constructor
61  ON_SimpleArray( ON_SimpleArray<T>&& ) ON_NOEXCEPT;
62 
63  // Clone assignment
65 #endif
66 
67  ON_SimpleArray(size_t); // size_t parameter = initial capacity
68 
69  // emergency bailout ///////////////////////////////////////////////////
70  void EmergencyDestroy(void); // call only when memory used by this array
71  // may have become invalid for reasons beyond
72  // your control. EmergencyDestroy() zeros
73  // anything that could possibly cause
74  // ~ON_SimpleArray() to crash.
75 
76  // query ///////////////////////////////////////////////////////////////
77 
78  int Count() const; // number of elements in array
79  unsigned int UnsignedCount() const;
80 
81  int Capacity() const; // capacity of array
82 
83  unsigned int SizeOfArray() const; // amount of memory in the m_a[] array
84 
85  unsigned int SizeOfElement() const; // amount of memory in an m_a[] array element
86 
87  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
88 
89  // The operator[] does to not check for valid indices.
90  // The caller is responsibile for insuring that 0 <= i < Capacity()
91  T& operator[]( int );
92  T& operator[]( unsigned int );
93  T& operator[]( ON__INT64 );
94  T& operator[]( ON__UINT64 );
95 #if defined(ON_RUNTIME_APPLE)
96  T& operator[]( size_t );
97 #endif
98 
99  const T& operator[]( int ) const;
100  const T& operator[]( unsigned int ) const;
101  const T& operator[]( ON__INT64 ) const;
102  const T& operator[]( ON__UINT64 ) const;
103 #if defined(ON_RUNTIME_APPLE)
104  const T& operator[]( size_t ) const;
105 #endif
106 
107  operator T*(); // The cast operators return a pointer
108  operator const T*() const; // to the array. If Count() is zero,
109  // this pointer is nullptr.
110 
111  T* First();
112  const T* First() const; // returns nullptr if count = 0
113 
114  // At(index) returns nullptr if index < 0 or index >= count
115  T* At( int );
116  T* At( unsigned int );
117  T* At( ON__INT64 );
118  T* At( ON__UINT64 );
119  const T* At( int ) const;
120  const T* At( unsigned int ) const;
121  const T* At( ON__INT64 ) const;
122  const T* At( ON__UINT64 ) const;
123 
124  T* Last();
125  const T* Last() const; // returns nullptr if count = 0
126 
127 
128  // array operations ////////////////////////////////////////////////////
129 
130  T& AppendNew(); // Most efficient way to add a new element
131  // to the array. Increases count by 1.
132  // Returned element is memset to zero.
133 
134  void Append( const T& ); // Append copy of element.
135  // Increments count by 1.
136 
137  void Append( int, const T* ); // Append copy of an array T[count]
138 
139 
140  void Insert( int, const T& ); // Insert copy of element. Uses
141  // memmove() to perform any
142  // necessary moving.
143  // Increases count by 1.
144 
145  void Remove(); // Removes last element. Decrements
146  // count by 1. Does not change capacity.
147 
148  virtual
149  void Remove( int ); // Removes element. Uses memmove() to
150  // perform any necessary shifting.
151  // Decrements count by 1. Does not change
152  // capacity
153 
154  void Empty(); // Sets count to 0, leaves capacity untouched.
155 
156  void Reverse(); // reverse order
157 
158  void Swap(int,int); // swap elements i and j
159 
160  //////////
161  // Search( e ) does a SLOW search of the array starting at array[0]
162  // and returns the index "i" of the first element that satisfies
163  // e == array[i]. (== is really memcmp()). If the search is not
164  // successful, then Search() returns -1. For Search(T) to work
165  // correctly, T must be a simple type. Use Search(p,compare())
166  // for Ts that are structs/classes that contain pointers. Search()
167  // is only suitable for performing infrequent searchs of small
168  // arrays. Sort the array and use BinarySearch() for performing
169  // efficient searches.
170  int Search( const T& ) const;
171 
172  //////////
173  // Search( p, compare ) does a SLOW search of the array starting
174  // at array[0] and returns the index "i" of the first element
175  // that satisfies compare(p,&array[i])==0. If the search is not
176  // successful, then Search() returns -1. Search() is only suitable
177  // for performing infrequent searches of small arrays. Sort the
178  // array and use BinarySearch() for performing efficient searches.
179  // See Also: ON_CompareIncreasing<T> and ON_CompareDeccreasing<T>
180  int Search( const T*, int (*)(const T*,const T*) ) const;
181 
182  //////////
183  // BinarySearch( p, compare ) does a fast search of a sorted array
184  // and returns the smallest index "i" of the element that satisifies
185  // 0==compare(p,&array[i]).
186  //
187  // BinarySearch( p, compare, count ) does a fast search of the first
188  // count element sorted array and returns the smallest index "i" of
189  // the element that satisifies 0==compare(p,&array[i]). The version
190  // that takes a "count" is useful when elements are being appended
191  // during a calculation and the appended elements are not sorted.
192  //
193  // If the search is successful,
194  // BinarySearch() returns the index of the element (>=0).
195  // If the search is not successful, BinarySearch() returns -1.
196  // Use QuickSort( compare ) or, in rare cases and after meaningful
197  // performance testing using optimzed release builds,
198  // HeapSort( compare ) to sort the array.
199  // See Also: ON_CompareIncreasing<T> and ON_CompareDeccreasing<T>
200  int BinarySearch( const T*, int (*)(const T*,const T*) ) const;
201  int BinarySearch( const T*, int (*)(const T*,const T*), int ) const;
202 
203  //////////
204  // Sorts the array using the heap sort algorithm.
205  // QuickSort() is generally the better choice.
206  bool HeapSort( int (*)(const T*,const T*) );
207 
208  //////////
209  // Sorts the array using the quick sort algorithm.
210  // See Also: ON_CompareIncreasing<T> and ON_CompareDeccreasing<T>
211  bool QuickSort( int (*)(const T*,const T*) );
212 
213  /*
214  Description:
215  Sort() fills in the index[] array so that
216  array[index[i]] <= array[index[i+1]].
217  The array is not modified.
218 
219  Parameters:
220  sort_algorithm - [in]
221  ON::sort_algorithm::quick_sort (best in general) or ON::sort_algorithm::heap_sort
222  Use ON::sort_algorithm::heap_sort only if you have done extensive testing with
223  optimized release builds and are confident heap sort is
224  significantly faster.
225  index - [out] an array of length Count() that is returned with
226  some permutation of (0,1,...,Count()-1).
227  compare - [in] compare function compare(a,b,p) should return
228  <0 if a<b, 0, if a==b, and >0 if a>b.
229  Returns:
230  true if successful
231  */
232  bool Sort(
233  ON::sort_algorithm sort_algorithm,
234  int* /* index[] */ ,
235  int (*)(const T*,const T*)
236  ) const;
237 
238  /*
239  Description:
240  Sort() fills in the index[] array so that
241  array[index[i]] <= array[index[i+1]].
242  The array is not modified.
243 
244  Parameters:
245  sort_algorithm - [in]
246  ON::sort_algorithm::quick_sort (best in general) or ON::sort_algorithm::heap_sort
247  Use ON::sort_algorithm::heap_sort only if you have done extensive testing with
248  optimized release builds and are confident heap sort is
249  significantly faster.
250  index - [out] an array of length Count() that is returned with
251  some permutation of (0,1,...,Count()-1).
252  compare - [in] compare function compare(a,b,p) should return
253  <0 if a<b, 0, if a==b, and >0 if a>b.
254  p - [in] pointer passed as third argument to compare.
255 
256  Returns:
257  true if successful
258  */
259  bool Sort(
260  ON::sort_algorithm sort_algorithm,
261  int*, // index[]
262  int (*)(const T*,const T*,void*), // int compare(const T*,const T*,void* p)
263  void* // p
264  ) const;
265 
266  //////////
267  // Permutes the array so that output[i] = input[index[i]].
268  // The index[] array should be a permutation of (0,...,Count()-1).
269  bool Permute( const int* /*index[]*/ );
270 
271  //////////
272  // Zeros all array memory.
273  // Count and capacity are not changed.
274  void Zero();
275 
276  //////////
277  // Sets all bytes in array memory to value.
278  // Count and capacity are not changed.
279  void MemSet(unsigned char);
280 
281  // memory managment ////////////////////////////////////////////////////
282 
283  T* Reserve( size_t ); // increase capacity to at least the requested value
284 
285  void Shrink(); // remove unused capacity
286 
287  void Destroy(); // onfree any memory and set count and capacity to zero
288 
289  // low level memory managment //////////////////////////////////////////
290 
291  // By default, ON_SimpleArray<> uses onrealloc() to manage
292  // the dynamic array memory. If you want to use something
293  // besides onrealloc() to manage the array memory, then override
294  // Realloc(). The T* Realloc(ptr, capacity) should do the following:
295  //
296  // 1) If ptr and capacity are zero, return nullptr.
297  // 2) If ptr is nullptr, an capacity > 0, allocate a memory block of
298  // capacity*sizeof(T) bytes and return a pointer to this block.
299  // If the allocation request fails, return nullptr.
300  // 3) If ptr is not nullptr and capacity is 0, free the memory block
301  // pointed to by ptr and return nullptr.
302  // 4) If ptr is not nullptr and capacity > 0, then reallocate the memory
303  // block and return a pointer to the reallocated block. If the
304  // reallocation request fails, return nullptr.
305  //
306  // NOTE WELL:
307  // Microsoft's VC 6.0 realloc() contains a bug that can cause
308  // crashes and should be avoided. See MSDN Knowledge Base article
309  // ID Q225099 for more information.
310  virtual
311  T* Realloc(T*,int); // (re)allocated capacity*sizeof(T) bytes
312 
313  T* Array(); // The Array() function return the
314 
315  const T* Array() const; // m_a pointer value.
316 
317  void SetCount( int ); // If value is <= Capacity(), then
318  // sets count to specified value.
319 
320  T* SetCapacity( size_t ); // Shrink/grows capacity. If value
321  // is < current Count(), then count
322  // is reduced to value.
323  //
324 
325  int NewCapacity() const; // When the dynamic array needs to grow,
326  // this calculates the new value for m_capacity.
327 
328  /*
329  Description:
330  Expert user tool to take charge of the memory used by
331  the dyanmic array.
332  Returns:
333  A pointer to the array and zeros out this class.
334  The returned pointer is on the heap and must be
335  deallocated by calling onfree().
336  */
337  T* KeepArray();
338 
339  /*
340  Description:
341  Do not use this version of SetArray(). Use the one that takes
342  a pointer, count and capacity.
343  */
344  void SetArray(T*);
345 
346  /*
347  Description:
348  Expert user tool to set the memory used by the dyanmic array.
349  Parameters:
350  T* pointer - [in]
351  int count [in]
352  int capacity - [in]
353  m_a is set to pointer, m_count is set to count, and m_capacity
354  is set to capacity. It is critical that the pointer be one
355  returned by onmalloc(sz), where sz >= capacity*sizeof(T[0]).
356  */
357  void SetArray(T*, int, int);
358 
359 protected:
360  // implimentation //////////////////////////////////////////////////////
361  void Move( int /* dest index*/, int /* src index */, int /* element count*/ );
362  T* m_a; // pointer to array memory
363  int m_count; // 0 <= m_count <= m_capacity
364  int m_capacity; // actual length of m_a[]
365 };
366 
367 
368 ////////////////////////////////////////////////////////////////
369 //
370 
371 #if defined(ON_DLL_TEMPLATE)
372 
373 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<bool>;
374 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<char>;
375 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<unsigned char>;
376 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<short>;
377 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<unsigned short>;
378 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<int>;
379 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<unsigned int>;
380 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<float>;
381 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<double>;
382 
383 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<bool*>;
384 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<char*>;
385 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<unsigned char*>;
386 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<short*>;
387 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<unsigned short*>;
388 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<int*>;
389 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<unsigned int*>;
390 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<float*>;
391 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<double*>;
392 
393 #endif
394 
395 
396 
397 ////////////////////////////////////////////////////////////////
398 //
399 // The ON_ClassArray<> template is designed to be used with
400 // classes that require non-trivial construction or destruction.
401 // Any class used with the ON_ClassArray<> template must have a
402 // robust operator=().
403 //
404 // By default, ON_ClassArray<> uses onrealloc() to manage
405 // the dynamic array memory. If you want to use something
406 // besides onrealloc() to manage the array memory, then override
407 // ON_ClassArray::Realloc(). In practice this means that if your
408 // class has members with back-pointers, then you cannot use
409 // it in the defaule ON_ClassArray. See ON_ObjectArray
410 // for an example.
411 //
412 template <class T> class ON_ClassArray
413 {
414 public:
415  // construction ////////////////////////////////////////////////////////
416  ON_ClassArray() ON_NOEXCEPT;
417  ON_ClassArray( size_t ); // size_t parameter = initial capacity
418 
419  // Copy constructor
421 
422  virtual
423  ~ON_ClassArray(); // override for struct member deallocation, etc.
424 
425  // Assignment operator
427 
428 #if defined(ON_HAS_RVALUEREF)
429  // Clone constructor
430  ON_ClassArray( ON_ClassArray<T>&& ) ON_NOEXCEPT;
431 
432  // Clone Assignment operator
433  ON_ClassArray<T>& operator=( ON_ClassArray<T>&& ) ON_NOEXCEPT;
434 #endif
435 
436  // emergency bailout ///////////////////////////////////////////////////
437  void EmergencyDestroy(void); // call only when memory used by this array
438  // may have become invalid for reasons beyond
439  // your control. EmergencyDestroy() zeros
440  // anything that could possibly cause
441  // ~ON_ClassArray() to crash.
442 
443  // query ///////////////////////////////////////////////////////////////
444 
445  int Count() const; // number of elements in array
446  unsigned int UnsignedCount() const;
447 
448  int Capacity() const; // capacity of array
449 
450  unsigned int SizeOfArray() const; // amount of memory in the m_a[] array
451 
452  unsigned int SizeOfElement() const; // amount of memory in an m_a[] array element
453 
454  // The operator[] does to not check for valid indices.
455  // The caller is responsibile for insuring that 0 <= i < Capacity()
456  T& operator[]( int );
457  T& operator[]( unsigned int );
458  T& operator[]( ON__INT64 );
459  T& operator[]( ON__UINT64 );
460 #if defined(ON_RUNTIME_APPLE)
461  T& operator[]( size_t );
462 #endif
463 
464  const T& operator[]( int ) const;
465  const T& operator[]( unsigned int ) const;
466  const T& operator[]( ON__INT64 ) const;
467  const T& operator[]( ON__UINT64 ) const;
468 #if defined(ON_RUNTIME_APPLE)
469  const T& operator[]( size_t ) const;
470 #endif
471 
472  operator T*(); // The cast operators return a pointer
473  operator const T*() const; // to the array. If Count() is zero,
474  // this pointer is nullptr.
475  T* First();
476  const T* First() const; // returns nullptr if count = 0
477 
478  // At(index) returns nullptr if index < 0 or index >= count
479  T* At( int );
480  T* At( unsigned int );
481  T* At( ON__INT64 );
482  T* At( ON__UINT64 );
483  const T* At( int ) const;
484  const T* At( unsigned int ) const;
485  const T* At( ON__INT64 ) const;
486  const T* At( ON__UINT64 ) const;
487 
488  T* Last();
489  const T* Last() const; // returns nullptr if count = 0
490 
491 
492  // array operations ////////////////////////////////////////////////////
493 
494  T& AppendNew(); // Most efficient way to add a new class
495  // to the array. Increases count by 1.
496 
497  void Append( const T& ); // Append copy of element.
498  // Increments count by 1.
499 
500  void Append( int, const T*); // Append copy of an array T[count]
501 
502  void Insert( int, const T& ); // Insert copy of element. Uses
503  // memmove() to perform any
504  // necessary moving.
505  // Increases count by 1.
506 
507  void Remove(); // Removes last element. Decrements
508  // count by 1. Does not change capacity.
509 
510  void Remove( int ); // Removes element. Uses memmove() to
511  // perform any necessary shifting.
512  // Decrements count by 1. Does not change
513  // capacity
514 
515  void Empty(); // Sets count to 0, leaves capacity untouched.
516 
517  void Reverse(); // reverse order
518 
519  void Swap(int,int); // swap elements i and j
520 
521  //////////
522  // Search( p, compare ) does a SLOW search of the array starting
523  // at array[0] and returns the index "i" of the first element
524  // that satisfies compare(p,&array[i])==0. If the search is not
525  // successful, then Search() returns -1. Search() is only suitable
526  // for performing infrequent searches of small arrays. Sort the
527  // array and use BinarySearch() for performing efficient searches.
528  int Search( const T*, int (*)(const T*,const T*) ) const;
529 
530  //////////
531  // BinarySearch( p, compare ) does a fast search of a sorted array
532  // and returns the smallest index "i" of the element that satisifies
533  // 0==compare(p,&array[i]).
534  //
535  // BinarySearch( p, compare, count ) does a fast search of the first
536  // count element sorted array and returns the smallest index "i" of
537  // the element that satisifies 0==compare(p,&array[i]). The version
538  // that takes a "count" is useful when elements are being appended
539  // during a calculation and the appended elements are not sorted.
540  //
541  // If the search is successful,
542  // BinarySearch() returns the index of the element (>=0).
543  // If the search is not successful, BinarySearch() returns -1.
544  // Use QuickSort( compare ) or, in rare cases and after meaningful
545  // performance testing using optimzed release builds,
546  // HeapSort( compare ) to sort the array.
547  // See Also: ON_CompareIncreasing<T> and ON_CompareDeccreasing<T>
548  int BinarySearch( const T*, int (*)(const T*,const T*) ) const;
549  int BinarySearch( const T*, int (*)(const T*,const T*), int ) const;
550 
551  //////////
552  // Sorts the array using the heap sort algorithm.
553  // See Also: ON_CompareIncreasing<T> and ON_CompareDeccreasing<T>
554  // QuickSort() is generally the better choice.
555  virtual
556  bool HeapSort( int (*)(const T*,const T*) );
557 
558  //////////
559  // Sorts the array using the heap sort algorithm.
560  virtual
561  bool QuickSort( int (*)(const T*,const T*) );
562 
563  /*
564  Description:
565  Sort() fills in the index[] array so that
566  array[index[i]] <= array[index[i+1]].
567  The array is not modified.
568 
569  Parameters:
570  sort_algorithm - [in]
571  ON::sort_algorithm::quick_sort (best in general) or ON::sort_algorithm::heap_sort
572  Use ON::sort_algorithm::heap_sort only if you have done extensive testing with
573  optimized release builds and are confident heap sort is
574  significantly faster.
575  index - [out] an array of length Count() that is returned with
576  some permutation of (0,1,...,Count()-1).
577  compare - [in] compare function compare(a,b) should return
578  <0 if a<b, 0, if a==b, and >0 if a>b.
579 
580  Returns:
581  true if successful
582  */
583  bool Sort(
584  ON::sort_algorithm sort_algorithm,
585  int* /* index[] */ ,
586  int (*)(const T*,const T*)
587  ) const;
588 
589  /*
590  Description:
591  Sort() fills in the index[] array so that
592  array[index[i]] <= array[index[i+1]].
593  The array is not modified.
594 
595  Parameters:
596  sort_algorithm - [in]
597  ON::sort_algorithm::quick_sort (best in general) or ON::sort_algorithm::heap_sort
598  Use ON::sort_algorithm::heap_sort only if you have done extensive testing with
599  optimized release builds and are confident heap sort is
600  significantly faster.
601  index - [out] an array of length Count() that is returned with
602  some permutation of (0,1,...,Count()-1).
603  compare - [in] compare function compare(a,b,p) should return
604  <0 if a<b, 0, if a==b, and >0 if a>b.
605  p - [in] pointer passed as third argument to compare.
606 
607  Returns:
608  true if successful
609  */
610  bool Sort(
611  ON::sort_algorithm sort_algorithm,
612  int*, // index[]
613  int (*)(const T*,const T*,void*), // int compare(const T*,const T*,void* p)
614  void* // p
615  ) const;
616 
617  //////////
618  // Permutes the array so that output[i] = input[index[i]].
619  // The index[] array should be a permutation of (0,...,Count()-1).
620  bool Permute( const int* /*index[]*/ );
621 
622  //////////
623  // Destroys all elements and fills them with values
624  // set by the defualt constructor.
625  // Count and capacity are not changed.
626  void Zero();
627 
628  // memory managment /////////////////////////////////////////////////
629 
630  T* Reserve( size_t ); // increase capacity to at least the requested value
631 
632  void Shrink(); // remove unused capacity
633 
634  void Destroy(); // onfree any memory and set count and capacity to zero
635 
636  // low level memory managment ///////////////////////////////////////
637 
638  // By default, ON_ClassArray<> uses onrealloc() to manage
639  // the dynamic array memory. If you want to use something
640  // besides onrealloc() to manage the array memory, then override
641  // Realloc(). The T* Realloc(ptr, capacity) should do the following:
642  //
643  // 1) If ptr and capacity are zero, return nullptr.
644  // 2) If ptr is nullptr, an capacity > 0, allocate a memory block of
645  // capacity*sizeof(T) bytes and return a pointer to this block.
646  // If the allocation request fails, return nullptr.
647  // 3) If ptr is not nullptr and capacity is 0, free the memory block
648  // pointed to by ptr and return nullptr.
649  // 4) If ptr is not nullptr and capacity > 0, then reallocate the memory
650  // block and return a pointer to the reallocated block. If the
651  // reallocation request fails, return nullptr.
652  //
653  // NOTE WELL:
654  // Microsoft's VC 6.0 realloc() contains a bug that can cause
655  // crashes and should be avoided. See MSDN Knowledge Base article
656  // ID Q225099 for more information.
657  virtual
658  T* Realloc(T*,int); // (re)allocated capacity*sizeof(T) bytes
659 
660  T* Array(); // The Array() function return the
661 
662  const T* Array() const; // m_a pointer value.
663 
664  void SetCount( int ); // If value is <= Capacity(), then
665  // sets count to specified value.
666 
667  T* SetCapacity( size_t ); // Shrink/grows capacity. If value
668  // is < current Count(), then count
669  // is reduced to value.
670 
671  int NewCapacity() const; // When the dynamic array needs to grow,
672  // this calculates the new value for m_capacity.
673 
674  T* KeepArray(); // returns pointer to array and zeros
675  // out this class. Caller is responsible
676  // for calling destructor on each element
677  // and then using onfree() to release array
678  // memory. E.g.,
679  //
680  // for (int i=capacity;i>=0;i--) {
681  // array[i].~T();
682  // }
683  // onfree(array);
684 
685  /*
686  Description:
687  Do not use this version of SetArray(). Use the one that takes
688  a pointer, count and capacity: SetArray(pointer,count,capacity)
689  */
690  void SetArray(T*);
691 
692  /*
693  Description:
694  Expert user tool to set the memory used by the dyanmic array.
695  Parameters:
696  T* pointer - [in]
697  int count - [in] 0 <= count <= capacity
698  int capacity - [in]
699  m_a is set to pointer, m_count is set to count, and m_capacity
700  is set to capacity. It is critical that the pointer be one
701  returned by onmalloc(sz), where sz >= capacity*sizeof(T[0]),
702  and that the in-place operator new has been used to initialize
703  each element of the array.
704  */
705  void SetArray(T*, int, int);
707 protected:
708  // implimentation //////////////////////////////////////////////////////
709  void Move( int /* dest index*/, int /* src index */, int /* element count*/ );
710  void ConstructDefaultElement(T*);
711  void DestroyElement(T&);
712  T* m_a; // pointer to array memory
713  int m_count; // 0 <= m_count <= m_capacity
714  int m_capacity; // actual length of m_a[]
715 };
716 
717 #if defined(ON_DLL_TEMPLATE)
718 
719 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_String>;
720 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_wString>;
721 
722 #endif
723 
724 /*
725 Description:
726  ON_Object array is used to store lists of classes that are
727  derived from ON_Object. It differs from ON_ClassArray in
728  that the virtual ON_Object::MemoryRelocate function is called
729  when growing the dynamic array requires changing the location
730  of the memory buffer used to store the elements in the array.
731 */
732 template <class T> class ON_ObjectArray : public ON_ClassArray<T>
733 {
734 public:
735  ON_ObjectArray();
736  ~ON_ObjectArray(); // override for struct member deallocation, etc.
737  ON_ObjectArray( size_t ); // size_t parameter = initial capacity
740 
741 #if defined(ON_HAS_RVALUEREF)
742  // Clone constructor
744 
745  // Clone Assignment operator
747 #endif
748 
749  ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
750 
751  // virtual ON_ClassArray<T> override that
752  // calls MemoryRelocate on each element after
753  // the reallocation.
754  T* Realloc(T*,int);
755 
756  // virtual ON_ClassArray<T> override that
757  // calls MemoryRelocate on each element after
758  // the heap sort.
759  // QuickSort() is generally the better choice.
760  bool HeapSort( int (*)(const T*,const T*) );
762  // virtual ON_ClassArray<T> override that
763  // calls MemoryRelocate on each element after
764  // the quick sort.
765  bool QuickSort( int (*)(const T*,const T*) );
766 };
767 
768 class ON_CLASS ON_UuidPair
769 {
770 public:
771  /*
772  Description:
773  Compares m_uuid[0] and ignores m_uuid[1]
774  */
775  static
776  int CompareFirstUuid(const class ON_UuidPair*,const class ON_UuidPair*);
777 
778  /*
779  Description:
780  Compares m_uuid[1] and ignores m_uuid[0]
781  */
782  static
783  int CompareSecondUuid(const class ON_UuidPair*,const class ON_UuidPair*);
784 
785  /*
786  Description:
787  Compares m_uuid[0] then m_uuid[1].
788  */
789  static
790  int Compare(const class ON_UuidPair*,const class ON_UuidPair*);
791 
792  ON_UuidPair();
793  ON_UUID m_uuid[2];
794 };
795 
796 #if defined(ON_DLL_TEMPLATE)
797 
798 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_UUID>;
799 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_UuidIndex>;
800 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_UuidPtr>;
801 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_UuidPair>;
802 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_SimpleArray<int> >;
804 #endif
805 
806 
807 /*
808 Description:
809  The ON_UuidList class provides a tool to efficiently
810  maintain a list of uuids and determine if a uuid is
811  in the list. This class is based on the premise that
812  there are no duplicate uuids in the list.
813 */
814 class ON_CLASS ON_UuidList : private ON_SimpleArray<ON_UUID>
815 {
816 public:
817  ON_UuidList();
818  ON_UuidList(int capacity);
819  ~ON_UuidList();
820  ON_UuidList(const ON_UuidList& src);
821  ON_UuidList& operator=(const ON_UuidList& src);
822 
823  /*
824  Description:
825  Fast uuid compare. Not necessarily the same
826  as ON_UuidCompare().
827  */
828  static
829  int CompareUuid( const ON_UUID* a, const ON_UUID* b );
830 
831  /*
832  Returns:
833  Number of active uuids in the list.
834  */
835  int Count() const;
836 
837  /*
838  Returns:
839  Array of uuids in the list. Sorted with
840  respect to ON_UuidList::CompareUuid().
841  Remarks:
842  Calling AddUuid() may grow the dynamic array
843  and make the pointer invalid.
844  */
845  const ON_UUID* Array() const;
846 
847  /*
848  Description:
849  Provides an efficient way to empty a list so that it
850  can be used again.
851  */
852  void Empty();
853 
854  /*
855  Description:
856  Destroy list. If list will be reused, Empty() is more
857  efficient.
858  */
859  void Destroy();
860 
861  void Reserve(size_t capacity);
862 
863  /*
864  Description:
865  Makes the uuid list as efficent as possible in both search
866  speed and memory usage. Use Compact() when a uuid list
867  will be in use but is not likely to be modifed. A list
868  that has been compacted can still be modified.
869  */
870  void Compact();
871 
872  /*
873  Description:
874  Adds a uuid to the list.
875  Parameters:
876  uuid - [in] id to add.
877  bCheckForDupicates - [in] if true, then the uuid
878  is not added if it is already in the list.
879  If you are certain that the uuid is not in the
880  list and you are going to have a large list of uuids,
881  then setting bCheckForDupicates=false will
882  speed up the addition of uuids.
883  Returns:
884  True if uuid was added. False if uuid was not added
885  because it is already in the collection.
886  */
887  bool AddUuid(ON_UUID uuid, bool bCheckForDupicates=true);
888 
889  /*
890  Description:
891  Removes a uuid from the list.
892  Parameters:
893  uuid - [in] id to remove
894  Returns:
895  True if uuid was in the list and was removed.
896  False if uuid was not in the list.
897  */
898  bool RemoveUuid(ON_UUID uuid);
899 
900  /*
901  Description:
902  Determine if a uuid is in the list.
903  Returns:
904  True if uuid is in the list.
905  */
906  bool FindUuid(ON_UUID uuid) const;
907 
908  /*
909  Description:
910  Saves the uuid list in an archive.
911  Parameters:
912  archive - [in] archive to write to.
913  Returns:
914  true if write was successful.
915  */
916  bool Write(
917  class ON_BinaryArchive& archive
918  ) const;
919 
920  /*
921  Description:
922  Saves the uuid list in an archive.
923  Parameters:
924  archive - [in] archive to write to.
925  bSortBeforeWrite - [in]
926  True if ids should be sorted before the write
927  so future lookups will be fast. False if
928  the current state of the sorted/unsorted bits
929  should be preserved.
930  Returns:
931  true if write was successful.
932  */
933  bool Write(
934  class ON_BinaryArchive& archive,
935  bool bSortBeforeWrite
936  ) const;
937 
938  /*
939  Description:
940  Read the uuid list from an archive.
941  Parameters:
942  archive - [in] archive to read from.
943  Returns:
944  true if the read was successful.
945  */
946  bool Read(
947  class ON_BinaryArchive& archive
948  );
949 
950  /*
951  Description:
952  Read the uuid list from an archive.
953  Parameters:
954  archive - [in]
955  archive to read from.
956  bool bSortAfterRead - [in]
957  True if ids should be sorted after the read
958  so future lookups will be fast. False if
959  the state of the sorted/unsorted bits that
960  existed at write time should be preserved.
961  Returns:
962  true if the read was successful.
963  */
964  bool Read(
965  class ON_BinaryArchive& archive,
966  bool bSortAferRead
967  );
968 
969  /*
970  Description:
971  Append the uuids in this class to uuid_list.
972  Parameters:
973  uuid_list - [in/out]
974  Returns:
975  Number of uuids added to uuid_list.
976  */
977  int GetUuids(
978  ON_SimpleArray<ON_UUID>& uuid_list
979  ) const;
980 
981  /*
982  Description:
983  This tool is used in rare situations when the object ids
984  stored in the uuid list need to be remapped.
985  Parameters:
986  uuid_remap - [in]
987  Is it critical that uuid_remap[] be sorted with respect
988  to ON_UuidPair::CompareFirstUuid.
989  */
990  void RemapUuids(
991  const ON_SimpleArray<ON_UuidPair>& uuid_remap
992  );
993 
994 private:
995  void PurgeHelper();
996  void SortHelper();
997  ON_UUID* SearchHelper(const ON_UUID*) const;
998  int m_sorted_count;
999  int m_removed_count;
1000 };
1001 
1002 /*
1003 Description:
1004  The ON_UuidList class provides a tool
1005  to efficiently maintain a list of uuid-index
1006  pairs and determine if a uuid is in the list.
1007  This class is based on the premise that there are
1008  no duplicate uuids in the list.
1009 */
1010 class ON_CLASS ON_UuidIndexList : private ON_SimpleArray<ON_UuidIndex>
1011 {
1012 public:
1013  ON_UuidIndexList() = default;
1014  ON_UuidIndexList(size_t capacity);
1015  ~ON_UuidIndexList() = default;
1016  ON_UuidIndexList(const ON_UuidIndexList& src);
1018 
1019  /*
1020  Returns:
1021  Number of active uuids in the list.
1022  */
1023  unsigned int Count() const;
1024 
1025  /*
1026  Description:
1027  Provides an efficient way to empty a list so that it
1028  can be used again.
1029  */
1030  void RemoveAll();
1031 
1032  void Reserve( size_t capacity );
1033 
1034  /*
1035  Description:
1036  Adds a uuid-index pair to the list.
1037  Parameters:
1038  uuid - [in] id to add.
1039  This uuid cannot be ON_max_uuid because ON_max_uuid
1040  is
1041  bCheckForDupicates - [in] if true, then the uuid
1042  is not added if it is already in the list.
1043  If you are certain that the uuid is not in the list
1044  and you have a have a large collection of uuids,
1045  then setting bCheckForDupicates=false will
1046  speed up the addition of uuids.
1047  Returns:
1048  True if uuid was added. False if uuid was not added
1049  because it is already in the collection.
1050  */
1051  bool AddUuidIndex(
1052  ON_UUID uuid,
1053  int index,
1054  bool bCheckForDupicates=true);
1055 
1056  /*
1057  Description:
1058  Removes an element with a matching uuid from the list.
1059  Parameters:
1060  uuid - [in] id to remove
1061  Returns:
1062  True if an element was removed. False if the uuid
1063  was not in the list.
1064  */
1065  bool RemoveUuid(
1066  ON_UUID uuid
1067  );
1068 
1069  /*
1070  Description:
1071  Determine if an element with a uuid is in the list.
1072  Parameters:
1073  index - [out] if not nullptr and a matching uuid is found,
1074  then *index is set to the value of the index.
1075  Returns:
1076  True if an element was found. Returns false if
1077  the uuid is not in the list.
1078  */
1079  bool FindUuid(ON_UUID uuid) const;
1080  bool FindUuid(ON_UUID uuid, int* index) const;
1081 
1082  /*
1083  Description:
1084  Determine if a uuid-index pair is in the list.
1085  Returns:
1086  True if the uuid-index pair is in the list.
1087  Returns false if the uuid-index pair is not
1088  in the list.
1089  */
1090  bool FindUuidIndex(ON_UUID uuid, int index) const;
1091 
1092  /*
1093  Description:
1094  Append the uuids in this class to uuid_list.
1095  Parameters:
1096  uuid_list - [in/out]
1097  Returns:
1098  Number of uuids added to uuid_list.
1099  */
1100  unsigned int GetUuids(
1101  ON_SimpleArray<ON_UUID>& uuid_list
1102  ) const;
1103 
1104  /*
1105  Description:
1106  If you will perform lots of searches before the next
1107  change to the list, then calling ImproveSearchSpeed()
1108  will speed up the searches by culling removed objects
1109  and completely sorting the list so only a binary search
1110  is required. You may edit the list at any time after
1111  calling ImproveSearchSpeed(). If you are performing
1112  a few searches between edits, then excessive calling
1113  of ImproveSearchSpeed() may actually decrease overall
1114  program performance.
1115  */
1116  void ImproveSearchSpeed();
1117 
1118 private:
1119  ON_UuidIndex* SearchHelper(const ON_UUID*) const;
1120  unsigned int m_sorted_count = 0;
1121  unsigned int m_removed_count = 0;
1122 };
1123 
1124 
1125 /*
1126 Description:
1127  The ON_UuidList class provides a tool
1128  to efficiently maintain a list of uuid-pointer
1129  pairs and determine if a uuid is in the list.
1130  This class is based on the premise that there are
1131  no duplicate uuids in the list.
1132 */
1133 class ON_CLASS ON_UuidPtrList : private ON_SimpleArray<ON_UuidPtr>
1134 {
1135 public:
1136  ON_UuidPtrList() = default;
1137  ON_UuidPtrList(size_t capacity);
1138  ~ON_UuidPtrList() = default;
1139  ON_UuidPtrList(const ON_UuidPtrList& src);
1141 
1142  /*
1143  Returns:
1144  Number of active uuids in the list.
1145  */
1146  unsigned int Count() const;
1147 
1148  /*
1149  Description:
1150  Provides an efficient way to empty a list so that it
1151  can be used again.
1152  */
1153  void RemoveAll();
1154 
1155  void Reserve( size_t capacity );
1156 
1157  /*
1158  Description:
1159  Adds a uuid-index pair to the list.
1160  Parameters:
1161  uuid - [in] id to add.
1162  This uuid cannot be ON_max_uuid because ON_max_uuid
1163  is
1164  bCheckForDupicates - [in] if true, then the uuid
1165  is not added if it is already in the list.
1166  If you are certain that the uuid is not in the list
1167  and you have a have a large collection of uuids,
1168  then setting bCheckForDupicates=false will
1169  speed up the addition of uuids.
1170  Returns:
1171  True if uuid was added. False if uuid was not added
1172  because it is already in the collection.
1173  */
1174  bool AddUuidPtr(
1175  ON_UUID uuid,
1176  ON__UINT_PTR ptr,
1177  bool bCheckForDupicates=true);
1178 
1179  /*
1180  Description:
1181  Removes an element with a matching uuid from the list.
1182  Parameters:
1183  uuid - [in] id to remove
1184  Returns:
1185  True if an element was removed. False if the uuid
1186  was not in the list.
1187  */
1188  bool RemoveUuid(
1189  ON_UUID uuid
1190  );
1191 
1192  /*
1193  Description:
1194  Determine if an element with a uuid is in the list.
1195  Parameters:
1196  ptr - [out] if not nullptr and a matching uuid is found,
1197  then *ptr is set to the value of the m_ptr.
1198  Returns:
1199  True if an element was found. Returns false if
1200  the uuid is not in the list.
1201  */
1202  bool FindUuid(ON_UUID uuid) const;
1203  bool FindUuid(ON_UUID uuid, ON__UINT_PTR* ptr) const;
1204 
1205  /*
1206  Description:
1207  Determine if a uuid-index pair is in the list.
1208  Returns:
1209  True if the uuid-index pair is in the list.
1210  Returns false if the uuid-index pair is not
1211  in the list.
1212  */
1213  bool FindUuidPtr(ON_UUID uuid, ON__UINT_PTR index) const;
1215  /*
1216  Description:
1217  Append the uuids in this class to uuid_list.
1218  Parameters:
1219  uuid_list - [in/out]
1220  Returns:
1221  Number of uuids added to uuid_list.
1222  */
1223  unsigned int GetUuids(
1224  ON_SimpleArray<ON_UUID>& uuid_list
1225  ) const;
1226 
1227  /*
1228  Description:
1229  If you will perform lots of searches before the next
1230  change to the list, then calling ImproveSearchSpeed()
1231  will speed up the searches by culling removed objects
1232  and completely sorting the list so only a binary search
1233  is required. You may edit the list at any time after
1234  calling ImproveSearchSpeed(). If you are performing
1235  a few searches between edits, then excessive calling
1236  of ImproveSearchSpeed() may actually decrease overall
1237  program performance.
1238  */
1239  void ImproveSearchSpeed();
1240 
1241 private:
1242  ON_UuidPtr* SearchHelper(const ON_UUID*) const;
1243  unsigned int m_sorted_count = 0;
1244  unsigned int m_removed_count = 0;
1245 };
1246 
1247 
1248 /*
1249 Description:
1250  The ON_UuidPairList class provides a tool
1251  to efficiently maintain a list of uuid pairs
1252  and determine if a uuid is in the list.
1253  This class is based on the premise that there are
1254  no duplicate uuids in the list.
1255 */
1256 class ON_CLASS ON_UuidPairList : private ON_SimpleArray<ON_UuidPair>
1257 {
1258 public:
1259  ON_UuidPairList();
1260  ON_UuidPairList(int capacity);
1261  ~ON_UuidPairList();
1262  ON_UuidPairList(const ON_UuidPairList& src);
1264 
1265  static const ON_UuidPairList EmptyList;
1266 
1267  /*
1268  Returns:
1269  Number of active uuids in the list.
1270  */
1271  int Count() const;
1272 
1273  /*
1274  Description:
1275  Provides an efficient way to empty a list so that it
1276  can be used again.
1277  */
1278  void Empty();
1279 
1280  void Reserve( size_t capacity );
1281 
1282  /*
1283  Description:
1284  Adds a uuid-index pair to the list.
1285  Parameters:
1286  id1 - [in] id to add.
1287  id2 - [in] id to add.
1288  bCheckForDupicates - [in] if true, then the pair
1289  is not added if id1 is already in the list.
1290  If you are certain that the id1 is not in the list
1291  and you have a have a large collection of uuids,
1292  then setting bCheckForDupicates=false will
1293  speed up the addition of uuids.
1294  Returns:
1295  True if the pair was added. False if the pair was not added
1296  because it is already in the collection.
1297  Remarks:
1298  You cannot add the pair value ( ON_max_uuid, ON_max_uuid ). This
1299  pair value is used to mark removed elements in the ON_UuidPairList[].
1300  */
1301  bool AddPair(
1302  ON_UUID id1,
1303  ON_UUID id2,
1304  bool bCheckForDupicates=true
1305  );
1306 
1307  /*
1308  Description:
1309  Removes an element with a matching id1 from the list.
1310  Parameters:
1311  id1 - [in] id to remove
1312  Returns:
1313  True if an element was removed. False if the id1
1314  was not in the list.
1315  */
1316  bool RemovePair(
1317  ON_UUID id1
1318  );
1319 
1320  /*
1321  Description:
1322  Removes an element with a matching id pair from the list.
1323  Parameters:
1324  id1 - [in]
1325  id2 - [in]
1326  Returns:
1327  True if an element was removed. False if the id pair
1328  does not appear in the list.
1329  */
1330  bool RemovePair(
1331  ON_UUID id1,
1332  ON_UUID id2
1333  );
1334 
1335  /*
1336  Description:
1337  Determine if an element with a uuid is in the list.
1338  Parameters:
1339  id1 - [in]
1340  id2 - [out] if not nullptr and a matching id1 is found,
1341  then *id2 is set to the value of the second uuid.
1342  Returns:
1343  True if an element was found. Returns false if
1344  the id1 is not in the list.
1345  */
1346  bool FindId1(ON_UUID id1, ON_UUID* id2=0) const;
1347 
1348  /*
1349  Description:
1350  Determine if an id pair is in the list.
1351  Returns:
1352  True if the id pair is in the list.
1353  False if the id pair is not in the list.
1354  */
1355  bool FindPair(ON_UUID id1, ON_UUID id2) const;
1356 
1357  /*
1358  Description:
1359  Append the value of the first id in each pair to uuid_list[].
1360  Parameters:
1361  uuid_list - [in/out]
1362  Returns:
1363  Number of ids appended to uuid_list[].
1364  */
1365  int GetId1s(
1366  ON_SimpleArray<ON_UUID>& uuid_list
1367  ) const;
1368 
1369  /*
1370  Description:
1371  If you will perform lots of searches before the next
1372  change to the list, then calling ImproveSearchSpeed()
1373  will speed up the searches by culling removed objects
1374  and completely sorting the list so only a binary search
1375  is required. You may edit the list at any time after
1376  calling ImproveSearchSpeed(). If you are performing
1377  a few searches between edits, then excessive calling
1378  of ImproveSearchSpeed() may actually decrease overall
1379  program performance.
1380  */
1381  void ImproveSearchSpeed();
1382 
1383  bool Write(
1384  class ON_BinaryArchive& archive
1385  ) const;
1386 
1387  bool Read(
1388  class ON_BinaryArchive& archive
1389  );
1390 
1391 private:
1392  ON_UuidPair* SearchHelper(const ON_UUID*) const;
1393  unsigned int m_sorted_count;
1394  unsigned int m_removed_count;
1395 };
1396 
1397 class ON_CLASS ON_2dexMap : private ON_SimpleArray<ON_2dex>
1398 {
1399 public:
1400  ON_2dexMap();
1401  ON_2dexMap(int capacity);
1402  ~ON_2dexMap();
1403 
1404  int Count() const;
1405 
1406  void Reserve(size_t capacity);
1407 
1408  const ON_2dex* Array() const;
1409 
1410  ON_2dex operator[](int i) const;
1411 
1412  /*
1413  Description:
1414  Creates an index map with the values
1415  (i0,j),...,(i0+count-1,j)
1416  Parameters:
1417  count - [in]
1418  number of elements
1419  i0 - [in]
1420  i value of first element
1421  j - [in]
1422  j value for all elements
1423  */
1424  void Create(int count, int i0, int j);
1425 
1426  /*
1427  Description:
1428  Searches for an element with a matching i
1429  and returns its j value. If no matching
1430  element is found, then not_found_rc is returned.
1431  Parameters:
1432  i - [in]
1433  value of i to search for
1434  not_found_rc - [in]
1435  value to return if there is not a match.
1436  Returns:
1437  j value
1438  */
1439  int FindIndex(
1440  int i,
1441  int not_found_rc
1442  ) const;
1443 
1444  /*
1445  Description:
1446  Adds and element (i,j). If there is already an entry with
1447  value (i,*), then no element is added.
1448  Parameters:
1449  i - [in]
1450  i - [in]
1451  Returns:
1452  True if and element it added.
1453  */
1454  bool AddIndex(
1455  int i,
1456  int j
1457  );
1458 
1459  /*
1460  Description:
1461  Searches for an element (i,*) and sets its j value to j.
1462  If there is no element with a matching i, then false
1463  is returned.
1464  Parameters:
1465  i - [in]
1466  j - [in]
1467  Returns:
1468  True if and element exists and was set.
1469  */
1470  bool SetIndex(
1471  int i,
1472  int j
1473  );
1474 
1475  /*
1476  Description:
1477  If an element (i,*) exists, its j value is set. Otherwise
1478  a new element with value (i,j) is added.
1479  Parameters:
1480  i - [in]
1481  j - [in]
1482  */
1483  void SetOrAddIndex(
1484  int i,
1485  int j
1486  );
1487 
1488  /*
1489  Description:
1490  If an element (i,*) exists, it is removed. If there is
1491  not an element with a matching i value, then false
1492  is returned.
1493  Parameters:
1494  i - [in]
1495  Returns:
1496  True if the element was removed
1497  */
1498  bool RemoveIndex(
1499  int i
1500  );
1501 
1502  const ON_2dex* Find2dex(int i) const;
1503 
1504 private:
1505  bool m_bSorted;
1506 };
1507 
1508 /*
1509 Description:
1510  Compare function for Sort and Search methods.
1511 Returns:
1512  -1 if *a < *b is true
1513  1 if *b < *a is true
1514  0 if niether *a <*b nor *b<*a is true
1515 Details:
1516  Use this template functions to sort ON_SimpleArray and
1517  ON_ClassArray objects into increasing order. The elements
1518  of the arrays must be a type with an operator < defined.
1519  In particular it works with built in types like double,
1520  int and pointers.
1521 Example:
1522 
1523  ON_SimpleArray<int> A;
1524  A = ...;
1525  // Sort A in increasing order
1526  A.QuickSort( ON_CompareIncreasing<double> );
1527 
1528 See Also:
1529  ON_CompareDecreasing
1530 */
1531 template< class T>
1532 static
1533 int ON_CompareIncreasing( const T* a, const T* b);
1534 
1535 /*
1536 Description:
1537  Compare function for Sort and Search methods.
1538 Returns:
1539  -1 if *b < *a is true
1540  1 if *a < *b is true
1541  0 if niether *a < *b nor *b < *a is true
1542 Details:
1543  Use this template functions to sort ON_SimpleArray and
1544  ON_ClassArray objects into decreasing order. The elements
1545  of the arrays must be a type with an operator < defined.
1546  In particular it works with built in types like double,
1547  int and pointers.
1548 Example:
1549 
1550  class C
1551  {
1552  public:
1553  ...
1554  bool operator<(const C&) const;
1555  };
1556  ...
1557  ON_ClassArray<C> A;
1558  A = ...;
1559  // Sort A in descrasing order
1560  A.QuickSort( ON_CompareDecreasing<C> );
1561 
1562 See Also:
1563  ON_CompareIncreasing
1564 */
1565 template< class T>
1566 static
1567 int ON_CompareDecreasing( const T* a, const T* b);
1568 
1569 
1570 // definitions of the template functions are in a different file
1571 // so that Microsoft's developer studio's autocomplete utility
1572 // will work on the template functions.
1573 #include "opennurbs_array_defs.h"
1574 
1575 
1576 #endif
void Empty()
Definition: opennurbs_array_defs.h:558
int Capacity() const
Definition: opennurbs_array_defs.h:173
virtual ~ON_SimpleArray()
Definition: opennurbs_array_defs.h:90
The ON_UuidList class provides a tool to efficiently maintain a list of uuid-index pairs and determin...
Definition: opennurbs_array.h:984
ON_SimpleArray() ON_NOEXCEPT
construction ////////////////////////////////////////////////////////
Definition: opennurbs_array_defs.h:63
T * At(int)
At(index) returns nullptr if index < 0 or index >= count.
Definition: opennurbs_array_defs.h:389
void Zero()
Definition: opennurbs_array_defs.h:778
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
void Move(int, int, int)
implimentation //////////////////////////////////////////////////////
Definition: opennurbs_array_defs.h:451
T * KeepArray()
Expert user tool to take charge of the memory used by the dyanmic array.
Definition: opennurbs_array_defs.h:349
T * Last()
Definition: opennurbs_array_defs.h:437
int m_count
Definition: opennurbs_array.h:360
The ON_UuidList class provides a tool to efficiently maintain a list of uuids and determine if a uuid...
Definition: opennurbs_array.h:803
void SetArray(T *)
Do not use this version of SetArray(). Use the one that takes a pointer, count and capacity...
Definition: opennurbs_array_defs.h:359
ON_SimpleArray< T > & operator=(const ON_SimpleArray< T > &)
Definition: opennurbs_array_defs.h:96
Definition: opennurbs_array.h:36
void Swap(int, int)
Definition: opennurbs_array_defs.h:583
void Shrink()
Definition: opennurbs_array_defs.h:804
T * SetCapacity(size_t)
Definition: opennurbs_array_defs.h:825
ON_Object array is used to store lists of classes that are derived from ON_Object. It differs from ON_ClassArray in that the virtual ON_Object::MemoryRelocate function is called when growing the dynamic array requires changing the location of the memory buffer used to store the elements in the array.
Definition: opennurbs_array.h:725
int BinarySearch(const T *, int(*)(const T *, const T *)) const
Definition: opennurbs_array_defs.h:614
int Search(const T &) const
Definition: opennurbs_array_defs.h:593
virtual T * Realloc(T *, int)
low level memory managment //////////////////////////////////////////
Definition: opennurbs_array_defs.h:57
T * m_a
Definition: opennurbs_array.h:359
void MemSet(unsigned char)
Definition: opennurbs_array_defs.h:786
bool QuickSort(int(*)(const T *, const T *))
Definition: opennurbs_array_defs.h:722
The ON_UuidList class provides a tool to efficiently maintain a list of uuid-pointer pairs and determ...
Definition: opennurbs_array.h:1099
void Insert(int, const T &)
Definition: opennurbs_array_defs.h:526
int m_capacity
Definition: opennurbs_array.h:361
bool Permute(const int *)
Definition: opennurbs_array_defs.h:762
unsigned int UnsignedCount() const
Definition: opennurbs_array_defs.h:167
void Append(const T &)
Definition: opennurbs_array_defs.h:482
T * Reserve(size_t)
memory managment ////////////////////////////////////////////////////
Definition: opennurbs_array_defs.h:796
bool Sort(ON::sort_algorithm sort_algorithm, int *, int(*)(const T *, const T *)) const
Sort() fills in the index[] array so that array[index[i]] <= array[index[i+1]]. The array is not modi...
Definition: opennurbs_array_defs.h:734
ON__UINT32 DataCRC(ON__UINT32 current_remainder) const
Definition: opennurbs_array_defs.h:192
unsigned int SizeOfElement() const
Definition: opennurbs_array_defs.h:185
T & operator[](int)
Definition: opennurbs_array_defs.h:198
void Reverse()
Definition: opennurbs_array_defs.h:566
unsigned int SizeOfArray() const
Definition: opennurbs_array_defs.h:179
Definition: opennurbs_array.h:409
void SetCount(int)
low level memory managment //////////////////////////////////////////
Definition: opennurbs_array_defs.h:818
Definition: opennurbs_archive.h:1783
The ON_UuidPairList class provides a tool to efficiently maintain a list of uuid pairs and determine ...
Definition: opennurbs_array.h:1214
T * First()
Definition: opennurbs_array_defs.h:377
void EmergencyDestroy(void)
emergency bailout ///////////////////////////////////////////////////
Definition: opennurbs_array_defs.h:151
int NewCapacity() const
Definition: opennurbs_array_defs.h:867
bool HeapSort(int(*)(const T *, const T *))
Definition: opennurbs_array_defs.h:710
Definition: opennurbs_array.h:1348
Definition: opennurbs_array.h:761
int Count() const
query ///////////////////////////////////////////////////////////////
Definition: opennurbs_array_defs.h:161
T * Array()
Definition: opennurbs_array_defs.h:337
void Remove()
Definition: opennurbs_array_defs.h:542
void Destroy()
Definition: opennurbs_array_defs.h:810
T & AppendNew()
array operations ////////////////////////////////////////////////////
Definition: opennurbs_array_defs.h:470