#
# This software is Copyright 2005 by Elsevier Inc.  You may use it
# under the terms of the license at http://perl.plover.com/hop/LICENSE.txt .
#



###
### partition-iterator
###

## Chapter 5 section 1.1.1

sub partition {
  my ($target, $treasures) = @_;
  return list_iterator([]) if $target == 0;
  return list_iterator(  ) if $target < 0 || @$treasures == 0;

  my ($first, @rest) = @$treasures;
  my $which_partition_iterator = 0;
  flatten( Iterator  {
    $which_partition_iterator++;
    if ($which_partition_iterator == 1) {
      return imap {[$first, @$_]} partition($target-$first, \@rest);
    } elsif ($which_partition_iterator == 2) {
      return                      partition($target,        \@rest);
    } else {
      return;
    }
  });
}
