Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:simplearray [2015/09/14]
developer:sdksamples:simplearray [2020/08/14] (current)
Line 1: Line 1:
 +====== ON_SimpleArray Questions ======
 +> **Developer:** //[[developer:cplusplusplugins|C++]]//
 +> **Summary:** //Demonstrates how to sort and cull simple arrays.//
  
 +=====Question=====
 +I have an ON_SimpleArray with data (doubles) in it. How can I sort the data and delete duplicates?
 +
 +=====Answer=====
 +The following sample code should provide the functionality you want.
 +
 +
 +<code c++>
 +void SortDoubles( ON_SimpleArray<double>& arr, bool bIncreasing = true )
 +{
 +  if( arr.Count() > 1 )
 +  {
 +    if( bIncreasing )
 +      arr.QuickSort( &ON_CompareIncreasing<double> );
 +    else
 +      arr.QuickSort( &ON_CompareDecreasing<double> );
 +  }
 +}
 +
 +void CullDoubles( ON_SimpleArray<double>& arr, double tolerance = ON_ZERO_TOLERANCE )
 +{
 +  const int count = arr.Count();
 +  if( count > 1 )
 +  {
 +    arr.QuickSort( &ON_CompareIncreasing<double> );
 +
 +    if( tolerance < ON_ZERO_TOLERANCE )
 +      tolerance = ON_ZERO_TOLERANCE;
 +
 +    double d = *arr.Last();
 +    int i;
 +    for( i = count - 2; i >= 0; i-- )
 +    {
 +      if( fabs(d - arr[i]) <= tolerance )
 +        arr.Remove(i);
 +      else
 +        d = arr[i];
 +    }
 +
 +    arr.Shrink();
 +  }
 +}
 +</code>
 +
 +
 +{{tag>Developer cplusplus}}
developer/sdksamples/simplearray.txt ยท Last modified: 2020/08/14 (external edit)