#!/usr/bin/env perl

# Given sample planar difference sets, find the lexically first
# lex-canonical sets of their respective spaces

use strict;
use warnings;
use Math::DifferenceSet::Planar 1.002;

$| = 1;

my $BAR_WIDTH = 64;

my $USAGE = "usage: pds_find_lex_ref [-f] [-v] [file]...\n";

my $force = 0;
my $pro_b = 0;
while (@ARGV && $ARGV[0] =~ /^-(.+)/s) {
    my $opt = $1;
    shift @ARGV;
    last if '-' eq $opt;
    $force = 1, next if 'f' eq $opt;
    $pro_b = 1, next if 'p' eq $opt;
    die $USAGE;
}

while(<<>>) {
    s/^\s+//;
    my @list = split /\s+/;
    next if !@list;
    die "integer numbers separated by whitespace expected\n"
        if grep { !/^(?:0|[1-9][0-9]*)\z/ } @list;

    my $s1 = !$force && Math::DifferenceSet::Planar->lex_reference($#list);
    if (!$s1) {
        my $s2 = Math::DifferenceSet::Planar->from_elements_fast(@list);
        my $ni = $s2->plane_principal_elements;
        my $it = $s2->iterate_principal_planes;
        if (!$ni) {
            $ni = $s2->n_planes;
            $it = $s2->iterate_planes;
        };
        progress_bar(0, $ni) if $pro_b;
        $s1 = $it->();
        my $i = 1;
        progress_bar($i, $ni) if $pro_b;
        while (my $s3 = $it->()) {
            $s1 = $s3 if $s3->compare($s1) < 0;
            progress_bar(++$i, $ni) if $pro_b;
        }
    }

    my @el = $s1->elements_sorted;
    print "@el\n";
}

sub progress_bar {
    my ($i, $ni) = @_;
    my $black = int 0.5 + $BAR_WIDTH * $i / $ni;
    my $white = $BAR_WIDTH - $black;
    print STDERR "\r", 'X' x $black, '.' x $white, " $i/$ni";
    print STDERR "\n" if $i >= $ni;
}

__END__

=head1 NAME

pds_find_lex_ref - find lexically minimal planar difference sets

=head1 SYNOPSIS

  pds_find_lex_ref [-f] [-p] [file]...

=head1 DESCRIPTION

This example program reads planar difference sets, one per line, as
integer numbers separated by whitespace, finds from each of their
respective spaces the lexically first one of all sets, and prints
the results.  It can be used to get lex reference sets from arbitrary
sample sets.

Option C<-f> forces recalculation of the reference set even if it is
already known.

Option C<-p> displays a progress bar for each set that needs to be
calculated.

The algorithm uses the fact that the planes containing a set with elements
0, 1, and 3, are precisely those planes with 1 among their principal
elements.  This greatly reduces the number of planes to be considered
for comparison.  Unlike the search for zeta-canonical reference sets,
this algorithm needs to compute complete sets, however.

=head1 AUTHOR

Martin Becker, E<lt>becker-cpan-mp I<at> cozap.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2024 by Martin Becker, Blaubeuren.

This library is free software; you can distribute it and/or modify it
under the terms of the Artistic License 2.0 (see the LICENSE file).

The licence grants freedom for related software development but does
not cover incorporating code or documentation into AI training material.
Please contact the copyright holder if you want to use the library whole
or in part for other purposes than stated in the licence.

=head1 DISCLAIMER OF WARRANTY

This library is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose.

=cut
