Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.4 KiB

  1. package Tie::Array;
  2. use vars qw($VERSION);
  3. use strict;
  4. $VERSION = '1.00';
  5. # Pod documentation after __END__ below.
  6. sub DESTROY { }
  7. sub EXTEND { }
  8. sub UNSHIFT { shift->SPLICE(0,0,@_) }
  9. sub SHIFT { shift->SPLICE(0,1) }
  10. sub CLEAR { shift->STORESIZE(0) }
  11. sub PUSH
  12. {
  13. my $obj = shift;
  14. my $i = $obj->FETCHSIZE;
  15. $obj->STORE($i++, shift) while (@_);
  16. }
  17. sub POP
  18. {
  19. my $obj = shift;
  20. my $newsize = $obj->FETCHSIZE - 1;
  21. my $val;
  22. if ($newsize >= 0)
  23. {
  24. $val = $obj->FETCH($newsize);
  25. $obj->STORESIZE($newsize);
  26. }
  27. $val;
  28. }
  29. sub SPLICE
  30. {
  31. my $obj = shift;
  32. my $sz = $obj->FETCHSIZE;
  33. my $off = (@_) ? shift : 0;
  34. $off += $sz if ($off < 0);
  35. my $len = (@_) ? shift : $sz - $off;
  36. my @result;
  37. for (my $i = 0; $i < $len; $i++)
  38. {
  39. push(@result,$obj->FETCH($off+$i));
  40. }
  41. if (@_ > $len)
  42. {
  43. # Move items up to make room
  44. my $d = @_ - $len;
  45. my $e = $off+$len;
  46. $obj->EXTEND($sz+$d);
  47. for (my $i=$sz-1; $i >= $e; $i--)
  48. {
  49. my $val = $obj->FETCH($i);
  50. $obj->STORE($i+$d,$val);
  51. }
  52. }
  53. elsif (@_ < $len)
  54. {
  55. # Move items down to close the gap
  56. my $d = $len - @_;
  57. my $e = $off+$len;
  58. for (my $i=$off+$len; $i < $sz; $i++)
  59. {
  60. my $val = $obj->FETCH($i);
  61. $obj->STORE($i-$d,$val);
  62. }
  63. $obj->STORESIZE($sz-$d);
  64. }
  65. for (my $i=0; $i < @_; $i++)
  66. {
  67. $obj->STORE($off+$i,$_[$i]);
  68. }
  69. return @result;
  70. }
  71. package Tie::StdArray;
  72. use vars qw(@ISA);
  73. @ISA = 'Tie::Array';
  74. sub TIEARRAY { bless [], $_[0] }
  75. sub FETCHSIZE { scalar @{$_[0]} }
  76. sub STORESIZE { $#{$_[0]} = $_[1]-1 }
  77. sub STORE { $_[0]->[$_[1]] = $_[2] }
  78. sub FETCH { $_[0]->[$_[1]] }
  79. sub CLEAR { @{$_[0]} = () }
  80. sub POP { pop(@{$_[0]}) }
  81. sub PUSH { my $o = shift; push(@$o,@_) }
  82. sub SHIFT { shift(@{$_[0]}) }
  83. sub UNSHIFT { my $o = shift; unshift(@$o,@_) }
  84. sub SPLICE
  85. {
  86. my $ob = shift;
  87. my $sz = $ob->FETCHSIZE;
  88. my $off = @_ ? shift : 0;
  89. $off += $sz if $off < 0;
  90. my $len = @_ ? shift : $sz-$off;
  91. return splice(@$ob,$off,$len,@_);
  92. }
  93. 1;
  94. __END__
  95. =head1 NAME
  96. Tie::Array - base class for tied arrays
  97. =head1 SYNOPSIS
  98. package NewArray;
  99. use Tie::Array;
  100. @ISA = ('Tie::Array');
  101. # mandatory methods
  102. sub TIEARRAY { ... }
  103. sub FETCH { ... }
  104. sub FETCHSIZE { ... }
  105. sub STORE { ... } # mandatory if elements writeable
  106. sub STORESIZE { ... } # mandatory if elements can be added/deleted
  107. # optional methods - for efficiency
  108. sub CLEAR { ... }
  109. sub PUSH { ... }
  110. sub POP { ... }
  111. sub SHIFT { ... }
  112. sub UNSHIFT { ... }
  113. sub SPLICE { ... }
  114. sub EXTEND { ... }
  115. sub DESTROY { ... }
  116. package NewStdArray;
  117. use Tie::Array;
  118. @ISA = ('Tie::StdArray');
  119. # all methods provided by default
  120. package main;
  121. $object = tie @somearray,Tie::NewArray;
  122. $object = tie @somearray,Tie::StdArray;
  123. $object = tie @somearray,Tie::NewStdArray;
  124. =head1 DESCRIPTION
  125. This module provides methods for array-tying classes. See
  126. L<perltie> for a list of the functions required in order to tie an array
  127. to a package. The basic B<Tie::Array> package provides stub C<DELETE>
  128. and C<EXTEND> methods, and implementations of C<PUSH>, C<POP>, C<SHIFT>,
  129. C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>,
  130. C<FETCHSIZE>, C<STORESIZE>.
  131. The B<Tie::StdArray> package provides efficient methods required for tied arrays
  132. which are implemented as blessed references to an "inner" perl array.
  133. It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly
  134. like standard arrays, allowing for selective overloading of methods.
  135. For developers wishing to write their own tied arrays, the required methods
  136. are briefly defined below. See the L<perltie> section for more detailed
  137. descriptive, as well as example code:
  138. =over
  139. =item TIEARRAY classname, LIST
  140. The class method is invoked by the command C<tie @array, classname>. Associates
  141. an array instance with the specified class. C<LIST> would represent
  142. additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
  143. to complete the association. The method should return an object of a class which
  144. provides the methods below.
  145. =item STORE this, index, value
  146. Store datum I<value> into I<index> for the tied array associated with
  147. object I<this>. If this makes the array larger then
  148. class's mapping of C<undef> should be returned for new positions.
  149. =item FETCH this, index
  150. Retrieve the datum in I<index> for the tied array associated with
  151. object I<this>.
  152. =item FETCHSIZE this
  153. Returns the total number of items in the tied array associated with
  154. object I<this>. (Equivalent to C<scalar(@array)>).
  155. =item STORESIZE this, count
  156. Sets the total number of items in the tied array associated with
  157. object I<this> to be I<count>. If this makes the array larger then
  158. class's mapping of C<undef> should be returned for new positions.
  159. If the array becomes smaller then entries beyond count should be
  160. deleted.
  161. =item EXTEND this, count
  162. Informative call that array is likely to grow to have I<count> entries.
  163. Can be used to optimize allocation. This method need do nothing.
  164. =item CLEAR this
  165. Clear (remove, delete, ...) all values from the tied array associated with
  166. object I<this>.
  167. =item DESTROY this
  168. Normal object destructor method.
  169. =item PUSH this, LIST
  170. Append elements of LIST to the array.
  171. =item POP this
  172. Remove last element of the array and return it.
  173. =item SHIFT this
  174. Remove the first element of the array (shifting other elements down)
  175. and return it.
  176. =item UNSHIFT this, LIST
  177. Insert LIST elements at the beginning of the array, moving existing elements
  178. up to make room.
  179. =item SPLICE this, offset, length, LIST
  180. Perform the equivalent of C<splice> on the array.
  181. I<offset> is optional and defaults to zero, negative values count back
  182. from the end of the array.
  183. I<length> is optional and defaults to rest of the array.
  184. I<LIST> may be empty.
  185. Returns a list of the original I<length> elements at I<offset>.
  186. =back
  187. =head1 CAVEATS
  188. There is no support at present for tied @ISA. There is a potential conflict
  189. between magic entries needed to notice setting of @ISA, and those needed to
  190. implement 'tie'.
  191. Very little consideration has been given to the behaviour of tied arrays
  192. when C<$[> is not default value of zero.
  193. =head1 AUTHOR
  194. Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>
  195. =cut