1 package DBM::Deep::Array;
5 # This is to allow DBM::Deep::Array to handle negative indices on
6 # its own. Otherwise, Perl would intercept the call to negative
7 # indices for us. This was causing bugs for negative index handling.
8 use vars qw( $NEGATIVE_INDICES );
16 eval { tied( @{$_[0]} ) } || $_[0]
21 # Tied array constructor method, called by Perl's tie() function.
24 my $args = $class->_get_args( @_ );
26 $args->{type} = $class->TYPE_ARRAY;
28 return $class->_init($args);
32 my $self = $_[0]->_get_self;
35 $self->lock( $self->LOCK_SH );
37 if ( $key =~ /^-?\d+$/ ) {
39 $key += $self->FETCHSIZE;
40 unless ( $key >= 0 ) {
46 $key = pack($DBM::Deep::LONG_PACK, $key);
49 my $rv = $self->SUPER::FETCH( $key );
57 my $self = shift->_get_self;
58 my ($key, $value) = @_;
60 $self->lock( $self->LOCK_EX );
63 my $size = $self->FETCHSIZE;
66 if ( $key =~ /^-?\d+$/ ) {
71 die( "Modification of non-creatable array value attempted, subscript $orig" );
75 $key = pack($DBM::Deep::LONG_PACK, $key);
78 my $rv = $self->SUPER::STORE( $key, $value );
80 if ( $numeric_idx && $rv == 2 && $orig >= $size ) {
81 $self->STORESIZE( $orig + 1 );
90 my $self = $_[0]->_get_self;
93 $self->lock( $self->LOCK_SH );
95 if ( $key =~ /^-?\d+$/ ) {
97 $key += $self->FETCHSIZE;
98 unless ( $key >= 0 ) {
104 $key = pack($DBM::Deep::LONG_PACK, $key);
107 my $rv = $self->SUPER::EXISTS( $key );
115 my $self = $_[0]->_get_self;
118 my $unpacked_key = $key;
120 $self->lock( $self->LOCK_EX );
122 my $size = $self->FETCHSIZE;
123 if ( $key =~ /^-?\d+$/ ) {
126 unless ( $key >= 0 ) {
132 $key = pack($DBM::Deep::LONG_PACK, $key);
135 my $rv = $self->SUPER::DELETE( $key );
137 if ($rv && $unpacked_key == $size - 1) {
138 $self->STORESIZE( $unpacked_key );
148 # Return the length of the array
150 my $self = shift->_get_self;
152 $self->lock( $self->LOCK_SH );
154 my $SAVE_FILTER = $self->root->{filter_fetch_value};
155 $self->root->{filter_fetch_value} = undef;
157 my $packed_size = $self->FETCH('length');
159 $self->root->{filter_fetch_value} = $SAVE_FILTER;
164 return int(unpack($DBM::Deep::LONG_PACK, $packed_size));
172 # Set the length of the array
174 my $self = $_[0]->_get_self;
175 my $new_length = $_[1];
177 $self->lock( $self->LOCK_EX );
179 my $SAVE_FILTER = $self->root->{filter_store_value};
180 $self->root->{filter_store_value} = undef;
182 my $result = $self->STORE('length', pack($DBM::Deep::LONG_PACK, $new_length));
184 $self->root->{filter_store_value} = $SAVE_FILTER;
193 # Remove and return the last element on the array
195 my $self = $_[0]->_get_self;
197 $self->lock( $self->LOCK_EX );
199 my $length = $self->FETCHSIZE();
202 my $content = $self->FETCH( $length - 1 );
203 $self->DELETE( $length - 1 );
217 # Add new element(s) to the end of the array
219 my $self = shift->_get_self;
221 $self->lock( $self->LOCK_EX );
223 my $length = $self->FETCHSIZE();
225 while (my $content = shift @_) {
226 $self->STORE( $length, $content );
237 # Remove and return first element on the array.
238 # Shift over remaining elements to take up space.
240 my $self = $_[0]->_get_self;
242 $self->lock( $self->LOCK_EX );
244 my $length = $self->FETCHSIZE();
247 my $content = $self->FETCH( 0 );
250 # Shift elements over and remove last one.
252 for (my $i = 0; $i < $length - 1; $i++) {
253 $self->STORE( $i, $self->FETCH($i + 1) );
255 $self->DELETE( $length - 1 );
269 # Insert new element(s) at beginning of array.
270 # Shift over other elements to make space.
272 my $self = shift->_get_self;
273 my @new_elements = @_;
275 $self->lock( $self->LOCK_EX );
277 my $length = $self->FETCHSIZE();
278 my $new_size = scalar @new_elements;
281 for (my $i = $length - 1; $i >= 0; $i--) {
282 $self->STORE( $i + $new_size, $self->FETCH($i) );
286 for (my $i = 0; $i < $new_size; $i++) {
287 $self->STORE( $i, $new_elements[$i] );
292 return $length + $new_size;
297 # Splices section of array with optional new section.
298 # Returns deleted section, or last element deleted in scalar context.
300 my $self = shift->_get_self;
302 $self->lock( $self->LOCK_EX );
304 my $length = $self->FETCHSIZE();
307 # Calculate offset and length of splice
309 my $offset = shift || 0;
310 if ($offset < 0) { $offset += $length; }
313 if (scalar @_) { $splice_length = shift; }
314 else { $splice_length = $length - $offset; }
315 if ($splice_length < 0) { $splice_length += ($length - $offset); }
318 # Setup array with new elements, and copy out old elements for return
320 my @new_elements = @_;
321 my $new_size = scalar @new_elements;
323 my @old_elements = ();
324 for (my $i = $offset; $i < $offset + $splice_length; $i++) {
325 push @old_elements, $self->FETCH( $i );
329 # Adjust array length, and shift elements to accomodate new section.
331 if ( $new_size != $splice_length ) {
332 if ($new_size > $splice_length) {
333 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
334 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
338 for (my $i = $offset + $splice_length; $i < $length; $i++) {
339 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
341 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
342 $self->DELETE( $length - 1 );
349 # Insert new elements into array
351 for (my $i = $offset; $i < $offset + $new_size; $i++) {
352 $self->STORE( $i, shift @new_elements );
358 # Return deleted section, or last element in scalar context.
360 return wantarray ? @old_elements : $old_elements[-1];
363 #XXX We don't need to define it, yet.
364 #XXX It will be useful, though, when we split out HASH and ARRAY
367 # Perl will call EXTEND() when the array is likely to grow.
368 # We don't care, but include it for compatibility.
373 # Public method aliases
375 *length = *FETCHSIZE;