Leaked source code of windows server 2003
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.

287 lines
7.5 KiB

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