1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Shannon-entropy machinery shared by the two process-entropy signals
//! added in issue #330.
//!
//! Two distinct metrics are built on the same [`shannon_entropy`] core:
//!
//! - **Change entropy** (Hassan, 2009 — *Predicting Faults Using the
//! Complexity of Code Changes*). Per commit, the entropy of the churn
//! distribution across the files it touched measures how *scattered*
//! that change was. Each file is then credited its History-Complexity
//! share `pᵢ · H` of every commit it took part in (Hassan's HCM with
//! the modification-probability attribution factor). The git backend
//! computes the per-commit term; this module only supplies the entropy
//! core so the math is unit-testable without a repository.
//! - **Co-change graph entropy** (arXiv 2504.18511, 2025). Files that
//! change in the same commit are joined by a weighted edge (weight =
//! number of shared commits). A file's co-change entropy is the Shannon
//! entropy of its edge-weight distribution: low when it always co-changes
//! with the same partner, high when its changes ripple across many
//! different files. [`CochangeGraph`] accumulates the edges during the
//! single history walk and computes the per-file value at finalisation.
//!
//! All entropies are in **bits** (base-2 logarithm), the convention in
//! both source papers.
use HashMap;
use ;
/// Initial-import commits touch thousands of files at once; a co-change
/// graph grows O(width²) in commit width, so commits wider than this are
/// excluded from the graph (only — their change entropy, which is O(width),
/// is still counted). Tuned per issue #330's worked example.
pub const MAX_COCHANGE_COMMIT_FILES: usize = 1000;
/// Shannon entropy (base 2, in bits) of a weight distribution.
///
/// Weights are normalised to a probability distribution `pᵢ = wᵢ / Σw`
/// and the entropy is `-Σ pᵢ·log₂(pᵢ)`. Non-positive weights contribute
/// nothing (a file with zero churn in a commit, or an absent edge). An
/// empty distribution, an all-zero distribution, and a single non-zero
/// weight all yield `0.0` — there is no uncertainty to measure.
///
/// Takes a cloneable iterator rather than a slice so callers on the
/// per-commit walk (churn distribution) and per-file finalise (edge
/// weights) feed it directly, with no intermediate `Vec`; the two passes
/// (total, then sum) clone the iterator.
/// Interned identifier for a file participating in the co-change graph.
///
/// Newtype rather than a bare `u32` so a node index can never be confused
/// with a co-change count (both are `u32`).
;
/// A sparse, undirected, weighted co-change graph built incrementally
/// across a history walk.
///
/// Paths are interned to [`FileId`]s and adjacency is stored sparsely
/// (`Vec<HashMap<FileId, u32>>` indexed by node), so memory scales with
/// the number of *observed* co-change pairs, not with files². Two graphs
/// are tracked in lock-step — the full long-window graph and the
/// recent-window subgraph — so both entropy variants come from one walk.
/// Increment the symmetric edge weight between `a` and `b` in `adjacency`.
/// Shannon entropy of one node's edge-weight distribution, or `0.0` when
/// the node has no edges in this graph (an absent node, or empty
/// adjacency — `shannon_entropy` returns `0.0` for the empty iterator).
///
/// The edge weights are summed in **`FileId`-sorted** order, not HashMap
/// iteration order. `shannon_entropy`'s `-Σ p·log2(p)` is a non-associative
/// float fold, so a HashMap's seed-dependent order would let the live walk
/// and a cache replay — separate processes with different `RandomState`
/// seeds — sum to ULP-divergent values, silently breaking the #334
/// bit-identity contract. Sorting pins one canonical summation order across
/// every process.