Advertisement
WarPie90

Lape array bug trigger

Jun 25th, 2023 (edited)
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.93 KB | None | 0 0
  1. program new;
  2.  
  3.  
  4.  
  5. type
  6.   TPathStruct = packed record pts: TPointArray; d: Double; end;
  7.   TQueueHelper = record
  8.     lo, hi: Int32;
  9.     data: array of TPathStruct;
  10.   end;
  11.  
  12. procedure TQueueHelper.Init(sz: Int32);
  13. begin
  14.   SetLength(Self.data, sz);
  15.  
  16.   Self.hi := -1;
  17.   Self.lo := 0;
  18. end;
  19.  
  20. function TQueueHelper.SearchClosest(value: Double): Int32;
  21. var
  22.   l, h: Int32;
  23. begin
  24.   l := Self.Lo;
  25.   h := Self.Hi;
  26.  
  27.   while l <= h do
  28.   begin
  29.     Result := (l + h) div 2;
  30.     if Self.Data[Result].d < value then
  31.       h := Result - 1
  32.     else if Self.Data[Result].d > value then
  33.       l := Result + 1
  34.     else
  35.       Exit(Result);
  36.   end;
  37. end;
  38.  
  39. function TQueueHelper.Insert(path:TPointArray; distance: Double): Int32;
  40. var
  41.   idx: Int32;
  42. begin
  43.   idx := Self.SearchClosest(distance);
  44.   if Self.Data[idx].d > distance then Inc(idx);
  45.  
  46.   if (self.hi+1-idx) > 0 then
  47.   begin
  48.     WriteLn('>>> move from: ', idx,' to ', idx+1,', bytes: ', (self.hi+1-idx)*(SizeOf(TPathStruct)));
  49.     Move(Self.data[idx], Self.data[idx+1], (self.hi+1-idx)*(SizeOf(TPathStruct)));
  50.   end;
  51.  
  52.   Self.data[idx].d   := distance;
  53.   Self.data[idx].pts := path;
  54.  
  55.   Inc(Self.hi);
  56. end;
  57.  
  58.  
  59. procedure TQueueHelper.Pop(out path:TPointArray; out distance: Double);
  60. begin
  61.   path     := self.data[self.hi].pts;
  62.   distance := self.data[self.hi].d;
  63.   Dec(Self.Hi);
  64. end;
  65.  
  66. function TQueueHelper.AsString(): String;
  67. var
  68.   tmp: array of TPathStruct;
  69. begin
  70.   tmp := self.data;
  71.   SetLength(tmp, self.hi+1);
  72.   Result := ToString(tmp);
  73. end;
  74.  
  75.  
  76.  
  77. var
  78.   q: TQueueHelper;
  79.   p: tPointArray;
  80.   d: Double;
  81. begin
  82.   q.Init(1000);
  83.  
  84.   q.Insert([[2,2]], 2);
  85.   WriteLn q.AsString;
  86.  
  87.   q.Insert([[2,1], [2,2]], 4);
  88.   WriteLn q.AsString;
  89.  
  90.   q.Insert([[2,2], [1,2]], 8);
  91.   WriteLn q.AsString;
  92.  
  93.   q.Insert([[2,2], [3,2]], 16);
  94.   WriteLn q.AsString;
  95.  
  96.   q.Insert([[2,2], [2,3]], 32);
  97.   WriteLn q.AsString;
  98.  
  99.   q.Insert([[2,2], [2,3], [4,4]], 64);
  100.   WriteLn q.AsString;
  101. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement