=pod =head1 NAME Class::STL::Containers - Perl extension for STL-like object management =head1 SYNOPSIS use stl; # Deque container... my $d = stl::deque(qw(first second third fourth)); $d->push_back($d->factory('fifth')); $d->push_front($d->factory('seventh')); $d->pop_front(); # remove element at front. $d->pop_back(); # remove element at back. stl::for_each($d->begin(), $d->end(), ptr_fun('::myprint')); sub myprint { print "Data:", @_, "\n"; } # Copy constructor... my $d_copy = stl::deque($d); # Algorithms -- find_if() print "Element 'second' was ", stl::find_if($d->begin(), $d->end(), stl::bind1st(stl::equal_to(), 'second')) ? 'found' : 'not found', "\n"; # Algorithms -- count_if() print "Number of elements matching /o/ = ", stl::count_if($d->begin(), $d->end(), stl::bind2nd(stl::matches(), 'o')), "\n"; # prints '2' -- matches 'second' and 'fourth' # Algorithms -- transform() stl::transform($d->begin(), $d->end(), $d2->begin(), stl::ptr_fun('ucfirst')); stl::transform($d->begin(), $d->end(), $d2->begin(), $d3->begin(), stl::ptr_fun_binary('::mybfun')); sub mybfun { return $_[0] . '-' . $_[1]; } # Function Adaptors -- bind1st stl::remove_if($v->begin(), $v->end(), stl::bind1st(stl::equal_to(), $v->back())); # remove element equal to back() -- ie remove last element. stl::remove_if($v->begin(), $v->end(), stl::bind2nd(stl::matches(), '^fi')); # remove all elements that match reg-ex '^fi' # Sort list according to elements cmp() function $v->sort(); # Queue containers -- FIFO my $v = stl::queue(qw(first second third fourth fifth)); print 'Back:', $v->back()->data(), "\n" # Back:fifth print 'Front:', $v->front()->data(), "\n" # Front:first $v->pop(); # pop element first in $v->push($v->factory('sixth')), "\n" print 'Back:', $v->back()->data(), "\n" # Back:sixth print 'Front:', $v->front()->data(), "\n" # Front:second # Iterators for (my $i = $v->begin(); !$i->at_end(); ++$i) { print "Data:", $i->p_element()->data(); } # Iterators -- reverse_iterator my $ri = stl::reverse_iterator($v->iter())->first(); while (!$ri->at_end()) { print "Data:", $ri->p_element()->data(); ++$ri; } # Inserters my $three2one = stl::list(qw(3 2 1)); my $four2six = stl::list(qw(4 5 6)); my $seven2nine = stl::list(qw(7 8 9)); my $result = stl::list(); stl::copy($three2one->begin(), $three2one->end(), stl::front_inserter($result)); stl::copy($seven2nine->begin(), $seven2nine->end(), stl::back_inserter($result)); my $iseven = stl::find($result->begin(), $result->end(), 7); stl::copy($four2six->begin(), $four2six->end(), stl::inserter($result, $iseven)); # $result now contains (1, 2, 3, 4, 5, 6, 7, 8, 9); # Vector container... my $v = stl::vector(qw(first second third fourth fifth)); my $e = $v->at(0); # return pointer to first element. print 'Element-0:', $e->data(), "\n"; # Element-0:first $e = $v->at($v->size()-1); # return pointer to last element. print 'Element-last:', $e->data(), "\n"; # Element-last:fifth $e = $v->at(2); # return pointer to 3rd element (idx=2). print 'Element-2:', $e->data(), "\n"; # Element-2:third # Priority Queue my $p = stl::priority_queue(); $p->push($p->factory(priority => 10, data => 'ten')); $p->push($p->factory(priority => 2, data => 'two')); $p->push($p->factory(priority => 12, data => 'twelve')); $p->push($p->factory(priority => 3, data => 'three')); $p->push($p->factory(priority => 11, data => 'eleven')); $p->push($p->factory(priority => 1, data => 'one')); $p->push($p->factory(priority => 1, data => 'one-2')); $p->push($p->factory(priority => 12, data => 'twelve-2')); $p->push($p->factory(priority => 20, data => 'twenty'), $p->factory(priority => 0, data => 'zero')); print "\$p->size()=", $p->size(), "\n"; print "\$p->top():", $p->top(), "\n"; $p->top()->priority(7); # change priority for top element. $p->refresh(); # refresh required after priority change. $p->pop(); # remove element with highest priority. print "\$p->top():", $p->top(), "\n"; # Clone $d container into $d1... my $d1 = $d->clone(); my $d2 = stl::deque(qw(sixth seventh eight)); # Append $d container to end of $d2 container... $d2 += $d; # DataMembers -- Class builder helper... { package MyClass; use Class::STL::ClassMembers ( qw(attrib1 attrib2), # data members Class::STL::ClassMembers::DataMember->new( name => 'attrib3', default => '100', validate => '^\d+$'), # data member with attributes Class::STL::ClassMembers::DataMember->new( name => 'attrib4', default => 'med', validate => '^(high|med|low)$'), ); use Class::STL::ClassMembers::Constructor; # produce class new() function } my $cl = MyClass->new(attrib1 => 'hello', attrib2 => 'world'); print $cl->attrib1(), " ", $cl->attrib2(), "\n"; # 'hello world' $cl->attrib1(ucfirst($cl->attrib1)); $cl->attrib2(ucfirst($cl->attrib2)); print $cl->attrib1(), " ", $cl->attrib2(), "\n"; # 'Hello World' $cl->attrib4('avg'); # Causes progam to die with '** Function attrib2 value failed validation...' =head1 DESCRIPTION This package provides a framework for rapid Object Oriented Perl application development. It consists of a number of base classes that are similar to the C++/STL framework, plus a number of I classes which provide the I to transparently generate common functions, and will enable you to put your Perl application together very quickly. The I functionality provided consists of F, F, F and F as follows: =item F vector, list, deque, queue, priority_queue, stack, tree. =item F iterator, bidirectional_iterator, reverse_iterator, forward_iterator. =item F find, find_if, for_each, transform, count, count_if, copy, copy_backward, remove, remove_if, remove_copy, remove_copy_if, replace, replace_if, replace_copy, replace_copy_if. =item F equal_to, not_equal_to, greater, greater_equal, less, less_equal, compare, bind1st, bind2nd, mem_fun, ptr_fun, ptr_fun_binary, matches, matches_ic, logical_and, logical_or, multiplies, divides, plus, minus, modulus. =head2 F Most of the functions have the same arguments and return types as their STL equivalent. There are some differences though between the C++/STL and this implementation: =item Iterators and the I function An I object points to a numeric position within the container, and not to an I. If new elements are inserted to, or removed from, a postion preceding the iterator, then the iterator will point to the same I but to a different element. The I function will return a newly constructed iterator object which will point to the I element within the container, unlike the C++/STL equivalent which points to I the last element. =item The I Container This container provides a hierarchical tree structure. Each element within a I container can be either a simple element or another container object. The I and overridden I functions will traverse the tree and pocess all element I within the tree. =item Utilities I, I functions These utilities provide unary functions for regular expression matching. The first or second argument will be a regular expression string. The I provides case insensitive matching. =item Container I function This function and the overridden C<+>, C<+=> operators will combine the two containers together. =item The I function This function returns a newly constructed object that is a copy of its caller object. =item The Container I function This function will return an array consisting of all element objects within the container. =item Container I type All containers contain collections of objects which are of type F, or classes derived from this type. The container classes are themselves, ultimately, derived from this I type. =cut -------------------------------------------------------------------------------- =head1 F These I classes can be used to generate code for various basic class functions. This module requires an import list consisting of target data member names or I objects. When using I B data members should be included in order for the generated I and I functions to function correctly. The constructor function code can be produces as well by using the package F, or F to create a singleton class type. The following target member functions will be generated and made available to the class: =item Data Member Accessor Get/Put Function This function will have the same name as the data member and should be used to I or I the value for the data member. Pass the value as the argument when setting the value for a data member. For I data members with a F attribute, a validation check will be performed when attempting to set the member value by matching the value against the I regular expression string. =item Class I Function This function should be called in the target class's I function after I<$self> has been blessed. It will perform the necessary data members initialisation. =item Class I Function This function will construct and return an object containing a copy of the caller object. =item Class I Function This function requires one argument consisting of an object of the same type as the caller. It will swap the caller object with this I object. =item Class I Function This function will return a pointer to an anonymous hash containing the data member names (as the key) and data member attibutes array list consisting of F and F attribute fields in that order. All data members, including inherited members are contained in this hash. =item Class I Function Same as F function except that only the data members local to the class are contained in the hash returned. =item Class::STL::ClassMembers::DataMember For more complex data members, this class may be used to provide additional information about the member. This information consist of: F, F, and F. The F attribute contains the member name; the F attribute contains a default value for the member when initialised; the F attribute consists of a regular expression string that will be used to validate the member value by matching it to this regex string. =item Class::STL::ClassMembers::Constructor The constructor function with the name F will be produced for a package that uses this module. It is recomended that this constructor is produced for any class (package) that uses the F package to produce the data members. This will ensure that the correct calls are done during construction and copy-construction of an object. This constructor will make a call to the C user member function F if it exists in the calling class. The F function will have the object reference passed as the first argument. =item Class::STL::ClassMembers::SingletonConstructor Use this package to produce a singleton class. This constructor will ensure that only one instance of this class will be constructed. =head2 Example =begin { package MyClass; use Class::STL::ClassMembers ( qw(attrib1 attrib2), Class::STL::ClassMembers::DataMember->new( name => 'attrib3', default => '100', validate => '^\d+$'), Class::STL::ClassMembers::DataMember->new( name => 'attrib4', default => 'med', validate => '^(high|med|low)$'), ); use Class::STL::ClassMembers::Constructor; # produce class new() function } my $cl = MyClass->new(attrib1 => 'hello', attrib2 => 'world'); print $cl->attrib1(), " ", $cl->attrib2(), "\n"; # 'hello world' $cl->attrib1(ucfirst($cl->attrib1)); $cl->attrib2(ucfirst($cl->attrib2)); print $cl->attrib1(), " ", $cl->attrib2(), "\n"; # 'Hello World' $cl->attrib4('avg'); # Causes progam to die with '** Function attrib2 value failed validation...' =end =cut -------------------------------------------------------------------------------- =head1 F =head2 Exports F, F, F, F, F, F, F. =cut -------------------------------------------------------------------------------- =head1 F This is the I base class for all other container classes. Objects should not be constructed directly from this class, but from any of the derived container classes. Common functions are documented here. =head2 Extends I =over =item F I I I I I The I function constructs an object for this class and returns a blessed reference to this object. All forms accept an optional I containing any of the following named arguments: I. The I defines the class type of element objects that the container will hold. I will default to F if not specified; when specified, the type must be derived from F. The second form is a I. It requires another container reference as the argument, and will return a copy of this container. The third form requires one or more element refs as arguments. These elements will be copied into the newly constructed container. The fourth form requires one I iterator and an optional I iterator. All the element objects with, and including, the I and I (or I if not specified) positions will be copied into the newly constructed container. The fifth form accepts a list of raw data values. Each of these values will be stored inside a F object constructed by the container's I function, with the element's I member containing the raw data value. =item F Returns a newly constructed object which is identical to the calling (this) object. =item F I The I function constructs a new element object and returns a reference to this. The type of object created is as specified by the I container attribute. The I argument consists of a hash and is passed on to the element class I function. Override this function if you want to avoid the 'eval' call. =item F I The I function requires one starting iterator and an optional finish iterator as arguments. It will delete all the elements in the container within, and including, these two iterator positions. The I funtion returns and iterator pointing to the element following the last deleted element. =item F I I I I The first form will insert copies of elements within the I and I positions before I. The second form will insert copies of elements within the I and I positions before I The third form will insert the element, or elements (I) before I. The fourth form will insert I copies of I before I. =item F I The I function requires no arguments. It will remove the element at the I of the container. =item F I The I function requires one or more arguments consisting of elements. This will append the element(s) to the end of the container. =item F I This function will delete all the elements from the container. =item F I The I function constructs and returns a new iterator object which points to the I element within the container. =item F I The I function constructs and returns a new iterator object which points to the F element within the container. **Note that, unlike C++/STL, this object points to the last element and not I. =item F I The I function is the reverse of the I function -- the newly constructed iterator points to the last element. =item F I The I function is the reverse of the I function -- the newly constructed iterator points to the first element. =item F I The I function requires no arguments. It will return an integer value containing the number of elements in the container. =item F I This function returns I<'1'> if the container is empty (ie. contains no elements), and I<'0'> if the container contains one or more elements. =item F I The I function returns an array containing the elements (references) from the container. =item F I The I function compares the I in this container with the I in the container refered to by the argument I. The elements are compared using the element I function. The function will return I<'1'> if both containers contain the same number of elements and all elements in one container are equal to, and in the same order as, all elements in the I container. =item F I Inverse of I function. =item F, F Append containers. =item F Containers equality comparison. =item F Containers non-equality comparison. =back =cut -------------------------------------------------------------------------------- =head1 F A list container can have elements pushed and popped from both ends, and also inserted at any location. Access to the elements is sequential. =head2 Extends I =over =item F I The I function will alter the order of the elements in list by reversing their order. =item F I The I function will alter the order of the elements in list by sorting the elements. Sorting is done based on the elements I comparison function. =back =head2 Example =begin use stl; # Construct the list object: my $list = list(qw( first second third fourth fifth)); # Display the number of elements in the list: print "Size:", $list->size(), "\n"; # Size:5 # Reverse the order of elements in the list: $list->reverse(); # Display the contents of the element at the front of the list: print 'Front:', $list->front(), "\n"; # Display the contents of the element at the back of the list: print 'Back:', $list->back(), "\n"; # Display the contents of all the elements in the list: for_each($list->begin(), $list->end(), MyPrint->new()); # Return an array of all elements-refs: my @arr = $l1->to_array(); # Delete all elements from list: $list->clear(); print "Size:", $list->size(), "\n"; # Size:0 print '$list container is ', $list->empty() ? 'empty' : 'not empty', "\n"; # MyPrint Unary Function -- used in for_each() above... { package MyPrint; use base qw(Class::STL::Utilities::FunctionObject::UnaryFunction); sub function_operator { my $self = shift; my $arg = shift; print "Data:", $arg->data(), "\n"; } } =end =cut -------------------------------------------------------------------------------- =head1 F A vector allows for random access to its elements via the I function. =head2 Extends I =over =item F I The I function requires one or more arguments consisting of elements. This will append the element(s) to the end of the I. =item F I The I function requires no arguments. It will remove the element at the I of the I. =item F I The I function requires no arguments. It returns a reference to the element at the I of the I. =item F The I function requires no arguments. It returns a reference to the element at the I of the I. =item F I The I function requires an I argument. This function will return a reference to the element at the location within the I specified by the argument I. =back =cut -------------------------------------------------------------------------------- =head1 F A double-ended container. Elements can be I and I at both ends. =head2 Extends I =over =item F I The I function requires one or more arguments consisting of elements. This will insert the element(s) to the front of the I. =item F I The I function requires no arguments. It will remove the element at the I of the I. =back =cut -------------------------------------------------------------------------------- =head1 F A queue is a FIFO (first-in-first-out) container. Elements can be I at the back and I from the front. =head2 Extends I =over =item F I The I function requires one or more arguments consisting of elements. This will append the element(s) to the back of the I. =item F I The I function requires no arguments. It will remove the element at the I of the I. This is the earliest inserted element. =item F I The I function requires no arguments. It returns a reference to the element at the I of the I. This is the element last inserted. =item F I The I function requires no arguments. It returns a reference to the element at the I of the I. This is the earliest inserted element. =back =cut -------------------------------------------------------------------------------- =head1 F A stack is a LIFO (last-in-first-out) container. Elements can be I at the top and I from the top. =head2 Extends I =over =item F I The I function requires one or more arguments consisting of elements. This will append the element(s) to the top of the I. =item F I The I function requires no arguments. It will remove the element at the I of the I. This is the last inserted element. =item F I The I function requires no arguments. It returns a reference to the element at the I of the I. This is the last inserted element. =back =cut -------------------------------------------------------------------------------- =head1 F A tree is a hierarchical structure. Each element within a I container can be either a simple element or another container object. The overridden I function will traverse the tree and return an array consisting of all the I in the tree. =head2 Extends I =over =item F I The overridden I function will traverse the tree and return an array consisting of all the element I in the tree container. =back =head2 Examples =begin # Tree containers; construct two trees from # previously construced containers: my $t1 = tree($l1); my $t2 = tree($l2); # Construct a third tree: my $tree = tree(); # Add other tree containers as elements to this tree: $tree->push_back($tree->factory($t1)); $tree->push_back($tree->factory($t2)); # Search for element ('pink') in tree: if (my $f = find_if($tree->begin(), $tree->end(), bind1st(equal_to(), 'pink')) print "FOUND:", $f->data(), "\n"; } else { print "'pink' NOT FOUND", "\n"; } # Traverse tree returning all element nodes: my @tarr = $tree->to_array(); =end =cut -------------------------------------------------------------------------------- =head1 F A priority queue will maintain the order of the elements based on their priority, with highest priority elements at the top of the container. Elements contained in a priority queue must be of the type, or derived from, I. This element type contains the attribute I, and needs to have its value set whenever an object of this element type is constructed. =head2 Extends I =head2 Element Type I =over =item F I The I function requires one or more arguments consisting of elements. This will place the element(s) in the queue according to their priority value. =item F I The I function requires no arguments. It will remove the element with the highest priority. =item F I The I function requires no arguments. It returns a reference to the element with the highest priority. =item F I The I function should be called whenever the priority value for an element has been order. This will update the ordering of the elements if required. =back =cut -------------------------------------------------------------------------------- =head1 F This module contains various algorithm functions. =head2 Exports F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F The F package consists of various I algorithm functions. The I / I argument must be derived from I and I respectively. Standard utility functions are provided in the I module. A function object contains the function I. This I function will, in turn, be called by the algorithm for each element traversed. The algorithm will pass the element reference as the argument to the I function. =over =item F I The I function will traverse the container starting from I and ending at I and execute the I with the element passed in as the argument. =item F I I The I functions has two forms. The first form will traverse the container starting from I and ending at I and execute the I with the element passed in as the argument, producing I. The second form will traverse two containers with the second one starting from I. The I will be called for each pair of elements. The resulting elements will be placed in I. =item F I =item F I The I function will traverse the container starting from I and ending at I and return a count of the elements that evaluate to true by the I. =item F I =item F I The I function will traverse the container starting from I and ending at I and return an I pointing to the first element that evaluate to true by the I. If no elements evaluates to true then 'o' is returned. =item F I =item F I =item F I =item F I The I function will traverse the container starting from I and ending at I and remove the elements that evaluate to true by the I. =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =item F I =back =head2 Examples =begin use Class::STL::Containers; use Class::STL::Algorithms; use Class::STL::Utilities; # Display all elements in list container '$list' # using unary-function 'myprint' and algorithm 'for_each': for_each($list->begin(), $list->end(), ptr_fun('::myprint')); sub myprint { print "Data:", @_, "\n"; } # Algorithms -- remove_if() # Remove element equal to back() -- ie remove last element: remove_if($list->begin(), $list->end(), bind1st(equal_to(), $list->back())); # Remove all elements that match regular expression '^fi': remove_if($v->begin(), $v->end(), bind2nd(matches(), '^fi')); # Search for element ('pink') in tree: if (my $f = $tree->find_if($tree->begin(), $tree->end(), bind1st(equal_to(), "pink"))) { print "FOUND:", $f->p_element()->data(), "\n"; } else { print "'pink' NOT FOUND", "\n"; } =end =cut -------------------------------------------------------------------------------- =head1 F =head2 Exports F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F. This module contains various utility function objects. Each object will be constructed automatically when the function name (eg. 'equal_to') is used. Each of the function objects are derived from either I or I. =over =item F F. This function-object will return the result of I between its argument and the object I attribute's value. The element's I function is used for the comparison. =item F F. This function is the inverse of I. =item F F. This function-object will return the result of I comparison between its argument and the object I attribute's value. The element's I function is used for the comparison. =item F F. This function-object will return the result of I comparison between its argument and the object I attribute's value. The element's I function is used for the comparison. =item F F. This function-object will return the result of I comparison between its argument and the object I attribute's value. The element's I function is used for the comparison. =item F F. This function-object will return the result of I comparison between its argument and the object I attribute's value. The element's I function is used for the comparison. =item F F. This function-object will return the result of I comparison between its argument and the object I attribute's value. The element's I function is used for the comparison. =item F F. This function-object will return the result (true or false) of the regular expression comparison between its first argument and its second argument which contains a regular expression string. =item F F. Case-insensitive version of the I function. =item F F. This function requires two arguments consisting of a I and a element or value argument. It will produce a I object whose I member will call the I with I as the first argument. =item F F. This function requires two arguments consisting of a I and a element or value argument. It will produce a I object whose I member will call the I with I as the second argument. =item F This function requires one argument consisting of the class member function name (string). It will construct an object whose I member will require an element object to be passed as the first argument. It will call the elements's member function as specified by the I argument. =item F F. This function requires one argument consisting of a global function name (string). =item F F. This function requires one argument consisting of global function name (string). =item F F. =item F F. =item F F. This function-object will return the result of I between its two element arguments. The element's I function is used for the calculation. It will return a newly construced element object containing the result. =item F F. This function-object will return the result of I between its two element arguments. The element's I
function is used for the calculation. It will return a newly construced element object containing the result. =item F F. This function-object will return the result of I between its two element arguments. The element's I function is used for the calculation. It will return a newly construced element object containing the result. =item F F. This function-object will return the result of I between its two element arguments. The element's I function is used for the calculation. It will return a newly construced element object containing the result. =item F. F. This function-object will return the result of I between its two element arguments. The element's I function is used for the calculation. It will return a newly construced element object containing the result. =back =cut -------------------------------------------------------------------------------- =head1 F This module contains the iterator classes. =head2 Exports F, F, F, F, F<++>, F<-->, F<==>, F, EF<=>, EF<=>, F<+>, F<+=>, F<->, F<-=>, F, F, F, F, F. =over =item F =item F Returns a reference to the container that is associated with the iterator. =item F This function will return a reference to the element pointed to by the iterator. =item F B. This function will return the I between two iterators. Both iterators must be from the same container. I must be positioned after I. I =item F B. Moves the iterator forward, or backwards if size is negative. I =item F B. I =item F B. I =item F B. I =item F =item F =item F =item F =item F =item F =item F =item F =item F =item F =item F =item F =back =head2 Examples =begin # Using overoaded inrement operator: for (my $i = $p->begin(); !$i->at_end(); $i++) { MyPrint->new()->function_operator($i->p_element()); } # Using overoaded decrement operator: for (my $i = $p->end(); !$i->at_end(); --$i) { MyPrint->new()->function_operator($i->p_element()); } # Reverse iterator: my $ri = reverse_iterator($p->iter())->first(); while (!$ri->at_end()) { MyPrint->new()->function_operator($ri->p_element()); $ri->next(); } =end =cut -------------------------------------------------------------------------------- =head1 SEE ALSO Sourceforge Project Page: C =cut -------------------------------------------------------------------------------- =head1 AUTHOR m gaffiero, Egaffie@users.sourceforge.netE =head1 COPYRIGHT AND LICENSE Copyright E1999-2007, Mario Gaffiero. All Rights Reserved. This file is part of Class::STL::Containers(TM). Class::STL::Containers is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. Class::STL::Containers is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Class::STL::Containers; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA =cut