Prefix.pm

Through the package Tree::Prefix it is possible to save and load a bunch of strings into a big prefix tree, stored as a file structure.

This is the documentation of all downloads.

Move your mouse over the code area and look at its top right corner where a set of icons for downloading, copy-pasting, etc. will appear.


#!/usr/bin/perl
package Tree::Prefix;

use strict;
use warnings;

# this one combines a hash-based and dir-based prefix tree
#    first, saves everything to a hash-based one
#    then, based on the results generates a dir-based one

use Tree::Prefix::Dir;
use Tree::Prefix::Hash;

sub new {
	my $classname=shift;

	my $self={};

	$self->{dir}=Tree::Prefix::Dir->new;
	$self->{hash}=Tree::Prefix::Hash->new;

	$self->{limit}=10;		# nodes containing less than $self->{limit} words are dropped
							# 10 is a default value

	bless($self);
	return $self;
}

# ============ return running directory of script ============
sub pwd {
	my $self=shift;

	my $pwd=`pwd`;			# use this on *nix systems
	#my $pwd=`cd`;			# use this on Windows systems
	chomp $pwd;
	return $pwd;
}

# ============ loads file of string and all words, all prefixes ============
sub load_file_to_hash {
	my $self=shift;
	my $file=shift;

	open IN, "<$file";
	while (<IN>) {
		chomp;
		$self->{hash}->insert_dir(lc $_);
	}
	close IN;
}

# ============ save $self->{hash} directory structure to $self->{dir} (dropping too small nodes) ============
sub hash_tree_2_dir_tree {
	my $self=shift;

	my $node;
	my $prefix;

	# if called without parameters, start processing from the root
	if (@_>0) {
		$node=shift;
		$prefix=shift;
	} else {
		$node=$self->{hash}->{prefix_tree};
		$prefix="";
	}

	# process a node - traverse the childs
	for my $letter (keys %$node) {
		# not a letter, don't process - either "count" or "list"
		if (length $letter > 1) { next; }

		$self->{dir}->insert_dir($prefix.$letter);

		# insert files (=completions), if any
		if (exists $node->{$letter}->{ls} && $node->{$letter}->{count} <= $self->{limit}) {
			#$self->{dir}->insert_files($prefix.$letter, $node->{$letter}->{ls});
			$self->{dir}->insert_files($prefix.$letter, $self->{hash}->list_files($prefix.$letter));
		}

		$self->hash_tree_2_dir_tree($node->{$letter}, $prefix.$letter);
	}
}

# ======== functions imported from Tree::Prefix::Dir ========
	# ============ set directory to save prefix tree to ============
	sub set_basedir {
		my $self=shift;
		my $basedir=shift;

		$self->{dir}->set_basedir($basedir);
	}

	# ============ lists all the subdirectories of a specified node ============
	sub list_dir {
		my $self=shift;
		my $dir=shift;

		return $self->{dir}->list_dir($dir);
	}

	# ============ check whether selected directory was already processed ============
	sub exists_file {
		my $self=shift;
		my $dir=shift;

		return $self->{dir}->exists_file($dir);
	}

	# ============ insert list into a specified node of the tree ============
	sub insert_files {
		my $self=shift;

		my $dir=shift;
		my $files=shift;

		return $self->{dir}->insert_files($dir, $files);
	}

	# ============ lists all the files of a specified node ============
	sub list_files {
		my $self=shift;
		my $dir=shift;

		return $self->{dir}->list_files($dir);
	}

	# ============ delete files of specified node of the tree ============
	sub delete_files {
		my $self=shift;
		my $dir=shift;

		$self->{dir}->delete_files($dir);
	}
1;