1 package DBM::Deep::Array;
12 eval { tied( @{$_[0]} ) } || $_[0]
17 # Tied array constructor method, called by Perl's tie() function.
20 my $args = $class->_get_args( @_ );
22 $args->{type} = $class->TYPE_ARRAY;
24 return $class->_init($args);
28 my $self = $_[0]->_get_self;
31 if ( $key =~ /^-?\d+$/ ) {
33 $key += $self->FETCHSIZE;
34 return unless $key >= 0;
37 $key = pack($DBM::Deep::LONG_PACK, $key);
40 return $self->SUPER::FETCH( $key );
44 my $self = shift->_get_self;
45 my ($key, $value) = @_;
48 my $size = $self->FETCHSIZE;
51 if ( $key =~ /^-?\d+$/ ) {
56 die( "Modification of non-creatable array value attempted, subscript $orig" );
60 $key = pack($DBM::Deep::LONG_PACK, $key);
63 my $rv = $self->SUPER::STORE( $key, $value );
65 if ( $numeric_idx && $rv == 2 && $orig >= $size ) {
66 $self->STORESIZE( $orig + 1 );
73 my $self = $_[0]->_get_self;
76 if ( $key =~ /^-?\d+$/ ) {
78 $key += $self->FETCHSIZE;
79 return unless $key >= 0;
82 $key = pack($DBM::Deep::LONG_PACK, $key);
85 return $self->SUPER::EXISTS( $key );
90 # Return the length of the array
92 my $self = $_[0]->_get_self;
94 my $SAVE_FILTER = $self->root->{filter_fetch_value};
95 $self->root->{filter_fetch_value} = undef;
97 my $packed_size = $self->FETCH('length');
99 $self->root->{filter_fetch_value} = $SAVE_FILTER;
102 return int(unpack($DBM::Deep::LONG_PACK, $packed_size));
110 # Set the length of the array
112 my $self = $_[0]->_get_self;
113 my $new_length = $_[1];
115 my $SAVE_FILTER = $self->root->{filter_store_value};
116 $self->root->{filter_store_value} = undef;
118 my $result = $self->STORE('length', pack($DBM::Deep::LONG_PACK, $new_length));
120 $self->root->{filter_store_value} = $SAVE_FILTER;
127 # Remove and return the last element on the array
129 my $self = $_[0]->_get_self;
130 my $length = $self->FETCHSIZE();
133 my $content = $self->FETCH( $length - 1 );
134 $self->DELETE( $length - 1 );
144 # Add new element(s) to the end of the array
146 my $self = shift->_get_self;
147 my $length = $self->FETCHSIZE();
149 while (my $content = shift @_) {
150 $self->STORE( $length, $content );
159 # Remove and return first element on the array.
160 # Shift over remaining elements to take up space.
162 my $self = $_[0]->_get_self;
163 my $length = $self->FETCHSIZE();
166 my $content = $self->FETCH( 0 );
169 # Shift elements over and remove last one.
171 for (my $i = 0; $i < $length - 1; $i++) {
172 $self->STORE( $i, $self->FETCH($i + 1) );
174 $self->DELETE( $length - 1 );
185 # Insert new element(s) at beginning of array.
186 # Shift over other elements to make space.
188 my $self = shift->_get_self;
189 my @new_elements = @_;
190 my $length = $self->FETCHSIZE();
191 my $new_size = scalar @new_elements;
194 for (my $i = $length - 1; $i >= 0; $i--) {
195 $self->STORE( $i + $new_size, $self->FETCH($i) );
199 for (my $i = 0; $i < $new_size; $i++) {
200 $self->STORE( $i, $new_elements[$i] );
203 return $length + $new_size;
208 # Splices section of array with optional new section.
209 # Returns deleted section, or last element deleted in scalar context.
211 my $self = shift->_get_self;
212 my $length = $self->FETCHSIZE();
215 # Calculate offset and length of splice
217 my $offset = shift || 0;
218 if ($offset < 0) { $offset += $length; }
221 if (scalar @_) { $splice_length = shift; }
222 else { $splice_length = $length - $offset; }
223 if ($splice_length < 0) { $splice_length += ($length - $offset); }
226 # Setup array with new elements, and copy out old elements for return
228 my @new_elements = @_;
229 my $new_size = scalar @new_elements;
231 my @old_elements = ();
232 for (my $i = $offset; $i < $offset + $splice_length; $i++) {
233 push @old_elements, $self->FETCH( $i );
237 # Adjust array length, and shift elements to accomodate new section.
239 if ( $new_size != $splice_length ) {
240 if ($new_size > $splice_length) {
241 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
242 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
246 for (my $i = $offset + $splice_length; $i < $length; $i++) {
247 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
249 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
250 $self->DELETE( $length - 1 );
257 # Insert new elements into array
259 for (my $i = $offset; $i < $offset + $new_size; $i++) {
260 $self->STORE( $i, shift @new_elements );
264 # Return deleted section, or last element in scalar context.
266 return wantarray ? @old_elements : $old_elements[-1];
269 #XXX We don't need to define it.
270 #XXX It will be useful, though, when we split out HASH and ARRAY
273 # Perl will call EXTEND() when the array is likely to grow.
274 # We don't care, but include it for compatibility.
279 # Public method aliases
281 *length = *FETCHSIZE;