1 package DBM::Deep::Array;
8 our $VERSION = '0.99_03';
10 # This is to allow DBM::Deep::Array to handle negative indices on
11 # its own. Otherwise, Perl would intercept the call to negative
12 # indices for us. This was causing bugs for negative index handling.
13 our $NEGATIVE_INDICES = 1;
20 eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
23 sub _repr { shift;[ @_ ] }
30 local $SIG{'__DIE__'};
31 $self->push( @$struct );
33 $self->_throw_error("Cannot import: type mismatch");
40 my $args = $class->_get_args( @_ );
42 $args->{type} = $class->TYPE_ARRAY;
44 return $class->_init($args);
48 my $self = shift->_get_self;
51 $self->lock( $self->LOCK_SH );
54 if ( $key =~ /^-?\d+$/ ) {
56 $key += $self->FETCHSIZE;
57 unless ( $key >= 0 ) {
68 my $rv = $self->SUPER::FETCH( $key, $orig_key );
76 my $self = shift->_get_self;
77 my ($key, $value) = @_;
79 $self->lock( $self->LOCK_EX );
83 if ( $key =~ /^\-?\d+$/ ) {
86 $size = $self->FETCHSIZE;
87 if ( $key + $size < 0 ) {
88 die( "Modification of non-creatable array value attempted, subscript $key" );
94 my $rv = $self->SUPER::STORE( $key, $value, ($key eq 'length' ? undef : $key) );
96 if ( $idx_is_numeric ) {
97 $size = $self->FETCHSIZE unless defined $size;
98 if ( $key >= $size ) {
99 $self->STORESIZE( $key + 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 ) {
124 my $rv = $self->SUPER::EXISTS( $key );
132 my $self = shift->_get_self;
135 $self->lock( $self->LOCK_EX );
137 my $size = $self->FETCHSIZE;
138 if ( $key =~ /^-?\d+$/ ) {
141 unless ( $key >= 0 ) {
148 my $rv = $self->SUPER::DELETE( $key );
150 if ($rv && $key == $size - 1) {
151 $self->STORESIZE( $key, ($key eq 'length' ? undef : $key) );
160 my $self = shift->_get_self;
162 $self->lock( $self->LOCK_SH );
164 my $SAVE_FILTER = $self->_storage->{filter_fetch_value};
165 $self->_storage->{filter_fetch_value} = undef;
167 my $packed_size = $self->FETCH('length');
169 $self->_storage->{filter_fetch_value} = $SAVE_FILTER;
174 return int(unpack($self->_engine->{long_pack}, $packed_size));
181 my $self = shift->_get_self;
182 my ($new_length) = @_;
184 $self->lock( $self->LOCK_EX );
186 my $SAVE_FILTER = $self->_storage->{filter_store_value};
187 $self->_storage->{filter_store_value} = undef;
189 my $result = $self->STORE('length', pack($self->_engine->{long_pack}, $new_length), 'length');
191 $self->_storage->{filter_store_value} = $SAVE_FILTER;
199 my $self = shift->_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 );
220 my $self = shift->_get_self;
222 $self->lock( $self->LOCK_EX );
224 my $length = $self->FETCHSIZE();
226 while (my $content = shift @_) {
227 $self->STORE( $length, $content );
237 my $self = shift->_get_self;
239 $self->lock( $self->LOCK_EX );
241 my $length = $self->FETCHSIZE();
244 my $content = $self->FETCH( 0 );
246 for (my $i = 0; $i < $length - 1; $i++) {
247 $self->STORE( $i, $self->FETCH($i + 1) );
249 $self->DELETE( $length - 1 );
262 my $self = shift->_get_self;
263 my @new_elements = @_;
265 $self->lock( $self->LOCK_EX );
267 my $length = $self->FETCHSIZE();
268 my $new_size = scalar @new_elements;
271 for (my $i = $length - 1; $i >= 0; $i--) {
272 $self->STORE( $i + $new_size, $self->FETCH($i) );
276 for (my $i = 0; $i < $new_size; $i++) {
277 $self->STORE( $i, $new_elements[$i] );
282 return $length + $new_size;
286 my $self = shift->_get_self;
288 $self->lock( $self->LOCK_EX );
290 my $length = $self->FETCHSIZE();
293 # Calculate offset and length of splice
296 $offset = 0 unless defined $offset;
297 if ($offset < 0) { $offset += $length; }
300 if (scalar @_) { $splice_length = shift; }
301 else { $splice_length = $length - $offset; }
302 if ($splice_length < 0) { $splice_length += ($length - $offset); }
305 # Setup array with new elements, and copy out old elements for return
307 my @new_elements = @_;
308 my $new_size = scalar @new_elements;
310 my @old_elements = map {
312 } $offset .. ($offset + $splice_length - 1);
315 # Adjust array length, and shift elements to accomodate new section.
317 if ( $new_size != $splice_length ) {
318 if ($new_size > $splice_length) {
319 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
320 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
324 for (my $i = $offset + $splice_length; $i < $length; $i++) {
325 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
327 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
328 $self->DELETE( $length - 1 );
335 # Insert new elements into array
337 for (my $i = $offset; $i < $offset + $new_size; $i++) {
338 $self->STORE( $i, shift @new_elements );
344 # Return deleted section, or last element in scalar context.
346 return wantarray ? @old_elements : $old_elements[-1];
349 # We don't need to define it, yet.
350 # It will be useful, though, when we split out HASH and ARRAY
353 # Perl will call EXTEND() when the array is likely to grow.
354 # We don't care, but include it because it gets called at times.
362 my $length = $self->length();
363 for (my $index = 0; $index < $length; $index++) {
364 my $value = $self->get($index);
365 $self->_copy_value( \$db_temp->[$index], $value );
372 # Public method aliases
374 sub length { (shift)->FETCHSIZE(@_) }
375 sub pop { (shift)->POP(@_) }
376 sub push { (shift)->PUSH(@_) }
377 sub unshift { (shift)->UNSHIFT(@_) }
378 sub splice { (shift)->SPLICE(@_) }
380 # This must be last otherwise we have to qualify all other calls to shift
381 # as calls to CORE::shift
382 sub shift { (CORE::shift)->SHIFT(@_) }