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