Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naoya committed Sep 6, 2008
0 parents commit 21c49ff
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/Array/Gap.pm
@@ -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;
32 changes: 32 additions & 0 deletions lib/Array/Gap/Iterator.pm
@@ -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;
21 changes: 21 additions & 0 deletions sample.pl
@@ -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;
}


3 changes: 3 additions & 0 deletions t/00-load.t
@@ -0,0 +1,3 @@
use Test::More tests => 1;

BEGIN { use_ok('Array::Gap') }
32 changes: 32 additions & 0 deletions t/01-array.t
@@ -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;

0 comments on commit 21c49ff

Please sign in to comment.