package SET;
# set operations on arrays

# usage: @result = SET::union(\@r1, \@r2);
sub orderedunion { # r1, r2
    my ($r1, $r2) = @_;
    my (@r3,%refs);
    foreach (@$r1, @$r2) {
	push(@r3,$_) if !defined $refs{$_};
	$refs{$_} = 1;
    }
    return @r3;
}

# all elements of r1 that aren't in r2
# usage: @result = SET::subtract(\@r1, \@r2);
sub subtract { # r1, r2
    my ($r1, $r2) = @_;
    my (@r3,%refs);
    %refs = map { $_, 1 } @$r2;
    @r3 = grep(!defined $refs{$_}, @$r1);
    return @r3;
}

1;
