diff options
author | nickysn <nickysn@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2021-01-27 23:54:53 +0000 |
---|---|---|
committer | nickysn <nickysn@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2021-01-27 23:54:53 +0000 |
commit | e9ef0175745e1cf37f957a92788ffaeea60ed703 (patch) | |
tree | 4c4056c133b603f95618246157d9e416d2d9f1de /packages/fcl-stl | |
parent | 9292b9a9fd5b34267923550d6bd40f63082e2f6c (diff) | |
parent | 3e4764099ffb4f63b912c4692837903b88bbdfec (diff) | |
download | fpc-e9ef0175745e1cf37f957a92788ffaeea60ed703.tar.gz |
* synchronized with trunk
git-svn-id: https://svn.freepascal.org/svn/fpc/branches/wasm@48440 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'packages/fcl-stl')
-rw-r--r-- | packages/fcl-stl/src/gdeque.pp | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/packages/fcl-stl/src/gdeque.pp b/packages/fcl-stl/src/gdeque.pp index 2fa9d2d443..ac106e5581 100644 --- a/packages/fcl-stl/src/gdeque.pp +++ b/packages/fcl-stl/src/gdeque.pp @@ -14,6 +14,11 @@ unit gdeque; +{ + Implements a generic double ended queue. + (See: https://en.wikipedia.org/wiki/Double-ended_queue) +} + interface type @@ -35,9 +40,13 @@ type procedure MoveSimpleData(StartIndex: SizeUInt; Offset: SizeInt; NrElems: SizeUInt); procedure MoveManagedData(StartIndex: SizeUInt; Offset: SizeInt; NrElems: SizeUInt); procedure MoveData(StartIndex: SizeUInt; Offset: SizeInt; NrElems: SizeUInt); + procedure ClearSingleDataEntry(Index: SizeUInt); virtual; + procedure ClearData; virtual; + property Data: TArr read FData; public function Size():SizeUInt;inline; constructor Create(); + destructor Destroy(); override; Procedure Clear; procedure PushBack(value:T);inline; procedure PushFront(value:T);inline; @@ -63,8 +72,15 @@ begin FStart:=0; end; +destructor TDeque.Destroy(); +begin + Clear; + inherited Destroy; +end; + procedure TDeque.Clear; begin + ClearData; FDataSize:=0; FStart:=0; end; @@ -91,6 +107,7 @@ procedure TDeque.PopFront();inline; begin if(FDataSize>0) then begin + ClearSingleDataEntry(FStart); inc(FStart); dec(FDataSize); if(FStart=FCapacity) then @@ -101,7 +118,10 @@ end; procedure TDeque.PopBack();inline; begin if(FDataSize>0) then + begin + ClearSingleDataEntry((FStart+FDataSize-1)mod FCapacity); dec(FDataSize); + end; end; procedure TDeque.PushFront(value:T);inline; @@ -131,8 +151,7 @@ end; procedure TDeque.SetValue(position:SizeUInt; value:T);inline; begin Assert(position < size, 'Deque access out of range'); - if IsManagedType(T) then - Finalize(FData[(FStart+position)mod FCapacity]); + ClearSingleDataEntry((FStart+position)mod FCapacity); FData[(FStart+position)mod FCapacity]:=value; end; @@ -149,7 +168,6 @@ begin end; - procedure TDeque.MoveSimpleData(StartIndex: SizeUInt; Offset: SizeInt; NrElems: SizeUInt); begin Move(FData[StartIndex], FData[StartIndex+Offset], NrElems*SizeOf(T)); @@ -182,6 +200,27 @@ begin MoveSimpleData(StartIndex, Offset, NrElems); end; +procedure TDeque.ClearSingleDataEntry(Index: SizeUInt); +begin + if IsManagedType(T) then + begin + Finalize(FData[Index]); + FillChar(FData[Index], SizeOf(T), 0); + end + else + FData[Index] := default(T); +end; + +procedure TDeque.ClearData; +var + i: SizeUint; +begin + if IsManagedType(T) then + for i := Low(FData) to High(FData) do + Finalize(FData[i]); + FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0); +end; + procedure TDeque.IncreaseCapacity; function Min(const A,B: SizeUInt): SizeUInt; inline; //no need to drag in the entire Math unit ;-) begin |