| 1 | package Perl::Structure::LinkedList; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Perl::Config; # don't use Perl::Types inside itself, in order to avoid circular includes | ||
| 5 | our $VERSION = 0.001_000; | ||
| 6 | |||
| 7 | # [[[ INCLUDES ]]] | ||
| 8 | # NEED FIX: this is also a valid Perl Class, but we can't inherit from Perl::Class due to circular references? | ||
| 9 | use Perl::Structure::Array; | ||
| 10 | |||
| 11 | package Perl::Structure::LinkedListReference; | ||
| 12 | use parent qw(Perl::Type::Modifier::Reference); | ||
| 13 | use Perl::Type::Modifier::Reference; | ||
| 14 | |||
| 15 | # linked lists are comprised of nodes | ||
| 16 | use Perl::Structure::LinkedList::Node; | ||
| 17 | |||
| 18 | our hashref $properties = | ||
| 19 | { | ||
| 20 | head => my Perl::Structure::LinkedList::NodeReference $TYPED_head = undef, # start with head = undef so we can test for empty list | ||
| 21 | }; | ||
| 22 | |||
| 23 | sub new_from_arrayref { | ||
| 24 | { my Perl::Structure::LinkedListReference $RETURN_TYPE }; | ||
| 25 | (my string $class, my arrayref $input) = @ARG; | ||
| 26 | # Perl::diag("in new_from_arrayref(), received \$class = '$class', and \$input =\n" . Dumper($input) . "\n"); | ||
| 27 | my unknown $output = $class->new(); | ||
| 28 | my integer $i; | ||
| 29 | for ($i = (scalar(@{$input}) - 1); $i >= 0; $i--) | ||
| 30 | { | ||
| 31 | linkedlist_unshift($output, $input->[$i]); | ||
| 32 | } | ||
| 33 | return $output; | ||
| 34 | } | ||
| 35 | |||
| 36 | # do not name just "unshift" to avoid confusion with Perl builtin | ||
| 37 | sub linkedlist_unshift { | ||
| 38 | { my void $RETURN_TYPE }; | ||
| 39 | (my Perl::Structure::LinkedListReference $list, my unknown $element) = @ARG; | ||
| 40 | my Perl::Structure::LinkedList::NodeReference $new_node = Perl::Structure::LinkedList::NodeReference->new(); | ||
| 41 | $new_node->{data} = $element; | ||
| 42 | $new_node->{next} = $list->{head}; | ||
| 43 | $list->{head} = $new_node; | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub DUMPER { { my string $RETURN_TYPE };(my Perl::Structure::LinkedListReference $data) = @ARG; return $data->{head}->DUMPER(); } | ||
| 48 | |||
| 49 | |||
| 50 | # [[[ LINKED LISTS ]]] | ||
| 51 | |||
| 52 | # ref to linked list | ||
| 53 | # DEV NOTE: we only provide data structure references, not the direct data structures themselves, | ||
| 54 | # because an Perl::Class is a blessed hash _reference_, and we are not natively implementing the data structures in C here; | ||
| 55 | # thus the slightly weird naming convention where some places have delimeters (:: or _) and some don't, | ||
| 56 | # I favored the consistency of user-side RPerl data type short-form package alias _ delimeter over the Perl system-side package name scope :: delimeter | ||
| 57 | package # hide from PAUSE indexing | ||
| 58 | linkedlistref; | ||
| 59 | use parent qw(Perl::Structure::LinkedListReference); | ||
| 60 | use Perl::Structure::LinkedList; | ||
| 61 | # TODO: check if these (and other) symbol copies can be shortened??? move integer import() subroutine to be automatically called by 'use' command? | ||
| 62 | our $properties = $properties; our $new_from_arrayref = $new_from_arrayref; our $linkedlist_unshift = $linkedlist_unshift; our $DUMPER = $DUMPER; | ||
| 63 | |||
| 64 | # [[[ INT LINKED LISTS ]]] | ||
| 65 | |||
| 66 | # (ref to linked list) of integers | ||
| 67 | package # hide from PAUSE indexing | ||
| 68 | linkedlistref::integer; | ||
| 69 | use parent qw(linkedlistref); | ||
| 70 | |||
| 71 | # NEED ADD: remaining sub-types | ||
| 72 | |||
| 73 | 1; # end of class |