1 package DBM::Deep::Array;
8 eval { tied( @{$_[0]} ) } || $_[0]
13 # Tied array constructor method, called by Perl's tie() function.
19 $class->_throw_error( "Odd number of parameters to TIEARRAY" );
23 #XXX This use of ref() is bad and is a bug
24 elsif (ref($_[0])) { $args = $_[0]; }
25 else { $args = { file => shift }; }
27 $args->{type} = $class->TYPE_ARRAY;
29 return $class->_init($args);
33 # The following methods are for arrays only
38 # Return the length of the array
40 my $self = $_[0]->_get_self;
42 my $SAVE_FILTER = $self->root->{filter_fetch_value};
43 $self->root->{filter_fetch_value} = undef;
45 my $packed_size = $self->FETCH('length');
47 $self->root->{filter_fetch_value} = $SAVE_FILTER;
49 if ($packed_size) { return int(unpack($DBM::Deep::LONG_PACK, $packed_size)); }
55 # Set the length of the array
57 my $self = $_[0]->_get_self;
58 my $new_length = $_[1];
60 my $SAVE_FILTER = $self->root->{filter_store_value};
61 $self->root->{filter_store_value} = undef;
63 my $result = $self->STORE('length', pack($DBM::Deep::LONG_PACK, $new_length));
65 $self->root->{filter_store_value} = $SAVE_FILTER;
72 # Remove and return the last element on the array
74 my $self = $_[0]->_get_self;
75 my $length = $self->FETCHSIZE();
78 my $content = $self->FETCH( $length - 1 );
79 $self->DELETE( $length - 1 );
89 # Add new element(s) to the end of the array
91 my $self = shift->_get_self;
92 my $length = $self->FETCHSIZE();
94 while (my $content = shift @_) {
95 $self->STORE( $length, $content );
104 # Remove and return first element on the array.
105 # Shift over remaining elements to take up space.
107 my $self = $_[0]->_get_self;
108 my $length = $self->FETCHSIZE();
111 my $content = $self->FETCH( 0 );
114 # Shift elements over and remove last one.
116 for (my $i = 0; $i < $length - 1; $i++) {
117 $self->STORE( $i, $self->FETCH($i + 1) );
119 $self->DELETE( $length - 1 );
130 # Insert new element(s) at beginning of array.
131 # Shift over other elements to make space.
133 my $self = shift->_get_self;
134 my @new_elements = @_;
135 my $length = $self->FETCHSIZE();
136 my $new_size = scalar @new_elements;
139 for (my $i = $length - 1; $i >= 0; $i--) {
140 $self->STORE( $i + $new_size, $self->FETCH($i) );
144 for (my $i = 0; $i < $new_size; $i++) {
145 $self->STORE( $i, $new_elements[$i] );
148 return $length + $new_size;
153 # Splices section of array with optional new section.
154 # Returns deleted section, or last element deleted in scalar context.
156 my $self = shift->_get_self;
157 my $length = $self->FETCHSIZE();
160 # Calculate offset and length of splice
162 my $offset = shift || 0;
163 if ($offset < 0) { $offset += $length; }
166 if (scalar @_) { $splice_length = shift; }
167 else { $splice_length = $length - $offset; }
168 if ($splice_length < 0) { $splice_length += ($length - $offset); }
171 # Setup array with new elements, and copy out old elements for return
173 my @new_elements = @_;
174 my $new_size = scalar @new_elements;
176 my @old_elements = ();
177 for (my $i = $offset; $i < $offset + $splice_length; $i++) {
178 push @old_elements, $self->FETCH( $i );
182 # Adjust array length, and shift elements to accomodate new section.
184 if ( $new_size != $splice_length ) {
185 if ($new_size > $splice_length) {
186 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
187 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
191 for (my $i = $offset + $splice_length; $i < $length; $i++) {
192 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
194 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
195 $self->DELETE( $length - 1 );
202 # Insert new elements into array
204 for (my $i = $offset; $i < $offset + $new_size; $i++) {
205 $self->STORE( $i, shift @new_elements );
209 # Return deleted section, or last element in scalar context.
211 return wantarray ? @old_elements : $old_elements[-1];
214 #XXX We don't need to define it.
215 #XXX It will be useful, though, when we split out HASH and ARRAY
218 # Perl will call EXTEND() when the array is likely to grow.
219 # We don't care, but include it for compatibility.
224 # Public method aliases
226 *length = *FETCHSIZE;