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]
19 sub _repr { shift;[ @_ ] }
26 local $SIG{'__DIE__'};
27 $self->push( @$struct );
29 $self->_throw_error("Cannot import: type mismatch");
36 # Tied array constructor method, called by Perl's tie() function.
39 my $args = $class->_get_args( @_ );
41 $args->{type} = $class->TYPE_ARRAY;
43 return $class->_init($args);
47 my $self = shift->_get_self;
50 $self->lock( $self->LOCK_SH );
52 if ( $key =~ /^-?\d+$/ ) {
54 $key += $self->FETCHSIZE;
55 unless ( $key >= 0 ) {
61 $key = pack($self->{engine}{long_pack}, $key);
64 my $rv = $self->SUPER::FETCH( $key );
72 my $self = shift->_get_self;
73 my ($key, $value) = @_;
75 $self->lock( $self->LOCK_EX );
81 if ( $key =~ /^\-?\d+$/ ) {
84 $size = $self->FETCHSIZE;
87 die( "Modification of non-creatable array value attempted, subscript $orig" );
91 $key = pack($self->{engine}{long_pack}, $key);
94 my $rv = $self->SUPER::STORE( $key, $value );
96 if ( $numeric_idx && $rv == 2 ) {
97 $size = $self->FETCHSIZE unless defined $size;
98 if ( $orig >= $size ) {
99 $self->STORESIZE( $orig + 1 );
109 my $self = shift->_get_self;
112 $self->lock( $self->LOCK_SH );
114 if ( $key =~ /^\-?\d+$/ ) {
116 $key += $self->FETCHSIZE;
117 unless ( $key >= 0 ) {
123 $key = pack($self->{engine}{long_pack}, $key);
126 my $rv = $self->SUPER::EXISTS( $key );
134 my $self = shift->_get_self;
137 my $unpacked_key = $key;
139 $self->lock( $self->LOCK_EX );
141 my $size = $self->FETCHSIZE;
142 if ( $key =~ /^-?\d+$/ ) {
145 unless ( $key >= 0 ) {
151 $key = pack($self->{engine}{long_pack}, $key);
154 my $rv = $self->SUPER::DELETE( $key );
156 if ($rv && $unpacked_key == $size - 1) {
157 $self->STORESIZE( $unpacked_key );
167 # Return the length of the array
169 my $self = shift->_get_self;
171 $self->lock( $self->LOCK_SH );
173 my $SAVE_FILTER = $self->_root->{filter_fetch_value};
174 $self->_root->{filter_fetch_value} = undef;
176 my $packed_size = $self->FETCH('length');
178 $self->_root->{filter_fetch_value} = $SAVE_FILTER;
183 return int(unpack($self->{engine}{long_pack}, $packed_size));
191 # Set the length of the array
193 my $self = shift->_get_self;
194 my ($new_length) = @_;
196 $self->lock( $self->LOCK_EX );
198 my $SAVE_FILTER = $self->_root->{filter_store_value};
199 $self->_root->{filter_store_value} = undef;
201 my $result = $self->STORE('length', pack($self->{engine}{long_pack}, $new_length));
203 $self->_root->{filter_store_value} = $SAVE_FILTER;
212 # Remove and return the last element on the array
214 my $self = shift->_get_self;
216 $self->lock( $self->LOCK_EX );
218 my $length = $self->FETCHSIZE();
221 my $content = $self->FETCH( $length - 1 );
222 $self->DELETE( $length - 1 );
236 # Add new element(s) to the end of the array
238 my $self = shift->_get_self;
240 $self->lock( $self->LOCK_EX );
242 my $length = $self->FETCHSIZE();
244 while (my $content = shift @_) {
245 $self->STORE( $length, $content );
256 # Remove and return first element on the array.
257 # Shift over remaining elements to take up space.
259 my $self = shift->_get_self;
261 $self->lock( $self->LOCK_EX );
263 my $length = $self->FETCHSIZE();
266 my $content = $self->FETCH( 0 );
269 # Shift elements over and remove last one.
271 for (my $i = 0; $i < $length - 1; $i++) {
272 $self->STORE( $i, $self->FETCH($i + 1) );
274 $self->DELETE( $length - 1 );
288 # Insert new element(s) at beginning of array.
289 # Shift over other elements to make space.
291 my $self = shift->_get_self;
292 my @new_elements = @_;
294 $self->lock( $self->LOCK_EX );
296 my $length = $self->FETCHSIZE();
297 my $new_size = scalar @new_elements;
300 for (my $i = $length - 1; $i >= 0; $i--) {
301 $self->STORE( $i + $new_size, $self->FETCH($i) );
305 for (my $i = 0; $i < $new_size; $i++) {
306 $self->STORE( $i, $new_elements[$i] );
311 return $length + $new_size;
316 # Splices section of array with optional new section.
317 # Returns deleted section, or last element deleted in scalar context.
319 my $self = shift->_get_self;
321 $self->lock( $self->LOCK_EX );
323 my $length = $self->FETCHSIZE();
326 # Calculate offset and length of splice
329 $offset = 0 unless defined $offset;
330 if ($offset < 0) { $offset += $length; }
333 if (scalar @_) { $splice_length = shift; }
334 else { $splice_length = $length - $offset; }
335 if ($splice_length < 0) { $splice_length += ($length - $offset); }
338 # Setup array with new elements, and copy out old elements for return
340 my @new_elements = @_;
341 my $new_size = scalar @new_elements;
343 my @old_elements = map {
345 } $offset .. ($offset + $splice_length - 1);
348 # Adjust array length, and shift elements to accomodate new section.
350 if ( $new_size != $splice_length ) {
351 if ($new_size > $splice_length) {
352 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
353 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
357 for (my $i = $offset + $splice_length; $i < $length; $i++) {
358 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
360 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
361 $self->DELETE( $length - 1 );
368 # Insert new elements into array
370 for (my $i = $offset; $i < $offset + $new_size; $i++) {
371 $self->STORE( $i, shift @new_elements );
377 # Return deleted section, or last element in scalar context.
379 return wantarray ? @old_elements : $old_elements[-1];
382 #XXX We don't need to define it, yet.
383 #XXX It will be useful, though, when we split out HASH and ARRAY
386 # Perl will call EXTEND() when the array is likely to grow.
387 # We don't care, but include it for compatibility.
395 my $length = $self->length();
396 for (my $index = 0; $index < $length; $index++) {
397 my $value = $self->get($index);
398 $self->_copy_value( \$db_temp->[$index], $value );
405 # Public method aliases
407 sub length { (shift)->FETCHSIZE(@_) }
408 sub pop { (shift)->POP(@_) }
409 sub push { (shift)->PUSH(@_) }
410 sub unshift { (shift)->UNSHIFT(@_) }
411 sub splice { (shift)->SPLICE(@_) }
413 # This must be last otherwise we have to qualify all other calls to shift
414 # as calls to CORE::shift
415 sub shift { (CORE::shift)->SHIFT(@_) }