Advertisement
WarPie90

Untyped `Pos`

Jun 26th, 2014
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.31 KB | None | 0 0
  1.  
  2. {
  3.   @params:
  4.     Haystack:  Ref to the haystack pos.
  5.     Needle:    The needle.
  6.     HaystkLen: Number of elements in haystack
  7.     ElmntSize: Size of each element.
  8. }
  9. function Find(var Haystack,Needle; HaystkLen:UInt32; ElmntSize:SizeInt): 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.   lo := Int32(PBData[0]);
  19.   hi := Int32(PBData[HaystkLen*ElmntSize] - ElmntSize);
  20.   while hi > UInt32(P) do
  21.   begin
  22.     if (Q^ <> P^) then begin
  23.       inc(p,ElmntSize);
  24.       continue;
  25.     end;
  26.     if CompareMem(Q, P, ElmntSize) then
  27.       Exit((UInt32(P)-lo) div ElmntSize);
  28.     inc(p,ElmntSize);
  29.   end;
  30.   Exit(-1);
  31. end;
  32.  
  33.  
  34. var
  35.   TIA: TIntegerArray;   ValI: Int32;
  36.   TBA: TByteArray;      ValBt: Byte;
  37.   TPA: TPointArray;     ValPT: TPoint;
  38. begin
  39.   //TIA
  40.   TIA := [0,1,2,3,4,5,6,7,8,9];
  41.   ValI := 5;
  42.  
  43.   //TBA
  44.   TBA := [0,1,2,3,4,5,6,7,8,9];
  45.   ValBt := 5;
  46.  
  47.   //TPA
  48.   TPA := [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]];
  49.   ValPt := Point(5,5);
  50.  
  51.  
  52.   //---- Find index of value:
  53.   WriteLn( Find(TIA[0], ValI,  Length(TIA), SizeOf(Int32))  );
  54.   WriteLn( Find(TPA[0], ValPt, Length(TPA), SizeOf(TPoint)) );
  55.   WriteLn( Find(TBA[0], ValBt, Length(TBA), SizeOf(Byte))   );
  56. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement