Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- type
- TPathStruct = packed record pts: TPointArray; d: Double; end;
- TQueueHelper = record
- lo, hi: Int32;
- data: array of TPathStruct;
- end;
- procedure TQueueHelper.Init(sz: Int32);
- begin
- SetLength(Self.data, sz);
- Self.hi := -1;
- Self.lo := 0;
- end;
- function TQueueHelper.SearchClosest(value: Double): Int32;
- var
- l, h: Int32;
- begin
- l := Self.Lo;
- h := Self.Hi;
- while l <= h do
- begin
- Result := (l + h) div 2;
- if Self.Data[Result].d < value then
- h := Result - 1
- else if Self.Data[Result].d > value then
- l := Result + 1
- else
- Exit(Result);
- end;
- end;
- function TQueueHelper.Insert(path:TPointArray; distance: Double): Int32;
- var
- idx: Int32;
- begin
- idx := Self.SearchClosest(distance);
- if Self.Data[idx].d > distance then Inc(idx);
- if (self.hi+1-idx) > 0 then
- begin
- WriteLn('>>> move from: ', idx,' to ', idx+1,', bytes: ', (self.hi+1-idx)*(SizeOf(TPathStruct)));
- Move(Self.data[idx], Self.data[idx+1], (self.hi+1-idx)*(SizeOf(TPathStruct)));
- end;
- Self.data[idx].d := distance;
- Self.data[idx].pts := path;
- Inc(Self.hi);
- end;
- procedure TQueueHelper.Pop(out path:TPointArray; out distance: Double);
- begin
- path := self.data[self.hi].pts;
- distance := self.data[self.hi].d;
- Dec(Self.Hi);
- end;
- function TQueueHelper.AsString(): String;
- var
- tmp: array of TPathStruct;
- begin
- tmp := self.data;
- SetLength(tmp, self.hi+1);
- Result := ToString(tmp);
- end;
- var
- q: TQueueHelper;
- p: tPointArray;
- d: Double;
- begin
- q.Init(1000);
- q.Insert([[2,2]], 2);
- WriteLn q.AsString;
- q.Insert([[2,1], [2,2]], 4);
- WriteLn q.AsString;
- q.Insert([[2,2], [1,2]], 8);
- WriteLn q.AsString;
- q.Insert([[2,2], [3,2]], 16);
- WriteLn q.AsString;
- q.Insert([[2,2], [2,3]], 32);
- WriteLn q.AsString;
- q.Insert([[2,2], [2,3], [4,4]], 64);
- WriteLn q.AsString;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement