Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
naoya
committed
Sep 6, 2008
0 parents
commit 21c49ff
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package Array::Gap; | ||
use strict; | ||
use warnings; | ||
use base qw/Class::Accessor::Lvalue::Fast/; | ||
|
||
use Array::Gap::Iterator; | ||
|
||
__PACKAGE__->mk_accessors(qw/array previous bin/); | ||
|
||
use overload | ||
'""' => 'as_string', | ||
'@{}' => 'as_array', | ||
fallback => 1; | ||
|
||
sub new { | ||
my ($class, $bin) = @_; | ||
my $self = bless $class->SUPER::new, $class; | ||
|
||
if ($bin) { | ||
$self->bin = $bin; | ||
$self->previous = $self->as_array->[-1]; ## hmm.. | ||
} else { | ||
$self->previous = 0; | ||
} | ||
|
||
return bless $self, $class; | ||
} | ||
|
||
sub push { | ||
my ($self, $n) = @_; | ||
|
||
$self->bin .= pack('w', $n - $self->previous); | ||
$self->previous = $n; | ||
|
||
return $n; | ||
} | ||
|
||
sub iterator { | ||
return Array::Gap::Iterator->new(shift->bin); | ||
} | ||
|
||
sub as_array { | ||
my @values; | ||
my $it = shift->iterator; | ||
while ($it->has_next) { | ||
CORE::push @values, $it->next; | ||
} | ||
return \@values; | ||
} | ||
|
||
sub as_string { | ||
return join ' ', unpack('B*', shift->bin); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package Array::Gap::Iterator; | ||
use strict; | ||
use warnings; | ||
use base qw/Class::Accessor::Lvalue::Fast/; | ||
|
||
__PACKAGE__->mk_accessors(qw/bin current/); | ||
|
||
sub new { | ||
my ($class, $bin) = @_; | ||
my $self = $class->SUPER::new({ | ||
bin => $bin, | ||
current => 0, | ||
}); | ||
} | ||
|
||
sub has_next { | ||
shift->bin ? 1 : 0; | ||
} | ||
|
||
sub next { | ||
my $self = shift; | ||
|
||
if (my $n = unpack('w', $self->bin)) { | ||
$self->bin = substr($self->bin, length pack('w', $n)); | ||
$self->current += $n; | ||
return $self->current; | ||
} | ||
|
||
return; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env/perl | ||
use strict; | ||
use warnings; | ||
|
||
use FindBin::libs; | ||
use Perl6::Say; | ||
use Array::Gap; | ||
|
||
my $array = Array::Gap->new; | ||
$array->push(824); | ||
$array->push(829); | ||
$array->push(830); | ||
$array->push(1024); | ||
$array->push(215406); | ||
|
||
my $it = $array->iterator; | ||
while ($it->has_next) { | ||
say $it->next; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use Test::More tests => 1; | ||
|
||
BEGIN { use_ok('Array::Gap') } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More qw/no_plan/; | ||
|
||
use Array::Gap; | ||
|
||
my $array = Array::Gap->new; | ||
ok defined $array; | ||
|
||
my @values = (824, 829, 830, 1024, 215406); | ||
|
||
$array->push($_) for @values; | ||
|
||
is '100001100011100000000101000000011000000101000010100011011000101001101110', $array->as_string; | ||
is_deeply \@values, $array->as_array; | ||
|
||
my @values_from_iter; | ||
my $it = $array->iterator; | ||
while ($it->has_next) { | ||
push @values_from_iter, $it->next; | ||
} | ||
|
||
is_deeply \@values, \@values_from_iter; | ||
|
||
my $bin = $array->bin; | ||
my $loaded = Array::Gap->new( $bin ); | ||
|
||
is_deeply \@values, $loaded->as_array; | ||
|
||
$loaded->push(215407); | ||
|
||
is_deeply [ @values, 215407 ], $loaded->as_array; |