	#!/usr/bin/perl
	#
	# seek for accesses patterns
	#
	
	#define constants
		
	$_Timestamp=0; 	$_Elapsed_Time=1;	$_HTTP_code=2;  
	$_Size=3; 	$_Method=4; 		$_URL=5;
	$_Exp_Date=6;	$_Next_Acc=7; 		$_Cacheable=8;
	$elapsed_time = 0;	#simulation time
	$estate=10;	#state to search transitions from
	for($i=0; $i<12; $i++){$stat[$i]=0;}


   	#open log file
	open(LOG, "+<rez_transitions_1state.txt") || die "$0: rez_transitions_1state.txt will not open.";
	seek(LOG, 0,2);

	#process all files in directory with extension ".new"

       	open(TRACES, "<rez_class_patterns.txt") || die "$file will not open.";
		print "\nOpened: ". $file. "\n";
		seek(TRACES, 0,2); $file_size = tell(TRACES); seek(TRACES, 0,0); 
		
		$pos = tell(TRACES);
		$prev_pos = tell(TRACES);
		$start_time = time();
		while(<TRACES>) { # takes data from $file
			$proc=tell(TRACES)/$file_size;
			print "\rdone: ".$proc." of 1     ";
			#if($proc>0.0025){last;}
			@list = split(" ", $_);
			&transify;
		}
		$end_time = time();
		close(TRACES);
		$elapsed_time += ($end_time-$start_time);
	
	print "\nClosed: rez_class_patterns.txt\n";

	print LOG "estate $estate cnt $cnt\n";
	for($i=0; $i<12; $i++){print LOG "$i ".$stat[$i]/$cnt."\n";}
	close(LOG);
	print "\nClosed: rez_transitions_1state.txt\n";


## end ##


#############################################
# algorythms
#############################################


################
# TRANSITIONS  #
################

	sub transify{
		my($del, $state);
		$state=11; 
		while(defined($del = shift(@list))){
			if($state==$estate){#state to search transitions from
				$stat[$del]++;	#transitions for that state
				$cnt++;	#all transitions
			}
			$state=$del;
		}
		$del=11;
		if($state==$estate){
			$stat[$del]++;	#transitions for that state
			$cnt++;	#all transitions
		}
		#print(LOG "\n");
	}

############################################
#subroutine for dumping asoc array to file #
############################################
#store key and value
sub storeaa($$){
	my $a=shift(@_), $f=shift(@_), $key;

	open(DMP, "$f") || die "$0: will not open.";
	
		while (($key, $val) = each (%$a) ){
			print DMP $key." ".$val."\n";
			#print DMP $$a{$key}."\n";
		}

	close DMP;
}

#store just value
sub storeaa_val($$){
	my $a=shift(@_), $f=shift(@_), $key;

	open(DMP, "$f") || die "$0: will not open.";
	
		while (($key, $val) = each (%$a) ){
			print DMP $val."\n";
		}

	close DMP;
}

#load key and value
sub loadaa($$){
	my $a=shift(@_), $f=shift(@_), $key;

	open(DMP, "<$f") || die "$0: will not open.";
	
		while(<DMP>) { # takes data from $file
			@list = split(" ", $_);
			$$a{$list[0]}=$list[1];
		}

	close DMP;
}
