mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-05-01 10:26:12 +03:00
OCC22278 No any sorting algorithm implemented for NCollection templates Developen by : KGV
This commit is contained in:
parent
bbf32d01fd
commit
e0a6799970
@ -74,3 +74,6 @@ NCollection_SparseArrayBase.cxx
|
|||||||
NCollection_CellFilter.hxx
|
NCollection_CellFilter.hxx
|
||||||
NCollection_Handle.hxx
|
NCollection_Handle.hxx
|
||||||
NCollection_Handle.cxx
|
NCollection_Handle.cxx
|
||||||
|
|
||||||
|
NCollection_Comparator.hxx
|
||||||
|
NCollection_QuickSort.hxx
|
||||||
|
69
src/NCollection/NCollection_Comparator.hxx
Executable file
69
src/NCollection/NCollection_Comparator.hxx
Executable file
@ -0,0 +1,69 @@
|
|||||||
|
// File NCollection_Comparator.hxx
|
||||||
|
// Created 27 January 2011
|
||||||
|
// Author KGV
|
||||||
|
// Copyright OpenCASCADE 2011
|
||||||
|
|
||||||
|
#ifndef _NCollection_Comparator_HeaderFile
|
||||||
|
#define _NCollection_Comparator_HeaderFile
|
||||||
|
|
||||||
|
#include <Precision.hxx>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class to define basic compare operations.
|
||||||
|
* Basic implementation use redirection to standard C++ operators.
|
||||||
|
* You can use standard C++ templates mechanisms to redefine these methods
|
||||||
|
* or to inherit basic implementation to create multiple comparators
|
||||||
|
* for same type with different rules.
|
||||||
|
*/
|
||||||
|
template<class TheItemType>
|
||||||
|
class NCollection_Comparator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
NCollection_Comparator (const Standard_Real theTolerance = Precision::Confusion())
|
||||||
|
: myTolerance (theTolerance) {}
|
||||||
|
|
||||||
|
virtual ~NCollection_Comparator() {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! Comparison functions which should be overridden
|
||||||
|
//! if standard operators are not defined for user type.
|
||||||
|
|
||||||
|
//! Should return true if Left value is greater than Right
|
||||||
|
virtual Standard_Boolean IsGreater (const TheItemType& theLeft, const TheItemType& theRight) const
|
||||||
|
{
|
||||||
|
return theLeft > theRight;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Should return true if values are equal
|
||||||
|
virtual Standard_Boolean IsEqual (const TheItemType& theLeft, const TheItemType& theRight) const
|
||||||
|
{
|
||||||
|
return theLeft == theRight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! Comparison functions which may be overridden for performance reasons
|
||||||
|
|
||||||
|
//! Should return true if Left value is lower than Right
|
||||||
|
virtual Standard_Boolean IsLower (const TheItemType& theLeft, const TheItemType& theRight) const
|
||||||
|
{
|
||||||
|
return !IsGreater (theLeft, theRight) && !IsEqual (theLeft, theRight);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Standard_Boolean IsLowerEqual (const TheItemType& theLeft, const TheItemType& theRight) const
|
||||||
|
{
|
||||||
|
return !IsGreater (theLeft, theRight);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Standard_Boolean IsGreaterEqual (const TheItemType& theLeft, const TheItemType& theRight) const
|
||||||
|
{
|
||||||
|
return IsGreater (theLeft, theRight) || IsEqual (theLeft, theRight);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
Standard_Real myTolerance;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /*_NCollection_Comparator_HeaderFile*/
|
97
src/NCollection/NCollection_QuickSort.hxx
Executable file
97
src/NCollection/NCollection_QuickSort.hxx
Executable file
@ -0,0 +1,97 @@
|
|||||||
|
// File NCollection_QuickSort.hxx
|
||||||
|
// Created 27 January 2011
|
||||||
|
// Author KGV
|
||||||
|
// Copyright OpenCASCADE 2011
|
||||||
|
|
||||||
|
#ifndef _NCollection_QuickSort_HeaderFile
|
||||||
|
#define _NCollection_QuickSort_HeaderFile
|
||||||
|
|
||||||
|
#include <NCollection_Comparator.hxx>
|
||||||
|
#include <Standard_Integer.hxx>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform sorting of enumerable collection with QuickSort algorithm.
|
||||||
|
* Enumerable collection should provide the random access to its values
|
||||||
|
* by index number with methods Value(theId) and ChangeValue(theId).
|
||||||
|
* Currently it supposed to be used with NCollection_Sequence and NCollection_Vector.
|
||||||
|
*
|
||||||
|
* Usage sample:
|
||||||
|
* // input sequence
|
||||||
|
* NCollection_Sequence<Standard_Real> aSequence;
|
||||||
|
* // perform sorting for the whole sequence.
|
||||||
|
* NCollection_QuickSort<NCollection_Sequence<Standard_Real>, Standard_Real>
|
||||||
|
* ::Perform (aSequence, NCollection_Comparator<Standard_Real>(),
|
||||||
|
* 1, aSequence.Size());
|
||||||
|
*/
|
||||||
|
template<class TheCollType, class TheItemType>
|
||||||
|
class NCollection_QuickSort
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! Main entry call to perform sorting
|
||||||
|
static void Perform (TheCollType& theEnumColl,
|
||||||
|
const NCollection_Comparator<TheItemType>& theComparator,
|
||||||
|
const Standard_Integer theLower,
|
||||||
|
const Standard_Integer theUpper)
|
||||||
|
{
|
||||||
|
if (theLower < theUpper)
|
||||||
|
{
|
||||||
|
Standard_Integer aPivotPosition = Partition (theEnumColl, theComparator,
|
||||||
|
theLower, theUpper);
|
||||||
|
if (theLower < aPivotPosition)
|
||||||
|
{
|
||||||
|
// recursive call
|
||||||
|
Perform (theEnumColl, theComparator,
|
||||||
|
theLower, aPivotPosition - 1);
|
||||||
|
}
|
||||||
|
// recursive call
|
||||||
|
Perform (theEnumColl, theComparator,
|
||||||
|
aPivotPosition + 1, theUpper);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
//! Auxiliary function
|
||||||
|
static void SwapValues (TheItemType& x, TheItemType& y)
|
||||||
|
{
|
||||||
|
TheItemType aCopy = x;
|
||||||
|
x = y;
|
||||||
|
y = aCopy;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Standard_Integer Partition (TheCollType& theEnumColl,
|
||||||
|
const NCollection_Comparator<TheItemType>& theComparator,
|
||||||
|
const Standard_Integer theLower,
|
||||||
|
const Standard_Integer theUpper)
|
||||||
|
{
|
||||||
|
Standard_Integer anIdLeft (theLower), anIdRight (theUpper);
|
||||||
|
const TheItemType aPivot = theEnumColl.Value (theLower);
|
||||||
|
|
||||||
|
while (anIdLeft < anIdRight)
|
||||||
|
{
|
||||||
|
while (theComparator.IsGreater (theEnumColl.Value (anIdRight), aPivot))
|
||||||
|
{
|
||||||
|
--anIdRight;
|
||||||
|
}
|
||||||
|
while ((anIdLeft < anIdRight) &&
|
||||||
|
theComparator.IsLowerEqual (theEnumColl.Value (anIdLeft), aPivot))
|
||||||
|
{
|
||||||
|
++anIdLeft;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (anIdLeft < anIdRight)
|
||||||
|
{
|
||||||
|
SwapValues (theEnumColl.ChangeValue (anIdLeft),
|
||||||
|
theEnumColl.ChangeValue (anIdRight));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
theEnumColl.ChangeValue (theLower) = theEnumColl.Value (anIdRight);
|
||||||
|
theEnumColl.ChangeValue (anIdRight) = aPivot;
|
||||||
|
return anIdRight;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /*_NCollection_QuickSort_HeaderFile*/
|
Loading…
x
Reference in New Issue
Block a user