#!/usr/bin/env perl

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

$| = 1;

my $NUM = qr/(?:0|[1-9][0-9]*)/;

while (@ARGV && $ARGV[0] =~ /^-(.+)/s) {
    my $opt = $1;
    shift @ARGV;
    last if $opt eq q[-];
    die "usage: pds_verify [file]...\n";
}

my $syntax  = 0;
my $range   = 0;
my $not     = 0;
my $proven  = 0;
while (<<>>) {
    s/^\s+//;
    my @e = split /\s+/;
    next if !@e;

    ++$syntax, next if grep { !/^$NUM\z/ } @e;
    my $check = Math::DifferenceSet::Planar->verify_elements(@e);
    ++$range, next if !defined $check;
    ++$not,   next if !$check;
    ++$proven;
    print "@e\n";
}

warn "$syntax set(s) syntactically wrong\n"  if $syntax;
warn "$range set(s) not normalized\n"        if $range;
warn "$not set(s) proven to be wrong\n"      if $not;
warn "$proven set(s) proven to be correct\n" if $proven;

exit $syntax + $range + $not? 1: 0;

__END__

=head1 NAME

pds_verify - verify lists of integers as planar difference sets

=head1 SYNOPSIS

  pds_verify [file]...

=head1 DESCRIPTION

This example program reads planar difference sets, one per line,
as integer numbers separated by whitespace, verifies them, and writes
those of them them back to standard output that are correct.  The program
concludes with a summary of the check results on standard error.  The exit
code is 0 (success) if no lines had to be filtered out, otherwise 1.

=head1 BUGS AND LIMITATIONS

The verification method of this tool is based on a full operator table
and thus does not scale very well.  Its answer is conclusive, though.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2019-2023 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).

=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
