Array::Circular - Provide an array data structure that can go around in circles.
Circular array, tracks how many times it's been round.
my $a = Array::Circular->new(qw/once upon a time there was/); my $current = $l->next; say "They are the same" if $current == $l->current; my $first = $l->previous; say "Also the same" if $first == $l->current; say "We went around the loop " . $l->loops . " times";
my $a = Array::Circular->new(qw/this is a test/);
my $element = $l->next; my $two_forward = $l->next(2);
my $element = $l->previous; my $two_back = $l->previous(2);
my $element = $l->current;
my $idx = $l->index; $idx = $l->index(3);
Gets or sets the current index. Always returns the value of the current index. Does not reset the loop counter. No validation is performed.
my $number_of_times_been_around = $l->loops $l->loops(-3);
The loops method keeps track of how many times the array has been around. Looping forwards increases the loop count. Looping back decreases it. Sending in a number will set the counter but beware, no validation is performed on set operations.
$l->reset
Resets the current index and loop count to 0.
my $internal_store = $l->me
This is the internal store that tracks the current state of the list. It's intended for internal use only.
Convenience method to return current value then proceed next. Same interface as next.
next
Convenience method to return current value then proceed previous. Same interface as previous.
previous
Peek n forward (defaults to 1). Take care if you're reliant on calculating the number of loops as part of this. This Returns the entry for each without updating the value of loops.
loops
Returns the number of elements in the list.
Some array modification operations are supported. For example splice, push and pop operations are untested. If you mutate the array you may consider using reset, loops and index to fix up the effects of your mutation.
reset
index
This module is implemented as an inside out object.
Array::Iterator::Circular provides similar functionality but it does not support previous. Array::Iterator contains a survey of various similar modules.
Not thread safe. See implementation of Hash::MultiValue for implementation of thread safety. Alternatively use Hash::Util::FieldHash or Hash::Util::FieldHash::Compat.
Copyright 2018 Kieren Diment zarquon@cpan.org. This software can be redistributed under the same terms as perl itself.
To install Array::Circular, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Array::Circular
CPAN shell
perl -MCPAN -e shell install Array::Circular
For more information on module installation, please visit the detailed CPAN module installation guide.