arrowspace 0.26.2

Graph Wiring for embeddings using physical networks wiring. Provides graph analysis, vector search and a energy-distribution stats.
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
//! ArrowSpace: enhanced with search-specific zero-copy operations.
//!
//! This module provides two core abstractions for working with row-major numeric
//! data in search/graph contexts:
//!
//! - ArrowItem: an owned row with convenience methods (norm, dot, cosine_similarity,
//!   lambda-aware similarity), in-place arithmetic, and iterator access.
//! - ArrowSpace: a dense, row-major, zero-copy container of rows with per-row
//!   spectral score `lambda`, supporting row views (immutable/mutable), iteration,
//!   and search utilities.
//!
//! Design goals:
//! - Zero-copy access to rows for performance-critical routines.
//! - Iterator-first APIs for cache-friendly, allocation-free operations.
//! - Spectral-aware scoring via Rayleigh quotient against a Graph Laplacian.
//!
//!
//! Zero-copy mutate a row using a mutable view and update its lambda from a graph:
//!
//!
//! Run documentation tests with `cargo test --doc`; Rustdoc extracts code blocks
//! and executes them as tests, ensuring examples stay correct over time.
//!
//! # Panics
//!
//! - Indexing functions panic on out-of-bounds row/column indices.
//! - Arithmetic between mismatched row lengths panics.
//!
//! # Performance
//!
//! - Row accessors favor zero-copy slices/views; prefer `row_view`/`row_view_mut`
//!   over `get_row` when allocation must be avoided.
//! - Batch operations rely on iterators to minimize bounds checks and enable
//!   vectorization opportunities.
//!
//! # Testing examples
//!
//! Rustdoc preprocesses examples: it injects the crate, wraps code in `fn main`
//! if missing, and allows common lints to reduce boilerplate. Keep examples
//! small and focused; add hidden setup lines with `#` when needed so that examples
//! compile while showing only the essential lines to readers.

use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::{BinaryHeap, HashSet};
use std::fmt::Debug;

use approx::relative_eq;
use rayon::prelude::*;
use smartcore::linalg::basic::arrays::{Array, Array2, MutArray};
use smartcore::linalg::basic::matrix::DenseMatrix;
use sprs::CsMat;

use crate::builder::ConfigValue;
use crate::graph::GraphLaplacian;
use crate::reduction::ImplicitProjection;
use crate::search::sorted_index::SortedLambdas;
use crate::search::taumode::TauMode;

// Add logging
use log::{debug, info, trace, warn};

/// A single owned row with an associated spectral score `lambda`.
///
/// ArrowItem provides iterator-based, allocation-free primitives (norm, dot,
/// cosine similarity, Euclidean distance) and in-place arithmetic. It is useful
/// both as a convenience handle returned by `ArrowSpace::get_row` and as a
/// standalone value in query-time computations.
///
/// # Examples
///
/// Construct, compute similarity, and scale in place:
///
/// ```
/// use arrowspace::core::ArrowItem;
///
/// let mut a = ArrowItem::new(vec![1.0, 2.0, 3.0].as_ref(), 0.5);
/// let b = vec![1.0, 0.0, 1.0];
///
/// let cos = a.cosine_similarity(&b);
/// assert!(cos.is_finite());
///
/// a.scale(2.0);
/// assert_eq!(a.len(), 3);
/// ```
#[derive(Clone, Debug)]
pub struct ArrowItem {
    pub item: Vec<f64>,
    pub lambda: f64,
}

/// A structure representing a feature-column
///  just the data for now but will be useful for index building
#[derive(Clone, Debug)]
pub struct ArrowFeature {
    pub feature: Vec<f64>,
}

impl ArrowItem {
    /// Creates a new ArrowItem from owned data.
    /// This just store the vector with a placeholder lambda, to compute the
    ///  lambda (Rayleigh quotient) use `new_with_graph` or precompute lambda
    ///  and pass it to this method.
    ///
    /// Prefer passing already-allocated vectors to avoid extra copies.
    ///
    /// # Examples
    ///
    /// ```
    /// use arrowspace::core::ArrowItem;
    /// let r = ArrowItem::new(vec![0.0, 1.0].as_ref(), 0.3);
    /// assert_eq!(r.len(), 2);
    /// ```
    #[inline]
    pub fn new(item: &[f64], lambda: f64) -> Self {
        trace!(
            "Creating ArrowItem with {} dimensions, lambda: {:.6}",
            item.len(),
            lambda
        );
        Self {
            item: item.to_vec(),
            lambda,
        }
    }

    /// Returns the length (dimensionality) of the row.
    #[inline]
    pub fn len(&self) -> usize {
        self.item.len()
    }

    /// Returns true if the row has zero length.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.item.is_empty()
    }

    /// Lambda component similarity (spectral distance)
    #[inline]
    pub fn lambda_component_similarity(&self, other: &ArrowItem) -> f64 {
        let lambda_diff = (self.lambda - other.lambda).abs();
        1.0 - lambda_diff.min(1.0)
    }

    /// Combined lambda-aware similarity
    /// Combines semantic (cosine) similarity and lambda proximity (Rayleigh plus dispersion).
    ///
    /// `alpha` weights semantic similarity; `beta` weights lambda proximity
    /// defined as `1 / (1 + |lambda_a - lambda_b|)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use arrowspace::core::ArrowItem;
    /// let a = ArrowItem::new(vec![1.0, 0.0].as_ref(), 0.5);
    /// let b = ArrowItem::new(vec![1.0, 0.0].as_ref(), 0.6);
    /// let s = a.lambda_similarity(&b, 0.7);
    /// assert!(s <= 1.0 && s >= 0.0);
    /// ```
    #[inline]
    pub fn lambda_similarity(&self, other: &ArrowItem, alpha: f64) -> f64 {
        assert_eq!(
            self.item.len(),
            other.item.len(),
            "items should be of the same length"
        );
        let cosine_sim = self.cosine_similarity(&other.item);
        let lambda_sim = self.lambda_component_similarity(other);

        let result = alpha * cosine_sim + cosine_sim.signum() * (1.0 - alpha) * lambda_sim;

        trace!(
            "Lambda similarity: semantic={:.6}, lambda={:.6}, combined={:.6}",
            cosine_sim, lambda_sim, result
        );

        result
    }

    /// Computes the dot product with another row without allocating.
    ///
    /// # Panics
    ///
    /// Panics if the lengths differ.
    ///
    /// # Examples
    ///
    /// ```
    /// use arrowspace::core::ArrowItem;
    /// let a = ArrowItem::new(vec![1.0, 2.0, 3.0].as_ref(), 0.0);
    /// let b = ArrowItem::new(vec![4.0, 5.0, 6.0].as_ref(), 0.0);
    /// assert_eq!(a.dot(&b), 32.0);
    /// ```
    #[inline]
    pub fn dot(&self, other: &ArrowItem) -> f64 {
        assert_eq!(self.len(), other.len(), "Dimension mismatch");
        let result = self
            .item
            .iter()
            .zip(other.item.iter())
            .map(|(a, b)| a * b)
            .sum();
        trace!("Computed dot product: {:.6}", result);
        result
    }

    /// Computes the Euclidean norm (L2) without allocating.
    #[inline]
    pub fn norm(a: &[f64]) -> f64 {
        let result = a.iter().map(|&x| x * x).sum::<f64>().sqrt();
        trace!("Computed norm: {:.6}", result);
        result
    }

    /// Computes cosine similarity, guarding against zero vectors.
    ///
    /// Returns 0.0 if either vector has zero norm.
    ///
    /// # Panics
    ///
    /// Panics if the lengths differ.
    ///
    /// # Examples
    ///
    /// ```
    /// use arrowspace::core::ArrowItem;
    /// let a = ArrowItem::new(vec![1.0, 0.0].as_ref(), 0.0);
    /// let b = vec![0.0, 1.0];
    /// assert!((a.cosine_similarity(&b) - 0.0).abs() < 1e-12);
    /// ```
    #[inline]
    pub fn cosine_similarity(&self, other: &[f64]) -> f64 {
        let denom = ArrowItem::norm(&self.item) * ArrowItem::norm(other);
        let result = if denom > 0.0 {
            self.dot(&ArrowItem::new(other, 0.0)) / denom
        } else {
            warn!("Zero vector encountered in cosine similarity computation");
            0.0
        };
        trace!("Computed cosine similarity: {:.6}", result);
        result
    }

    /// Computes Euclidean distance without allocation.
    ///
    /// # Panics
    ///
    /// Panics if the lengths differ.
    ///
    /// # Examples
    ///
    /// ```
    /// use arrowspace::core::ArrowItem;
    /// let a = ArrowItem::new(vec![1.0, 1.0].as_ref(), 0.0);
    /// let b = ArrowItem::new(vec![4.0, 5.0].as_ref(), 0.0);
    /// assert!((a.euclidean_distance(&b) - 5.0).abs() < 1e-12);
    /// ```
    #[inline]
    pub fn euclidean_distance(&self, other: &ArrowItem) -> f64 {
        assert_eq!(self.len(), other.len(), "Dimension mismatch");
        let result = self
            .item
            .iter()
            .zip(other.item.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt();
        trace!("Computed Euclidean distance: {:.6}", result);
        result
    }

    /// Adds another row element-wise in-place.
    ///
    /// # Panics
    ///
    /// Panics if the lengths differ.
    #[inline]
    pub fn add_inplace(&mut self, other: &ArrowItem) {
        assert_eq!(self.len(), other.len(), "Dimension mismatch");
        trace!("Adding vectors in-place");
        self.item
            .iter_mut()
            .zip(other.item.iter())
            .for_each(|(a, b)| *a += *b);
    }

    /// Multiplies element-wise in-place by another row.
    ///
    /// # Panics
    ///
    /// Panics if the lengths differ.
    #[inline]
    pub fn mul_inplace(&mut self, other: &ArrowItem) {
        assert_eq!(self.len(), other.len(), "Dimension mismatch");
        trace!("Multiplying vectors element-wise in-place");
        self.item
            .iter_mut()
            .zip(other.item.iter())
            .for_each(|(a, b)| *a *= *b);
    }

    /// Scales all elements by a scalar in place.
    #[inline]
    pub fn scale(&mut self, scalar: f64) {
        trace!("Scaling vector by {:.6}", scalar);
        self.item.iter_mut().for_each(|x| *x *= scalar);
    }

    /// Immutable iterator over elements.
    #[inline]
    pub fn iter(&self) -> std::slice::Iter<'_, f64> {
        self.item.iter()
    }

    /// Mutable iterator over elements.
    #[inline]
    pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, f64> {
        self.item.iter_mut()
    }
}

/// Scored item for min-heap (keeps top-k by popping smallest)
#[derive(Debug, Clone, Copy)]
struct ScoredItem {
    index: usize,
    score: f64,
}

impl PartialEq for ScoredItem {
    fn eq(&self, other: &Self) -> bool {
        self.score == other.score
    }
}

impl Eq for ScoredItem {}

impl PartialOrd for ScoredItem {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // Reverse for min-heap (smallest score at top)
        other.score.partial_cmp(&self.score)
    }
}

impl Ord for ScoredItem {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Equal)
    }
}

/// A dense, row-major matrix of f64 with per-row spectral scores (`lambda`).
///
/// ArrowSpace stores all data in a flattened row-major `Vec<f64>` and maintains
/// a parallel `lambdas` array. It exposes allocation-free row views and
/// search-oriented operations that recompute spectral scores on mutation.
///
/// # Construction
///
/// - `from_rows` builds from a `Vec<Vec<f64>>`, validating consistent width.
///
///
/// # Panics
///
/// - Constructors panic if row lengths are inconsistent or lambda length mismatches.
/// - Indexing methods panic on out-of-bound indices.
///
/// # Performance
///
#[derive(Clone, Debug)]
pub struct ArrowSpace {
    pub nfeatures: usize, // F: original dimensions
    pub nitems: usize,
    pub data: DenseMatrix<f64>,        // NxF raw data
    pub signals: CsMat<f64>,           // Laplacian(Transpose(FfxFn))
    pub lambdas: Vec<f64>,             // N lambdas (every lambda is a lambda for an item-row)
    pub lambdas_sorted: SortedLambdas, // sorted by lambda ascending
    pub taumode: TauMode,              // tau_mode as in select_tau_mode

    // lambdas normalisation
    pub min_lambdas: f64,
    pub max_lambdas: f64,
    pub(crate) range_lambdas: f64,

    pub n_clusters: usize,
    /// Cluster assignment per original row (N entries, each in 0..X or None for outliers)
    pub cluster_assignments: Vec<Option<usize>>,
    /// Cluster sizes (X entries)
    pub cluster_sizes: Vec<usize>,
    /// Squared distance threshold used during clustering
    pub cluster_radius: f64,

    // Projection data: dims reduction data (needed to prepare the query vector)
    pub projection_matrix: Option<ImplicitProjection>, // F × r (if projection was used)
    pub reduced_dim: Option<usize>, // r (reduced dimension, None if no projection)
    pub extra_reduced_dim: bool,    // optional extra dimensionality reduction for energymaps

    // energymaps specific
    pub centroid_map: Option<Vec<usize>>, // Maps item_idx -> centroid_idx
    pub sub_centroids: Option<DenseMatrix<f64>>,
    pub subcentroid_lambdas: Option<Vec<f64>>,

    /// Pre-computed L2 norms for tie-breaking (energy mode)
    ///
    /// Computed during build to accelerate cosine similarity in search.
    /// Only used when items have identical lambdas (same subcentroid).
    pub item_norms: Option<Vec<f64>>,
}

pub const TAUDEFAULT: TauMode = TauMode::Median;

impl Default for ArrowSpace {
    fn default() -> Self {
        debug!("Creating default ArrowSpace");
        Self {
            nfeatures: 0,
            nitems: 0,
            data: DenseMatrix::new(0, 0, Vec::new(), true).unwrap(),
            signals: sprs::CsMat::zero((0, 0)),
            lambdas: Vec::new(),
            lambdas_sorted: SortedLambdas::new(),
            // lambdas normalisation
            min_lambdas: -1.0,
            max_lambdas: -1.0,
            range_lambdas: -1.0,
            // enable synthetic λ with Median τ by default
            taumode: TAUDEFAULT,
            // Clustering defaults
            n_clusters: 0,
            cluster_assignments: Vec::new(),
            cluster_sizes: Vec::new(),
            cluster_radius: 0.0,
            // projection
            projection_matrix: None,
            reduced_dim: None,
            extra_reduced_dim: false,
            // energymaps
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        }
    }
}

impl ArrowSpace {
    /// Returns an empty space from the initial data
    pub(crate) fn new(items: Vec<Vec<f64>>, taumode: TauMode) -> Self {
        assert!(!items.is_empty(), "items cannot be empty");
        assert!(
            items.len() > 1,
            "cannot create a arrowspace of one arrow only"
        );
        let n_items = items.len(); // Number of items (columns in final layout)
        let n_features = items[0].len(); // Number of features (rows in final layout)
        Self {
            nfeatures: n_features,
            nitems: n_items,
            data: DenseMatrix::from_2d_vec(&items).unwrap(),
            signals: sprs::CsMat::zero((0, 0)), // will be computed later
            lambdas: vec![0.0; n_items],        // will be computed later
            lambdas_sorted: SortedLambdas::new(),
            // lambdas normalisation
            min_lambdas: -1.0,
            max_lambdas: -1.0,
            range_lambdas: -1.0,
            taumode,
            // Clustering defaults
            n_clusters: 0,
            cluster_assignments: Vec::new(),
            cluster_sizes: Vec::new(),
            cluster_radius: 0.0,
            // projection
            projection_matrix: None,
            reduced_dim: None,
            extra_reduced_dim: false,
            // energymaps
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        }
    }

    /// Convenience method to generate a temporary `ArrowSpace` to reproject vectors
    pub fn empty_with_projection(
        proj_data: HashMap<String, ConfigValue>,
        nrows: usize,
        ncols: usize,
    ) -> Self {
        debug!(
            "ArrowSpace::empty_with_projection called with nrows={}, ncols={}",
            nrows, ncols
        );

        let extra_reduced = proj_data["extra_reduced_dim"].as_bool().unwrap();
        debug!("extra_reduced_dim from proj_data: {}", extra_reduced);
        assert!(
            extra_reduced == false,
            "Reconstructing with extra dim reduction is not implemented yet"
        );

        let has_projection = proj_data["pj_mtx_original_dim"].as_usize().is_some();
        debug!("projection present in proj_data: {}", has_projection);

        let mut aspace = Self::default();
        aspace.nitems = nrows;
        aspace.nfeatures = ncols;

        if has_projection {
            let original_dim = proj_data["pj_mtx_original_dim"]
                .as_usize()
                .expect("pj_mtx_original_dim must be usize when projection is present");
            let reduced_dim = proj_data["pj_mtx_reduced_dim"]
                .as_usize()
                .expect("pj_mtx_reduced_dim must be usize when projection is present");
            let seed = proj_data["pj_mtx_seed"]
                .as_u64()
                .expect("pj_mtx_seed must be u64 when projection is present");

            info!(
                "Reconstructing ImplicitProjection: original_dim={}, reduced_dim={}, seed={}",
                original_dim, reduced_dim, seed
            );

            aspace.projection_matrix = Some(ImplicitProjection {
                original_dim,
                reduced_dim,
                seed,
            });
            aspace.reduced_dim = Some(reduced_dim);
            aspace.extra_reduced_dim = extra_reduced;
        } else {
            warn!(
                "empty_with_projection called without projection metadata; \
                returning ArrowSpace without projection_matrix"
            );
        }

        debug!(
            "ArrowSpace::empty_with_projection created ArrowSpace \
            with nitems={}, nfeatures={}, reduced_dim={:?}",
            aspace.nitems, aspace.nfeatures, aspace.reduced_dim
        );

        aspace
    }

    /// Recreates an ArrowSpace from a aspace configuration HashMap.
    ///
    /// This method reconstructs a workable ArrowSpace with all properties set
    /// from the builder configuration, but with an empty data matrix.
    ///
    /// Returns:
    /// A fully configured ArrowSpace with empty data
    pub fn from_config(config: HashMap<String, ConfigValue>) -> Self {
        let nitems = config
            .get("nitems")
            .and_then(|v| v.as_usize())
            .expect("from_config: missing nitems");
        let nfeatures = config
            .get("nfeatures")
            .and_then(|v| v.as_usize())
            .expect("from_config: missing nfeatures");

        debug!(
            "ArrowSpace::from_config called (nitems={}, nfeatures={})",
            nitems, nfeatures
        );

        // --- Projection matrix ---
        let projection_matrix = if let (
            Some(ConfigValue::OptionUsize(Some(original_dim))),
            Some(ConfigValue::OptionUsize(Some(reduced_dim))),
            Some(ConfigValue::OptionU64(Some(seed))),
        ) = (
            config.get("pj_mtx_original_dim"),
            config.get("pj_mtx_reduced_dim"),
            config.get("pj_mtx_seed"),
        ) {
            info!(
                "ArrowSpace::from_config: projection matrix used: original_dim={}, reduced_dim={}",
                original_dim, reduced_dim
            );
            Some(ImplicitProjection {
                original_dim: *original_dim,
                reduced_dim: *reduced_dim,
                seed: *seed,
            })
        } else {
            debug!("ArrowSpace::from_config: projection matrix not used");
            None
        };
        let reduced_dim = match config.get("pj_mtx_reduced_dim") {
            Some(ConfigValue::OptionUsize(Some(d))) => Some(*d),
            _ => None,
        };
        let extra_reduced_dim = config
            .get("extra_reduced_dim")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        // --- Tau mode (synthesis) ---
        let taumode = config
            .get("taumode")
            .and_then(|v| match v {
                ConfigValue::TauMode(t) => Some(t.clone()),
                _ => None,
            })
            .unwrap_or_default();

        // --- Clustering ---
        let n_clusters = config
            .get("n_clusters")
            .and_then(|v| v.as_usize())
            .unwrap_or(0);
        let cluster_radius = config
            .get("cluster_radius")
            .and_then(|v| v.as_f64())
            .unwrap_or(0.0);

        info!(
            "ArrowSpace::from_config: n_clusters={}, cluster_radius={}",
            n_clusters, cluster_radius
        );

        // --- Empty data and auxiliary fields ---
        let data = DenseMatrix::new(0, 0, Vec::new(), true).unwrap();
        let signals = sprs::CsMat::zero((0, 0));
        let lambdas = vec![0.0; nitems];
        let lambdas_sorted = SortedLambdas::new();

        let aspace = ArrowSpace {
            nfeatures,
            nitems,
            data,
            signals,
            lambdas,
            lambdas_sorted,
            // Normalization fields
            min_lambdas: -1.0,
            max_lambdas: -1.0,
            range_lambdas: -1.0,
            taumode,
            n_clusters,
            cluster_assignments: Vec::new(),
            cluster_sizes: Vec::new(),
            cluster_radius,
            // Projection
            projection_matrix,
            reduced_dim,
            extra_reduced_dim,
            // Energy-maps related fields
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        };

        debug!(
            "ArrowSpace::from_config created ArrowSpace: nitems={}, nfeatures={}, n_clusters={}, reduced_dim={:?}, extra_reduced_dim={}",
            aspace.nitems,
            aspace.nfeatures,
            aspace.n_clusters,
            aspace.reduced_dim,
            aspace.extra_reduced_dim
        );
        aspace
    }

    // drop stored in-memory data
    // to be used after data has been persisted to file
    pub fn drop_data(&mut self) {
        info!("Freeing raw input memory, should have been persisted to file");
        self.data = DenseMatrix::new(0, 0, vec![], true).unwrap();
    }

    /// Returns an empty space from the initial data
    pub(crate) fn new_from_dense(items: DenseMatrix<f64>, taumode: TauMode) -> Self {
        let n_items = items.shape().0; // Number of items (columns in final layout)
        let n_features = items.shape().1; // Number of features (rows in final layout)
        let empty = items.is_empty();
        info!(r#"new_from_dense: {n_items}x{n_features} -> empty: {empty}"#);
        assert!(
            items.shape().0 > 1,
            "cannot create a arrowspace of one arrow only"
        );
        Self {
            nfeatures: n_features,
            nitems: n_items,
            data: items,
            signals: sprs::CsMat::zero((0, 0)), // will be computed later
            lambdas: vec![0.0; n_items],        // will be computed later
            lambdas_sorted: SortedLambdas::new(),
            // lambdas normalisation
            min_lambdas: -1.0,
            max_lambdas: -1.0,
            range_lambdas: -1.0,
            taumode,
            // Clustering defaults
            n_clusters: 0,
            cluster_assignments: Vec::new(),
            cluster_sizes: Vec::new(),
            cluster_radius: 0.0,
            // projection
            projection_matrix: None,
            reduced_dim: None,
            extra_reduced_dim: false,
            // energymaps
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        }
    }

    /// Builds from a vector of equally-sized rows and per-row lambdas.
    /// Only to be used in tests. Use `ArrowSpaceBuilder`
    #[inline]
    #[cfg(test)]
    #[allow(dead_code)]
    pub(crate) fn from_items(items: Vec<Vec<f64>>, taumode: TauMode) -> Self {
        warn!(
            "This is just a test method. Use ArrowSpaceBuilder. Creating ArrowSpace from {} items with custom tau mode: {:?}",
            items.len(),
            taumode
        );
        let mut aspace = Self::from_items_default(items);
        aspace.taumode = taumode;
        aspace
    }

    /// Builds from a vector of equally-sized rows and per-row lambdas.
    /// Only to be used in tests. `ArrowSpaceBuilder`
    #[inline]
    #[cfg(test)]
    #[allow(dead_code)]
    pub(crate) fn from_items_default(items: Vec<Vec<f64>>) -> Self {
        assert!(!items.is_empty(), "items cannot be empty");
        assert!(
            items.len() > 1,
            "cannot create a arrowspace of one arrow only"
        );
        let n_items = items.len(); // Number of items (columns in final layout)
        let n_features = items[0].len(); // Number of features (rows in final layout)

        warn!(
            "This is a test method, use ArrowSpaceBuilder. Creating ArrowSpace from {} items with {} features",
            n_items, n_features
        );
        debug!("Using default tau mode: {:?}", TAUDEFAULT);

        assert!(
            items.iter().all(|item| item.len() == n_features),
            "All items must have identical number of features"
        );

        trace!("Constructing DenseMatrix from 2D vector");
        let data_matrix = DenseMatrix::from_2d_vec(&items).unwrap();
        debug!("ArrowSpace data matrix shape: {:?}", data_matrix.shape());

        Self {
            nfeatures: n_features,
            nitems: n_items,
            data: data_matrix,
            signals: sprs::CsMat::zero((0, 0)), // will be computed later
            lambdas: vec![0.0; n_items],        // will be computed later
            lambdas_sorted: SortedLambdas::new(),
            // lambdas normalisation
            min_lambdas: -1.0,
            max_lambdas: -1.0,
            range_lambdas: -1.0,
            taumode: TAUDEFAULT,
            // Clustering defaults
            n_clusters: 0,
            cluster_assignments: Vec::new(),
            cluster_sizes: Vec::new(),
            cluster_radius: 0.0,
            // projection
            projection_matrix: None,
            reduced_dim: None,
            extra_reduced_dim: false,
            // energymaps
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        }
    }

    // core.rs
    pub(crate) fn subcentroids_from_dense_matrix(matrix: DenseMatrix<f64>) -> Self {
        let (n_rows, n_cols) = matrix.shape();
        // For subcentroids, rows = C (items), cols = F (features)
        let nitems = n_rows;
        let nfeatures = n_cols;

        info!(
            "Creating subcentroid ArrowSpace from DenseMatrix({}, {})",
            n_rows, n_cols
        );
        info!(
            "→ Interpreted as: {} subcentroids × {} features (row-major)",
            nitems, nfeatures
        );

        Self {
            data: matrix,
            nitems,
            nfeatures,
            signals: sprs::CsMat::zero((0, 0)),
            lambdas: vec![0.0; nitems],
            lambdas_sorted: SortedLambdas::new(),
            min_lambdas: -1.0,
            max_lambdas: -1.0,
            range_lambdas: -1.0,
            taumode: TAUDEFAULT,
            n_clusters: 0,
            cluster_assignments: Vec::new(),
            cluster_sizes: Vec::new(),
            cluster_radius: 0.0,
            projection_matrix: None,
            reduced_dim: None,
            extra_reduced_dim: false,
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        }
    }

    /// Project query vector to reduced space if projection was used during indexing
    ///
    /// # Arguments
    /// * `query` - Original F-dimensional query vector
    ///
    /// # Returns
    /// * If projection was used: r-dimensional projected query
    /// * If no projection: original query unchanged
    pub fn project_query(&self, query: &[f64]) -> Vec<f64> {
        assert_eq!(
            query.len(),
            self.nfeatures,
            "Query dimension {} doesn't match index original dimension {}",
            query.len(),
            self.nfeatures
        );

        if let Some(ref proj) = self.projection_matrix {
            trace!(
                "Projecting query: {} → {} dimensions using seed-based projection",
                self.nfeatures,
                self.reduced_dim.unwrap()
            );
            proj.project(query)
        } else {
            trace!("No projection applied, returning original query");
            query.to_vec()
        }
    }

    /// Compute query lambda for mode
    ///
    /// Maps query to nearest subcentroid and returns its lambda.
    /// Pre-computed subcentroids and lambdas are already stored in ArrowSpace.
    pub fn prepare_query_item(&self, query: &[f64], gl: &GraphLaplacian) -> f64 {
        assert!(
            query.iter().all(|x| x.is_finite()),
            "query item has non-finite values"
        );

        // Energy mode: subcentroid mapping (fast)
        if let (Some(subcentroids), Some(sc_lambdas)) =
            (&self.sub_centroids, &self.subcentroid_lambdas)
        {
            let mut best_idx = 0;
            let mut best_dist = f64::INFINITY;

            // Find nearest subcentroid
            for sc_idx in 0..subcentroids.shape().0 {
                let query = if self.extra_reduced_dim {
                    &self.project_query(query)
                } else {
                    query
                };
                let dist: f64 = query
                    .iter()
                    .zip(subcentroids.get_row(sc_idx).iterator(0))
                    .map(|(a, b)| (a - b).powi(2))
                    .sum::<f64>()
                    .sqrt();

                if dist < best_dist {
                    best_dist = dist;
                    best_idx = sc_idx;
                }
            }

            let lambda = sc_lambdas[best_idx];

            trace!(
                "Query mapped to subcentroid {}/{} with λ={:.6} (dist={:.4})",
                best_idx,
                subcentroids.shape().0,
                lambda,
                best_dist
            );

            return lambda;
        }

        // Eigen mode
        let tau = TauMode::select_tau(&query, self.taumode);
        let raw_lambda = TauMode::compute_synthetic_lambda(
            &query,
            self.projection_matrix.clone(),
            &gl.matrix,
            tau,
        );

        // Normalize if stats are available
        let msg = "Check your eps parameter for the builder, every dataset has an optimal eps. \n \
            Also, the query item may be out of context for the dataset (undecidable), \
            despite all safeguards its lambda is 0.0";
        if self.range_lambdas.is_finite() {
            if relative_eq!(raw_lambda, 0.0, epsilon = 1e-12) {
                panic!("{}", msg)
            }
            return self.normalise_query_lambda(raw_lambda);
        } else {
            if relative_eq!(raw_lambda, 0.0, epsilon = 1e-12) {
                panic!("{}", msg)
            }
            return raw_lambda;
        }
    }

    /// Build the sorted index
    pub fn build_lambdas_sorted(&mut self) {
        self.lambdas_sorted.build_from(&self.lambdas);
    }

    /// Returns a shared reference to all lambdas.
    #[inline]
    pub fn lambdas(&self) -> &[f64] {
        self.lambdas.as_ref()
    }

    /// Returns cluster assignment for row i (None if outlier or not clustered).
    #[inline]
    pub fn cluster_of(&self, i: usize) -> Option<usize> {
        self.cluster_assignments.get(i).copied().flatten()
    }

    /// Returns an owned ArrowFeature copy of the requested column.
    #[inline]
    pub fn get_feature(&self, i: usize) -> ArrowFeature {
        assert!(i < self.nfeatures, "feature index out of bounds");
        trace!("Extracting feature {} from ArrowSpace", i);
        ArrowFeature {
            feature: self.data.get_col(i).iterator(0).copied().collect(),
        }
    }

    /// Modify feature column in-place
    #[allow(dead_code)]
    fn set_feature(&mut self, f: usize, values: ArrowFeature) {
        assert!(f < self.nfeatures, "feature index out of bounds");
        debug!("Setting feature {} in-place", f);
        // Modify each element in the column
        for i in 0..self.nitems {
            self.data.set((i, f), values.feature[i]);
        }
    }

    /// Returns an owned ArrowItem for the requested item (row).
    #[inline]
    pub fn get_item(&self, i: usize) -> ArrowItem {
        assert!(i < self.nitems, "item index out of bounds");
        trace!("Extracting item {} with lambda {:.6}", i, self.lambdas[i]);

        ArrowItem::new(
            self.data
                .get_row(i)
                .iterator(0)
                .copied()
                .collect::<Vec<f64>>()
                .as_ref(),
            self.lambdas[i],
        )
    }

    /// Modify item row in-place
    fn set_item(&mut self, i: usize, values: ArrowItem) {
        assert!(i < self.nitems, "item index out of bounds");
        debug!("Setting item {} in-place", i);
        // Modify each element in the column
        for f in 0..self.nfeatures {
            self.data.set((i, f), values.item[f]);
        }
    }

    /// Adds item `b` into item `a` in-place and recomputes feature lambdas.
    ///
    /// This method:
    /// 1. Extracts item `a` and item `b` as complete ArrowItem vectors
    /// 2. Performs element-wise addition: item_a += item_b
    /// 3. Writes the result back
    /// 4. Recomputes feature lambdas
    #[inline]
    pub fn add_items(&mut self, a: usize, b: usize, gl: &GraphLaplacian) {
        assert!(
            a < self.nitems && b < self.nitems,
            "Item indices out of bounds: a={}, b={}, ncols={}",
            a,
            b,
            self.nitems
        );
        assert_eq!(
            gl.nnodes, self.nitems,
            "Laplacian nodes must match number of items"
        );

        debug!("Adding item {} into item {}", b, a);
        debug!(
            "Graph Laplacian has {} nodes, ArrowSpace has {} items",
            gl.nnodes, self.nitems
        );

        // Extract both items as complete ArrowItem vectors
        let mut item_a = self.get_item(a);
        let item_b = self.get_item(b);

        // Perform the addition: item_a += item_b
        item_a.add_inplace(&item_b);

        self.set_item(a, item_a);

        // Recompute lambdas for all features since item values changed
        debug!("Recomputing lambdas after item addition");
        self.recompute_lambdas(gl);
    }

    /// Multiplies item `a` element-wise by item `b` and recomputes feature lambdas.
    #[inline]
    pub fn mul_items(&mut self, a: usize, b: usize, gl: &GraphLaplacian) {
        assert!(
            a < self.nitems && b < self.nitems,
            "Item indices out of bounds: a={}, b={}, ncols={}",
            a,
            b,
            self.nitems
        );
        assert_eq!(
            gl.nnodes, self.nitems,
            "Laplacian nodes must match number of items"
        );

        debug!("Multiplying item {} by item {}", a, b);

        // Extract both items as complete ArrowItem vectors
        let mut item_a = self.get_item(a);
        let item_b = self.get_item(b);

        // Perform the multiplication: item_a *= item_b
        item_a.mul_inplace(&item_b);

        self.set_item(a, item_a);

        // Recompute lambdas for all features since item values changed
        debug!("Recomputing lambdas after item multiplication");
        self.recompute_lambdas(gl);
    }

    /// Scales item `a` by a scalar value and recomputes feature lambdas.
    #[inline]
    pub fn scale_item(&mut self, a: usize, scalar: f64, gl: &GraphLaplacian) {
        assert!(
            a < self.nitems,
            "Item index out of bounds: a={}, ncols={}",
            a,
            self.nitems
        );
        assert_eq!(
            gl.nnodes, self.nitems,
            "Laplacian nodes must match number of items"
        );

        debug!("Scaling item {} by factor {:.6}", a, scalar);

        // Extract item as complete ArrowItem vector
        let mut item_a = self.get_item(a);

        // Perform the scaling: item_a *= scalar
        item_a.scale(scalar);

        self.set_item(a, item_a);

        // Recompute lambdas for all features since item values changed
        debug!("Recomputing lambdas after item scaling");
        self.recompute_lambdas(gl);
    }

    /// Recomputes all feature lambdas using the provided Graph Laplacian.
    ///
    /// The Laplacian must have nodes equal to the number of items.
    #[inline]
    pub fn recompute_lambdas(&mut self, gl: &GraphLaplacian) {
        debug!("Recomputing lambdas with tau mode: {:?}", self.taumode);
        // Use the existing synthetic lambda computation
        TauMode::compute_taumode_lambdas_parallel(self, gl, self.taumode);

        let lambda_stats = {
            let min = self.lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            let max: f64 = self.lambdas.iter().fold(0.0, |a, &b| a.max(b));
            let mean = self.lambdas.iter().sum::<f64>() / self.lambdas.len() as f64;
            (min, max, mean)
        };

        debug!(
            "Lambda recomputation completed - min: {:.6}, max: {:.6}, mean: {:.6}",
            lambda_stats.0, lambda_stats.1, lambda_stats.2
        );
    }

    /// Lambda-aware top-k search against an ArrowItem query.
    /// Single-pass parallel dual scoring with `ArrowScore`.
    ///
    /// Returns indices and scores sorted descending by similarity.
    /// Algorithm:
    /// 1. Single parallel scan maintains:
    ///    - Semantic top-1 (best cosine)
    ///    - High semantic matches (cosine > 0.95)
    ///    - Top-k lambda-aware candidates (min-heap)
    /// 2. Union: high semantic + top-k lambda (deduplicated)
    /// 3. Replace lowest lambda score with semantic top-1 if needed
    ///
    /// # Performance
    /// - O(N) scan with O(k log k) heap operations per thread
    /// - No full array allocation or sorting
    /// - Lock-free concurrent top-k tracking via DashMap
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use arrowspace::core::{ArrowItem, ArrowSpace};
    ///
    /// let aspace = ArrowSpace::from_items_default(
    ///     vec![vec![1.0, 0.0], vec![1.0, 1.0], vec![0.0, 1.0]]
    /// );
    /// let q = ArrowItem::new(vec![1.0, 0.1], 0.5);
    /// let res = aspace.search_lambda_aware(&q, 2, 0.7, 0.3);
    /// assert_eq!(res.len(), 2);
    /// assert!(res.1 >= 0.0);
    /// ```
    #[inline]
    pub fn search_lambda_aware(
        &self,
        query: &ArrowItem,
        k: usize,
        alpha: f64,
    ) -> Vec<(usize, f64)> {
        info!("Lambda-aware search: k={}", k);
        debug!(
            "Query vector dimension: {}, lambda: {:.6}",
            query.len(),
            query.lambda
        );

        assert_ne!(
            query.lambda, 0.0,
            "Lambda of the item is 0.0, prepare the item before searching"
        );

        let mut results: Vec<_> = (0..self.nitems)
            .map(|i| {
                let item = self.get_item(i);
                let similarity = query.lambda_similarity(&item, alpha);
                (i, similarity)
            })
            .collect();
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        results.truncate(k);

        debug!("Search completed, returning {} results", results.len());
        if !results.is_empty() {
            trace!(
                "Top result: index={}, score={:.6}",
                results[0].0, results[0].1
            );
        }

        results
    }

    /// A version of `search_lambda_aware` that mix-in results from pure cosine similarity
    #[inline]
    pub fn search_lambda_aware_hybrid(
        &self,
        query: &ArrowItem,
        k: usize,
        alpha: f64,
    ) -> Vec<(usize, f64)> {
        info!("Hybrid search: k={}, alpha={}", k, alpha);

        if k == 0 {
            return Vec::new();
        }

        let beta = 1.0 - alpha;
        let semantic_threshold = 0.9999;

        // Parallel fold-reduce with thread-local state
        let (lambda_heap, semantic_top, high_semantic_vec) = (0..self.nitems)
            .into_par_iter()
            .fold(
                || {
                    (
                        BinaryHeap::with_capacity(k), // Lambda top-k heap
                        (0usize, f64::NEG_INFINITY),  // Semantic best
                        Vec::new(),                   // High semantic matches
                    )
                },
                |(mut heap, mut sem_best, mut high_sem), i| {
                    let item = self.get_item(i);

                    // Single computation of both similarities
                    let cosine = query.cosine_similarity(&item.item);
                    let lambda_component = query.lambda_component_similarity(&item);
                    let lambda_score = alpha * cosine + beta * lambda_component;

                    // Track semantic best
                    if cosine > sem_best.1 {
                        sem_best = (i, cosine);
                    }

                    // Track high semantic matches
                    if cosine > semantic_threshold {
                        high_sem.push((i, cosine));
                    }

                    // Maintain lambda top-k heap
                    if heap.len() < k {
                        heap.push(ScoredItem {
                            index: i,
                            score: lambda_score,
                        });
                    } else if let Some(&min) = heap.peek() {
                        if lambda_score > min.score {
                            heap.pop();
                            heap.push(ScoredItem {
                                index: i,
                                score: lambda_score,
                            });
                        }
                    }

                    (heap, sem_best, high_sem)
                },
            )
            .reduce(
                || (BinaryHeap::new(), (0, f64::NEG_INFINITY), Vec::new()),
                |(mut h1, s1, mut hs1), (h2, s2, hs2)| {
                    // Merge semantic best
                    let sem = if s1.1 > s2.1 { s1 } else { s2 };

                    // Merge high semantic
                    hs1.extend(hs2);

                    // Merge heaps
                    for item in h2 {
                        if h1.len() < k {
                            h1.push(item);
                        } else if let Some(&min) = h1.peek() {
                            if item.score > min.score {
                                h1.pop();
                                h1.push(item);
                            }
                        }
                    }

                    (h1, sem, hs1)
                },
            );

        debug!(
            "Semantic top: idx={}, score={:.6}, high_semantic_count={}",
            semantic_top.0,
            semantic_top.1,
            high_semantic_vec.len()
        );

        // Union: high semantic + lambda top-k
        let mut result_set = HashSet::with_capacity(k + high_semantic_vec.len());
        let mut score_map = std::collections::HashMap::with_capacity(result_set.capacity());

        // Add high semantic matches
        for (idx, score) in high_semantic_vec {
            result_set.insert(idx);
            score_map.insert(idx, score);
        }

        // Add lambda top-k
        for item in lambda_heap.into_sorted_vec().into_iter().rev() {
            result_set.insert(item.index);
            score_map.entry(item.index).or_insert(item.score);
        }

        // Ensure semantic top-1 is included
        result_set.insert(semantic_top.0);
        score_map.entry(semantic_top.0).or_insert(semantic_top.1);

        // Convert to vector and sort
        let mut final_results: Vec<(usize, f64)> = result_set
            .into_iter()
            .map(|idx| (idx, *score_map.get(&idx).unwrap()))
            .collect();

        final_results.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        final_results.truncate(k);

        debug!("Final: {} results", final_results.len());
        final_results
    }

    /// Search using only taumode lambdas sorted index
    pub fn search_linear_sorted(
        &self,
        query: &[f64],
        gl: &GraphLaplacian,
        k: usize,
    ) -> Vec<(usize, f64)> {
        let q_lambda = self.prepare_query_item(query, gl); // f64
        self.lambdas_sorted
            .range_bylambda(q_lambda, k, gl.graph_params.p)
    }

    /// Normalise lambdas to [0, 1] range for consistent search behavior
    /// This is called every time taumode is recomputed
    /// Lambdas stats are used by `prepare_query` to normalise the query item
    #[inline]
    pub fn normalise_lambdas(&mut self) {
        self.min_lambdas = self.lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        self.max_lambdas = self.lambdas.iter().fold(0.0_f64, |a, &b| a.max(b));
        self.range_lambdas = (self.max_lambdas - self.min_lambdas).max(1e-9);

        for lambda in &mut self.lambdas {
            *lambda = (*lambda - self.min_lambdas) / self.range_lambdas;
        }

        info!(
            "Normalized lambdas to [0, 1] range (original spread: {:.6})",
            self.range_lambdas
        );
    }

    /// Normalise a single query lambda to [0, 1] range using stored statistics
    ///
    /// This method applies the same normalization transform used for indexed lambdas
    /// to a raw query lambda value, ensuring consistent distance calculations.
    #[inline]
    pub fn normalise_query_lambda(&self, raw_lambda: f64) -> f64 {
        debug_assert!(
            self.range_lambdas > 0.0,
            "Call normalise_lambdas() before normalising query lambdas"
        );

        // Apply same transform as batch normalization
        let normalized = (raw_lambda - self.min_lambdas) / self.range_lambdas;

        // Clamp to [0, 1] to handle edge cases where query lambda is outside
        // the training range (e.g., out-of-distribution queries)
        normalized.clamp(0.0, 1.0)
    }

    /// Range search by Euclidean distance within `radius`.
    ///
    /// Returns (row_index, distance) pairs for all rows whose distance is
    /// ≤ `radius` from the query.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use arrowspace::core::{ArrowItem, ArrowSpace};
    ///
    /// let q = ArrowItem::new(vec![0.5, 0.0], 0.0);
    /// let hits = aspace.range_search(&q, 0.6);
    /// ```
    #[inline]
    pub fn range_search(
        &self,
        query: &ArrowItem,
        gl: &GraphLaplacian,
        eps: f64,
    ) -> Vec<(usize, f64)> {
        info!("Range search with radius: {:.6}", eps);
        debug!("Query vector dimension: {}", query.len());

        let query: ArrowItem = if relative_eq!(query.lambda, 0.0, epsilon = 1e-9) {
            ArrowItem::new(
                query.item.as_ref(),
                self.prepare_query_item(&query.item, gl),
            )
        } else {
            query.clone()
        };

        let results: Vec<(usize, f64)> = (0..self.nitems)
            .filter_map(|i| {
                let item = self.get_item(i);
                let distance = query.lambda - item.lambda;
                if distance <= eps {
                    Some((i, distance))
                } else {
                    None
                }
            })
            .collect();

        debug!(
            "Range search completed, found {} items within radius",
            results.len()
        );
        results
    }

    /// Update the lambdas with new synthetic values
    pub fn update_lambdas(&mut self, new_lambdas: Vec<f64>) {
        assert_eq!(
            new_lambdas.len(),
            self.lambdas.len(),
            "New lambdas length must match existing lambdas length"
        );

        info!("Updating lambdas with {} new values", new_lambdas.len());
        let old_stats = {
            let min = self.lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            let max: f64 = self.lambdas.iter().fold(0.0, |a, &b| a.max(b));
            (min, max)
        };

        self.lambdas = new_lambdas;
        self.normalise_lambdas();

        let new_stats = {
            let min = self.lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            let max: f64 = self.lambdas.iter().fold(0.0, |a, &b| a.max(b));
            (min, max)
        };

        debug!(
            "Lambda update: old range [{:.6}, {:.6}] -> new range [{:.6}, {:.6}]",
            old_stats.0, old_stats.1, new_stats.0, new_stats.1
        );
    }

    /// Convert all ArrowSpace configuration into a typed hashmap.
    ///
    /// This extracts all parameters needed to rebuild an ArrowSpace with identical
    /// behavior: tau mode, projection settings, and dimension metadata.
    pub fn arrowspace_config_typed(&self) -> HashMap<String, ConfigValue> {
        let mut config = HashMap::new();

        config.insert("nitems".to_string(), ConfigValue::Usize(self.nitems));
        config.insert("nfeatures".to_string(), ConfigValue::Usize(self.nfeatures));

        config.insert(
            "min_lambdas".to_string(),
            ConfigValue::F64(self.min_lambdas),
        );
        config.insert(
            "max_lambdas".to_string(),
            ConfigValue::F64(self.max_lambdas),
        );
        config.insert(
            "range_lambdas".to_string(),
            ConfigValue::F64(self.range_lambdas),
        );

        // projection matrix
        if self.projection_matrix.is_some() {
            config.insert(
                "pj_mtx_original_dim".to_string(),
                ConfigValue::OptionUsize(Some(
                    self.projection_matrix.as_ref().unwrap().original_dim,
                )),
            );
            config.insert(
                "pj_mtx_reduced_dim".to_string(),
                ConfigValue::OptionUsize(Some(
                    self.projection_matrix.as_ref().unwrap().reduced_dim,
                )),
            );
            config.insert(
                "pj_mtx_seed".to_string(),
                ConfigValue::OptionU64(Some(self.projection_matrix.as_ref().unwrap().seed)),
            );

            config.insert(
                "extra_reduced_dim".to_string(),
                ConfigValue::Bool(self.extra_reduced_dim),
            );
        } else {
            config.insert(
                "pj_mtx_original_dim".to_string(),
                ConfigValue::OptionUsize(None),
            );
            config.insert(
                "pj_mtx_reduced_dim".to_string(),
                ConfigValue::OptionUsize(None),
            );
            config.insert("pj_mtx_seed".to_string(), ConfigValue::OptionU64(None));
            config.insert("extra_reduced_dim".to_string(), ConfigValue::Bool(false));
        }

        config.insert(
            "taumode".to_string(),
            ConfigValue::TauMode(self.taumode.clone()),
        );

        config.insert(
            "n_clusters".to_string(),
            ConfigValue::Usize(self.n_clusters),
        );
        config.insert(
            "cluster_radius".to_string(),
            ConfigValue::F64(self.cluster_radius),
        );

        config.insert(
            "min_lambdas".to_string(),
            ConfigValue::F64(self.min_lambdas),
        );
        config.insert(
            "max_lambdas".to_string(),
            ConfigValue::F64(self.max_lambdas),
        );
        config.insert(
            "range_lambdas".to_string(),
            ConfigValue::F64(self.range_lambdas),
        );

        config
    }

    /// Reconstruct ArrowSpace from stored parquet files.
    ///
    /// This method loads all necessary components from disk without recomputation.
    ///
    /// # Arguments
    /// * `storage_path` - Directory containing the parquet files
    /// * `dataset_name` - Prefix of the files (e.g., "dorothea_highdim")
    ///
    /// # Example
    /// ```ignore
    /// let aspace = ArrowSpace::new_from_storage("storage/", "dorothea_highdim")?;
    /// ```
    #[cfg(feature = "storage")]
    pub fn new_from_storage(
        storage_path: impl AsRef<std::path::Path>,
        dataset_name: &str,
    ) -> Result<Self, crate::storage::StorageError> {
        use crate::reduction::ImplicitProjection;
        use crate::storage::parquet::load_lambda;

        let base_path = storage_path.as_ref();

        // 1. Load ArrowSpace Metadata
        let metadata_path = base_path.join(format!("{}-arrowspace_metadata.json", dataset_name));
        info!("Loading storage from {}", metadata_path.display());
        let metadata_content = std::fs::read_to_string(&metadata_path).map_err(|e| {
            crate::storage::StorageError::Io(format!("Failed to read arrowspace metadata: {}", e))
        })?;

        let config: HashMap<String, crate::builder::ConfigValue> =
            serde_json::from_str(&metadata_content).map_err(|e| {
                crate::storage::StorageError::Invalid(format!(
                    "Failed to parse arrowspace metadata: {}",
                    e
                ))
            })?;

        // 2. Load Raw Input Data
        let raw_path = base_path.join(format!("{}-raw_input.parquet", dataset_name));
        let data = crate::storage::parquet::load_dense_matrix(&raw_path)?;
        let (nitems, nfeatures) = data.shape();

        // 3. Load Lambdas
        let lambdas_path = base_path.join(format!("{}-lambdas.parquet", dataset_name));
        let lambdas = load_lambda(&lambdas_path)?;

        if lambdas.len() != nitems {
            return Err(crate::storage::StorageError::Invalid(format!(
                "Lambda count ({}) doesn't match items ({})",
                lambdas.len(),
                nitems
            )));
        }

        // 4. Extract Projection from ArrowSpace Metadata
        let projection_matrix = if let (Some(orig), Some(red), Some(seed)) = (
            config
                .get("pj_mtx_original_dim")
                .and_then(|v| Some(v.as_usize()))
                .unwrap_or(None),
            config
                .get("pj_mtx_reduced_dim")
                .and_then(|v| Some(v.as_usize()))
                .unwrap_or(None),
            config
                .get("pj_mtx_seed")
                .and_then(|v| Some(v.as_u64()))
                .unwrap_or(None),
        ) {
            info!(
                "Restoring ImplicitProjection: {} -> {} (seed: {})",
                orig, red, seed
            );
            Some(ImplicitProjection::new(orig, red, Some(seed)))
        } else {
            None
        };

        let reduced_dim = config
            .get("pj_mtx_reduced_dim")
            .and_then(|v| Some(v.as_usize()))
            .unwrap_or(None);

        // 5. Extract other fields from metadata
        let min_lambdas = config
            .get("min_lambdas")
            .and_then(|v| v.as_f64())
            .unwrap_or_else(|| lambdas.iter().fold(f64::INFINITY, |a, &b| a.min(b)));

        let max_lambdas = config
            .get("max_lambdas")
            .and_then(|v| v.as_f64())
            .unwrap_or_else(|| lambdas.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)));

        let range_lambdas = config
            .get("range_lambdas")
            .and_then(|v| v.as_f64())
            .unwrap_or(max_lambdas - min_lambdas);

        let taumode = config
            .get("taumode")
            .and_then(|v| v.as_tau_mode())
            .unwrap_or(TauMode::Median);

        let n_clusters = config
            .get("n_clusters")
            .and_then(|v| v.as_usize())
            .unwrap_or(0);

        let cluster_radius = config
            .get("cluster_radius")
            .and_then(|v| v.as_f64())
            .unwrap_or(0.0);

        let extra_reduced_dim = config
            .get("extra_reduced_dim")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        // 6. Build the ArrowSpace struct
        let mut aspace = Self {
            nfeatures,
            nitems,
            data,
            signals: sprs::CsMat::zero((0, 0)),
            lambdas,
            lambdas_sorted: crate::search::sorted_index::SortedLambdas::new(),
            min_lambdas,
            max_lambdas,
            range_lambdas,
            taumode,
            n_clusters,
            cluster_assignments: vec![],
            cluster_sizes: vec![],
            cluster_radius,
            projection_matrix: projection_matrix.clone(),
            reduced_dim,
            extra_reduced_dim,
            centroid_map: None,
            sub_centroids: None,
            subcentroid_lambdas: None,
            item_norms: None,
        };

        // 7. Build sorted index
        aspace.build_lambdas_sorted();

        info!(
            "Loaded ArrowSpace: {} items × {} features, projection: {}, tau: {:?}",
            nitems,
            nfeatures,
            projection_matrix.is_some(),
            taumode
        );

        Ok(aspace)
    }
}

// Flattened AsRef/AsMut for ArrowSpace
impl AsRef<DenseMatrix<f64>> for ArrowSpace {
    #[inline]
    fn as_ref(&self) -> &DenseMatrix<f64> {
        &self.data
    }
}
impl AsMut<DenseMatrix<f64>> for ArrowSpace {
    #[inline]
    fn as_mut(&mut self) -> &mut DenseMatrix<f64> {
        &mut self.data
    }
}

// Iterate all elements by reference (feature-major)
impl<'a> IntoIterator for &'a ArrowItem {
    type Item = &'a f64;
    type IntoIter = std::slice::Iter<'a, f64>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.item.iter()
    }
}

// Iterate all elements mutably (feature-major)
impl<'a> IntoIterator for &'a mut ArrowItem {
    type Item = &'a mut f64;
    type IntoIter = std::slice::IterMut<'a, f64>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.item.iter_mut()
    }
}

pub fn densematrix_to_vecvec(matrix: &DenseMatrix<f64>) -> Vec<Vec<f64>> {
    let (rows, cols) = matrix.shape();
    (0..rows)
        .map(|r| (0..cols).map(|c| matrix.get((r, c)).clone()).collect())
        .collect()
}