TreeSort.pm


NAME

Communiware::TreeSort - implements tree-like sorting


SYNOPSIS

  my $comparator = make_comparator(\&type_map,@keys);
  @sorted = sort &$comparator($a,$b),@array_of_hash; 
  
  @sorted = tree_sort($ref,\&typemap,$id,$parent,@keys);


DESCRIPTION

This module implements tree sorting for Communiware. It exports function make_comparator which constructs correct comparation function from the list of Communiware attributes (which allows to sort array of hash references as returned by fetchall_arrayref({})) from list of attribute names, optionally prefixed by plus and minus signs

and function tree_sort which recieves array of hashes each of which has key attribute (typically item_id) and link attribute which refers (is equal to) key attribute of parent record.

It returns array of hashes ordered parent first, childern then, where chlildres are sorted according to rest of specified attributes and attributes indicated structure of tree is added to hash indicating depts of current row in the inheritance tree.

ATTRIBUTES ADDED

LEVEL
Indicates distance from the root of tree to the current item.

PREV_SIBLING
Id of previous item on the same level (if any)

NEXT_SIBLING
Id of next item on the same level (if any)

UNCLES
List of zeros and ones with length of LEVEL. 0 at particular position means that ancessor on this level has no NEXT_SIBLING, 1, that there is some.


FUNCTIONS

treesort

Recieves array of hash references, typemap function reference, and three or more key names (specified keys should present in all hashes). Typically, result of DBI fetchall_arrayref({}) is passed.

Typemap function should recieve single parameter - name of hash key and return string NUMBER if this parameter should be compared numerically or anything else if it should be compared lexicographically.

Specified keys should have following meaning:

  1. Unique id of record.

  2. Id of parent record (may be undef) Used to link record to its parent

  3. All other keys are used to sort siblings (childrens of one record) They are passed to the make_comparator manpage

    These keys may be prefixed with + or - to specify sorting order.

treesort returns list resorted following way element which have no parent (or parent id, which is not in this list) child of element above child of child second child of top element (in order specified by third and extra keys) child of second child Element with key LEVEL is added to all hashes, which specifies depth of nesting.

make_comparator

Recieves function which maps key names to their types (really, only NUMBER/not-NUMBER is significant) and list of keys. Returns anonymous subroutine, which should be called as


 &$comparator($a,$b);

and compares two global hash references $a and $b, by comparing listed keys in order using cmp for non-numeric and <=> for numeric attributes and returning after first non-zero result or when key list exhausted.

Key names may be prefixed by + or - to specify sort order;


INTERNAL FUNCTIONS

sort_sibling

called recursively from the treesort manpage for each node of tree build from original list. Sorts sibling of given node and pushes them into result array (passed by reference. Arguments: Reference to node, reference to result, current level and list of extra keys.


BUGS

Some functionality duplicated in Communiware::Unifilters

16 октябрь 2007 13:45