Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- @params:
- Haystack: Pointer to the haystack.
- Needle: Pointer to the needle.
- ElmntSize: Size of each element.
- HaystkLen: Number of elements in haystack
- NeedleLen: Number of elements in needle
- }
- function Find(Haystack:Pointer; Needle:Pointer; ElmntSize:SizeInt; HaystkLen, NeedleLen:UInt32): Int32;
- var
- i,hi,lo,ss:Int32;
- PBData,PBSeek,P,Q:PChar;
- begin
- PBData := PChar(Haystack);
- PBSeek := PChar(Needle);
- P := PBData[0];
- Q := PBSeek[0];
- SS := (ElmntSize*NeedleLen);
- lo := Int32(PBData[0]);
- hi := Int32(PBData[HaystkLen*ElmntSize] - SS);
- while hi > UInt32(P) do
- begin
- if (Q^ <> P^) then begin
- inc(p,ElmntSize);
- continue;
- end;
- if CompareMem(Q, P, SS) then
- Exit((UInt32(P)-lo) div ElmntSize);
- inc(p,ElmntSize);
- end;
- Exit(-1);
- end;
- var
- TIA: TIntegerArray; ValI: Int32;
- TBA: TByteArray; ValB: Byte;
- TPA: TPointArray; ValTPA: TPointArray; ValPT: TPoint;
- begin
- // Int in integer array.
- TIA := [0,1,2,3,4,5,6,7,8,9];
- ValI := 5;
- WriteLn( Find( Pointer(TIA), @ValI, SizeOf(Int32), Length(TIA), 1 ) );
- // Point-Array in Point-Array
- TPA := [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]];
- ValTPA := [[5,5], [6,6]];
- WriteLn( Find( Pointer(TPA), Pointer(ValTPA), SizeOf(TPoint), Length(TPA), Length(ValTPA) ) );
- // Point in Point-Array
- ValPT := Point(5,5);
- WriteLn( Find( Pointer(TPA), @ValPT, SizeOf(TPoint), Length(TPA), 1 ) );
- //byte in Byte-Array.
- TBA := [0,1,2,3,4,5,6,7,8,9];
- ValB := 5;
- WriteLn( Find( Pointer(TBA), @ValB, SizeOf(Byte), Length(TBA), 1 ) );
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement