package Interval;

#interface
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&int_encode &int_decode &print_help_int &int_count_bits);

#implementation
use strict;
use POSIX;
use Common;

###############################################################################
# INTERVALOVE KODOVANI                                                       ##
###############################################################################

sub print_help_int
{
	print "\nIntervalové kódování\n\
Intervalové kódování, stejně jako metoda BSTW, kóduje přímo slova přirozeného \
jazyka. Pro každé vstupní slovo se uchovává pozice jeho posledního výskytu. \
Na výstup se posílá buď nové slovo (pokud je to jeho první výskyt) nebo rozdíl \
nové a staré pozice. Tato metoda je tedy také vhodná pro lokální výskyt stejných \
slov, protože se na výstup posílají menší rozdíly pozic.
Pro výpočet délky kódu se předpokládá, že přirozená čísla budou kódována pomocí \
gama' kódování.
\n\n"; 
}

sub int_count_bits
{
# in:
# encoded (ref array)
# out:
# count of bits

	my @encoded = @{shift @_};
	my $bits = 0;
	foreach $a (@encoded) {
		my @r = split /_/, $a;
		$bits += 1; # rozlisovaci bit
		if ($r[0] == 0) { # cislo - index
# 			$bits += 5; #binarni kod pevne delky 5 bitu
			$bits += 2*(ceil(log($r[1])/log(2))-1); #gama' kodovani
		}
		else { # slovo
			$bits += length($r[1]) * 8;
		}
	}
	return $bits;
}

# encoding INTERVALOVE KODOVANI

sub int_encode
{
# in:
# input (ref array)
# mode (scalar)
# out:
# encoded (array)

	my $ref_input = shift @_;
	my $mode = shift;

	my @input = split /\s/, $$ref_input;
	my $input_length = $#input + 1;
	my @encoded = ();
	my %dict = ();
	my $position = 0;

	foreach my $c (@input) {
		$position++;
		my $out;
		if (exists $dict{$c}) {
			$out =  "0_" . ($position - $dict{$c});
			$dict{$c} = $position;
		}
		else {
			$dict{$c} = $position;
			$out = "1_$c";
		}
		push @encoded, $out;
		if (no_silent($mode)) {
			printf "%-10s %10s =>  %-12s .... dict: ",  $c, "(pos: $position)", translate($out)  if no_silent($mode); 
			foreach my $key (sort {$dict{$a} <=> $dict{$b}} keys %dict) {
				print "$key: $dict{$key} ";
			}
			print "\n";
		}
		my_wait($mode);
	}
	return @encoded;
}

# decoding INTERVALOVE KODOVANI

sub int_decode
{
# in:
# encoded (ref array)
# mode (scalar)
# out:
# decoded (array)

	my $ref_encoded = shift @_;
	my $mode = shift;

	my @encoded = @$ref_encoded;
	my @decoded = ();
	my %dict = ();
	my $position = 0;

	foreach my $c (@encoded) {
		$position++;
		my @r = split /_/, $c;
		my $out;
		if ($r[0] == 1) { #nove slovo
			$dict{$r[1]} = $position;
			$out = $r[1];
		}
		else {
			my $pom = $position - $r[1];
			while (my ($key, $value) = each %dict) {
				if ($value == $pom) {
					$out = $key;
					$dict{$key} = $position;
				}
			}
		}
		push @decoded, $out;

		if (no_silent($mode)) {
			printf "%-12s =>  %-10s %10s .... dict: ",  translate($c), $out, "($position)" if no_silent($mode); 
			foreach my $key (sort {$dict{$a} <=> $dict{$b}} keys %dict) {
				print "$key: $dict{$key} ";
			}
			print "\n";
		}
		my_wait($mode);
	}
	return (join ' ', @decoded);
}


1;