Re: [code-review] Tie::Array::Sorted
JS Bangs <[email protected]> Wed, 12 Nov 2003 13:31:34 -0800 (PST)
| Newsgroups | gmane.comp.lang.perl.code-review-ladder |
|---|---|
| Message-ID | <[email protected]> |
Mark Dominus sikyal:
> I would guess that an implementation like the following would probably
> be much faster than the current implementation, and also faster than
> Tony's idea:
>
> sub PUSH {
> my ($self, @items) = @_;
> push @{$self->{array}}, @items;
> undef $self->{sorted};
> }
>
> sub FETCH {
> my ($self, $key) = @_;
> $self->sortme unless $self->{sorted};
> $self->{array}[$key];
> }
>
> sub sortme {
> my $self = shift;
> my $c = $self->{comparator};
> @{$self->{array}} = sort $c @{$self->{array}};
> $self->{sorted} = 1;
> }
I definitely second this motion. It's hard to see when it wouldn't be
faster in any normal situation, and it wins in terms of code size as well
as elegance. I would have suggested this myself if Mark hadn't beat me to
it.
Also, in the POD:
> Direct stores (C<$a[10] = "wibble">) effectively splice out the original
> value and insert the new element. It's not clear why you'd want to use
> direct stores like that, but this module does the right thing if you do.
I didn't really get what you meant by this until I read the source code.
Perhaps I'm just slow, or perhaps you should make it clearer. Consider
somethig like "C<$a[10] = "wibble"> effectively splices out the current
$a[10] and then inserts "wibble" into the correct place." Hmm, not much
different, but it would have helped me out.
--
Jesse S. Bangs [email protected]
http://blog.glossopoesis.org
"We're counting on our virtues,
Cause it's too hard to count the dead."
- Jason Webley