	#!/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

   	#open log file
	open(LOG, ">rez_class_patterns.txt") || die "$0: rez_class_delays.txt will not open.";
	seek(LOG, 0,2);

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

       	open(TRACES, "<rez_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
			print "\rdone: ".(tell(TRACES)/$file_size)." of 1     ";
			$proc=tell(TRACES)/$file_size;
			#if($proc>0.0025){last;}
			@list = split(" ", $_);
			&classify;
		}
		$end_time = time();
		close(TRACES);
		$elapsed_time += ($end_time-$start_time);
	
	print "\nClosed: rez_patterns.txt\n";
	close(LOG);
	print "\nClosed: rez_class_patterns.txt\n";


## end ##


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


###########
# DELAYS  #
###########

	sub classify{
		my($del);
		$del = shift(@list);
		while(defined($del = shift(@list))){
			if($del>=0){
			  if($del<=32){
				print(LOG "0 ");
			  }elsif($del<=195){
				print(LOG "1 ");
			  }elsif($del<=1005){
				print(LOG "2 ");
			  }elsif($del<=3116){
				print(LOG "3 ");
			  }elsif($del<=7910){
				print(LOG "4 ");
			  }elsif($del<=17463){
				print(LOG "5 ");
			  }elsif($del<=34918){
				print(LOG "6 ");
			  }elsif($del<=66842){
				print(LOG "7 ");
			  }elsif($del<=116272){
				print(LOG "8 ");
			  }elsif($del<=343966){
				print(LOG "9 ");
			  }else{
				print(LOG "10 ");
			  }
			}else{
			  print(LOG "-1 ");
			}
		}
		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;
}
