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 { local $SIG{'__DIE__'}; 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 );
66 if ( $key =~ /^\-?\d+$/ ) {
69 $size = $self->FETCHSIZE;
72 die( "Modification of non-creatable array value attempted, subscript $orig" );
76 $key = pack($DBM::Deep::LONG_PACK, $key);
79 my $rv = $self->SUPER::STORE( $key, $value );
81 if ( $numeric_idx && $rv == 2 ) {
82 $size = $self->FETCHSIZE unless defined $size;
83 if ( $orig >= $size ) {
84 $self->STORESIZE( $orig + 1 );
94 my $self = $_[0]->_get_self;
97 $self->lock( $self->LOCK_SH );
99 if ( $key =~ /^\-?\d+$/ ) {
101 $key += $self->FETCHSIZE;
102 unless ( $key >= 0 ) {
108 $key = pack($DBM::Deep::LONG_PACK, $key);
111 my $rv = $self->SUPER::EXISTS( $key );
119 my $self = $_[0]->_get_self;
122 my $unpacked_key = $key;
124 $self->lock( $self->LOCK_EX );
126 my $size = $self->FETCHSIZE;
127 if ( $key =~ /^-?\d+$/ ) {
130 unless ( $key >= 0 ) {
136 $key = pack($DBM::Deep::LONG_PACK, $key);
139 my $rv = $self->SUPER::DELETE( $key );
141 if ($rv && $unpacked_key == $size - 1) {
142 $self->STORESIZE( $unpacked_key );
152 # Return the length of the array
154 my $self = shift->_get_self;
156 $self->lock( $self->LOCK_SH );
158 my $SAVE_FILTER = $self->root->{filter_fetch_value};
159 $self->root->{filter_fetch_value} = undef;
161 my $packed_size = $self->FETCH('length');
163 $self->root->{filter_fetch_value} = $SAVE_FILTER;
168 return int(unpack($DBM::Deep::LONG_PACK, $packed_size));
176 # Set the length of the array
178 my $self = $_[0]->_get_self;
179 my $new_length = $_[1];
181 $self->lock( $self->LOCK_EX );
183 my $SAVE_FILTER = $self->root->{filter_store_value};
184 $self->root->{filter_store_value} = undef;
186 my $result = $self->STORE('length', pack($DBM::Deep::LONG_PACK, $new_length));
188 $self->root->{filter_store_value} = $SAVE_FILTER;
197 # Remove and return the last element on the array
199 my $self = $_[0]->_get_self;
201 $self->lock( $self->LOCK_EX );
203 my $length = $self->FETCHSIZE();
206 my $content = $self->FETCH( $length - 1 );
207 $self->DELETE( $length - 1 );
221 # Add new element(s) to the end of the array
223 my $self = shift->_get_self;
225 $self->lock( $self->LOCK_EX );
227 my $length = $self->FETCHSIZE();
229 while (my $content = shift @_) {
230 $self->STORE( $length, $content );
241 # Remove and return first element on the array.
242 # Shift over remaining elements to take up space.
244 my $self = $_[0]->_get_self;
246 $self->lock( $self->LOCK_EX );
248 my $length = $self->FETCHSIZE();
251 my $content = $self->FETCH( 0 );
254 # Shift elements over and remove last one.
256 for (my $i = 0; $i < $length - 1; $i++) {
257 $self->STORE( $i, $self->FETCH($i + 1) );
259 $self->DELETE( $length - 1 );
273 # Insert new element(s) at beginning of array.
274 # Shift over other elements to make space.
276 my $self = shift->_get_self;
277 my @new_elements = @_;
279 $self->lock( $self->LOCK_EX );
281 my $length = $self->FETCHSIZE();
282 my $new_size = scalar @new_elements;
285 for (my $i = $length - 1; $i >= 0; $i--) {
286 $self->STORE( $i + $new_size, $self->FETCH($i) );
290 for (my $i = 0; $i < $new_size; $i++) {
291 $self->STORE( $i, $new_elements[$i] );
296 return $length + $new_size;
301 # Splices section of array with optional new section.
302 # Returns deleted section, or last element deleted in scalar context.
304 my $self = shift->_get_self;
306 $self->lock( $self->LOCK_EX );
308 my $length = $self->FETCHSIZE();
311 # Calculate offset and length of splice
313 my $offset = shift || 0;
314 if ($offset < 0) { $offset += $length; }
317 if (scalar @_) { $splice_length = shift; }
318 else { $splice_length = $length - $offset; }
319 if ($splice_length < 0) { $splice_length += ($length - $offset); }
322 # Setup array with new elements, and copy out old elements for return
324 my @new_elements = @_;
325 my $new_size = scalar @new_elements;
327 my @old_elements = map {
329 } $offset .. ($offset + $splice_length - 1);
332 # Adjust array length, and shift elements to accomodate new section.
334 if ( $new_size != $splice_length ) {
335 if ($new_size > $splice_length) {
336 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
337 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
341 for (my $i = $offset + $splice_length; $i < $length; $i++) {
342 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
344 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
345 $self->DELETE( $length - 1 );
352 # Insert new elements into array
354 for (my $i = $offset; $i < $offset + $new_size; $i++) {
355 $self->STORE( $i, shift @new_elements );
361 # Return deleted section, or last element in scalar context.
363 return wantarray ? @old_elements : $old_elements[-1];
366 #XXX We don't need to define it, yet.
367 #XXX It will be useful, though, when we split out HASH and ARRAY
370 # Perl will call EXTEND() when the array is likely to grow.
371 # We don't care, but include it for compatibility.
376 # Public method aliases
378 *length = *FETCHSIZE;