#!/usr/bin/perl


$filename = $ARGV[0]; 
$cwnd = $ARGV[1];


# setup
@hstcp_table_cwnd;
@hstcp_table_a;
@hstcp_table_b;

&get_vals( $filename );

print "size of arrays: $#hstcp_table_cwnd, $#hstcp_table_a, $#hstcp_table_b\n";
#print "@hstcp_table_a";



@ans = &get_hstcp_val( $cwnd );

print "cwnd=$ans[0], a=$ans[1], b=$ans[2]\n";

exit;


sub get_vals {

    my $file = @_[0];

    print "loading file $file... ";

    open ( FILE, "<$file" );
    
    my $i = 0;

    while ( <FILE> ) {

#	my $line = chomp( $_ );


	if ( /(\d+)\s+(\d+)\s+(\d+)/ ) {
	    $hstcp_table_cwnd[$i] = $1; $hstcp_table_a[$i] = $2; $hstcp_table_b[$i] = $3;
#	    print " $hstcp_table_cwnd[$i],  $hstcp_table_a[$i] ,  $hstcp_table_b[$i]\n";
	}

	$i++
    }
    close( FILE );
    print "done!\n";

}


sub get_hstcp_val{

    my $this_cwnd = @_[0];

    # do a binary search
    my $left, $right, $mid;

    $left = 0;
    $right = $#hstcp_table_cwnd;

    while( ($right-$left) ) {

	$mid = ( $left + $right )>>1;

	print "$hstcp_table_cwnd[$mid]:$this_cwnd\t\t$left\t$mid\t$right\n";

	if ( $hstcp_table_cwnd[$mid] < $this_cwnd ) {
	    $left = $mid + 1;
	}
	else {
	    $right = $mid ;

	}
    }

   # $mid+=1;

#    print "mid=$mid\n";

    return ( $hstcp_table_cwnd[$left], $hstcp_table_a[$left], $hstcp_table_b[$left] );

}


1;
