Tree::Radix
is a collection of classes for developing algorithms
that use binary radix trees (e.g. routing tables). For references on
the underlying algorithm, see Knuth Volume 3, Section 6.3 on
digital trees, particularly patricia trees, or Sedgewick's
Algorithms in Language on same.
The top-level Tree::Radix
class is a container class representing
entire trees.
Tree::Radix::Key
objects encapsulate the key data that is encoded in
the radix tree. They are currently implemented using Bit::Vector
objects.
Three other classes, Tree::Radix::Node
,
Tree::Radix::Node::Branch
and Tree::Radix::Node::Leaf
are used
by the module to represent the structure of the tree. The Branch and
Leaf classes are descended from the Node class. The Node class is
only used as a base class; objects of this type are never created.
Tree::Radix::Key
objects represent the keys that you are interested in
inserting into your Radix tree. At some point you generally want to
try to match arbitrary data against the keys you have inserted.
This class also abstracts the radix tree's idea of ``endianess'' of keys. The tree's internal representation always roots the tree at zero, and counts upward. The key class decides which end of the key is considered zero. The most signifigant bit of the key is considered zero, although this is designed to be easily overridden.
The Tree::Radix::Key
class has the following methods:
ARGS
be passed in a hash like fashion.
At least one of the following arguments is required in the hash to
create a key object:
keyvector - A Bit::Vector
object to clone.
keyhex - A hexidecimal string (with no leading ``0x'' or similar prefix) representing the key.
keypacked - A packed binary string (ala pack) to use for the key data.
The number of bits used to represent newly created keys (this does
apply to keys created from Bit::Vector
objects) defaults to 32.
You can pass a keybits option in the hash to use a different number
of bits.
If new is called like a method on an already existing object, it will clone that object.
Bit::Vector
object representing the key. Given
NEWKEY
, mutates this slot of the object. This function is
primarily for internal use.
NEWDATA
, mutates this slot of the object. This slot is provided
for the conveniance of the user who doesn't want to go to the trouble
of subclassing this object just to add space for some arbitrary data.
BIT
is set in self's key. Override this method if
you need to change which way the radix tree's endianness works.
Tree::Radix
uses to create new
internal nodes in the tree. Provided to be overrided by subclasses.
Tree::Radix
uses to create new leaf
nodes in the tree. Provided to be overrided by subclasses.
Tree::Radix
object. The created tree will be empty. If
called as a method from an already created tree, it will create a new
tree that is a deep clone (i.e. the contained node objects are copied
as well) of the given tree.
Tree::Radix::Node
derivative representing the tree
root. Given NEWROOT
, mutates this slot of the object. This
function is primarily for internal use.
KEYOBJ
, returning the matching
key object from the tree if it exists, or undef if it doesn't.
KEYOBJ
into the tree. Will call croak if there
is a key collision (Radix trees require unique keys). Returns the key
object as it exists in the tree.
KEYOBJ
from the tree, returning the deleted key object if
it existed.
HOW
is a string,
``depth'' or ``breadth'' representing which way to walk the tree. SUB
is a subroutine that should expect a single argument, a
Tree::Radix::Node
derivative.
Tree::Radix
classes are represented as blessed hashes.
The radix tree is internally represented by derivatives of the
Tree::Radix::Node
class. A given Tree::Radix
's root
hash
member points to the root of the radix tree. All node objects have a
parent
hash member that points to the node's parent node, or is
undef if the node is the tree root.
Tree::Radix::Node::Leaf
classes represent tree leaves; each has a
Tree::Radix::Key
associated with its key
hash member.
Tree::Radix::Node::Branch
classes represent tree branches.
In a binary radix tree, there are always two populated branches in an
internal node; one way branching is not allowed.
The node objects are created during by the Tree::Radix::insert
method, by using the new_leaf
and new_branch
methods of the
key object passed to insert.
::Leaf
and
::Branch
classes instead.
Tree::Radix::Node
derivative representing this node's
parent (it's undefined if the node doesn't have a parent). Given
NEWPARENT
, mutates this slot of the object. This function is
primarily for internal use.
::Branch
and ::Leaf
classes subclass this to provide more
information.
NEWBIT
, mutates this slot of the object. This function is
primarily for internal use.
NEWARRAYREF
, mutates this slot of
the object. This function is primarily for internal use.
Tree::Radix::Key
object stored at this leaf. Given
NEWKEY
, mutates this slot of the object. This function is
primarily for internal use.
Making the radix tree support a radix other than two would take some slashing.
More documentation wouldn't hurt.
perl(1).