CSortedArray / CSortedArrayEx v1.55

CSortedArray is a class derived from the MFC template class "CArray" or ATL template class "CAtlArray". It provides for ordered insertion of items into an array as well as searching and sorting of its items. The compare function used to determine ordering in the array can be easily customised at run-time.

CSortedArrayEx is a specialized version of CSortedArray which uses functors rather than a standard comparison function. Using this version provides up to a 300% speed improvement because functors allow function inlining in optimizing compilers while a raw function pointer can never be inlined.

 

 

Features
Usage
Copyright
History
API Reference
Contacting the Author

 

 

 

Features

 

 

 

Usage

 

 

 

Copyright

 

 

 

History

v1.55 (2 April 2022)

v1.54 (10 May 2020)

v1.53 (23 March 2020)

v1.52 (23 December 2019)

v1.51 (22 September 2019)

v1.50 (26 December 2018)

v1.49 (19 November 2017)

v1.48 (27 April 2017)

v1.47 (31 January 2016)

v1.46 (24 January 2015)

v1.45 (20 April 2012)

v1.44 (16 March 2012)

v1.43 (6 November 2010)

v1.42 (11 July 2010)

v1.41 (7 September 2009)

v1.40 (11 August 2009)

v1.39 (26 July 2009)

v1.38 (29 June 2008)

v1.37 (29 July 2006)

v1.36 (7 July 2006)

v1.35 (12 October 2005)

v1.34 (22 December 2004)

v1.33 (16 October 2004)

v1.32 (13 November 2003)

v1.31 (18 August 2003)

v1.30 (24 January 2003)

v1.29 (11 December 2002)

v1.28 (6 December 2002)

v1.27 (29 May 2002)

v1.26 (16 February 2002)

v1.25 (24 December 2001)

v1.24 (26 October 2001)

v1.23 (1 October 2001)

v1.22 (11 August 2001)

v1.21 (11 August 2001)

v1.2 (5 August 2001)

v1.15 (5 May 2001)

v1.14 (2 October 2000)

v1.13 (20 September 2000)

v1.12 (5 September 2000)

v1.11 (27 August 2000)

v1.10 (24 July 2000)

v1.09 (18 July 2000)

v1.08 (13 May 2000)

v1.07 (2 April 2000)

v1.06 (29 February 2000)

v1.05 (22 February 2000)

v1.04 (21 February 2000)

v1.03 (31 January 2000)

v1.02 (25 January 2000)

v1.01 (12 January 2000)

 

 

 

API Reference

The following functions are provided by CSortedArray and the functor version CSortedArrayEx:

CSortedArray::OrderedInsert
CSortedArray::FindOrderedInsertIndex
CSortedArray::Sort
CSortedArray::StableSort
CSortedArray::UniqueSort
CSortedArray::Find
CSortedArray::SetCompareFunction
CSortedArray::GetCompareFunction
CSortedArrayEx::SetCompareFunction
CSortedArrayEx::GetCompareFunction

 

CSortedArray::OrderedInsert

INT_PTR OrderedInsert(ARG_TYPE newElement, INT_PTR nCount = 1, CSortedArrayEnums::InsertionBehaviour behavior = CSortedArrayEnums::AllowDuplicate);

Return Value

The index where the item has been inserted.

Parameters

element The item to add to the array. Its type is determined when the class is instantiated as with the parent class CArray.

nCount The number of times this element should be inserted (defaults to 1).

behaviour The behavior to use if a duplicate element already exists in the array. Can be "AllowDuplicate", "OverwriteIfDuplicate", "LeaveIfDuplicate" or "FailIfDuplicate".

Remarks

Inserts the element into the array. The code internally uses a binary search to determine where the item should be inserted. This assumes that the elements in the array are already ordered. If they are not then you should call the Sort method first. Because the class is publicly derived from CArray / CAtlArray, you can call all of parents methods in addition to the methods implemented in CSortedArray.

 

CSortedArray::FindOrderedInsertIndex

INT_PTR FindOrderedInsertIndex(ARG_TYPE element, CSortedArrayEnums::InsertionBehaviour behavior = CSortedArrayEnums::AllowDuplicate);

Return Value

The index where the item has been inserted.

Parameters

element The item to add to the array. Its type is determined when the class is instantiated as with the parent class CArray.

behaviour The behavior to use if a duplicate element already exists in the array. Can be "AllowDuplicate", "OverwriteIfDuplicate", "LeaveIfDuplicate" or "FailIfDuplicate".

Remarks

Inserts the element into the array. The code internally uses a binary search to determine where the item should be inserted. This assumes that the elements in the array are already ordered. If they are not then you should call the Sort method first. Because the class is publicly derived from CArray / CAtlArray, you can call all of parents methods in addition to the methods implemented in CSortedArray.

 

CSortedArray::Sort

void Sort(INT_PTR nLowIndex = 0, INT_PTR nHighIndex = -1);

Parameters

nLowIndexThe index of the first element in the array to sort.

nHighIndex The index of the last element in the array to sort. The default value of -1 represents the last element in the array.

Remarks

Performs a sort of the specified elements in the array. Internally the code will use the "Quicksort" algorithm to do the sort.

 

CSortedArray::StableSort

void StableSort(INT_PTR nLowIndex = 0, INT_PTR nHighIndex = -1);

Parameters

nLowIndexThe index of the first element in the array to sort.

nHighIndex The index of the last element in the array to sort. The default value of -1 represents the last element in the array.

Remarks

Performs a sort of the specified elements in the array. Internally the code will use the "Insertion Sort" algorithm to do the sort. This method performs a "stable" sort of the table unlike "Sort" which is an unstable algorithm.

 

CSortedArray::UniqueSort

void UniqueSort(INT_PTR nLowIndex = 0, INT_PTR nHighIndex = -1);

Parameters

nLowIndexThe index of the first element in the array to sort.

nHighIndex The index of the last element in the array to sort. The default value of -1 represents the last element in the array.

Remarks

Performs a sort of the specified elements in the array and removes any duplicates found. Internally this method calls the standard "Sort" method and then removes any duplicates found.

 

CSortedArray::Find

INT_PTR Find(ARG_TYPE element, INT_PTR nLowIndex = 0, INT_PTR nHighLowIndex = -1);

Return Value

The index of the item if found otherwise -1 if not.

Parameters

element The item to search for in the array.

nLowIndex The index of the first element in the array to search.

nHighIndex The index of the last element in the array to search. The default value of -1 represents the last element in the array.

Remarks

Searches for an item in the array. The code internally uses a binary search to determine if the element is present or not.

 

CSortedArray::SetCompareFunction

void SetCompareFunction(LPCOMPARE_FUNCTION lpfnCompareFunction);

Parameters

lpfnCompareFunction The new function to use for comparisons to determine the ordering of elements in the array.

Remarks

This sets the function which is used internally for item comparisons. LPCOMPARE_FUNCTION is a pointer to a function as defined:

typedef int COMPARE_FUNCTION(ARG_TYPE element1, ARG_TYPE element2);
typedef COMPARE_FUNCTION* LPCOMPARE_FUNCTION;

The comparison function should operate the same way as the qsort "C" runtime function, i.e. it should return a negative value when element1 is logically less than element2, 0 for equality and a positive value when element1 is logically greater than element2. Note that prior to v1.35 of the code, the code incorrectly handled return values from the Compare function and in fact only handled -1, 0 and 1!.

 

CSortedArray::GetCompareFunction

LPCOMPAREFUNCTION GetCompareFunction() const;

Return Value

A pointer to the function which is currently used to determine the ordering of elements in the array.

Remarks

This is the corollary function to the SetCompareFunction function.

 

 

CSortedArrayEx::SetCompareFunction

void SetCompareFunction(const COMPARE_TYPE& pCompareFunctor);

Parameters

pCompareFunctor The new comparison functor to use for comparisons to determine the ordering of elements in the array.

Remarks

This sets the functor which is used internally for item comparisons. 

The comparison functor should operate the same way as the qsort "C" runtime function, i.e. it should return a negative value when element1 is logically less than element2, 0 for equality and a positive value when element1 is logically greater than element2. Note that prior to v1.35 of the code, the code incorrectly handled return values from the Compare function and in fact only handled -1, 0 and 1!.

 

CSortedArrayEx::GetCompareFunction

COMPARE_TYPE GetCompareFunction() const;

Return Value

the functor which is currently used to determine the ordering of elements in the array.

Remarks

This is the corollary function to the SetCompareFunction function.

 

 

 

Contacting the Author

PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
2 April 2022