# Загружает CSV с результатами и строит столбчатую диаграмму сравнения

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def main():
    df = pd.read_csv('experiment_results.csv')

    mean_times = df.groupby(['Structure', 'Mode'])[['Insert (sec)', 'Search (sec)', 'Delete (sec)']].mean().reset_index()

    structures = mean_times['Structure'].unique()
    operations = ['Insert (sec)', 'Search (sec)', 'Delete (sec)']
    titles = ['Insertion', 'Search', 'Deletion']

    fig, axes = plt.subplots(1, 3, figsize=(15, 5))

    for ax, op, title in zip(axes, operations, titles):
        x = np.arange(len(structures))
        width = 0.35

        random_vals = []
        sorted_vals = []
        for s in structures:
            rand = mean_times[(mean_times['Structure'] == s) & (mean_times['Mode'] == 'random')]
            sort = mean_times[(mean_times['Structure'] == s) & (mean_times['Mode'] == 'sorted')]
            random_vals.append(rand[op].values[0] if not rand.empty else 0)
            sorted_vals.append(sort[op].values[0] if not sort.empty else 0)

        ax.bar(x - width/2, random_vals, width, label='Random order')
        ax.bar(x + width/2, sorted_vals, width, label='Sorted order')
        ax.set_xticks(x)
        ax.set_xticklabels(structures)
        ax.set_ylabel('Time (seconds)')
        ax.set_title(title)
        ax.legend()

    plt.tight_layout()
    plt.savefig('performance_comparison.png', dpi=150)
    plt.show()

if __name__ == '__main__':
    main()