home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8711.arc
/
SHAMMNOV.LST
< prev
next >
Wrap
File List
|
1987-10-08
|
7KB
|
265 lines
Listing 1. Pascal function for the simple heuristic search in an unordered array.
FUNCTION Search1(VAR Data : AnyArrayType; { input }
VAR Index : IntegerArray; { in/out }
NData : INTEGER; { input }
Item : ScalarType { input }) : INTEGER;
{ function returns the index of the matching
array, or zero if no match is found }
VAR I, Tempo : INTEGER;
BEGIN
I := 1;
{ scan array }
WHILE (Item <> Data[ Index[I] ]) AND (I <= NData) DO
I := I + 1;
IF I <= NData
THEN BEGIN { match found }
Search1 := Index[I]; { returned result }
{ Swap indices ? }
IF I > 1 THEN BEGIN
Tempo := Index[I];
Index[I] := Index[I-1];
Index[I-1] := Index[I];
END { IF }
END
ELSE Search1 := 0; { not found }
END; { Search1 }
Listing 2. Pascal function for the heuristic search method for unordered arrays.
FUNCTION Search2(VAR Data : AnyArrayType; { input }
VAR Index : IntegerArray; { in/out }
VAR Loctn : SmallIntArray;{ in/out }
NData : INTEGER; { input }
Item : ScalarType { input }) : INTEGER;
{ function returns the index of the matching
array, or zero if no match is found }
CONST Factor = 0.7; { time-series factor }
{ limits used to select search schemes }
UPPER_LIMIT = 0.65; { = 0.5 + 0.15 }
LOWER_LIMIT = 0.35; { = 0.5 - 0.15 }
{ number of Loctn elements used in predicting the next
matching location }
N = 4;
VAR Continue : BOOLEAN;
I, J, Tempo, This_Location, Median,
Skip, Result : INTEGER;
Next_Location, Power : REAL;
PROCEDURE Swap_Indices(K : INTEGER);
{ Procedure used to swap indices }
BEGIN
{ Swap indices ? }
IF K > 1 THEN BEGIN
Tempo := Index[K];
Index[K] := Index[K-1];
Index[K-1] := Index[K];
END { IF }
END;
BEGIN
{ Estimate the next location }
Next_Location := 0.0; { initialize next location }
Power := 1.0;
FOR I := N DOWNTO 1 DO BEGIN
Next_Location := Next_Location + Power * Loctn[I];
Power := Power * Factor;
END;
{ calculate predicted next search location as a fraction }
Next_Location := (1.0 - Factor) * Next_Location / NData;
Result := 0; { default value for no match }
IF Next_Location > UPPER_LIMIT THEN BEGIN
{ Search last-to-first }
I := NData;
WHILE (Item <> Data[ Index[I] ]) AND (I > 0) DO
I := I - 1;
IF I > 0 THEN BEGIN
Result := Index[I];
Swap_Indices(I); { swap indices }
END { IF }
END
ELSE IF Next_Location < LOWER_LIMIT THEN BEGIN
{ Search first-to-last }
I := 1;
WHILE (Item <> Data[ Index[I] ]) AND (I <= NData) DO
I := I + 1;
IF I <= NData THEN BEGIN
Result := Index[I];
Swap_Indices(I); { swap indices }
END { IF }
END
ELSE
{ Perform bidirectional search starting at the middle }
Median := NData div 2;
IF Item <> Data[ Index[Median] ] THEN BEGIN
Skip := 1;
Continue := TRUE;
REPEAT
I := Median + Skip;
IF I <= NData THEN
IF Item = Data[ Index[I] ] THEN BEGIN
Result := Index[I];
Continue := FALSE;
Swap_Indices(I); { swap indices }
END; { IF }
J := Median - Skip;
IF J > 0 THEN
IF Item = Data[ Index[J] ] THEN BEGIN
Result := Index[J];
Continue := FALSE;
Swap_Indices(J); { swap indices }
END; { IF }
IF (I > NData) AND (J < 1) THEN { out of bounds }
Continue := FALSE;
Skip := Skip + 1;
UNTIL (NOT Continue);
END
ELSE BEGIN
Result := Index[Median];
Swap_Indices(Media)
END; { IF }
END; { IF }
IF Result > 0 THEN BEGIN
{ Update location array }
FOR I := 1 TO N-1 DO
Loctn[I] := Loctn[I+1];
Loctn[N] := Result
END; { IF }
Search2 := Result; { return result }
END; { Search 2 }
Listing 3. Pascal code for function implementing the first heuristic search method for fixed ordered arrays.
PROCEDURE Initialize(VAR Data : AnyArrayType; { input }
VAR Table : RecordArray; { in/out }
TableSize : INTEGER; { input }
NData : INTEGER { input });
{ initialize index table by inserting data at equal intervals }
{
TYPE TableRecord = RECORD
Key : ScalarType;
Index : INTEGER;
END;
RecordArray = ARRAY[1..MAX_TABLE_SIZE] OF TableRecord;
}
VAR I, J, Delta : INTEGER;
BEGIN
Delta := NData div TableSize;
J := 1;
FOR I := 1 TO TableSize DO BEGIN
Table[I].Key := Data[J];
Table[I].Index := J;
J := J + Delta;
END;
END; { initialize }
FUNCTION Search3(VAR Data : AnyArrayType; { input }
VAR Table : RecordArray; { in/out }
TableSize, { input }
NData : INTEGER; { input }
Item : ScalarType { input }) : INTEGER;
VAR Found, NoMatch : BOOLEAN;
First, Last, I, K, Result : INTEGER;
BEGIN
Result := 0; { initialize result with default value }
{ Search for Item in index table }
I := 1;
WHILE (Item > Table[I].Key) AND (I <= TableSize) DO
I := I + 1;
Found := FALSE;
IF I <= TableSize
THEN Found := (Item = Table[I].Key)
ELSE I := I - 1;
IF Found
THEN
Result := Table[I].Index
ELSE
{ Get range for search limits }
First := Table[I-1].Index;
Last := Table[I].Index;
K := I-1;
NoMatch := TRUE;
I := First;
WHILE (I <= Last) AND NoMatch DO
IF Item <> Data[I].Key THEN I := I + 1
ELSE NoMatch := FALSE;
IF NOT NoMatch THEN BEGIN
Result := Table[I].Index; { store result }
IF K > 1 THEN BEGIN { update table entry }
Table[K].Key := Item;
Table[K].Index := Result
END; { IF }
END; { IF }
END; { IF }
Search3 := Result { return result }
END; { Search3 }