# run this with: perl mrutest.pl <files>

my @mru;
my $max_depth = 32;

sub in_mru {
    my $word = shift;

    # if word found in mru, move to front of list
    # and return 1

    for (my $i = 0; $i <= $#mru; $i++) {
        if ($word eq $mru[$i]) {
            splice @mru, $i, 1;
            unshift @mru, $word;
            return 1;
        }
    }

    # else add word to mru, ensuring the mru is no
    # more than max_depth deep; return 0

    splice @mru, $#mru, 1 if ($#mru+1 >= $max_depth);
    unshift @mru, $word;
    return 0;
}

my %symbols;    # total symbols searched
my %hits;       # number of mru hits
my $lastFile;   # last file processed

while (<>) {
    if ($lastFile ne $ARGV) {   # reset mru for every file
        $lastFile = $ARGV;
        @mru = ();
    }

    s/\\.*$//;      # get rid of line comments
    s/ \( .*?\)//;  # get rid of paren comments
    s/" .*?"/"/;    # get rid of strings

    foreach (split) {
        $symbols{$ARGV}++;
        $hits{$ARGV}++ if in_mru($_);
    }
}

foreach (keys %symbols) {
    printf(
        "%25s: symbols=%5d, hits=%5d, hits%=%5.1f\n", 
        $_, $symbols{$_}, $hits{$_}, ($hits{$_}/$symbols{$_})*100
    );
}
