blib/lib/XAS/Lib/Iterator.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 123 CC 14 SCAR 23.6
CC
14
Cov
0%
new
20.0
_bsearch
20.0
index
12.0
CRAP 10.6
1 package XAS::Lib::Iterator;
2  
3 our $VERSION = '0.01';
4  
5 use XAS::Class
6 debug => 0,
7 version => $VERSION,
8 base => 'XAS::Base',
9 utils => 'blessed dotid',
10 constants => 'ARRAY HASH DELIMITER CODE',
11 constant => {
12 SELF => 0,
13 DATA => 0,
14 SIZE => 1,
15 INDEX => 2,
16 },
17 ;
18  
19 #use Data::Dumper;
20  
21 # ----------------------------------------------------------------------
22 # Public Methods
23 # ----------------------------------------------------------------------
24  
25 sub new {
26 my $class = shift;
27 my $data = shift || [];
28  
29 my $ref = ref($data);
30  
31 if ($ref eq HASH) {
32  
33 $data = [
34 map {{ key => $_, value => $data->{$_} }} sort keys %$data
35 ];
36  
37 } elsif (blessed($data) && $data->can('as_list')) {
38  
39 $data = $data->as_list();
40  
41 } elsif ($ref ne ARRAY) {
42  
43 $data = [sort(split(DELIMITER, $data))];
44  
45 }
46  
47 return bless [$data, scalar @$data, -1], $class;
48  
49 }
50  
51 sub index {
52  
53 if (defined($_[1])) {
54  
55 if (($_[1] > 0) && ($_[1] <= $_[SELF]->[SIZE])) {
56  
57 $_[SELF]->[INDEX] = $_[1] - 1;
58 return 1;
59  
60 } else {
61  
62 return undef;
63  
64 }
65  
66 } else {
67  
68 return $_[SELF]->[INDEX];
69  
70 }
71  
72 }
73  
74 sub count {
75 $_[SELF]->[SIZE];
76 }
77  
78 sub first {
79 $_[SELF]->[INDEX] = 0;
80 }
81  
82 sub last {
83 $_[SELF]->[INDEX] = $_[SELF]->[SIZE] - 1;
84 }
85  
86 sub prev {
87 $_[SELF]->[INDEX] < 1
88 ? undef
89 : $_[SELF]->[DATA]->[ --$_[SELF]->[INDEX] ];
90 }
91  
92 sub next {
93 $_[SELF]->[INDEX] >= $_[SELF]->[SIZE]
94 ? undef
95 : $_[SELF]->[DATA]->[ ++$_[SELF]->[INDEX] ];
96 }
97  
98 sub find {
99  
100 my $left = 0;
101 my $right = $_[SELF]->[SIZE] - 1;
102 my $callback = $_[1] || undef;
103  
104 if (defined($callback) && (ref($callback) eq CODE)) {
105  
106 return _bsearch($_[SELF], $left, $right, $callback);
107  
108 } else {
109  
110 $_[SELF]->throw_msg(
111 dotid($_[SELF]->class) . '.find',
112 'invparams',
113 'parameter 1 needs to be a code reference',
114 );
115  
116 }
117  
118 }
119  
120 sub item {
121 $_[SELF]->[INDEX] >= 0 && $_[SELF]->[INDEX] < $_[SELF]->[SIZE]
122 ? $_[SELF]->[DATA]->[ $_[SELF]->[INDEX] ]
123 : undef;
124 }
125  
126 sub items {
127 my $self = shift;
128  
129 if ($self->[INDEX] < 0) {
130  
131 $self->[INDEX] = $self->[SIZE];
132 return $self->[DATA];
133  
134 } else {
135  
136 my $data = $self->[DATA];
137 my $slice = [
138 @$data[ $self->[INDEX] .. $self->[SIZE] - 1 ]
139 ];
140  
141 $self->[INDEX] = $self->[SIZE];
142 return $slice;
143  
144 }
145  
146 }
147  
148 # ----------------------------------------------------------------------
149 # Private Methods
150 # ----------------------------------------------------------------------
151  
152 sub _bsearch {
153 my $self = shift;
154 my $left = shift;
155 my $right = shift;
156 my $compare = shift;
157  
158 if ($right < $left) {
159  
160 return -1;
161  
162 }
163  
164 my $mid = ($left + $right) >> 1;
165 my $item = $self->[DATA]->[$mid];
166 my $x = $compare->($item);
167  
168 if ($x > 0) {
169  
170 return _bsearch($self, $mid + 1, $right, $compare);
171  
172 } elsif ($x < 0) {
173  
174 return _bsearch($self, $left, $mid - 1, $compare);
175  
176 } else {
177  
178 return $mid + 1;
179  
180 }
181  
182 }
183  
184 1;
185  
186 __END__
187  
188 =head1 NAME
189  
190 XAS::Lib::Iterator - A class for the XAS environment
191  
192 =head1 SYNOPSIS
193  
194 use XAS::Lib::Iterator;
195  
196 my @data = ['test1', 'test2', 'test3', 'test4'];
197
198 my $iterator = XAS::Lib::Iterator->new(@data);
199  
200 while (my $item = $iterator->next) {
201  
202 printf("item: %s\n", $item);
203  
204 }
205  
206 =head1 DESCRIPTION
207  
208 This is a general purpose iterator for an array of items. Once the items are
209 composed, you can then transverse over them and return items.
210  
211 =head1 METHODS
212  
213 =head2 new($items);
214  
215 Initialize the object and process the supplied items.
216  
217 =over 4
218  
219 =item B<$items>
220  
221 If the item is a blessed object with a as_list() method, then what is returned
222 from that call is used.
223  
224 If the item is a hash, it will be decomposed into an array of hashes
225 with the fields "key" and "value". Where "key" is the orignal key and "value"
226 is the original value. The items will be sorted by the key.
227  
228 And lastly, you can use a delimited string. The delimiter can be white space
229 or commas. This will be split into an array of sorted items.
230  
231 Sorting is done by Perls builtin sort function.
232  
233 =back
234  
235 =head2 count
236  
237 Return the number of items.
238  
239 =head2 first
240  
241 Set the index to the first item.
242  
243 =head2 last
244  
245 Set the index to the last item.
246  
247 =head2 index($position)
248  
249 Return or set the current position of the index.
250  
251 =over 4
252  
253 =item B<$position>
254  
255 The optional position within the index. This is ones based.
256  
257 =back
258  
259 =head2 find($callback)
260  
261 This method will find an item within the items. It will return the position
262 in the index or -1.
263  
264 =over 4
265  
266 =item B<$callback>
267  
268 The comparison routine. It is passed an item for comparision. The routine
269 should return the following:
270  
271 =over 4
272  
273 =item * 1 if the item is greater then what is wanted.
274  
275 =item * -1 if the item is lesser then what is wanted.
276  
277 =item * 0 if they match.
278  
279 =back
280  
281 =back
282  
283 =head2 next
284  
285 Return the next item and increment the index.
286  
287 =head2 prev
288  
289 Return the previous item and decrements the index.
290  
291 =head2 item
292  
293 Return the item at the current index position.
294  
295 =head2 items
296  
297 Return a slice of the items from the current index position to the end.
298  
299 =head1 SEE ALSO
300  
301 =over 4
302  
303 =item L<XAS|XAS>
304  
305 =back
306  
307 =head1 AUTHOR
308  
309 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
310  
311 =head1 COPYRIGHT AND LICENSE
312  
313 Copyright (c) 2015 Kevin L. Esteb
314  
315 This is free software; you can redistribute it and/or modify it under
316 the terms of the Artistic License 2.0. For details, see the full text
317 of the license at http://www.perlfoundation.org/artistic_license_2_0.
318  
319 =cut