import csv
import matplotlib.pyplot as plt
from collections import defaultdict


file_path = "results.csv"

data = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
# data[order][operation][structure] -> list of times


# читаем CSV
with open(file_path, "r") as f:
    reader = csv.DictReader(f)

    for row in reader:
        structure = row["Structure"]
        order = row["Order"]
        operation = row["Operation"]
        time = float(row["Time"])

        data[order][operation][structure].append(time)


def get_avg(order, operation, structure):
    values = data[order][operation][structure]
    return sum(values) / len(values)


def plot_hist(operation):
    structures = ["LinkedList", "HashTable", "BST"]
    orders = ["shuffled", "sorted"]

    for order in orders:
        values = [get_avg(order, operation, s) for s in structures]

        plt.figure()
        plt.bar(structures, values)

        plt.title(f"{operation.upper()} (order: {order})")
        plt.ylabel("Time (seconds)")

    plt.show()


# 3 графика-гистограммы
plot_hist("insert")
plot_hist("find")
plot_hist("delete")