from __future__ import annotations

from collections.abc import Hashable
from typing import cast

import networkx as nx

from graph.exploration.algorithms.in_memory.graph_features.common import FeatureRowsByKey


def apply_clustering(rows_by_key: FeatureRowsByKey, graph: nx.Graph | nx.DiGraph) -> None:
    for node_key, value in cast(dict[Hashable, float], nx.clustering(graph)).items():
        rows_by_key[node_key]["clustering_coefficient"] = float(value)
