From 91abdc06b3eedaae49ce157331ef98b0bbf72239 Mon Sep 17 00:00:00 2001 From: vladob Date: Fri, 8 Apr 2011 22:19:39 +0000 Subject: next permutation git-svn-id: http://svn.freepascal.org/svn/fpc/trunk@17273 3ad0048d-3df7-0310-abae-a5850022a9f2 --- packages/fcl-stl/src/garrayutils.pp | 27 +++++++ packages/fcl-stl/tests/garrayutilstest.pp | 116 +++++++++++++++++++++++++++ packages/fcl-stl/tests/gpriorityqueuetest.pp | 12 +-- packages/fcl-stl/tests/gsorttest.pp | 52 ------------ packages/fcl-stl/tests/run-all-tests | 4 +- packages/fcl-stl/tests/suiteconfig.pp | 4 +- 6 files changed, 153 insertions(+), 62 deletions(-) create mode 100644 packages/fcl-stl/tests/garrayutilstest.pp delete mode 100644 packages/fcl-stl/tests/gsorttest.pp diff --git a/packages/fcl-stl/src/garrayutils.pp b/packages/fcl-stl/src/garrayutils.pp index 75e3c3fc1a..f9773b63db 100644 --- a/packages/fcl-stl/src/garrayutils.pp +++ b/packages/fcl-stl/src/garrayutils.pp @@ -32,6 +32,7 @@ type class function Parent(a:SizeUInt):SizeUInt;inline; public class procedure Sort(var Arr: TArr; size:SizeUInt); + class function NextPermutation(var Arr: TArr; size:SizeUInt):boolean; end; generic TArrayUtils=class @@ -212,6 +213,32 @@ begin end; end; +class function TOrderingArrayUtils.NextPermutation(var Arr: TArr; size: SizeUInt):boolean; +var i,f:SizeUInt; temp:TValue; +begin + f := -1; + for i:=size-1 downto 1 do begin + if (TCompare.c(arr[i-1], arr[i])) then begin + f := i-1; + break; + end; + end; + if f = -1 then exit(false); + for i:=size-1 downto 1 do begin + if (TCompare.c(arr[f], arr[i])) then begin + temp:=arr[f]; arr[f] := arr[i]; arr[i] := temp; + break; + end; + end; + i:= size-1; + inc(f); + while (i > f) do begin + temp:=arr[f]; arr[f] := arr[i]; arr[i] := temp; + dec(i); inc(f); + end; + NextPermutation := true; +end; + class procedure TArrayUtils.RandomShuffle(Arr: TArr; size: SizeUInt); var i,r:SizeUInt; temp:Tvalue; begin diff --git a/packages/fcl-stl/tests/garrayutilstest.pp b/packages/fcl-stl/tests/garrayutilstest.pp new file mode 100644 index 0000000000..3a1bb20605 --- /dev/null +++ b/packages/fcl-stl/tests/garrayutilstest.pp @@ -0,0 +1,116 @@ +{$mode objfpc} + +unit garrayutilstest; + +interface + +uses fpcunit, testregistry, gvector, garrayutils, gutil; + +type vectorlli=specialize TVector; + lesslli=specialize TLess; + sortlli=specialize TOrderingArrayUtils; + +type TGArrayUtilsTest = class(TTestCase) + Published + procedure SortRandomTest; + procedure SortZeroOneTest; + procedure NextPermutationTest1; + procedure NextPermutationTest2; + procedure NextPermutationTest3; + procedure NextPermutationTest4; + public + procedure Setup;override; + private + data:vectorlli; + end; + +implementation + +procedure TGArrayUtilsTest.SortRandomTest; +var i:longint; +begin + for i:=0 to 5000 do + data.pushBack(random(10000)); + sortlli.sort(data, 5001); + for i:=0 to 4999 do + AssertEquals('Wrong order', false, data[i+1]; - queuelli=specialize TPriorityQueue; +{type lesslli=specialize TLess; + queuelli=specialize TPriorityQueue;} type TGPQueueTest = class(TTestCase) Published @@ -15,7 +15,7 @@ type TGPQueueTest = class(TTestCase) public procedure Setup;override; private - data:queuelli; + { data:queuelli;} end; implementation @@ -23,7 +23,7 @@ implementation procedure TGPQueueTest.QueueTest; var i,last:longint; begin - AssertEquals('Not IsEmpty', true, data.IsEmpty); +{ AssertEquals('Not IsEmpty', true, data.IsEmpty); for i:=0 to 10 do data.push(random(10000)); last:=data.top; @@ -34,12 +34,12 @@ begin last:=data.top; data.pop; end; - AssertEquals('Not IsEmpty', true, data.IsEmpty); + AssertEquals('Not IsEmpty', true, data.IsEmpty);} end; procedure TGPQueueTest.Setup; begin - data:=queuelli.create; +{ data:=queuelli.create;} end; initialization diff --git a/packages/fcl-stl/tests/gsorttest.pp b/packages/fcl-stl/tests/gsorttest.pp deleted file mode 100644 index a51e8ee493..0000000000 --- a/packages/fcl-stl/tests/gsorttest.pp +++ /dev/null @@ -1,52 +0,0 @@ -{$mode objfpc} - -unit gsorttest; - -interface - -uses fpcunit, testregistry, gvector, garrayutils, gutil; - -type vectorlli=specialize TVector; - lesslli=specialize TLess; - sortlli=specialize TOrderingArrayUtils; - -type TGSortTest = class(TTestCase) - Published - procedure SortRandomTest; - procedure SortZeroOneTest; - public - procedure Setup;override; - private - data:vectorlli; - end; - -implementation - -procedure TGSortTest.SortRandomTest; -var i:longint; -begin - for i:=0 to 5000 do - data.pushBack(random(10000)); - sortlli.sort(data, 5001); - for i:=0 to 4999 do - AssertEquals('Wrong order', false, data[i+1]