home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
PASCAL
/
PAS_0593
/
SORTQ.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-30
|
6KB
|
165 lines
{─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 493 of 527
From : Alexander Christov 2:341/34.0 14 May 93 12:42
To : All
Subj : Sorting Routines (2/3)
────────────────────────────────────────────────────────────────────────────────
Hi All!}
{$A-,B-,D+,E-,F+,G+,I-,L+,N-,O+,P+,Q-,R-,S-,T-,V-,X+,Y+}
{ I don't know which settings are really necessary for this unit, but since
I always work with the above, I'm including them to make sure the unit
compiles in your computer. The only critical ones (I Think) are R- and F+
}
UNIT SORTQ;
{─────────────────────────────────────────────────────────────────────────}
{ Purpose: Unit that implements a generic QSort, similar to the }
{ one in the standard C library, but a lot more general }
{ This new version allows ordering of almost anything, }
{ even structures whose elements are not contiguous in memory }
{ or have strange mutual dependancies that don't allow "happy }
{ swapping". Obviously, this version is slower than the }
{ previous one. If you won't be sorting Linked Lists or }
{ Collections, use the previous one. }
{ Author : Alexander Christov }
{ Notes : Very instructive on the use of pointers in TP. }
{ This version does not limit the number of elements to }
{ 65535 since the need not be contiguous. }
{ }
{ Use freely. }
{ }
{─────────────────────────────────────────────────────────────────────────}
INTERFACE
TYPE CmpFunc=Function(El1,El2:Pointer):Boolean;
AddrFunc=Function(Base:Pointer;Size,N:LongInt):Pointer;
SwapProc=Procedure(El1,El2:Pointer;Size:LongInt);
Procedure QSort(Base:Pointer; { Pointer to the first element.
If the user writes his own GT, Addr and
Swap, this isn't really necessary.
}
Elements:LongInt; { Total number of elements }
Size:Word; { Size of an element in bytes }
GT:CmpFunc; { Comparing function }
Addr:AddrFunc; { Addressing function }
Swap:SwapProc); { Swapping function }
{
GT - A funcion of type CmpFunc that compares the elements pointed
to by its first and second arguments, and returns TRUE if the
first element is Greater Than the second one. This unit defines
some commonly used CmpFuncs:
bGT - Compares BYTEs
wGT - Compares WORDs
lGT - Compares LONGINTs
rGT - Compares REALs
Addr - A Function that receives the index of an element and must
return a pointer to it.
This unit defines the function
LinearAddr
which can be used whenever the elements are located
contiguously in memory.
Swap - A Procedure that swaps the elements pointed by its arguments.
DirectSwap
is defined in the unit, which can be used whenever the elements
are mutually independent or no external processes are needed
when swapping two elements
}
{ Commonly used CmpFuncs }
Function bGT(El1,El2:Pointer):Boolean; { Compares ^BYTE }
Function wGT(El1,El2:Pointer):Boolean; { Compares ^WORD }
Function lGT(El1,El2:Pointer):Boolean; { Compares ^LONGINT }
Function rGT(El1,El2:Pointer):Boolean; { Compares ^REAL }
Function LinearAddr(Base:Pointer;Size,N:LongInt):Pointer;
Procedure DirectSwap(El1,El2:Pointer;Size:LongInt);
IMPLEMENTATION
{$F+}
TYPE Dummy=ARRAY[0..0] OF BYTE;
pDummy=^Dummy;
VAR X,Middle:Pointer;
Procedure
_Sort(Base:Pointer;L,R:LongInt;Size:Word;GT:CmpFunc;Addr:AddrFunc;Swap:SwapProc
);
VAR I,J:LongInt;
BEGIN
I:=L;
J:=R;
Move(Addr(Base,Size,(L+R) div 2)^,Middle^,Size);
REPEAT
While GT(Middle,Addr(Base,Size,I)) DO INC(I);
While GT(Addr(Base,Size,J),Middle) DO DEC(J);
IF I<=J THEN BEGIN
IF I<>J THEN Swap(Addr(Base,Size,I),Addr(Base,Size,J),Size);
INC(I);
DEC(J);
END;
UNTIL I>J;
IF L<J THEN _Sort(Base,L,J,Size,GT,Addr,Swap);
IF I<R THEN _Sort(Base,I,R,Size,GT,Addr,Swap);
END;
Procedure QSort;
BEGIN
GetMem(X,Size); { <- Made in Arturo Ramirez 8-) }
GetMem(Middle,Size);
_Sort(Base,0,Elements-1,Size,GT,Addr,Swap);
FreeMem(X,Size);
FreeMem(Middle,Size);
END;
Function bGT(El1,El2:Pointer):Boolean;
TYPE pByte=^Byte;
BEGIN
bGt:=(pByte(El1)^>pByte(El2)^);
END;
Function wGT(El1,El2:Pointer):Boolean;
TYPE pWord=^Word;
BEGIN
wGt:=(pWord(El1)^>pWord(El2)^);
END;
Function lGT(El1,El2:Pointer):Boolean;
TYPE pLongint=^Longint;
BEGIN
lGt:=(pLongInt(El1)^>pLongInt(El2)^);
END;
Function rGT(El1,El2:Pointer):Boolean;
TYPE pReal=^Real;
BEGIN
rGt:=(pReal(El1)^>pReal(El2)^);
END;
{ Linear Addressing }
Function LinearAddr;
BEGIN
LinearAddr:=@pdummy(Base)^[N*Size];
END;
{ Direct swapping of elements. With the use of Addr() it is quite more
legible 8-) }
Procedure DirectSwap;
VAR Tmp:Pointer;
BEGIN
GetMem(Tmp,Size);
Move(El1^,Tmp^,Size);
Move(El2^,El1^,Size);
Move(Tmp^,El2^,Size);
FreeMem(Tmp,Size);
END;
END.