| 1 | use strict; use warnings; | ||
| 2 | package Lingy::HashSet; | ||
| 3 | |||
| 4 | use base 'Lingy::Class'; | ||
| 5 | |||
| 6 | use Lingy::Common; | ||
| 7 | |||
| 8 | use Hash::Ordered; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, $list) = @_; | ||
| 12 | tie my %hash, 'Hash::Ordered'; | ||
| 13 | for (my $i = 0; $i < @$list; $i++) { | ||
| 14 | my $val = $list->[$i]; | ||
| 15 | my $key = $class->_get_key($val); | ||
| 16 | delete $hash{$key} if exists $hash{$key}; | ||
| 17 | $hash{$key} = $val; | ||
| 18 | } | ||
| 19 | bless \%hash, $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub clone { | ||
| 23 | HASHSET->new([ %{$_[0]} ]); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _get_key { | ||
| 27 | my ($self, $key) = @_; | ||
| 28 | my $type = ref($key); | ||
| 29 | $type eq '' ? qq<$key> : | ||
| 30 | $type eq STRING ? qq<"$key> : | ||
| 31 | $type eq SYMBOL ? qq<$key > : | ||
| 32 | $type->isa(SCALARTYPE) ? qq<$key> : | ||
| 33 | ( # Quoted symbol: | ||
| 34 | $type eq LIST and | ||
| 35 | ref($key->[0]) eq SYMBOL and | ||
| 36 | ${$key->[0]} eq 'quote' and | ||
| 37 | ref($key->[1]) eq SYMBOL | ||
| 38 | ) ? ${$key->[1]} . ' ' : | ||
| 39 | err "Type '$type' not supported as a hash-map key"; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _to_seq { | ||
| 43 | my ($map) = @_; | ||
| 44 | return nil unless %$map; | ||
| 45 | LIST->new([ | ||
| 46 | map { | ||
| 47 | $map->{$_}; | ||
| 48 | } keys %{$_[0]} | ||
| 49 | ]); | ||
| 50 | } | ||
| 51 | |||
| 52 | 1; |