• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DrHyde / perl-modules-Data-CompactReadonly / 4283302153

pending completion
4283302153

Pull #4

github

GitHub
Merge a864d6171 into a0bdf9dc5
Pull Request #4: Bump cross-platform-actions/action from 0.3.1 to 0.10.0

481 of 485 relevant lines covered (99.18%)

15134.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/Data/CompactReadonly/V0/Array.pm
1
package Data::CompactReadonly::V0::Array;
2
our $VERSION = '0.1.0';
3

4
use warnings;
4✔
5
use strict;
4✔
6
use base qw(Data::CompactReadonly::V0::Collection Data::CompactReadonly::Array);
4✔
7

8
use Data::CompactReadonly::V0::TiedArray;
4✔
9

10
sub _init {
11
    my($class, %args) = @_;
51✔
12
    my($root, $offset) = @args{qw(root offset)};
51✔
13

14
    my $object = bless({
51✔
15
        root => $root,
16
        offset => $offset
17
    }, $class);
18

19
    if($root->_tied()) {
51✔
20
        tie my @array, 'Data::CompactReadonly::V0::TiedArray', $object;
20✔
21
        return \@array;
20✔
22
    } else {
23
        return $object;
31✔
24
    }
25
}
26

27
# write an Array to the file at the current offset
28
sub _create {
29
    my($class, %args) = @_;
21✔
30
    my $fh = $args{fh};
21✔
31
    $class->_stash_already_seen(%args);
21✔
32
    (my $scalar_type = $class) =~ s/Array/Scalar/;
21✔
33

34
    # node header
35
    print $fh $class->_type_byte_from_class().
36
              $scalar_type->_get_bytes_from_word(1 + $#{$args{data}});
21✔
37

38
    # empty pointer table
39
    my $table_start_ptr = tell($fh);
21✔
40
    print $fh "\x00" x $args{ptr_size} x (1 + $#{$args{data}});
21✔
41
    $class->_set_next_free_ptr(%args);
21✔
42

43
    # write a pointer to each item in turn, and if necessary also write
44
    # item, which can be of any type
45
    foreach my $index (0 .. $#{$args{data}}) {
21✔
46
        my $this_data = $args{data}->[$index];
90✔
47
        $class->_seek(%args, pointer => $table_start_ptr + $index * $args{ptr_size});
90✔
48
        if(my $ptr = $class->_get_already_seen(%args, data => $this_data)) {
90✔
49
            print $fh $class->_encode_ptr(%args, pointer => $ptr);
20✔
50
        } else {
51
            print $fh $class->_encode_ptr(%args, pointer => $class->_get_next_free_ptr(%args));
70✔
52
            $class->_seek(%args, pointer => $class->_get_next_free_ptr(%args));
70✔
53
            Data::CompactReadonly::V0::Node->_create(%args, data => $this_data);
69✔
54
        }
55
    }
56
}
57

58
sub exists {
59
    my($self, $element) = @_;
7✔
60
    eval { $self->element($element) };
7✔
61
    if($@ =~ /out of range/) {
7✔
62
        return 0;
3✔
63
    } elsif($@) {
64
        die($@);
2✔
65
    } else {
66
        return 1;
2✔
67
    }
68
}
69

70
sub element {
71
    my($self, $element) = @_;
134✔
72
    no warnings 'numeric';
4✔
73
    die("$self: Invalid element: $element: negative\n")
134✔
74
        if($element < 0);
75
    die("$self: Invalid element: $element: non-integer\n")
132✔
76
        if($element =~ /[^0-9]/);
77
    die("$self: Invalid element: $element: out of range\n")
129✔
78
        if($element > $self->count() - 1);
79

80
    $self->_seek($self->_offset() + $self->_scalar_type_bytes() + $element * $self->_ptr_size());
123✔
81
    my $ptr = $self->_decode_ptr(
123✔
82
        $self->_bytes_at_current_offset($self->_ptr_size())
83
    );
84
    $self->_seek($ptr);
123✔
85
    return $self->_node_at_current_offset();
123✔
86
}
87

88
sub indices {
89
    my $self = shift;
2✔
90
    
91
    return [] if($self->count() == 0);
2✔
92
    return [(0 .. $self->count() - 1)];
1✔
93
}
94

95
1;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc