Advertisement
WarPie90

Pointer 'Pos'

May 15th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.63 KB | None | 0 0
  1. {
  2.   @params:
  3.     Haystack:  Pointer to the haystack.
  4.     Needle:    Pointer to the needle.
  5.     ElmntSize: Size of each element.
  6.     HaystkLen: Number of elements in haystack
  7.     NeedleLen: Number of elements in needle
  8. }
  9. function Find(Haystack:Pointer; Needle:Pointer; ElmntSize:SizeInt; HaystkLen, NeedleLen:UInt32): Int32;
  10. var
  11.   i,hi,lo,ss:Int32;
  12.   PBData,PBSeek,P,Q:PChar;
  13. begin
  14.   PBData := PChar(Haystack);
  15.   PBSeek := PChar(Needle);
  16.   P := PBData[0];
  17.   Q := PBSeek[0];
  18.   SS := (ElmntSize*NeedleLen);
  19.   lo := Int32(PBData[0]);
  20.   hi := Int32(PBData[HaystkLen*ElmntSize] - SS);
  21.   while hi > UInt32(P) do
  22.   begin
  23.     if (Q^ <> P^) then begin
  24.       inc(p,ElmntSize);
  25.       continue;
  26.     end;
  27.     if CompareMem(Q, P, SS) then
  28.       Exit((UInt32(P)-lo) div ElmntSize);
  29.     inc(p,ElmntSize);
  30.   end;
  31.   Exit(-1);
  32. end;
  33.  
  34.  
  35. var
  36.   TIA: TIntegerArray;   ValI: Int32;
  37.   TBA: TByteArray;      ValB: Byte;
  38.   TPA: TPointArray;     ValTPA: TPointArray;     ValPT: TPoint;
  39. begin
  40.   // Int in integer array.
  41.   TIA := [0,1,2,3,4,5,6,7,8,9];
  42.   ValI := 5;
  43.   WriteLn( Find( Pointer(TIA), @ValI, SizeOf(Int32), Length(TIA), 1 ) );
  44.  
  45.   // Point-Array in Point-Array
  46.   TPA := [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]];
  47.   ValTPA := [[5,5], [6,6]];
  48.   WriteLn( Find( Pointer(TPA), Pointer(ValTPA), SizeOf(TPoint), Length(TPA), Length(ValTPA) ) );
  49.  
  50.   // Point in Point-Array
  51.   ValPT := Point(5,5);
  52.   WriteLn( Find( Pointer(TPA), @ValPT, SizeOf(TPoint), Length(TPA), 1 ) );
  53.  
  54.   //byte in Byte-Array.
  55.   TBA := [0,1,2,3,4,5,6,7,8,9];
  56.   ValB := 5;
  57.   WriteLn( Find( Pointer(TBA), @ValB, SizeOf(Byte), Length(TBA), 1 ) );
  58. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement