bixverse-methods 0.8.1

Bulk omics methods for bixverse: enrichment, graphs, ontologies, matrix factorisation and differential expression.
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
//! Leiden and Louvain community detection on modularity.
//!
//! One engine for both: Louvain is Leiden with the refinement phase switched
//! off. Phase semantics follow igraph's `igraph_community_leiden` (fast local
//! moving with a queue of unstable nodes, randomised constrained merging in the
//! refinement, aggregation on the refined partition seeded with the unrefined
//! one). Weights are read in `T`, but every node, cluster and edge total is
//! held in `f64`, since the cluster totals are updated millions of times.
//!
//! Departure from igraph: the graph is relabelled into BFS order first, and
//! Leiden's local moving visits nodes in that order from a seeded random
//! start rather than in a full shuffle. On kNN graphs of unordered cells this
//! keeps the neighbour scans cache-local and needs about half the node
//! evaluations, at the same modularity. Louvain keeps the full shuffle, as the
//! ordered sweep costs it modularity without a refinement to repair it.
//!
//! Local moving defaults to the sequential queue. [`LocalMoving::Coloured`]
//! parallelises it on large levels, see its docs; it stays independent of the
//! thread count.
//!
//! Determinism: the queue is sequential and seeded. Refinement
//! runs in parallel over clusters, each cluster with its own RNG derived from
//! `(seed, iteration, level, cluster)`, and aggregation is parallel over
//! refined clusters with a fixed reduction order. The partition therefore
//! depends on the seed only, not on the thread count.

use num_traits::{ToPrimitive, Zero};
use rand::prelude::*;
use rayon::prelude::*;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};

use crate::graph::community_detections::remap_communities_by_size;
use crate::prelude::*;

////////////
// Consts //
////////////

/// Nodes in a level below which refinement and aggregation run sequentially.
/// Louvain is also called from inside parallel loops over seeds and
/// resolutions (fast_clusters), where small levels are not worth a fork.
const LEIDEN_PAR_MIN_NODES: usize = 4096;

/// Safety cap on rounds of the parallel local-moving modes. Each round only
/// revisits nodes whose neighbourhood changed, so real graphs stop far below
/// it.
const PAR_MOVE_MAX_ROUNDS: usize = 1000;

/// Nodes per rayon task when deciding moves in parallel. Per-node work follows
/// the degree, so small tasks balance better.
const PAR_MOVE_MIN_LEN: usize = 64;

/// Target id of a move into an empty cluster, resolved when it is applied.
const NEW_CLUSTER: u32 = u32::MAX;

/// Safety cap on full iterations when iterating until stable
/// (`n_iterations: None`), against a partition that oscillates.
const MAX_ITERATIONS: usize = 1000;

/// Nodes per partial sum of the level-0 modularity. Fixed, so the stopping
/// decision of `min_gain` does not depend on the thread count.
const QUALITY_CHUNK: usize = 4096;

////////////
// Params //
////////////

/// How the local-moving phase visits the nodes.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum LocalMoving {
    /// Traag's fast local moving: a sequential queue of unstable nodes
    #[default]
    Queue,
    /// Greedy colour classes of a seeded node order. Each class decides its
    /// moves in parallel against the state frozen at its start (no two nodes
    /// of a class are neighbours, so their edge weights to clusters are
    /// exact), then applies them in node order, re-checking each gain against
    /// the live cluster totals. The scheme is the one in `clust-libreville-rs`
    /// and `leiden-rs`.
    Coloured,
}

/// Parse a [`LocalMoving`] name.
///
/// ### Params
///
/// * `s` - `"queue"` or `"coloured"`, case-insensitive
///
/// ### Returns
///
/// The [`LocalMoving`], or `None` for an unknown name
pub fn parse_local_moving(s: &str) -> Option<LocalMoving> {
    match s.to_lowercase().as_str() {
        "queue" => Some(LocalMoving::Queue),
        "coloured" | "colored" => Some(LocalMoving::Coloured),
        _ => None,
    }
}

/// Parameters for [`leiden_sparse_graph`].
///
/// ### Fields
///
/// * `resolution` - Modularity resolution parameter (gamma)
/// * `beta` - Randomness of the refinement merges; the target is drawn with
///   probability proportional to `exp(gain / beta)`. igraph default 0.01.
/// * `n_iterations` - Number of full Leiden iterations, each starting from
///   the previous partition. `None` iterates until an iteration changes
///   nothing (capped at 1000). Default 5; igraph's R default is 2.
/// * `min_gain` - With `n_iterations: None`, also stop once an iteration
///   raises the modularity by no more than this. 0 waits for a fixed point.
/// * `refine` - Run the Leiden refinement phase. `false` gives Louvain.
/// * `seed` - Seed for the node orders and refinement draws
/// * `local_moving` - How local moving visits the nodes; the parallel modes
///   only run on levels of at least `LEIDEN_PAR_MIN_NODES` (4096) nodes,
///   smaller ones use the queue
#[derive(Clone, Copy, Debug)]
pub struct LeidenParams {
    /// Modularity resolution parameter (gamma)
    pub resolution: f64,
    /// Randomness of the refinement merges
    pub beta: f64,
    /// Number of full iterations; `None` until stable
    pub n_iterations: Option<usize>,
    /// Modularity gain at or below which `n_iterations: None` stops
    pub min_gain: f64,
    /// Run the refinement phase (Leiden) or not (Louvain)
    pub refine: bool,
    /// Random seed
    pub seed: u64,
    /// Local-moving strategy
    pub local_moving: LocalMoving,
}

impl LeidenParams {
    /// Generate new Leiden parameters.
    ///
    /// ### Params
    ///
    /// * `resolution` - Modularity resolution parameter (gamma)
    /// * `beta` - Randomness of the refinement merges
    /// * `n_iterations` - Number of full iterations; `None` until stable
    /// * `refine` - Run the refinement phase (Leiden) or not (Louvain)
    /// * `seed` - Random seed
    ///
    /// ### Returns
    ///
    /// Initialised self, with `min_gain` 0
    pub fn new(
        resolution: f64,
        beta: f64,
        n_iterations: Option<usize>,
        refine: bool,
        seed: u64,
    ) -> Self {
        Self {
            resolution,
            beta,
            n_iterations,
            min_gain: 0.0,
            refine,
            seed,
            local_moving: LocalMoving::Queue,
        }
    }
}

/// Defaults match `igraph::cluster_leiden(objective_function = "modularity")`,
/// except 5 iterations rather than 2, as two leave Leiden short of converged
/// on large graphs.
impl Default for LeidenParams {
    fn default() -> Self {
        Self {
            resolution: 1.0,
            beta: 0.01,
            n_iterations: Some(5),
            min_gain: 0.0,
            refine: true,
            seed: 42,
            local_moving: LocalMoving::Queue,
        }
    }
}

////////////////
// LevelGraph //
////////////////

/// Read access to the adjacency of one level of the hierarchy.
///
/// Level 0 is the caller's graph relabelled into BFS order, every later level
/// an aggregate; both are an [`AggGraph`]. [`SparseGraph`] implements it too,
/// for the BFS and the relabelling themselves.
trait LevelGraph: Sync {
    /// Edge weight type
    type W: Copy + Zero + Send + Sync + ToPrimitive;

    /// Number of nodes in this level
    fn n(&self) -> usize;

    /// Neighbours and weights of node `i`
    fn row(&self, i: usize) -> (&[u32], &[Self::W]);
}

impl<T> LevelGraph for SparseGraph<T>
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
{
    type W = T;

    #[inline]
    fn n(&self) -> usize {
        self.get_node_number()
    }

    #[inline]
    fn row(&self, i: usize) -> (&[u32], &[T]) {
        self.get_neighbours_raw(i)
    }
}

//////////////
// AggGraph //
//////////////

/// Symmetric CSR for one level of the hierarchy.
///
/// Level 0 is the caller's graph relabelled into BFS order, weights in the
/// caller's type. Aggregated levels hold f64 weights and no self-loops: local
/// moving and refinement ignore them, and the node weights carry the full
/// degree separately.
struct AggGraph<W> {
    /// Row offsets, length n + 1
    indptr: Vec<usize>,
    /// Column indices
    indices: Vec<u32>,
    /// Edge weights
    weights: Vec<W>,
}

impl<W: Copy + Zero + Send + Sync + ToPrimitive> LevelGraph for AggGraph<W> {
    type W = W;

    #[inline]
    fn n(&self) -> usize {
        self.indptr.len() - 1
    }

    #[inline]
    fn row(&self, i: usize) -> (&[u32], &[W]) {
        let (s, e) = (self.indptr[i], self.indptr[i + 1]);
        (&self.indices[s..e], &self.weights[s..e])
    }
}

/////////////
// Helpers //
/////////////

/// Weight to f64. `ToPrimitive` on f32/f64 cannot fail.
///
/// ### Params
///
/// * `w` - Edge weight
///
/// ### Returns
///
/// The weight as f64
#[inline(always)]
fn wf<W: ToPrimitive>(w: W) -> f64 {
    w.to_f64().expect("float to f64 cannot fail")
}

/// SplitMix64 finaliser, used to derive independent per-cluster seeds.
///
/// ### Params
///
/// * `x` - Value to mix
///
/// ### Returns
///
/// Mixed 64-bit value
#[inline]
fn splitmix64(mut x: u64) -> u64 {
    x = x.wrapping_add(0x9E37_79B9_7F4A_7C15);
    x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    x ^ (x >> 31)
}

/// Derive a seed from a base seed and a list of indices.
///
/// ### Params
///
/// * `seed` - Base seed
/// * `parts` - Indices identifying the stream (iteration, level, cluster)
///
/// ### Returns
///
/// Derived seed
fn derive_seed(seed: u64, parts: &[u64]) -> u64 {
    parts
        .iter()
        .fold(splitmix64(seed), |acc, &p| splitmix64(acc ^ p))
}

/// Renumber labels to `0..k` in order of first appearance.
///
/// ### Params
///
/// * `labels` - Labels, each `< labels.len()`; renumbered in place
///
/// ### Returns
///
/// Number of distinct labels
fn reindex(labels: &mut [u32]) -> usize {
    let mut map = vec![u32::MAX; labels.len()];
    let mut next = 0u32;
    for l in labels.iter_mut() {
        let m = &mut map[*l as usize];
        if *m == u32::MAX {
            *m = next;
            next += 1;
        }
        *l = *m;
    }
    next as usize
}

/// Group nodes by label (counting sort), members in increasing node order.
///
/// ### Params
///
/// * `labels` - Label per node, contiguous in `0..k`
/// * `k` - Number of labels
///
/// ### Returns
///
/// `(offsets, members)`: members of label `c` are
/// `members[offsets[c]..offsets[c + 1]]`
fn group_by_label(labels: &[u32], k: usize) -> (Vec<usize>, Vec<u32>) {
    let mut offsets = vec![0usize; k + 1];
    for &l in labels {
        offsets[l as usize + 1] += 1;
    }
    for c in 0..k {
        offsets[c + 1] += offsets[c];
    }
    let mut cursor = offsets.clone();
    let mut members = vec![0u32; labels.len()];
    for (v, &l) in labels.iter().enumerate() {
        members[cursor[l as usize]] = v as u32;
        cursor[l as usize] += 1;
    }
    (offsets, members)
}

/// Modularity of a partition of one level, as the stopping rule of
/// `min_gain` sees it.
///
/// The internal weight is summed in chunks of [`QUALITY_CHUNK`] nodes, combined
/// in order, so the value (and the decision to stop) does not depend on the
/// thread count.
///
/// ### Params
///
/// * `g` - Graph of this level, self-loops included
/// * `node_w` - Node weights
/// * `membership` - Cluster per node, labels `< n`
/// * `res` - Resolution divided by `2m`
/// * `two_m` - Total node weight
///
/// ### Returns
///
/// The modularity
fn level_quality<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &[u32],
    res: f64,
    two_m: f64,
) -> f64 {
    let n = g.n();
    let partials: Vec<f64> = (0..n.div_ceil(QUALITY_CHUNK))
        .into_par_iter()
        .map(|chunk| {
            let mut s = 0.0;
            for v in chunk * QUALITY_CHUNK..((chunk + 1) * QUALITY_CHUNK).min(n) {
                let c = membership[v];
                let (nbrs, ws) = g.row(v);
                for (&u, &w) in nbrs.iter().zip(ws) {
                    if membership[u as usize] == c {
                        s += wf(w);
                    }
                }
            }
            s
        })
        .collect();
    let internal: f64 = partials.iter().sum();
    let mut tot = vec![0.0f64; n];
    for (&c, &k) in membership.iter().zip(node_w) {
        tot[c as usize] += k;
    }
    let squares: f64 = tot.iter().map(|t| t * t).sum();
    (internal - res * squares) / two_m
}

/// Modularity of a partition.
///
/// `Q = 1/2m * sum_c (e_c - gamma * K_c^2 / 2m)`, with `e_c` twice the internal
/// edge weight and `K_c` the summed degree of cluster `c`. Self-loops count
/// once per CSR entry, as in igraph.
///
/// ### Params
///
/// * `graph` - Undirected [`SparseGraph`]
/// * `labels` - Community per node
/// * `resolution` - Resolution parameter (gamma)
///
/// ### Returns
///
/// The modularity
pub fn modularity<T>(graph: &SparseGraph<T>, labels: &[usize], resolution: f64) -> f64
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
{
    let n = graph.get_node_number();
    let k = labels.iter().copied().max().map_or(0, |m| m + 1);
    let mut internal = 0.0f64;
    let mut two_m = 0.0f64;
    let mut cluster_w = vec![0.0f64; k];
    for v in 0..n {
        let (nbrs, ws) = graph.get_neighbours_raw(v);
        let mut deg = 0.0;
        for (&u, &w) in nbrs.iter().zip(ws) {
            let w = wf(w);
            deg += w;
            if labels[u as usize] == labels[v] {
                internal += w;
            }
        }
        two_m += deg;
        cluster_w[labels[v]] += deg;
    }
    if two_m == 0.0 {
        return 0.0;
    }
    let penalty: f64 = cluster_w.iter().map(|&c| c * c).sum::<f64>() * resolution / two_m;
    (internal - penalty) / two_m
}

//////////////////
// Local moving //
//////////////////

/// Fast local moving (Traag et al. 2019, Alg. A.2 `MoveNodesFast`).
///
/// Nodes sit in a queue, initially in shuffled or rotated order. A node takes
/// the neighbouring cluster (or an empty one) with the strictly best gain over
/// staying; when it moves, its stable neighbours outside the new cluster are
/// re-queued. Self-loops are skipped in the scan.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights (degrees at level 0, summed degrees after)
/// * `membership` - Initial partition, labels `< n`; updated and reindexed
/// * `res` - Resolution divided by `2m` of the level-0 graph
/// * `max_evals` - Cap on node evaluations (Louvain safeguard)
/// * `shuffle` - Visit nodes in random order instead of rotated node order
/// * `rng` - Random number generator for the initial order
///
/// ### Returns
///
/// `(number of clusters, whether any node moved)`
fn move_nodes<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &mut [u32],
    res: f64,
    max_evals: usize,
    shuffle: bool,
    rng: &mut StdRng,
) -> (usize, bool) {
    let n = g.n();

    // Without shuffling, nodes are visited in node order from a random start.
    // Node order is BFS order at level 0 and follows it at coarser levels, so
    // the scans stay cache-local and need about half the evaluations. Leiden
    // only: without refinement, the ordered sweep snowballs clusters and costs
    // Louvain modularity.
    let queue_order: Vec<u32> = if shuffle {
        let mut order: Vec<u32> = (0..n as u32).collect();
        order.shuffle(rng);
        order
    } else {
        let start = if n > 0 { rng.random_range(0..n) } else { 0 };
        (start..n).chain(0..start).map(|v| v as u32).collect()
    };
    let mut changed = false;
    let mut queue: VecDeque<u32> = queue_order.into();
    let mut stable = vec![false; n];

    let mut cluster_w = vec![0.0f64; n];
    let mut cluster_n = vec![0u32; n];
    for v in 0..n {
        let c = membership[v] as usize;
        cluster_w[c] += node_w[v];
        cluster_n[c] += 1;
    }
    let mut empty: Vec<u32> = (0..n as u32)
        .filter(|&c| cluster_n[c as usize] == 0)
        .collect();

    let mut w_to = vec![0.0f64; n];
    let mut added = vec![false; n];
    let mut nbr_clusters: Vec<u32> = Vec::with_capacity(64);

    let mut evals = 0usize;
    while let Some(v) = queue.pop_front() {
        if evals >= max_evals {
            break;
        }
        evals += 1;

        let v = v as usize;
        let cur = membership[v];
        let kv = node_w[v];

        cluster_w[cur as usize] -= kv;
        cluster_n[cur as usize] -= 1;
        if cluster_n[cur as usize] == 0 {
            empty.push(cur);
        }

        // an empty cluster is always a candidate (the node on its own)
        let e = *empty
            .last()
            .expect("node just left a cluster, so one is free or v is alone");
        nbr_clusters.push(e);
        added[e as usize] = true;

        let (nbrs, ws) = g.row(v);
        for (&u, &w) in nbrs.iter().zip(ws) {
            if u as usize == v {
                continue;
            }
            let c = membership[u as usize];
            if !added[c as usize] {
                added[c as usize] = true;
                nbr_clusters.push(c);
            }
            w_to[c as usize] += wf(w);
        }

        let mut best = cur;
        let mut best_gain = w_to[cur as usize] - kv * cluster_w[cur as usize] * res;
        for &c in &nbr_clusters {
            let gain = w_to[c as usize] - kv * cluster_w[c as usize] * res;
            // strictly improving only, needed for convergence
            if gain > best_gain {
                best = c;
                best_gain = gain;
            }
            w_to[c as usize] = 0.0;
            added[c as usize] = false;
        }
        nbr_clusters.clear();

        cluster_w[best as usize] += kv;
        cluster_n[best as usize] += 1;
        if empty.last() == Some(&best) {
            empty.pop();
        }

        stable[v] = true;
        if best != cur {
            changed = true;
            membership[v] = best;
            for &u in nbrs {
                let u = u as usize;
                if stable[u] && membership[u] != best {
                    stable[u] = false;
                    queue.push_back(u as u32);
                }
            }
        }
    }

    (reindex(membership), changed)
}

///////////////////////////
// Parallel local moving //
///////////////////////////

/// Open-addressing map from cluster id to edge weight, for one row.
///
/// Sized to the row rather than to the cluster count, so the per-thread
/// scratch does not grow with n, as an n-long array per thread would.
#[derive(Default)]
struct ClusterTable {
    /// Cluster id per slot, `u32::MAX` when free
    keys: Vec<u32>,
    /// Summed edge weight per slot
    vals: Vec<f64>,
    /// Occupied slots in insertion order
    used: Vec<u32>,
}

impl ClusterTable {
    /// Make room for a row of `deg` entries; the table must be empty.
    ///
    /// ### Params
    ///
    /// * `deg` - Entries in the row
    #[inline]
    fn reserve(&mut self, deg: usize) {
        let cap = (2 * deg).next_power_of_two().max(16);
        if self.keys.len() < cap {
            self.keys = vec![u32::MAX; cap];
            self.vals = vec![0.0; cap];
        }
    }

    /// Slot of `c`, or the free slot where it goes.
    ///
    /// ### Params
    ///
    /// * `c` - Cluster id
    ///
    /// ### Returns
    ///
    /// The slot index
    #[inline]
    fn slot(&self, c: u32) -> usize {
        let mask = self.keys.len() - 1;
        let mut i = (c.wrapping_mul(0x9E37_79B1) as usize) & mask;
        while self.keys[i] != c && self.keys[i] != u32::MAX {
            i = (i + 1) & mask;
        }
        i
    }

    /// Add `w` to cluster `c`.
    ///
    /// ### Params
    ///
    /// * `c` - Cluster id
    /// * `w` - Edge weight
    #[inline]
    fn add(&mut self, c: u32, w: f64) {
        let i = self.slot(c);
        if self.keys[i] == u32::MAX {
            self.keys[i] = c;
            self.vals[i] = w;
            self.used.push(i as u32);
        } else {
            self.vals[i] += w;
        }
    }

    /// Summed weight to cluster `c`, 0 if absent.
    ///
    /// ### Params
    ///
    /// * `c` - Cluster id
    ///
    /// ### Returns
    ///
    /// The weight
    #[inline]
    fn get(&self, c: u32) -> f64 {
        let i = self.slot(c);
        if self.keys[i] == c { self.vals[i] } else { 0.0 }
    }

    /// Empty the table.
    #[inline]
    fn clear(&mut self) {
        for &i in &self.used {
            self.keys[i as usize] = u32::MAX;
        }
        self.used.clear();
    }
}

thread_local! {
    /// Per-thread [`ClusterTable`] for [`best_move`]. Empty between uses.
    static MOVE_SCRATCH: RefCell<ClusterTable> = RefCell::new(ClusterTable::default());
}

/// A move decided by [`best_move`].
#[derive(Clone, Copy, Debug)]
struct Move {
    /// Node
    v: u32,
    /// Target cluster, or [`NEW_CLUSTER`]
    to: u32,
    /// Edge weight from `v` to the target (0 for a new cluster)
    w_to: f64,
    /// Edge weight from `v` to its own cluster, `v` excluded
    w_from: f64,
}

/// Local moving with the strategy in `mode`.
///
/// Levels below [`LEIDEN_PAR_MIN_NODES`] nodes always take the queue: they
/// are cheap and gain nothing from a fork.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights
/// * `membership` - Initial partition, labels `< n`; updated and reindexed
/// * `res` - Resolution divided by `2m`
/// * `max_evals` - Cap on node evaluations
/// * `shuffle` - Queue only: random instead of rotated node order
/// * `mode` - The [`LocalMoving`] strategy
/// * `rng` - Random number generator for the node orders
///
/// ### Returns
///
/// `(number of clusters, whether any node moved)`
#[allow(clippy::too_many_arguments)]
fn local_move<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &mut [u32],
    res: f64,
    max_evals: usize,
    shuffle: bool,
    mode: LocalMoving,
    rng: &mut StdRng,
) -> (usize, bool) {
    if g.n() < LEIDEN_PAR_MIN_NODES {
        return move_nodes(g, node_w, membership, res, max_evals, shuffle, rng);
    }
    match mode {
        LocalMoving::Queue => move_nodes(g, node_w, membership, res, max_evals, shuffle, rng),
        LocalMoving::Coloured => move_nodes_coloured(g, node_w, membership, res, max_evals, rng),
    }
}

/// Best move of `v` against fixed cluster totals.
///
/// Candidates are the clusters of `v`'s neighbours and, if `v` is not alone,
/// an empty cluster at gain 0. Only a strict improvement over staying counts;
/// ties between candidates go to the lower cluster id, and an empty cluster
/// is only taken when no neighbouring cluster beats staying.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `v` - The node
/// * `membership` - Cluster per node
/// * `cluster_w` - Summed node weight per cluster, `v` included
/// * `cluster_n` - Nodes per cluster
/// * `kv` - Node weight of `v`
/// * `res` - Resolution divided by `2m`
/// * `table` - Scratch, empty
///
/// ### Returns
///
/// The move, or `None` to stay
#[allow(clippy::too_many_arguments)]
#[inline]
fn best_move<G: LevelGraph>(
    g: &G,
    v: u32,
    membership: &[u32],
    cluster_w: &[f64],
    cluster_n: &[u32],
    kv: f64,
    res: f64,
    table: &mut ClusterTable,
) -> Option<Move> {
    let (nbrs, ws) = g.row(v as usize);
    table.reserve(nbrs.len());
    for (&u, &w) in nbrs.iter().zip(ws) {
        if u != v {
            table.add(membership[u as usize], wf(w));
        }
    }
    let own = membership[v as usize];
    let w_from = table.get(own);
    let stay = w_from - kv * (cluster_w[own as usize] - kv) * res;

    let mut best = (own, stay);
    let mut w_best = 0.0;
    for &i in &table.used {
        let c = table.keys[i as usize];
        if c == own {
            continue;
        }
        let gain = table.vals[i as usize] - kv * cluster_w[c as usize] * res;
        if gain > best.1 || (gain == best.1 && best.0 != own && c < best.0) {
            best = (c, gain);
            w_best = table.vals[i as usize];
        }
    }
    if best.0 == own && cluster_n[own as usize] > 1 && 0.0 > stay {
        best = (NEW_CLUSTER, 0.0);
        w_best = 0.0;
    }
    table.clear();
    (best.0 != own).then_some(Move {
        v,
        to: best.0,
        w_to: w_best,
        w_from,
    })
}

/// Decide the moves of `nodes` in parallel against fixed cluster totals.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `nodes` - Nodes to evaluate, sorted
/// * `membership` - Cluster per node
/// * `cluster_w` - Summed node weight per cluster
/// * `cluster_n` - Nodes per cluster
/// * `node_w` - Node weights
/// * `res` - Resolution divided by `2m`
///
/// ### Returns
///
/// The moves, in the order of `nodes`
fn decide_moves<G: LevelGraph>(
    g: &G,
    nodes: &[u32],
    membership: &[u32],
    cluster_w: &[f64],
    cluster_n: &[u32],
    node_w: &[f64],
    res: f64,
) -> Vec<Move> {
    let decide = |&v: &u32| {
        MOVE_SCRATCH.with(|s| {
            best_move(
                g,
                v,
                membership,
                cluster_w,
                cluster_n,
                node_w[v as usize],
                res,
                &mut s.borrow_mut(),
            )
        })
    };
    nodes
        .par_iter()
        .with_min_len(PAR_MOVE_MIN_LEN)
        .filter_map(decide)
        .collect()
}

/// Flag the neighbours of the moved nodes that now sit in another cluster.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `moved` - Nodes that moved
/// * `membership` - Cluster per node, after the moves
/// * `pending` - Pending flag per node
///
/// ### Returns
///
/// The nodes whose flag this call set, in no particular order
fn flag_neighbours<G: LevelGraph>(
    g: &G,
    moved: &[u32],
    membership: &[u32],
    pending: &[AtomicBool],
) -> Vec<u32> {
    // load first: most neighbours are already pending, and a swap would
    // write their cache line anyway
    let flag = |&v: &u32| {
        let c = membership[v as usize];
        g.row(v as usize).0.iter().copied().filter(move |&u| {
            let p = &pending[u as usize];
            membership[u as usize] != c && !p.load(Relaxed) && !p.swap(true, Relaxed)
        })
    };
    moved
        .par_iter()
        .with_min_len(PAR_MOVE_MIN_LEN)
        .flat_map_iter(flag)
        .collect()
}

/// Take a pending list: sort it (so the result does not depend on the order
/// in which the flags were set) and clear the flags.
///
/// ### Params
///
/// * `list` - Pending nodes; emptied
/// * `pending` - Pending flag per node
///
/// ### Returns
///
/// The nodes, sorted
fn take_pending(list: &mut Vec<u32>, pending: &[AtomicBool]) -> Vec<u32> {
    let mut nodes = std::mem::take(list);
    nodes.par_sort_unstable();
    nodes
        .par_iter()
        .with_min_len(PAR_MOVE_MIN_LEN)
        .for_each(|&v| pending[v as usize].store(false, Relaxed));
    nodes
}

/// Summed node weight and node count per cluster.
///
/// ### Params
///
/// * `membership` - Cluster per node, labels `< n`
/// * `node_w` - Node weights
///
/// ### Returns
///
/// `(cluster weights, cluster sizes, empty clusters in descending id order)`
fn cluster_totals(membership: &[u32], node_w: &[f64]) -> (Vec<f64>, Vec<u32>, Vec<u32>) {
    let n = membership.len();
    let mut cluster_w = vec![0.0f64; n];
    let mut cluster_n = vec![0u32; n];
    for (v, &c) in membership.iter().enumerate() {
        cluster_w[c as usize] += node_w[v];
        cluster_n[c as usize] += 1;
    }
    let empty = (0..n as u32)
        .rev()
        .filter(|&c| cluster_n[c as usize] == 0)
        .collect();
    (cluster_w, cluster_n, empty)
}

/// Greedy first-fit colouring in the given order.
///
/// The colours of a node's neighbours below 64 go into one `u64` mask, so the
/// first free colour is a `trailing_ones`; only a node whose neighbours hold
/// all of 0..64 rescans with a full flag array. kNN graphs need far fewer
/// colours than that.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `order` - Visiting order
///
/// ### Returns
///
/// `(colour classes, each sorted by node id; colour per node)`
fn greedy_colouring<G: LevelGraph>(g: &G, order: &[u32]) -> (Vec<Vec<u32>>, Vec<u32>) {
    let n = g.n();
    let mut colour = vec![u32::MAX; n];
    let mut used: Vec<bool> = Vec::new();
    let mut classes: Vec<Vec<u32>> = Vec::new();
    for &v in order {
        let nbrs = g.row(v as usize).0;
        let mut mask = 0u64;
        for &u in nbrs {
            let c = colour[u as usize];
            if c < 64 {
                mask |= 1 << c;
            }
        }
        let c = if mask != u64::MAX {
            mask.trailing_ones() as usize
        } else {
            used.resize(classes.len(), false);
            for &u in nbrs {
                let cu = colour[u as usize];
                if cu != u32::MAX {
                    used[cu as usize] = true;
                }
            }
            let c = used.iter().position(|&x| !x).unwrap_or(used.len());
            used.iter_mut().for_each(|x| *x = false);
            c
        };
        if c == classes.len() {
            classes.push(Vec::new());
        }
        colour[v as usize] = c as u32;
        classes[c].push(v);
    }
    classes.par_iter_mut().for_each(|c| c.sort_unstable());
    (classes, colour)
}

/// Greedy colouring in node order from a seeded start.
///
/// At level 0 node order is BFS order, which keeps the scan cache-local.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `rng` - Random number generator for the start
///
/// ### Returns
///
/// `(colour classes, each sorted by node id; colour per node)`
fn seeded_colouring<G: LevelGraph>(g: &G, rng: &mut StdRng) -> (Vec<Vec<u32>>, Vec<u32>) {
    let n = g.n();
    let start = rng.random_range(0..n) as u32;
    let order: Vec<u32> = (start..n as u32).chain(0..start).collect();
    greedy_colouring(g, &order)
}

/// Local moving over greedy colour classes, see [`LocalMoving::Coloured`].
///
/// Colours come from [`seeded_colouring`]. Each class keeps a list of its
/// pending nodes; the classes are swept in turn until none is pending, so a
/// sweep costs what its pending nodes cost.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights
/// * `membership` - Initial partition, labels `< n`; updated and reindexed
/// * `res` - Resolution divided by `2m`
/// * `max_evals` - Cap on node evaluations
/// * `rng` - Random number generator for the colouring start
///
/// ### Returns
///
/// `(number of clusters, whether any node moved)`
///
/// ### References
///
/// Lu, Halappanavar & Kalyanaraman, Parallel Computing, 2015 (colouring);
/// Sahu, arXiv:2312.13936, 2023 (GVE-Leiden, pruning)
fn move_nodes_coloured<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &mut [u32],
    res: f64,
    max_evals: usize,
    rng: &mut StdRng,
) -> (usize, bool) {
    let n = g.n();
    let (classes, colour) = seeded_colouring(g, rng);

    let (mut cluster_w, mut cluster_n, mut empty) = cluster_totals(membership, node_w);
    let pending: Vec<AtomicBool> = (0..n).map(|_| AtomicBool::new(true)).collect();
    let mut lists = classes;
    let mut moved: Vec<u32> = Vec::new();
    let mut changed = false;
    let mut evals = 0usize;

    'sweeps: for _ in 0..PAR_MOVE_MAX_ROUNDS {
        if lists.iter().all(|l| l.is_empty()) {
            break;
        }
        for c in 0..lists.len() {
            if lists[c].is_empty() {
                continue;
            }
            let nodes = take_pending(&mut lists[c], &pending);
            evals += nodes.len();
            let moves = decide_moves(g, &nodes, membership, &cluster_w, &cluster_n, node_w, res);
            moved.clear();
            for m in moves {
                let (v, kv) = (m.v as usize, node_w[m.v as usize]);
                let from = membership[v] as usize;
                let stay = m.w_from - kv * (cluster_w[from] - kv) * res;
                let go = if m.to == NEW_CLUSTER {
                    0.0
                } else {
                    m.w_to - kv * cluster_w[m.to as usize] * res
                };
                // no longer an improvement, or already alone
                if go <= stay || (m.to == NEW_CLUSTER && cluster_n[from] == 1) {
                    continue;
                }
                let to = if m.to == NEW_CLUSTER {
                    empty
                        .pop()
                        .expect("a cluster with two or more nodes leaves one id free")
                } else {
                    m.to
                } as usize;
                cluster_w[from] -= kv;
                cluster_n[from] -= 1;
                if cluster_n[from] == 0 {
                    empty.push(from as u32);
                }
                cluster_w[to] += kv;
                cluster_n[to] += 1;
                membership[v] = to as u32;
                moved.push(v as u32);
            }
            changed |= !moved.is_empty();
            for u in flag_neighbours(g, &moved, membership, &pending) {
                lists[colour[u as usize] as usize].push(u);
            }
            if evals >= max_evals {
                break 'sweeps;
            }
        }
    }

    (reindex(membership), changed)
}

////////////////
// Refinement //
////////////////

/// Per-thread scratch for refining one cluster, indexed by local position.
///
/// ### Fields
///
/// * `cluster_w` - Summed node weight per refined sub-cluster
/// * `ext` - Edge weight from a sub-cluster to the rest of its cluster
/// * `w_to` - Edge weight from the current node to each sub-cluster
/// * `added` - Membership flags for `cands`
/// * `non_singleton` - Sub-cluster has already absorbed a node
/// * `cands` - Candidate sub-clusters for the current node
/// * `cum` - Cumulative sampling weights aligned with `cands`
/// * `gains` - Gains aligned with `cands` (`NAN` when not eligible)
#[derive(Default)]
struct RefineScratch {
    cluster_w: Vec<f64>,
    ext: Vec<f64>,
    w_to: Vec<f64>,
    added: Vec<bool>,
    non_singleton: Vec<bool>,
    cands: Vec<u32>,
    cum: Vec<f64>,
    gains: Vec<f64>,
}

impl RefineScratch {
    /// Reset the scratch for a cluster of `s` nodes.
    ///
    /// ### Params
    ///
    /// * `s` - Number of nodes in the cluster
    fn reset(&mut self, s: usize) {
        for v in [&mut self.cluster_w, &mut self.ext, &mut self.w_to] {
            v.clear();
            v.resize(s, 0.0);
        }
        for v in [&mut self.added, &mut self.non_singleton] {
            v.clear();
            v.resize(s, false);
        }
    }
}

/// Refine one cluster by randomised constrained merging (Traag et al. 2019,
/// Alg. A.2 `MergeNodesSubset`).
///
/// Every node starts as its own sub-cluster. Each node is visited once in a
/// random order; if it is still a singleton and well connected to the rest of
/// the cluster, it joins a well-connected sub-cluster with non-negative gain,
/// drawn with probability proportional to `exp(gain / beta)`. Staying put has
/// gain 0 and is always a candidate.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights
/// * `membership` - Unrefined partition of this level
/// * `local_idx` - Position of each node within its cluster's member list
/// * `members` - Nodes of this cluster, in increasing order
/// * `cluster` - Label of this cluster in `membership`
/// * `res` - Resolution divided by `2m`
/// * `beta` - Randomness of the merges
/// * `seed` - Seed for this cluster
/// * `sc` - Scratch buffers
///
/// ### Returns
///
/// `(local sub-cluster per member, number of sub-clusters)`, labels
/// contiguous in order of first appearance
#[allow(clippy::too_many_arguments)]
fn refine_cluster<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &[u32],
    local_idx: &[u32],
    members: &[u32],
    cluster: u32,
    res: f64,
    beta: f64,
    seed: u64,
    sc: &mut RefineScratch,
) -> (Vec<u32>, usize) {
    let s = members.len();
    let mut label: Vec<u32> = (0..s as u32).collect();
    if s == 1 {
        return (label, 1);
    }
    sc.reset(s);

    let mut total_w = 0.0f64;
    for (i, &v) in members.iter().enumerate() {
        let v = v as usize;
        sc.cluster_w[i] = node_w[v];
        total_w += node_w[v];
        let (nbrs, ws) = g.row(v);
        for (&u, &w) in nbrs.iter().zip(ws) {
            if u as usize != v && membership[u as usize] == cluster {
                sc.ext[i] += wf(w);
            }
        }
    }

    let mut rng = StdRng::seed_from_u64(seed);
    let mut order: Vec<u32> = (0..s as u32).collect();
    order.shuffle(&mut rng);

    for &i in &order {
        let i = i as usize;
        let cur = label[i] as usize;
        let kv = node_w[members[i] as usize];
        let well_connected = |sc: &RefineScratch, c: usize| {
            sc.ext[c] >= sc.cluster_w[c] * (total_w - sc.cluster_w[c]) * res
        };

        if sc.non_singleton[cur] || !well_connected(sc, cur) {
            continue;
        }

        let ext_v = sc.ext[cur];
        sc.cluster_w[cur] = 0.0;

        sc.cands.push(cur as u32);
        sc.added[cur] = true;
        let (nbrs, ws) = g.row(members[i] as usize);
        for (&u, &w) in nbrs.iter().zip(ws) {
            let u = u as usize;
            if u == members[i] as usize || membership[u] != cluster {
                continue;
            }
            let c = label[local_idx[u] as usize] as usize;
            if !sc.added[c] {
                sc.added[c] = true;
                sc.cands.push(c as u32);
            }
            sc.w_to[c] += wf(w);
        }

        let mut best = cur;
        let mut max_gain = 0.0f64;
        for &c in &sc.cands {
            let c = c as usize;
            let gain = if well_connected(sc, c) {
                sc.w_to[c] - kv * sc.cluster_w[c] * res
            } else {
                f64::NAN
            };
            if gain > max_gain {
                max_gain = gain;
                best = c;
            }
            sc.gains.push(gain);
        }

        // shift by the max gain so exp() cannot overflow; same distribution
        let mut total = 0.0f64;
        for &gain in &sc.gains {
            if gain >= 0.0 {
                total += ((gain - max_gain) / beta).exp();
            }
            sc.cum.push(total);
        }
        let chosen = if total > 0.0 && total.is_finite() {
            let r = rng.random::<f64>() * total;
            let idx = sc.cum.partition_point(|&x| x <= r).min(sc.cands.len() - 1);
            sc.cands[idx] as usize
        } else {
            best
        };

        let w_chosen = sc.w_to[chosen];
        for &c in &sc.cands {
            sc.w_to[c as usize] = 0.0;
            sc.added[c as usize] = false;
        }
        sc.cands.clear();
        sc.gains.clear();
        sc.cum.clear();

        sc.cluster_w[chosen] += kv;
        if chosen != cur {
            // E(T + v, C - T - v) = E(T, C - T) + E(v, C - v) - 2 E(v, T)
            sc.ext[chosen] += ext_v - 2.0 * w_chosen;
            label[i] = chosen as u32;
            sc.non_singleton[chosen] = true;
        }
    }

    let k = reindex(&mut label);
    (label, k)
}

/// Refine every cluster of the unrefined partition.
///
/// Parallel over clusters when the level is large enough. Each cluster draws
/// from its own seeded RNG, and sub-cluster ids are laid out in cluster order,
/// so the result does not depend on the thread count.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights
/// * `membership` - Unrefined partition, contiguous labels
/// * `n_clusters` - Number of clusters in `membership`
/// * `res` - Resolution divided by `2m`
/// * `beta` - Randomness of the merges
/// * `seed` - Seed for this iteration and level
///
/// ### Returns
///
/// `(refined partition, number of refined clusters)`
fn refine_partition<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &[u32],
    n_clusters: usize,
    res: f64,
    beta: f64,
    seed: u64,
) -> (Vec<u32>, usize) {
    let n = g.n();
    let (offsets, members) = group_by_label(membership, n_clusters);
    let mut local_idx = vec![0u32; n];
    for c in 0..n_clusters {
        for (i, &v) in members[offsets[c]..offsets[c + 1]].iter().enumerate() {
            local_idx[v as usize] = i as u32;
        }
    }

    let run = |sc: &mut RefineScratch, c: usize| {
        refine_cluster(
            g,
            node_w,
            membership,
            &local_idx,
            &members[offsets[c]..offsets[c + 1]],
            c as u32,
            res,
            beta,
            derive_seed(seed, &[c as u64]),
            sc,
        )
    };
    let per_cluster: Vec<(Vec<u32>, usize)> = if n >= LEIDEN_PAR_MIN_NODES {
        (0..n_clusters)
            .into_par_iter()
            .map_init(RefineScratch::default, run)
            .collect()
    } else {
        let mut sc = RefineScratch::default();
        (0..n_clusters).map(|c| run(&mut sc, c)).collect()
    };

    let mut refined = vec![0u32; n];
    let mut base = 0u32;
    for (c, (labels, k)) in per_cluster.into_iter().enumerate() {
        for (&v, &l) in members[offsets[c]..offsets[c + 1]].iter().zip(&labels) {
            refined[v as usize] = base + l;
        }
        base += k as u32;
    }
    (refined, base as usize)
}

/////////////////
// Aggregation //
/////////////////

/// Collapse each refined cluster into one node.
///
/// Parallel over refined clusters when the level is large enough, with a
/// dense per-thread accumulator cleared through its touched list. Rows come
/// out in member order, so the sums are deterministic.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights
/// * `refined` - Refined partition, contiguous labels
/// * `n_refined` - Number of refined clusters
/// * `membership` - Unrefined partition; refines `refined`
///
/// ### Returns
///
/// `(aggregated graph, aggregated node weights, initial aggregated partition)`
fn aggregate<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    refined: &[u32],
    n_refined: usize,
    membership: &[u32],
) -> (AggGraph<f64>, Vec<f64>, Vec<u32>) {
    let (offsets, members) = group_by_label(refined, n_refined);

    let row = |(acc, seen, touched): &mut (Vec<f64>, Vec<bool>, Vec<u32>), c: usize| {
        if acc.len() < n_refined {
            acc.resize(n_refined, 0.0);
            seen.resize(n_refined, false);
        }
        let mut w_sum = 0.0;
        for &v in &members[offsets[c]..offsets[c + 1]] {
            w_sum += node_w[v as usize];
            let (nbrs, ws) = g.row(v as usize);
            for (&u, &w) in nbrs.iter().zip(ws) {
                let c2 = refined[u as usize];
                if c2 as usize == c {
                    continue;
                }
                if !seen[c2 as usize] {
                    seen[c2 as usize] = true;
                    touched.push(c2);
                }
                acc[c2 as usize] += wf(w);
            }
        }
        let mut idx = Vec::with_capacity(touched.len());
        let mut wts = Vec::with_capacity(touched.len());
        for &c2 in touched.iter() {
            idx.push(c2);
            wts.push(acc[c2 as usize]);
            acc[c2 as usize] = 0.0;
            seen[c2 as usize] = false;
        }
        touched.clear();
        (idx, wts, w_sum)
    };

    let rows: Vec<(Vec<u32>, Vec<f64>, f64)> = if g.n() >= LEIDEN_PAR_MIN_NODES {
        (0..n_refined)
            .into_par_iter()
            .map_init(|| (Vec::new(), Vec::new(), Vec::new()), row)
            .collect()
    } else {
        let mut scratch = (Vec::new(), Vec::new(), Vec::new());
        (0..n_refined).map(|c| row(&mut scratch, c)).collect()
    };

    let mut indptr = Vec::with_capacity(n_refined + 1);
    indptr.push(0usize);
    for r in &rows {
        indptr.push(indptr.last().unwrap() + r.0.len());
    }
    let nnz = *indptr.last().unwrap();
    let mut indices = Vec::with_capacity(nnz);
    let mut weights = Vec::with_capacity(nnz);
    let mut agg_w = Vec::with_capacity(n_refined);
    for (idx, wts, w_sum) in rows {
        indices.extend_from_slice(&idx);
        weights.extend_from_slice(&wts);
        agg_w.push(w_sum);
    }

    let agg_membership = (0..n_refined)
        .map(|c| membership[members[offsets[c]] as usize])
        .collect();

    (
        AggGraph {
            indptr,
            indices,
            weights,
        },
        agg_w,
        agg_membership,
    )
}

////////////////
// Reordering //
////////////////

/// Breadth-first node order, restarting at the lowest unvisited node for
/// each further component.
///
/// ### Params
///
/// * `g` - Graph to order
///
/// ### Returns
///
/// `order`, with `order[new] = old`
fn bfs_order<G: LevelGraph>(g: &G) -> Vec<u32> {
    let n = g.n();
    let mut seen = vec![false; n];
    let mut order = Vec::with_capacity(n);
    for s in 0..n {
        if seen[s] {
            continue;
        }
        seen[s] = true;
        let mut head = order.len();
        order.push(s as u32);
        while head < order.len() {
            let v = order[head] as usize;
            head += 1;
            for &u in g.row(v).0 {
                if !seen[u as usize] {
                    seen[u as usize] = true;
                    order.push(u);
                }
            }
        }
    }
    order
}

/// Relabel a graph into a new node order.
///
/// ### Params
///
/// * `g` - Graph to relabel
/// * `order` - `order[new] = old`
/// * `pos` - Inverse of `order`, `pos[old] = new`
///
/// ### Returns
///
/// The relabelled graph, weights in the original type
fn permute_graph<G: LevelGraph>(g: &G, order: &[u32], pos: &[u32]) -> AggGraph<G::W> {
    let mut indptr = Vec::with_capacity(order.len() + 1);
    indptr.push(0usize);
    for &old in order {
        indptr.push(indptr.last().unwrap() + g.row(old as usize).0.len());
    }
    let nnz = *indptr.last().unwrap();
    let mut indices = vec![0u32; nnz];
    let mut weights = vec![G::W::zero(); nnz];

    // one disjoint (indices, weights) slice per new row, filled in parallel
    let mut rows = Vec::with_capacity(order.len());
    let (mut idx_rest, mut w_rest) = (indices.as_mut_slice(), weights.as_mut_slice());
    for r in 0..order.len() {
        let len = indptr[r + 1] - indptr[r];
        let (idx_row, idx_tail) = idx_rest.split_at_mut(len);
        let (w_row, w_tail) = w_rest.split_at_mut(len);
        rows.push((idx_row, w_row));
        idx_rest = idx_tail;
        w_rest = w_tail;
    }
    rows.par_iter_mut()
        .zip(order.par_iter())
        .for_each(|((idx_row, w_row), &old)| {
            let (nbrs, ws) = g.row(old as usize);
            for (d, &u) in idx_row.iter_mut().zip(nbrs) {
                *d = pos[u as usize];
            }
            w_row.copy_from_slice(ws);
        });

    AggGraph {
        indptr,
        indices,
        weights,
    }
}

//////////
// Core //
//////////

/// One full Leiden (or Louvain) iteration, starting from `membership`.
///
/// Local moving, refinement and aggregation per level, until local moving
/// leaves every node of a level in its own cluster.
///
/// ### Params
///
/// * `graph` - Level-0 graph
/// * `node_w` - Level-0 node weights (degrees)
/// * `membership` - Partition of the level-0 nodes; updated
/// * `res` - Resolution divided by `2m`
/// * `params` - The [`LeidenParams`]
/// * `iteration` - Index of this iteration, for seeding
/// * `max_evals` - Node-evaluation cap per local-moving phase, in units of n
/// * `multi_level` - `false` stops after the first local-moving phase
/// * `level0` - Local moving of level 0: `(graph, node weights, membership,
///   res, rng) -> (number of clusters, whether any node moved)`, membership
///   reindexed
///
/// ### Returns
///
/// Whether any node moved
#[allow(clippy::too_many_arguments)]
fn leiden_iteration<G, E, F>(
    graph: &G,
    node_w: &[f64],
    membership: &mut [u32],
    res: f64,
    params: &LeidenParams,
    iteration: u64,
    max_evals: usize,
    multi_level: bool,
    level0: &mut F,
) -> Result<bool, E>
where
    G: LevelGraph,
    F: FnMut(&G, &[f64], &mut [u32], f64, &mut StdRng) -> Result<(usize, bool), E>,
{
    let n = graph.n();
    let mut rng = StdRng::seed_from_u64(derive_seed(params.seed, &[iteration]));
    reindex(membership);

    let (n_clusters, mut changed) = level0(graph, node_w, membership, res, &mut rng)?;
    if !multi_level || n_clusters == n {
        return Ok(changed);
    }

    let (refined, n_refined) = refined_or_unrefined(
        graph, node_w, membership, n_clusters, res, params, iteration, 0,
    );
    let mut aggregate_node = refined.clone();
    let (mut g, mut w, mut agg_memb) = aggregate(graph, node_w, &refined, n_refined, membership);

    let mut level = 1u64;
    loop {
        let gn = g.n();
        let (nc, ch) = local_move(
            &g,
            &w,
            &mut agg_memb,
            res,
            max_evals.saturating_mul(gn),
            !params.refine,
            params.local_moving,
            &mut rng,
        );
        changed |= ch;
        for (m, &a) in membership.iter_mut().zip(&aggregate_node) {
            *m = agg_memb[a as usize];
        }
        if nc == gn {
            break;
        }

        let (refined, n_refined) =
            refined_or_unrefined(&g, &w, &agg_memb, nc, res, params, iteration, level);
        for a in aggregate_node.iter_mut() {
            *a = refined[*a as usize];
        }
        (g, w, agg_memb) = aggregate(&g, &w, &refined, n_refined, &agg_memb);
        level += 1;
    }

    Ok(changed)
}

/// Refined partition for Leiden, the unrefined one for Louvain.
///
/// As in igraph, a refinement that merged nothing falls back to the unrefined
/// partition, or the hierarchy would not shrink.
///
/// ### Params
///
/// * `g` - Graph of this level
/// * `node_w` - Node weights
/// * `membership` - Unrefined partition, contiguous labels
/// * `n_clusters` - Number of clusters in `membership`
/// * `res` - Resolution divided by `2m`
/// * `params` - The [`LeidenParams`]
/// * `iteration` - Iteration index, for seeding
/// * `level` - Level index, for seeding
///
/// ### Returns
///
/// `(partition to aggregate on, number of clusters in it)`
#[allow(clippy::too_many_arguments)]
fn refined_or_unrefined<G: LevelGraph>(
    g: &G,
    node_w: &[f64],
    membership: &[u32],
    n_clusters: usize,
    res: f64,
    params: &LeidenParams,
    iteration: u64,
    level: u64,
) -> (Vec<u32>, usize) {
    if params.refine {
        let seed = derive_seed(params.seed, &[iteration, level]);
        let (refined, k) =
            refine_partition(g, node_w, membership, n_clusters, res, params.beta, seed);
        if k < g.n() {
            return (refined, k);
        }
    }
    (membership.to_vec(), n_clusters)
}

/// Level-0 local moving on another backend (a GPU), for
/// [`community_detection_with`].
///
/// The engine hands over the BFS-relabelled level-0 graph once, then one
/// colouring per iteration. Coarser levels, refinement and aggregation stay on
/// the CPU.
pub trait LocalMover {
    /// Backend error
    type Error: From<MethodsErrors>;

    /// Take the level-0 graph. Called once, before the first
    /// [`LocalMover::move_nodes`].
    ///
    /// ### Params
    ///
    /// * `indptr` - Row offsets, length n + 1
    /// * `indices` - Column indices, symmetric, self-loops possible
    /// * `weights` - Edge weights
    /// * `node_w` - Node weights (weighted degree, self-loops included)
    ///
    /// ### Returns
    ///
    /// `Ok(())`, or the backend's error
    fn prepare<W: ToPrimitive + Sync>(
        &mut self,
        indptr: &[usize],
        indices: &[u32],
        weights: &[W],
        node_w: &[f64],
    ) -> Result<(), Self::Error>;

    /// Coloured local moving on the prepared graph, see
    /// [`LocalMoving::Coloured`]. The apply rule may differ from the CPU's.
    ///
    /// ### Params
    ///
    /// * `membership` - Initial partition, labels `< n`; updated, labels
    ///   `< n` but not necessarily contiguous
    /// * `classes` - Colour classes, each sorted by node id
    /// * `colour` - Colour per node
    /// * `res` - Resolution divided by `2m`
    ///
    /// ### Returns
    ///
    /// Whether any node moved
    fn move_nodes(
        &mut self,
        membership: &mut [u32],
        classes: &[Vec<u32>],
        colour: &[u32],
        res: f64,
    ) -> Result<bool, Self::Error>;
}

/// Shared body of [`community_detection`] and [`community_detection_with`].
///
/// ### Params
///
/// * `graph` - Undirected [`SparseGraph`]
/// * `params` - The [`LeidenParams`]
/// * `max_evals` - Node-evaluation cap per local-moving phase, in units of n
/// * `multi_level` - `false` stops after the first local-moving phase
/// * `level0` - Level-0 local moving, see [`leiden_iteration`]
///
/// ### Returns
///
/// Community per node, `0` the largest
fn detect<T, E, F>(
    graph: &SparseGraph<T>,
    params: &LeidenParams,
    max_evals: usize,
    multi_level: bool,
    mut level0: F,
) -> Result<Vec<usize>, E>
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
    E: From<MethodsErrors> + Send,
    F: FnMut(&AggGraph<T>, &[f64], &mut [u32], f64, &mut StdRng) -> Result<(usize, bool), E> + Send,
{
    if graph.is_directed() {
        return Err(MethodsErrors::GraphDirectedError.into());
    }
    let n = graph.get_node_number();
    if n == 0 {
        return Ok(Vec::new());
    }
    assert!(n < u32::MAX as usize, "more than u32::MAX nodes");

    let order = bfs_order(graph);
    let mut pos = vec![0u32; n];
    for (new, &old) in order.iter().enumerate() {
        pos[old as usize] = new as u32;
    }
    let g = permute_graph(graph, &order, &pos);

    let node_w: Vec<f64> = (0..n)
        .into_par_iter()
        .map(|v| g.row(v).1.iter().map(|&w| wf(w)).sum())
        .collect();
    let two_m: f64 = node_w.iter().sum();

    let mut membership: Vec<u32> = (0..n as u32).collect();
    if two_m > 0.0 {
        let res = params.resolution / two_m;
        // Run the loop on a pool thread: the parallel local-moving modes issue
        // thousands of short parallel calls, and each one from outside the pool
        // is an injection plus a blocking wake-up.
        rayon::scope(|_| -> Result<(), E> {
            let until_stable = params.n_iterations.is_none();
            let cap = params.n_iterations.map_or(MAX_ITERATIONS, |k| k.max(1));
            let use_gain = until_stable && params.min_gain > 0.0;
            let mut quality = if use_gain {
                level_quality(&g, &node_w, &membership, res, two_m)
            } else {
                0.0
            };
            for it in 0..cap {
                let before = until_stable.then(|| membership.clone());
                leiden_iteration(
                    &g,
                    &node_w,
                    &mut membership,
                    res,
                    params,
                    it as u64,
                    max_evals,
                    multi_level,
                    &mut level0,
                )?;
                if let Some(mut before) = before {
                    // compare partitions, not labels; `before` is already
                    // reindexed unless this is the first iteration
                    reindex(&mut before);
                    let mut after = membership.clone();
                    reindex(&mut after);
                    if before == after {
                        break;
                    }
                }
                if use_gain {
                    let q = level_quality(&g, &node_w, &membership, res, two_m);
                    if q - quality <= params.min_gain {
                        break;
                    }
                    quality = q;
                }
            }
            Ok(())
        })?;
    }

    // back from BFS labels to the caller's node order
    let labels: Vec<usize> = pos
        .iter()
        .map(|&new| membership[new as usize] as usize)
        .collect();
    Ok(remap_communities_by_size(&labels))
}

/// Shared entry for Leiden and Louvain.
///
/// [`leiden_sparse_graph`] and [`louvain_sparse_graph`] are thin wrappers;
/// call this directly to cap the node evaluations or stop after one level.
///
/// ### Params
///
/// * `graph` - Undirected [`SparseGraph`]
/// * `params` - The [`LeidenParams`]
/// * `max_evals` - Node-evaluation cap per local-moving phase, in units of n
/// * `multi_level` - `false` stops after the first local-moving phase
///
/// ### Returns
///
/// Community per node, `0` the largest
pub fn community_detection<T>(
    graph: &SparseGraph<T>,
    params: &LeidenParams,
    max_evals: usize,
    multi_level: bool,
) -> Result<Vec<usize>, MethodsErrors>
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
{
    let (shuffle, mode) = (!params.refine, params.local_moving);
    detect(
        graph,
        params,
        max_evals,
        multi_level,
        |g, node_w, membership, res, rng| {
            let evals = max_evals.saturating_mul(g.n());
            Ok(local_move(
                g, node_w, membership, res, evals, shuffle, mode, rng,
            ))
        },
    )
}

/// [`community_detection`] with level-0 local moving on a [`LocalMover`].
///
/// Every iteration colours level 0 as [`LocalMoving::Coloured`] does and
/// hands the classes to `mover`; coarser levels use `params.local_moving` on
/// the CPU. `max_evals` caps the CPU levels only.
///
/// ### Params
///
/// * `graph` - Undirected [`SparseGraph`]
/// * `params` - The [`LeidenParams`]
/// * `max_evals` - Node-evaluation cap per CPU local-moving phase, in units
///   of n
/// * `multi_level` - `false` stops after the first local-moving phase
/// * `mover` - The level-0 [`LocalMover`]
///
/// ### Returns
///
/// Community per node, `0` the largest
pub fn community_detection_with<T, M>(
    graph: &SparseGraph<T>,
    params: &LeidenParams,
    max_evals: usize,
    multi_level: bool,
    mover: &mut M,
) -> Result<Vec<usize>, M::Error>
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
    M: LocalMover + Send,
    M::Error: Send,
{
    let mut prepared = false;
    detect(
        graph,
        params,
        max_evals,
        multi_level,
        |g, node_w, membership, res, rng| {
            if !prepared {
                mover.prepare(&g.indptr, &g.indices, &g.weights, node_w)?;
                prepared = true;
            }
            let (classes, colour) = seeded_colouring(g, rng);
            let changed = mover.move_nodes(membership, &classes, &colour, res)?;
            Ok((reindex(membership), changed))
        },
    )
}

//////////
// Main //
//////////

/// Leiden community detection on modularity.
///
/// Mirrors `igraph::cluster_leiden(objective_function = "modularity")`.
/// Communities are guaranteed to be connected.
///
/// ### Params
///
/// * `graph` - Undirected [`SparseGraph`]
/// * `params` - Optional [`LeidenParams`]; defaults match igraph
///
/// ### Returns
///
/// Community per node, relabelled by size (`0` the largest)
///
/// ### References
///
/// Traag, Waltman & van Eck, Scientific Reports, 2019
pub fn leiden_sparse_graph<T>(
    graph: &SparseGraph<T>,
    params: Option<LeidenParams>,
) -> Result<Vec<usize>, MethodsErrors>
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
{
    let params = params.unwrap_or_default();
    community_detection(graph, &params, usize::MAX, true)
}

/// Louvain community detection on modularity.
///
/// The Leiden engine with the refinement switched off. Multi-level runs two
/// iterations, since the second still improves the partition and later ones
/// rarely change it. Single-level runs one, the single local-moving phase of
/// Phenograph and doubletdetection.
///
/// ### Params
///
/// * `graph` - Undirected [`SparseGraph`]
/// * `resolution` - Resolution parameter
/// * `max_iter` - Cap on node evaluations per local-moving phase, in units of
///   the level's node count. A safeguard for degenerate graphs; the queue
///   terminates on its own otherwise.
/// * `multi_level` - If `true`, run the full multi-level Louvain. If `false`,
///   run only the first local-moving phase (Phenograph / doubletdetection
///   behaviour).
/// * `seed` - Seed for reproducibility
///
/// ### Returns
///
/// Community per node, relabelled by size (`0` the largest)
///
/// ### References
///
/// Blondel et al., J. Stat. Mech., 2008
pub fn louvain_sparse_graph<T>(
    graph: &SparseGraph<T>,
    resolution: T,
    max_iter: usize,
    multi_level: bool,
    seed: usize,
) -> Result<Vec<usize>, MethodsErrors>
where
    T: BixverseFloat + Send + Sync + std::iter::Sum,
{
    let iterations = if multi_level { 2 } else { 1 };
    let params = LeidenParams::new(wf(resolution), 0.01, Some(iterations), false, seed as u64);
    community_detection(graph, &params, max_iter, multi_level)
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;
    use bixverse_core::core::math::sparse::coo_to_csr;

    /// Zachary karate club, 0-based, from `igraph::make_graph("Zachary")`.
    const KARATE: [usize; 156] = [
        0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 10, 0, 11, 0, 12, 0, 13, 0, 17, 0, 19,
        0, 21, 0, 31, 1, 2, 1, 3, 1, 7, 1, 13, 1, 17, 1, 19, 1, 21, 1, 30, 2, 3, 2, 7, 2, 27, 2,
        28, 2, 32, 2, 9, 2, 8, 2, 13, 3, 7, 3, 12, 3, 13, 4, 6, 4, 10, 5, 6, 5, 10, 5, 16, 6, 16,
        8, 30, 8, 32, 8, 33, 9, 33, 13, 33, 14, 32, 14, 33, 15, 32, 15, 33, 18, 32, 18, 33, 19, 33,
        20, 32, 20, 33, 22, 32, 22, 33, 23, 25, 23, 27, 23, 32, 23, 33, 23, 29, 24, 25, 24, 27, 24,
        31, 25, 31, 26, 29, 26, 33, 27, 33, 28, 31, 28, 33, 29, 32, 29, 33, 30, 32, 30, 33, 31, 32,
        31, 33, 32, 33,
    ];

    fn build_undirected<T: BixverseFloat + BixverseNumeric + std::iter::Sum>(
        n: usize,
        edges: &[(usize, usize, T)],
        directed: bool,
    ) -> SparseGraph<T> {
        let mut rows = Vec::with_capacity(edges.len() * 2);
        let mut cols = Vec::with_capacity(edges.len() * 2);
        let mut vals = Vec::with_capacity(edges.len() * 2);
        for &(u, v, w) in edges {
            rows.extend([u, v]);
            cols.extend([v, u]);
            vals.extend([w, w]);
        }
        let csr = coo_to_csr(&rows.index_cast(), &cols.index_cast(), &vals, (n, n));
        SparseGraph::new(n, csr, directed)
    }

    fn karate() -> SparseGraph<f64> {
        let edges: Vec<(usize, usize, f64)> =
            KARATE.chunks_exact(2).map(|e| (e[0], e[1], 1.0)).collect();
        build_undirected(34, &edges, false)
    }

    /// Planted partition: `k` blocks of `size` nodes, `d_in` random edges per
    /// node inside its block and `d_out` outside, integer weights 1..=3.
    fn planted(
        k: usize,
        size: usize,
        d_in: usize,
        d_out: usize,
        seed: u64,
    ) -> Vec<(usize, usize, f64)> {
        let n = k * size;
        let mut rng = StdRng::seed_from_u64(seed);
        let mut edges = Vec::new();
        for v in 0..n {
            let b = v / size;
            for _ in 0..d_in {
                let u = b * size + rng.random_range(0..size);
                if u != v {
                    edges.push((v, u, rng.random_range(1..=3) as f64));
                }
            }
            for _ in 0..d_out {
                let u = rng.random_range(0..n);
                if u / size != b {
                    edges.push((v, u, 1.0));
                }
            }
        }
        edges
    }

    fn n_communities(labels: &[usize]) -> usize {
        labels.iter().max().map_or(0, |m| m + 1)
    }

    /// Every community induces a connected subgraph.
    fn all_connected(graph: &SparseGraph<f64>, labels: &[usize]) -> bool {
        let n = graph.get_node_number();
        let mut seen = vec![false; n];
        let mut roots = vec![false; n_communities(labels)];
        for s in 0..n {
            if seen[s] {
                continue;
            }
            if roots[labels[s]] {
                return false;
            }
            roots[labels[s]] = true;
            let mut stack = vec![s];
            seen[s] = true;
            while let Some(v) = stack.pop() {
                for &u in graph.get_neighbours_raw(v).0 {
                    let u = u as usize;
                    if !seen[u] && labels[u] == labels[v] {
                        seen[u] = true;
                        stack.push(u);
                    }
                }
            }
        }
        true
    }

    /// Modularity matches `igraph::modularity` (values from Rscript).
    #[test]
    fn test_modularity_matches_igraph() {
        let g = karate();
        let labels: Vec<usize> = (0..34).map(|i| i % 2).collect();
        approx::assert_relative_eq!(
            modularity(&g, &labels, 1.0),
            -0.000082182774,
            epsilon = 1e-10
        );
        approx::assert_relative_eq!(
            modularity(&g, &labels, 0.5),
            0.249958908613,
            epsilon = 1e-10
        );
    }

    /// Leiden on karate reaches igraph's seed range (Rscript, 50 seeds:
    /// min 0.418803, median and max 0.419790).
    #[test]
    fn test_leiden_karate_modularity() {
        let g = karate();
        for seed in 0..10 {
            let p = LeidenParams {
                seed,
                ..Default::default()
            };
            let labels = leiden_sparse_graph(&g, Some(p)).unwrap();
            let q = modularity(&g, &labels, 1.0);
            assert!(q >= 0.418803 - 1e-6, "seed {seed}: modularity {q}");
            assert!(all_connected(&g, &labels));
        }
    }

    /// Louvain on karate stays within igraph's Louvain range (min 0.388560).
    #[test]
    fn test_louvain_karate_modularity() {
        let g = karate();
        for seed in 0..10 {
            let labels = louvain_sparse_graph(&g, 1.0, 50, true, seed).unwrap();
            let q = modularity(&g, &labels, 1.0);
            assert!(q >= 0.388560 - 1e-6, "seed {seed}: modularity {q}");
        }
    }

    /// Planted blocks are recovered exactly.
    #[test]
    fn test_leiden_recovers_planted_blocks() {
        let edges = planted(8, 200, 10, 1, 7);
        let g = build_undirected(1600, &edges, false);
        let labels = leiden_sparse_graph(&g, None).unwrap();
        assert_eq!(n_communities(&labels), 8);
        for v in 0..1600 {
            assert_eq!(labels[v], labels[(v / 200) * 200]);
        }
    }

    /// Same seed gives the same partition at 1 and 4 threads, on a graph large
    /// enough to take the parallel refinement and aggregation paths.
    #[test]
    fn test_leiden_thread_count_invariant() {
        let edges = planted(20, 400, 8, 3, 11);
        let g = build_undirected(8000, &edges, false);
        assert!(g.get_node_number() >= LEIDEN_PAR_MIN_NODES);
        let run = |threads: usize| {
            rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .unwrap()
                .install(|| leiden_sparse_graph(&g, None).unwrap())
        };
        let one = run(1);
        assert_eq!(one, run(4));
        assert!(all_connected(&g, &one));
    }

    /// Iterating until stable terminates, gives the same partition at 1 and 4
    /// threads (with and without `min_gain`, whose modularity sums are fixed
    /// order), keeps Leiden's communities connected, and lands at least as
    /// high as two iterations on a noisy planted partition.
    #[test]
    fn test_leiden_until_stable() {
        let edges = planted(20, 400, 8, 3, 11);
        let g = build_undirected(8000, &edges, false);
        let q_two = modularity(&g, &leiden_sparse_graph(&g, None).unwrap(), 1.0);
        for min_gain in [0.0, 1e-4] {
            let p = LeidenParams {
                n_iterations: None,
                min_gain,
                ..Default::default()
            };
            let run = |threads: usize| {
                rayon::ThreadPoolBuilder::new()
                    .num_threads(threads)
                    .build()
                    .unwrap()
                    .install(|| leiden_sparse_graph(&g, Some(p)).unwrap())
            };
            let one = run(1);
            assert_eq!(one, run(4), "min_gain {min_gain}");
            assert!(all_connected(&g, &one));
            let q = modularity(&g, &one, 1.0);
            assert!(q >= q_two - 1e-3, "min_gain {min_gain}: {q} vs {q_two}");
        }
    }

    /// Leiden or Louvain with the given local-moving mode.
    fn run_mode(g: &SparseGraph<f64>, mode: LocalMoving, refine: bool, seed: u64) -> Vec<usize> {
        let p = LeidenParams {
            refine,
            n_iterations: Some(if refine { 2 } else { 1 }),
            seed,
            local_moving: mode,
            ..Default::default()
        };
        community_detection(g, &p, usize::MAX, true).unwrap()
    }

    /// Coloured gives the same partition at 1 and 4 threads, for Leiden and
    /// Louvain, and Leiden's communities stay connected.
    #[test]
    fn test_parallel_local_moving_thread_count_invariant() {
        let edges = planted(20, 400, 8, 3, 11);
        let g = build_undirected(8000, &edges, false);
        for refine in [true, false] {
            let run = |threads: usize| {
                rayon::ThreadPoolBuilder::new()
                    .num_threads(threads)
                    .build()
                    .unwrap()
                    .install(|| run_mode(&g, LocalMoving::Coloured, refine, 3))
            };
            let one = run(1);
            assert_eq!(one, run(4), "refine {refine}");
            if refine {
                assert!(all_connected(&g, &one));
            }
        }
    }

    /// Coloured recovers planted blocks exactly, on a graph large enough that
    /// it runs instead of the queue.
    #[test]
    fn test_parallel_local_moving_recovers_planted_blocks() {
        let edges = planted(8, 600, 10, 1, 7);
        let g = build_undirected(4800, &edges, false);
        assert!(g.get_node_number() >= LEIDEN_PAR_MIN_NODES);
        for refine in [true, false] {
            let labels = run_mode(&g, LocalMoving::Coloured, refine, 1);
            assert_eq!(n_communities(&labels), 8, "refine {refine}");
            for v in 0..4800 {
                assert_eq!(labels[v], labels[(v / 600) * 600], "refine {refine}");
            }
        }
    }

    /// The mask colouring equals a plain first-fit pass, including past 64
    /// colours (an 80-node clique on top of a planted graph).
    #[test]
    fn test_greedy_colouring_matches_first_fit() {
        let mut edges = planted(4, 100, 12, 2, 5);
        for u in 0..80 {
            for v in u + 1..80 {
                edges.push((u, v, 1.0));
            }
        }
        let g = build_undirected(400, &edges, false);
        let order: Vec<u32> = (150..400).chain(0..150).collect();
        let (classes, colour) = greedy_colouring(&g, &order);

        let mut want = vec![u32::MAX; 400];
        for &v in &order {
            let taken: Vec<u32> = g
                .row(v as usize)
                .0
                .iter()
                .map(|&u| want[u as usize])
                .collect();
            want[v as usize] = (0..).find(|c| !taken.contains(c)).unwrap();
        }
        assert_eq!(colour, want);
        assert!(classes.len() >= 80);
        for (c, members) in classes.iter().enumerate() {
            assert!(members.is_sorted());
            assert!(members.iter().all(|&v| colour[v as usize] == c as u32));
        }
    }

    /// Below `LEIDEN_PAR_MIN_NODES` Coloured takes the queue, so karate gives
    /// the queue's partition.
    #[test]
    fn test_parallel_local_moving_small_graph_uses_queue() {
        let g = karate();
        assert_eq!(
            run_mode(&g, LocalMoving::Coloured, true, 5),
            run_mode(&g, LocalMoving::Queue, true, 5)
        );
    }

    /// On a noisy planted partition Coloured lands within 1% of the queue's
    /// modularity, for Leiden and Louvain.
    #[test]
    fn test_parallel_local_moving_modularity_close_to_queue() {
        let edges = planted(20, 400, 8, 3, 11);
        let g = build_undirected(8000, &edges, false);
        for refine in [true, false] {
            let q_queue = modularity(&g, &run_mode(&g, LocalMoving::Queue, refine, 2), 1.0);
            let q = modularity(&g, &run_mode(&g, LocalMoving::Coloured, refine, 2), 1.0);
            assert!(
                q >= 0.99 * q_queue,
                "refine {refine}: {q} vs queue {q_queue}"
            );
        }
    }

    /// The same seed never gives a different partition.
    #[test]
    fn test_leiden_same_seed_same_labels() {
        let edges = planted(10, 100, 6, 3, 3);
        let g = build_undirected(1000, &edges, false);
        let p = LeidenParams {
            seed: 5,
            ..Default::default()
        };
        assert_eq!(
            leiden_sparse_graph(&g, Some(p)).unwrap(),
            leiden_sparse_graph(&g, Some(p)).unwrap()
        );
    }

    /// f32 and f64 weights give the same partition when the weights are exact
    /// in both, since all totals are held in f64.
    #[test]
    fn test_leiden_f32_matches_f64() {
        let edges = planted(6, 150, 6, 3, 9);
        let edges32: Vec<(usize, usize, f32)> =
            edges.iter().map(|&(u, v, w)| (u, v, w as f32)).collect();
        let g64 = build_undirected(900, &edges, false);
        let g32 = build_undirected(900, &edges32, false);
        assert_eq!(
            leiden_sparse_graph(&g64, None).unwrap(),
            leiden_sparse_graph(&g32, None).unwrap()
        );
    }

    /// Leiden communities are connected on a sparse noisy graph.
    #[test]
    fn test_leiden_communities_connected() {
        let edges = planted(30, 60, 3, 2, 21);
        let g = build_undirected(1800, &edges, false);
        for seed in 0..5 {
            let p = LeidenParams {
                seed,
                ..Default::default()
            };
            let labels = leiden_sparse_graph(&g, Some(p)).unwrap();
            assert!(all_connected(&g, &labels), "seed {seed}");
        }
    }

    #[test]
    fn test_leiden_directed_errors() {
        let edges: Vec<(usize, usize, f64)> =
            KARATE.chunks_exact(2).map(|e| (e[0], e[1], 1.0)).collect();
        let g = build_undirected(34, &edges, true);
        assert!(leiden_sparse_graph(&g, None).is_err());
    }

    #[test]
    fn test_leiden_empty_and_edgeless() {
        let g = build_undirected::<f64>(0, &[], false);
        assert!(leiden_sparse_graph(&g, None).unwrap().is_empty());
        let g = build_undirected::<f64>(3, &[], false);
        assert_eq!(leiden_sparse_graph(&g, None).unwrap().len(), 3);
    }
}