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

df = pd.read_csv("results.csv", encoding="utf-8-sig")
df_avg = df[df["Операция"].str.contains("СРЕДНЕЕ")].copy()
df_avg["Операция"] = df_avg["Операция"].str.replace(" (СРЕДНЕЕ)", "")

modes = ["случайный", "отсортированный"]
operations = ["insert", "find", "delete"]
structures = ["LinkedList", "HashTable", "BST"]

x = np.arange(len(structures))
width = 0.35

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

for i, op in enumerate(operations):
    ax = axes[i]
    for j, mode in enumerate(modes):
        mask = (df_avg["Операция"] == op) & (df_avg["Режим"] == mode)
        times = df_avg[mask]["Время (сек)"].values
        label = "Случайные данные" if mode == "случайный" else "Отсортированные данные"
        ax.bar(x + j * width, times, width, label=label)

    ax.set_title(f"Операция: {op.upper()}")
    ax.set_xticks(x + width / 2)
    ax.set_xticklabels(structures)
    ax.set_ylabel("Время (сек)")
    ax.legend()
    ax.grid(axis="y", linestyle="--", alpha=0.7)

plt.tight_layout()
plt.savefig("performance_comparison.png", dpi=300)
plt.show()