#!/usr/bin/env perl

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

$| = 1;

my $method = 'elements_sorted';
while (@ARGV && $ARGV[0] =~ /^-(.+)\z/s) {
    my $opt = $1;
    shift @ARGV;
    last                       if '-' eq $opt;
    $method = 'elements', next if 'c' eq $opt;
    die "usage: pds_sort_elements [-c] [file]...\n";
}

while (<<>>) {
    s/^\s+//;
    my @e = split /\s+/;
    next if !@e;

    die "integer numbers separated by whitespace expected\n"
        if grep { !/^(?:0|[1-9][0-9]*)\z/ } @e;

    my $s = Math::DifferenceSet::Planar->from_elements(@e);
    my @e2 = $s->$method;
    print "@e2\n";
}

__END__

=head1 NAME

pds_sort_elements - numerically sort planar difference set elements

=head1 SYNOPSIS

  pds_sort_elements [-c] [file]...

=head1 DESCRIPTION

This example program reads planar difference sets, one per line, as
integer numbers separated by whitespace, sorts the elements numerically,
and writes the result to standard output.

With option B<-c>, elements are sorted conventionally instead.
This means the elements are first sorted numerically and then
rotated cyclically so that the output starts with the elements
with difference one.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

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

=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
