import pandas as pd
import numpy as np
import os
import sys
from pathlib import Path

sys.path.insert(0, os.path.dirname(__file__))
from common import *

infile_attrib = os.path.dirname(os.path.abspath(__file__)) + '/../build/plot-string-allocn-attrib.dat'
attribs = pd.read_csv(infile_attrib, 
                        sep='\t',
                        header=0)

infile_sepcmp = os.path.dirname(os.path.abspath(__file__)) + '/../benchmarks/string/string-separate-compilation-overhead.csv'
sepcmps = pd.read_csv(infile_sepcmp, 
                        header=0)

# putting the sepcmp bar at the top of 'other', so just below ctor-dtor
category_above_semcmp = 'ctor-dtor'

# print(attribs)
# print(sepcmps)

# print(attribs.columns)

isfirst = True

for _,scr in sepcmps.iterrows():
    size = scr['size']
#    tuning = r['tuning']
    attrib_key = attribs[ (attribs['corpus-meanlen-tgt'] == size) &
                            (attribs['sut-platform'] == 'cfa') &
                           ( attribs['category'] == category_above_semcmp )]
    attrib_key = attrib_key.copy()
#    print(attrib_key)
    assert( len(attrib_key) == 1 )
#    attrib_key = attrib_key.iloc[0] # convert from singleton table to row
    attrib_key['sepcmp-Delta'] = scr['mean DeltaOpDur']
    attrib_key['sepcmp-adjusted'] = attrib_key['grp-prior-duration-ns'] - attrib_key['sepcmp-Delta']

    print(attrib_key.to_csv(header=isfirst, index=False, sep='\t', na_rep="0"), end='')
    isfirst = False

