in reply to Module for sets of strings, ordered, case-insensitive?

I'm assuming you don't need regex matching, just simple string equality and that the sequence is static and doesn't change after first init.

I'd combine two data structures an array (holding the sequence of $strings ) and a hash with lc($string) => arr_pos pairs ° which are initialized in the ->new constructor.

Then

Doesn't look that difficult to me...

Or am I missing something?

°) In case your strings are not unique, you'll need a HoA lc($string) => [ @arr_positions ] holding multiple positions. Of course ->after would need to check all previous positions then.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Module for sets of strings, ordered, case-insensitive?
by LanX (Saint) on Dec 27, 2020 at 02:01 UTC
    after coding I realized that you didn't ask to keep the last match position - attribute last_pos - as state information. Right?

    In that case this double data structure to grant you fast indexed access to sequences is overkill, and you should rather go with grandfathers design.

    use strict; use warnings; use Data::Dump qw/pp dd/; use 5.10.0; # ========= Tests my $set = Some::Module->new(qw(foo bar bat)); say $set->contains('fox'); # false say $set->contains('foo'); # true say $set->contains('FOO'); # true say $set->after('foo'); # 'bar' say $set->is_last('bar'); # false say $set->is_last('bat'); # true package Some::Module; use Data::Dump qw/pp dd/; sub new { my ( $class, @sequence ) = @_; my $pos = 0; my %positions = map { lc($_) => $pos++ } @sequence; my $count = @sequence; my $obj = { sequence => \@sequence, positions => \%positions, count => $count, last_pos => undef, }; bless $obj, $class; } sub contains { my ( $self, $key ) = @_; my $pos = $self->{positions}{lc($key)}; $self->{last_pos} = $pos; return defined $pos; } sub after { my ($self, $key) = @_; my $pos = $self->{positions}{lc($key)}; return $self->{sequence}[$pos+1]; } sub is_last { my ($self, $key) = @_; my $pos = $self->{positions}{lc($key)}; return $pos == $self->{count} -1; }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery