mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-08-09 13:22:24 +03:00
Integration of OCCT 6.5.0 from SVN
This commit is contained in:
114
src/SortTools/SortTools.cdl
Executable file
114
src/SortTools/SortTools.cdl
Executable file
@@ -0,0 +1,114 @@
|
||||
-- File: SortTools.cdl
|
||||
-- Created: Thu Mar 7 09:00:21 1991
|
||||
-- Author: Herve Legrand
|
||||
-- <hl@topsn3>
|
||||
---Copyright: Matra Datavision 1991
|
||||
-- Revised: Fri Oct 30 1992
|
||||
-- By : Mireille MERCIEN
|
||||
|
||||
|
||||
package SortTools
|
||||
|
||||
---Purpose: This package provides the basic sorting tools generally
|
||||
-- used.
|
||||
|
||||
uses TCollection,
|
||||
TColStd
|
||||
|
||||
is
|
||||
generic class HeapSort;
|
||||
---Purpose: Sort an array using the heap sort algorithm.
|
||||
|
||||
generic class QuickSort;
|
||||
---Purpose: Sort an array using the quick sort algorithm.
|
||||
|
||||
generic class ShellSort;
|
||||
---Purpose: Sort an array using the shell sort algorithm.
|
||||
|
||||
generic class StraightInsertionSort;
|
||||
---Purpose: Sort an array using the straight insertion sort algorithm.
|
||||
|
||||
|
||||
-- The table below summarizes the most important characteristics that
|
||||
-- must be considered when selecting a particular sorting algorithm.
|
||||
-- A sorting algorithm is known as stable if the initial order of items
|
||||
-- with equals keys is preserved after the sorting operation.
|
||||
|
||||
------------------------------------------------------------------------
|
||||
-- Algorithm Stable Comparisons Moves
|
||||
-- Min Avg Max Min Avg Max
|
||||
------------------------------------------------------------------------
|
||||
-- 2 2
|
||||
-- QuickSort No nlogn nlogn n nlogn nlogn n
|
||||
--
|
||||
------------------------------------------------------------------------
|
||||
--
|
||||
-- HeapSort No nlogn nlogn nlogn nlogn nlogn nlogn
|
||||
--
|
||||
------------------------------------------------------------------------
|
||||
-- 1.25 1.25
|
||||
-- ShellSort No - n - - n -
|
||||
--
|
||||
------------------------------------------------------------------------
|
||||
-- 2 2 2 2
|
||||
-- StraightInsertion Yes n n n n n n
|
||||
--
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
--+ Heap sort exhibits an interesting time complexity, in that it runs
|
||||
-- on the order of nlogn for the best, average, and worst case.
|
||||
--+ Quick sort is still faster on the average(its constant of proportiona-
|
||||
-- lity is lower), but it does not guarantee such good worst-case perfor-
|
||||
-- mance.
|
||||
--+ Shell sort is not sensitive to the initial ordering and offers accepta-
|
||||
-- ble running time even for moderately larges arrays (say, 1000 elements).
|
||||
--+ Insertion sort is the method of choice for "almost sorted" arrays with
|
||||
-- few inversions : for such cases, it will outperform even the more
|
||||
-- sophisticated method (quick sort, heap sort).
|
||||
|
||||
|
||||
-- Instantiations --
|
||||
-- ************** --
|
||||
------------------------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Instantiations QuickSort (Integer,Real)
|
||||
-- ***************************************
|
||||
class QuickSortOfInteger instantiates QuickSort(Integer,
|
||||
Array1OfInteger from TColStd,
|
||||
CompareOfInteger from TCollection);
|
||||
class QuickSortOfReal instantiates QuickSort(Real,
|
||||
Array1OfReal from TColStd,
|
||||
CompareOfReal from TCollection );
|
||||
|
||||
-- Instantiations HeapSort (Integer,Real)
|
||||
-- ***************************************
|
||||
class HeapSortOfInteger instantiates HeapSort(Integer,
|
||||
Array1OfInteger from TColStd,
|
||||
CompareOfInteger from TCollection);
|
||||
class HeapSortOfReal instantiates HeapSort(Real,
|
||||
Array1OfReal from TColStd,
|
||||
CompareOfReal from TCollection);
|
||||
|
||||
-- Instantiations ShellSort (Integer,Real)
|
||||
-- ***************************************
|
||||
class ShellSortOfInteger instantiates ShellSort(Integer,
|
||||
Array1OfInteger from TColStd,
|
||||
CompareOfInteger from TCollection);
|
||||
class ShellSortOfReal instantiates ShellSort(Real,
|
||||
Array1OfReal from TColStd,
|
||||
CompareOfReal from TCollection );
|
||||
|
||||
-- Instantiations StraightInsertionSort (Integer,Real)
|
||||
-- ***************************************************
|
||||
class StraightInsertionSortOfInteger instantiates
|
||||
StraightInsertionSort(Integer,
|
||||
Array1OfInteger from TColStd,
|
||||
CompareOfInteger from TCollection);
|
||||
class StraightInsertionSortOfReal instantiates
|
||||
StraightInsertionSort(Real,
|
||||
Array1OfReal from TColStd,
|
||||
CompareOfReal from TCollection);
|
||||
|
||||
end SortTools;
|
||||
|
20
src/SortTools/SortTools_HeapSort.cdl
Executable file
20
src/SortTools/SortTools_HeapSort.cdl
Executable file
@@ -0,0 +1,20 @@
|
||||
-- File: HeapSort.cdl
|
||||
-- Created: Tue Mar 5 10:50:42 1991
|
||||
-- Author: Herve Legrand
|
||||
-- <hl@topsn3>
|
||||
---Copyright: Matra Datavision 1991
|
||||
|
||||
generic class HeapSort from SortTools (Item as any;
|
||||
Array as Array1 from TCollection(Item);
|
||||
Comparator as any)
|
||||
|
||||
---Purpose: This class provides the HeapSort algorithm.
|
||||
|
||||
is
|
||||
|
||||
Sort(myclass; TheArray : in out Array; Comp : Comparator);
|
||||
---Purpose: Sort an array using the HeapSort algorithm.
|
||||
---Level: Public
|
||||
|
||||
end;
|
||||
|
52
src/SortTools/SortTools_HeapSort.gxx
Executable file
52
src/SortTools/SortTools_HeapSort.gxx
Executable file
@@ -0,0 +1,52 @@
|
||||
// SortTools_HeapSort.gxx
|
||||
// cree le 04/11/91 par ASI
|
||||
// Reference : Software Conponents with ADA, Grady Booch.
|
||||
|
||||
void Shift(Array& TheArray,
|
||||
const Comparator& Comp,
|
||||
const Standard_Integer Left, const Standard_Integer Right)
|
||||
{
|
||||
Item Temp = TheArray(Left);
|
||||
Standard_Integer Front = Left;
|
||||
Standard_Integer Back = Front * 2;
|
||||
while (Back <= Right) {
|
||||
if (Back < Right) {
|
||||
if(Comp.IsLower(TheArray(Back), TheArray(Back + 1))) {
|
||||
Back = Back + 1;
|
||||
}
|
||||
}
|
||||
if(!Comp.IsLower(Temp, TheArray(Back))) break;
|
||||
TheArray(Front) = TheArray(Back);
|
||||
Front = Back;
|
||||
if(Front * 2 > TheArray.Upper()) break;
|
||||
Back = Front * 2;
|
||||
}
|
||||
TheArray(Front) = Temp;
|
||||
}
|
||||
|
||||
void SortTools_HeapSort::Sort(Array& TheArray,
|
||||
const Comparator& Comp)
|
||||
{
|
||||
Item TempItem;
|
||||
Standard_Integer Left;
|
||||
Standard_Integer Right;
|
||||
|
||||
Left = ((TheArray.Upper() - TheArray.Lower() + 1) / 2) + 1;
|
||||
Right = TheArray.Upper();
|
||||
while (Left > TheArray.Lower()) {
|
||||
Left = Left - 1;
|
||||
Shift(TheArray, Comp, Left, Right);
|
||||
}
|
||||
while (Right > TheArray.Lower()) {
|
||||
TempItem = TheArray(TheArray.Lower());
|
||||
TheArray(TheArray.Lower()) = TheArray(Right);
|
||||
TheArray(Right) = TempItem;
|
||||
Right = Right - 1;
|
||||
Shift(TheArray, Comp, Left, Right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
21
src/SortTools/SortTools_QuickSort.cdl
Executable file
21
src/SortTools/SortTools_QuickSort.cdl
Executable file
@@ -0,0 +1,21 @@
|
||||
-- File: QuickSort.cdl
|
||||
-- Created: Tue Mar 5 10:50:42 1991
|
||||
-- Author: Herve Legrand, Mireille MERCIEN
|
||||
-- <hl@topsn3>
|
||||
---Copyright: Matra Datavision 1991
|
||||
|
||||
|
||||
generic class QuickSort from SortTools (Item as any;
|
||||
Array as Array1 from TCollection(Item);
|
||||
Comparator as any)
|
||||
|
||||
---Purpose: This class provides the QuickSort algorithm.
|
||||
|
||||
is
|
||||
|
||||
Sort(myclass; TheArray : in out Array; Comp : Comparator);
|
||||
---Purpose: Sort an array using the QuickSort algorithm.
|
||||
---Level: Public
|
||||
|
||||
end;
|
||||
|
68
src/SortTools/SortTools_QuickSort.gxx
Executable file
68
src/SortTools/SortTools_QuickSort.gxx
Executable file
@@ -0,0 +1,68 @@
|
||||
// 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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
19
src/SortTools/SortTools_ShellSort.cdl
Executable file
19
src/SortTools/SortTools_ShellSort.cdl
Executable file
@@ -0,0 +1,19 @@
|
||||
-- File: ShellSort.cdl
|
||||
-- Created: Tue Mar 5 10:50:42 1991
|
||||
-- Author: Herve Legrand,Mireille MERCIEN
|
||||
-- <hl@topsn3>
|
||||
---Copyright: Matra Datavision 1991
|
||||
|
||||
generic class ShellSort from SortTools (Item as any;
|
||||
Array as Array1 from TCollection(Item);
|
||||
Comparator as any)
|
||||
|
||||
---Purpose: This class provides the ShellSort algorithm.
|
||||
|
||||
is
|
||||
|
||||
Sort(myclass; TheArray : in out Array; Comp : Comparator);
|
||||
---Purpose: Sort an array using the ShellSort algorithm.
|
||||
---Level: Public
|
||||
|
||||
end;
|
40
src/SortTools/SortTools_ShellSort.gxx
Executable file
40
src/SortTools/SortTools_ShellSort.gxx
Executable file
@@ -0,0 +1,40 @@
|
||||
// SortTools_ShellSort.gxx
|
||||
// cree le 04/11/91 par ASI
|
||||
// Reference : Software Conponents with ADA, Grady Booch.
|
||||
|
||||
void SortTools_ShellSort::Sort(Array& TheArray,
|
||||
const Comparator& Comp)
|
||||
{
|
||||
|
||||
Item TempItem;
|
||||
Standard_Integer Outer;
|
||||
Standard_Integer Inner;
|
||||
Standard_Integer Inc = 1;
|
||||
|
||||
for(;;) {
|
||||
if((9 * Inc) + 4 >= TheArray.Upper() - TheArray.Lower() + 1) break;
|
||||
Inc = (Inc * 3) + 1;
|
||||
}
|
||||
for(;;) {
|
||||
Outer = TheArray.Lower() + Inc;
|
||||
for(;;) {
|
||||
TempItem = TheArray(Outer);
|
||||
Inner = Outer;
|
||||
while (Comp.IsLower(TempItem, TheArray(Inner - Inc))) {
|
||||
TheArray(Inner) = TheArray(Inner - Inc);
|
||||
Inner = Inner - Inc;
|
||||
if(Inner - Inc < TheArray.Lower()) break;
|
||||
}
|
||||
TheArray(Inner) = TempItem;
|
||||
if(Outer + Inc > TheArray.Upper()) break;
|
||||
Outer = Outer + Inc;
|
||||
}
|
||||
if(Inc == 1) break;
|
||||
Inc = (Inc - 1) / 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
20
src/SortTools/SortTools_StraightInsertionSort.cdl
Executable file
20
src/SortTools/SortTools_StraightInsertionSort.cdl
Executable file
@@ -0,0 +1,20 @@
|
||||
-- File: StraightInsertionSort.cdl
|
||||
-- Created: Tue Mar 5 10:50:42 1991
|
||||
-- Author: Herve Legrand, Mireille MERCIEN
|
||||
-- <hl@topsn3>
|
||||
---Copyright: Matra Datavision 1991
|
||||
|
||||
generic class StraightInsertionSort from SortTools (
|
||||
Item as any;
|
||||
Array as Array1 from TCollection(Item);
|
||||
Comparator as any)
|
||||
|
||||
---Purpose: This class provides the StraightInsertionSort algorithm.
|
||||
|
||||
is
|
||||
|
||||
Sort(myclass; TheArray : in out Array; Comp : Comparator);
|
||||
---Purpose: Sort an array using the StraightInsertionSort algorithm.
|
||||
---Level: Public
|
||||
|
||||
end;
|
26
src/SortTools/SortTools_StraightInsertionSort.gxx
Executable file
26
src/SortTools/SortTools_StraightInsertionSort.gxx
Executable file
@@ -0,0 +1,26 @@
|
||||
// SortTools_StraightInsertionSort.gxx
|
||||
// cree le 04/11/91 par ASI
|
||||
// Reference : Software Conponents with ADA, Grady Booch.
|
||||
|
||||
void SortTools_StraightInsertionSort::Sort(Array& TheArray,
|
||||
const Comparator& Comp)
|
||||
{
|
||||
Item TempItem;
|
||||
Standard_Integer J;
|
||||
|
||||
for(Standard_Integer I = TheArray.Lower() + 1; I <= TheArray.Upper(); I++) {
|
||||
TempItem = TheArray(I);
|
||||
J = I;
|
||||
while (Comp.IsLower(TempItem, TheArray(J - 1))) {
|
||||
TheArray(J) = TheArray(J - 1);
|
||||
J = J - 1;
|
||||
if (J == TheArray.Lower()) break;
|
||||
}
|
||||
TheArray(J) = TempItem;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user