1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-26 10:19:45 +03:00
occt/src/SortTools/SortTools_QuickSort.gxx
bugmster 973c2be1e1 0024428: Implementation of LGPL license
The copying permission statements at the beginning of source files updated to refer to LGPL.
Copyright dates extended till 2014 in advance.
2013-12-17 12:42:41 +04:00

83 lines
2.3 KiB
Plaintext

// Copyright (c) 1998-1999 Matra Datavision
// Copyright (c) 1999-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and / or modify it
// under the terms of the GNU Lesser General Public version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
// SortTools_QuickSort.gxx
// cree le 04/11/91 par ASI
// Reference : Software Conponents with ADA, Grady Booch.
inline void Exchange(Item& Left, Item& Right)
{
Item Temp = Left;
Left = Right;
Right = Temp;
}
static void SortRecursive(Array& TheArray,
const Comparator& Comp,
const Standard_Integer Left,
const Standard_Integer Right)
{
Item Pivot;
Standard_Integer Front, Back, Middle;
if(Left < Right) {
Middle = (Left + Right) / 2;
if(Comp.IsLower(TheArray(Middle), TheArray(Left))) {
Exchange(TheArray(Middle), TheArray(Left));
}
if(Comp.IsLower(TheArray(Right), TheArray(Left))) {
Exchange(TheArray(Right), TheArray(Left));
}
if(Comp.IsLower(TheArray(Right), TheArray(Middle))) {
Exchange(TheArray(Right), TheArray(Middle));
}
Pivot = TheArray(Middle);
Exchange(TheArray(Middle), TheArray(Right - 1));
Front = Left + 1;
Back = Right - 1;
if(Back != TheArray.Lower()) {
Back = Back - 1;
}
for(;;) {
while (Comp.IsLower(TheArray(Front), Pivot)) {
Front = Front + 1;
}
while (Comp.IsLower(Pivot, TheArray(Back))) {
Back = Back - 1;
}
if(Front <= Back) {
if(Front == TheArray.Upper()) return;
if(Back == TheArray.Lower()) return;
Exchange(TheArray(Front), TheArray(Back));
Front = Front + 1;
Back = Back - 1;
}
if(Front > Back) break;
}
SortRecursive(TheArray, Comp, Left, Back);
SortRecursive(TheArray, Comp, Front, Right);
}
}
void SortTools_QuickSort::Sort(Array& TheArray,
const Comparator& Comp)
{
SortRecursive(TheArray, Comp, TheArray.Lower(), TheArray.Upper());
}