📄 cppart.pas
字号:
{*********************************************}
{ }
{ COMPONENT for MS DOS and MS WINDOWS }
{ }
{ Source code for Turbo Pascal 6.0 and }
{ Turbo Pasacal for Windows 1.0 compilers. }
{ }
{ (c) 1991, Roderic D. M. Page }
{ }
{*********************************************}
{*
History
=======
16 Oct 1991 Serious bug found in procedure PP that caused
it to compute spurious parttion products.
*}
unit cppart;
interface
uses
cpvars,
cpset;
{*
A partition of a set S, say [1,.., 9] can be represented as
a vector P. E.g.,
[1..3] [4,5] [6] [7..9] = 123|45|6|789
1 2 3 4 5 6 7 8 9
-----------------
P = 1 1 1 2 2 3 4 4 4
Each element of the same subset has the
same value.
To get the partition product of two partitions we can
compare values in the vector.
For example. Given the partitions
12345 | 6789 and 123 | 456789
the first partition is
1 2 3 4 5 6 7 8 9
-----------------
P = 1 1 1 1 1 2 2 2 2
Going through the second partition, we use the
rule that if i in Sj (where Sj is the jth partition of S)
and P[i] = P[i-1] then in the partition product (PP),
i is in the same partition as i-1:
1 2 3 4 5 6 7 8 9
-----------------
P1= 1 1 1 1 1 2 2 2 2
PP= 1 1 1 2 2 3 3 3 3
so that PP (P1, P2) = 123 | 45 | 6789.
PartObj uses a [1..2,1..n] array to hold
two partitions, the last partition product (PP)
and the one presently being calculated.
With each new PP, the oldest partition is
overwritten.
If a column is not in the current set it has the value
"0", otherwise its value is the subset of the partition
to which it belongs.
*}
type
Partition = array[1..2,1..MAXLEAVES] of 0..MAXLEAVES;
PartObj = object
P : Partition;
Count : 0..MAXLEAVES; { counter }
Max : 0..MAXLEAVES; { maximal size of element in the set }
Parts : 0..MAXLEAVES;
CurRow,
LastRow : 1..2; { ptrs to the two rows }
procedure Show(var f:text);
constructor Init (n:integer);
procedure EnterSet (var S:CLUSTEROBJ);
procedure PP (var S:CLUSTEROBJ);
procedure SetUp;
procedure SwapRows;
procedure FirstSubSet (var S:CLUSTEROBJ);
procedure NextSubSet (var S:CLUSTEROBJ);
function MoreSubsets:Boolean;
end;
implementation
constructor PartObj.Init (n:integer);
{ Clear the PP }
var
i:integer;
begin
Max := n; { the maximal element of any partition }
Count := 0;
for i := 1 to Max do begin
P[1,i] := 0;
P[2,i] := 0;
end;
end;
procedure PartObj.Show (var f:text);
var
i:integer;
begin
writeln (f, 'Partition table');
writeln (f, 'Count = ',Count);
writeln (output, 'CurRow = ',CurRow, ' LastRow = ',LastRow);
for i := 1 to Max do
write (f,i:3);
writeln (f);
for i := 1 to Max do
write (f, '---');
writeln (f);
for i := 1 to Max do
write (f, P[1,i]:3);
writeln (f);
for i := 1 to Max do
write (f, P[2,i]:3);
writeln (f);
end;
procedure PartObj.EnterSet (var S:CLUSTEROBJ);
{ Enter the set S into the
first row of the partition block. All i
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -