NAME
List::DoubleLinked - Double Linked Lists for Perl
VERSION
version 0.005
SYNOPSIS
use List::DoubleLinked;
my $list = List::DoubleLinked->new(qw/foo bar baz/);
$list->begin->insert_after(qw/quz/);
$list->erase($list->end->previous);
DESCRIPTION
This module provides a double linked list for Perl. You should ordinarily use arrays instead of this, they are faster for almost any usage. However there is a small set of use-cases where linked lists are necessary. While you can use the list as an object directly, for most purposes it's recommended to use iterators. begin()
and end()
will give you iterators pointing at the start and end of the list.
METHODS
new(@elements)
Create a new double linked list. @elements is pushed to the list.
begin()
Return an iterator to the first element of the list.
end()
Return an iterator to the last element of the list.
flatten()
Return an array containing the same values as the list does. This runs in linear time.
push(@elements)
Add @elements to the end of the list.
pop()
Remove an element from the end of the list and return it
unshift(@elements)
Add @elements to the start of the list.
shift()
Remove an element from the end of the list and return it
front()
Return the first element in the list
back()
Return the last element in the list.
empty()
Returns true if the list has no elements in it, returns false otherwise.
size()
Return the length of the list. This runs in linear time.
erase($iterator)
Remove the element under $iterator. Note that this invalidates $iterator
, therefore it returns the next iterator.
WTF WHERE YOU THINKING?
This module is a rather an exercise in C programming. I was surprised that I was ever going to need this (and even more surprised no one ever uploaded something like this to CPAN before), but I needed a data structure that provided me with stable iterators. I need to be able to splice off any arbitrary element without affecting any other arbitrary element. You can't really implement that using arrays, you need a double linked list for that.
This module is aiming for correctness, it is not optimized for speed. Linked lists in Perl are practically never faster than arrays anyways, if you're looking at this because you think it will be faster please think again. splice is your friend.
AUTHOR
Leon Timmermans <fawaka@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Leon Timmermans.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.