#!/bin/bash

set -eu
#set -o pipefail

max_duration=3000

function pad
{
	echo "$1                                   " | sed 's/^\(..............................\).*$/\1/'
}

function timer
{
	n="$1"
	shift
	basename="$1"
	name="$1.dat"
	shift
    cmd="$*"
	t0=$(python3 -c 'import time; print(time.time())')
	# shellcheck disable=SC2086
	if maxtime "$max_duration" $cmd
    then
        t1=$(python3 -c 'import time; print(time.time())')
        duration=$( (echo scale=2; echo "$t1-$t0") | bc)
        echo "$n" "$duration" >> "$name"
        echo "$(pad "$basename")$duration" 1>&2
    else
        # Return without adding to $name
        echo "$(pad "$basename")timeout" 1>&2
    fi
}

rm -f ./*-m.dat

n=$(xz --decompress -c files-stat.xz | wc -l)

top=12

for exponent in $(seq 0 $top)
do
	m=$((10**exponent))
    if [ "$m" -gt "$n" ]
    then
        break
    fi
	echo "$exponent (going to $top) m=$m n=$n $(date)"
	./random-numbers | timer "$m" "highest-treap-m" ./highest --use-treap -n "$m" > /dev/null
	./random-numbers | timer "$m" "highest-heap-m" ./highest --use-heap -n "$m" > /dev/null
	./random-numbers | timer "$m" "highest-bisect-m" ./highest --use-bisect -n "$m" > /dev/null
	./random-numbers | timer "$m" "highest-rbtree-m" ./highest --use-rbtree -n "$m" > /dev/null
	./random-numbers | timer "$m" "highest-fibonacci-heap-m" ./highest --use-fibonacci-heap -n "$m" > /dev/null
	./random-numbers | timer "$m" "sort-m" ./'sort-head' -n "$m" > /dev/null
	./random-numbers | timer "$m" "highest-treap-m-no-dups" ./highest --use-treap -n "$m" --disallow-duplicates > /dev/null
	./random-numbers | timer "$m" "highest-bisect-m-no-dups" ./highest --use-bisect -n "$m" --disallow-duplicates > /dev/null
	./random-numbers | timer "$m" "highest-rbtree-m-no-dups" ./highest --use-rbtree -n "$m" --disallow-duplicates > /dev/null
	./random-numbers | timer "$m" "sort-m-no-dups" ./'sort-head' -n "$m" --disallow-duplicates > /dev/null
	echo
done