oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
use crate::{Vector, VectorData, VectorError};
use half::f16;
use std::io::{Read, Write};

#[derive(Debug, Clone, Default)]
pub enum CompressionMethod {
    #[default]
    None,
    Zstd {
        level: i32,
    },
    Quantization {
        bits: u8,
    },
    ProductQuantization {
        subvectors: usize,
        codebook_size: usize,
    },
    Pca {
        components: usize,
    },
    Adaptive {
        quality_level: AdaptiveQuality,
        analysis_samples: usize,
    },
}

#[derive(Debug, Clone)]
pub enum AdaptiveQuality {
    Fast,      // Prioritize speed, moderate compression
    Balanced,  // Balance speed and compression ratio
    BestRatio, // Prioritize compression ratio over speed
}

pub trait VectorCompressor: Send + Sync {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError>;
    fn decompress(&self, data: &[u8], dimensions: usize) -> Result<Vector, VectorError>;
    fn compression_ratio(&self) -> f32;
}

pub struct ZstdCompressor {
    level: i32,
}

impl ZstdCompressor {
    pub fn new(level: i32) -> Self {
        Self {
            level: level.clamp(1, 22),
        }
    }
}

impl VectorCompressor for ZstdCompressor {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError> {
        let bytes = vector_to_bytes(vector)?;
        oxiarc_zstd::encode_all(&bytes, self.level)
            .map_err(|e| VectorError::CompressionError(e.to_string()))
    }

    fn decompress(&self, data: &[u8], dimensions: usize) -> Result<Vector, VectorError> {
        let decompressed = oxiarc_zstd::decode_all(data)
            .map_err(|e| VectorError::CompressionError(e.to_string()))?;
        bytes_to_vector(&decompressed, dimensions)
    }

    fn compression_ratio(&self) -> f32 {
        // Typical compression ratio for float data with zstd
        match self.level {
            1..=3 => 0.7,
            4..=9 => 0.5,
            10..=15 => 0.4,
            16..=22 => 0.3,
            _ => 1.0,
        }
    }
}

pub struct ScalarQuantizer {
    bits: u8,
    min_val: f32,
    max_val: f32,
}

impl ScalarQuantizer {
    pub fn new(bits: u8) -> Self {
        Self {
            bits: bits.clamp(1, 16),
            min_val: 0.0,
            max_val: 1.0,
        }
    }

    pub fn with_range(bits: u8, min_val: f32, max_val: f32) -> Self {
        Self {
            bits: bits.clamp(1, 16),
            min_val,
            max_val,
        }
    }

    pub fn train(&mut self, vectors: &[Vector]) -> Result<(), VectorError> {
        if vectors.is_empty() {
            return Err(VectorError::InvalidDimensions(
                "No vectors to train on".to_string(),
            ));
        }

        let mut min = f32::INFINITY;
        let mut max = f32::NEG_INFINITY;

        for vector in vectors {
            match &vector.values {
                VectorData::F32(v) => {
                    for &val in v {
                        min = min.min(val);
                        max = max.max(val);
                    }
                }
                VectorData::F64(v) => {
                    for &val in v {
                        min = min.min(val as f32);
                        max = max.max(val as f32);
                    }
                }
                _ => {}
            }
        }

        self.min_val = min;
        self.max_val = max;
        Ok(())
    }

    fn quantize_value(&self, value: f32) -> u16 {
        let normalized = ((value - self.min_val) / (self.max_val - self.min_val)).clamp(0.0, 1.0);
        let max_quant_val = (1u32 << self.bits) - 1;
        (normalized * max_quant_val as f32).round() as u16
    }

    fn dequantize_value(&self, quantized: u16) -> f32 {
        let max_quant_val = (1u32 << self.bits) - 1;
        let normalized = quantized as f32 / max_quant_val as f32;
        normalized * (self.max_val - self.min_val) + self.min_val
    }
}

impl VectorCompressor for ScalarQuantizer {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError> {
        let values = match &vector.values {
            VectorData::F32(v) => v.clone(),
            VectorData::F64(v) => v.iter().map(|&x| x as f32).collect(),
            _ => {
                return Err(VectorError::UnsupportedOperation(
                    "Quantization only supports float vectors".to_string(),
                ))
            }
        };

        let mut compressed = Vec::new();

        // Write header: bits, min_val, max_val
        compressed.write_all(&self.bits.to_le_bytes())?;
        compressed.write_all(&self.min_val.to_le_bytes())?;
        compressed.write_all(&self.max_val.to_le_bytes())?;

        // Quantize and pack values
        if self.bits <= 8 {
            for val in values {
                let quantized = self.quantize_value(val) as u8;
                compressed.push(quantized);
            }
        } else {
            for val in values {
                let quantized = self.quantize_value(val);
                compressed.write_all(&quantized.to_le_bytes())?;
            }
        }

        Ok(compressed)
    }

    fn decompress(&self, data: &[u8], dimensions: usize) -> Result<Vector, VectorError> {
        let mut cursor = std::io::Cursor::new(data);

        // Read header
        let mut bits_buf = [0u8; 1];
        cursor.read_exact(&mut bits_buf)?;
        let bits = bits_buf[0];

        let mut min_buf = [0u8; 4];
        cursor.read_exact(&mut min_buf)?;
        let min_val = f32::from_le_bytes(min_buf);

        let mut max_buf = [0u8; 4];
        cursor.read_exact(&mut max_buf)?;
        let max_val = f32::from_le_bytes(max_buf);

        // Create temporary quantizer with loaded params
        let quantizer = ScalarQuantizer {
            bits,
            min_val,
            max_val,
        };

        // Dequantize values
        let mut values = Vec::with_capacity(dimensions);

        if bits <= 8 {
            let mut buf = [0u8; 1];
            for _ in 0..dimensions {
                cursor.read_exact(&mut buf)?;
                let quantized = buf[0] as u16;
                values.push(quantizer.dequantize_value(quantized));
            }
        } else {
            let mut buf = [0u8; 2];
            for _ in 0..dimensions {
                cursor.read_exact(&mut buf)?;
                let quantized = u16::from_le_bytes(buf);
                values.push(quantizer.dequantize_value(quantized));
            }
        }

        Ok(Vector::new(values))
    }

    fn compression_ratio(&self) -> f32 {
        // Original: 32 bits per float, compressed: self.bits per float
        self.bits as f32 / 32.0
    }
}

pub struct PcaCompressor {
    components: usize,
    mean: Vec<f32>,
    components_matrix: Vec<Vec<f32>>,
    explained_variance_ratio: Vec<f32>,
}

impl PcaCompressor {
    pub fn new(components: usize) -> Self {
        Self {
            components,
            mean: Vec::new(),
            components_matrix: Vec::new(),
            explained_variance_ratio: Vec::new(),
        }
    }

    pub fn train(&mut self, vectors: &[Vector]) -> Result<(), VectorError> {
        if vectors.is_empty() {
            return Err(VectorError::InvalidDimensions(
                "No vectors to train on".to_string(),
            ));
        }

        // Convert all vectors to f32
        let data: Vec<Vec<f32>> = vectors
            .iter()
            .map(|v| match &v.values {
                VectorData::F32(vals) => Ok(vals.clone()),
                VectorData::F64(vals) => Ok(vals.iter().map(|&x| x as f32).collect()),
                _ => Err(VectorError::UnsupportedOperation(
                    "PCA only supports float vectors".to_string(),
                )),
            })
            .collect::<Result<Vec<_>, _>>()?;

        let n_samples = data.len();
        let n_features = data[0].len();

        // Compute mean
        self.mean = vec![0.0; n_features];
        for sample in &data {
            for (i, &val) in sample.iter().enumerate() {
                self.mean[i] += val;
            }
        }
        for val in &mut self.mean {
            *val /= n_samples as f32;
        }

        // Center data
        let mut centered = data.clone();
        for sample in &mut centered {
            for (i, val) in sample.iter_mut().enumerate() {
                *val -= self.mean[i];
            }
        }

        // Proper SVD-based PCA implementation
        self.components_matrix = Vec::with_capacity(self.components);

        // Create covariance matrix using nalgebra
        use nalgebra::DMatrix;

        // Convert vectors to training data
        let training_data: Result<Vec<Vec<f32>>, _> = vectors
            .iter()
            .map(|v| match &v.values {
                VectorData::F32(vals) => Ok(vals.clone()),
                VectorData::F64(vals) => Ok(vals.iter().map(|&x| x as f32).collect()),
                _ => Err(VectorError::UnsupportedOperation(
                    "PCA only supports float vectors".to_string(),
                )),
            })
            .collect();

        let training_data = training_data?;
        let n_samples = training_data.len();
        if n_samples == 0 {
            return Err(VectorError::InvalidDimensions(
                "No training data provided for PCA".to_string(),
            ));
        }

        // Build data matrix (samples x features)
        let mut data_matrix = DMatrix::<f32>::zeros(n_samples, n_features);
        for (i, sample) in training_data.iter().enumerate() {
            for (j, &val) in sample.iter().enumerate() {
                data_matrix[(i, j)] = val - self.mean[j];
            }
        }

        // Compute covariance matrix: C = (1/n) * X^T * X
        let covariance = data_matrix.transpose() * &data_matrix / (n_samples as f32 - 1.0);

        // Perform SVD on covariance matrix
        let svd = covariance.svd(true, true);

        if let Some(u) = svd.u {
            // Store the top components (principal components)
            let num_components = self.components.min(u.ncols());

            // Store singular values for explained variance calculation
            let singular_values = &svd.singular_values;
            let total_variance: f32 = singular_values.iter().sum();
            let mut explained_variance = Vec::with_capacity(num_components);

            for i in 0..num_components {
                let component: Vec<f32> = u.column(i).iter().cloned().collect();
                self.components_matrix.push(component);

                let variance_ratio = singular_values[i] / total_variance;
                explained_variance.push(variance_ratio);
            }

            // Store explained variance for diagnostics
            self.explained_variance_ratio = explained_variance;
        } else {
            return Err(VectorError::CompressionError(
                "SVD decomposition failed for PCA".to_string(),
            ));
        }

        Ok(())
    }

    fn project(&self, vector: &[f32]) -> Vec<f32> {
        let mut centered = vector.to_vec();
        for (i, val) in centered.iter_mut().enumerate() {
            *val -= self.mean.get(i).unwrap_or(&0.0);
        }

        let mut projected = vec![0.0; self.components];
        for (i, component) in self.components_matrix.iter().enumerate() {
            let mut dot = 0.0;
            for (j, &val) in centered.iter().enumerate() {
                dot += val * component.get(j).unwrap_or(&0.0);
            }
            projected[i] = dot;
        }

        projected
    }

    fn reconstruct(&self, projected: &[f32]) -> Vec<f32> {
        let n_features = self.mean.len();
        let mut reconstructed = self.mean.clone();

        for (i, &coeff) in projected.iter().enumerate() {
            if let Some(component) = self.components_matrix.get(i) {
                for (j, &comp_val) in component.iter().enumerate() {
                    if j < n_features {
                        reconstructed[j] += coeff * comp_val;
                    }
                }
            }
        }

        reconstructed
    }

    /// Get the explained variance ratio for each principal component
    pub fn explained_variance_ratio(&self) -> &[f32] {
        &self.explained_variance_ratio
    }

    /// Get the total explained variance for the selected components
    pub fn total_explained_variance(&self) -> f32 {
        self.explained_variance_ratio.iter().sum()
    }
}

impl VectorCompressor for PcaCompressor {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError> {
        let values = match &vector.values {
            VectorData::F32(v) => v.clone(),
            VectorData::F64(v) => v.iter().map(|&x| x as f32).collect(),
            _ => {
                return Err(VectorError::UnsupportedOperation(
                    "PCA only supports float vectors".to_string(),
                ))
            }
        };

        let projected = self.project(&values);

        let mut compressed = Vec::new();
        // Write header with components count
        compressed.write_all(&(self.components as u32).to_le_bytes())?;

        // Write projected values
        for val in projected {
            compressed.write_all(&val.to_le_bytes())?;
        }

        Ok(compressed)
    }

    fn decompress(&self, data: &[u8], _dimensions: usize) -> Result<Vector, VectorError> {
        let mut cursor = std::io::Cursor::new(data);

        // Read header
        let mut components_buf = [0u8; 4];
        cursor.read_exact(&mut components_buf)?;
        let components = u32::from_le_bytes(components_buf) as usize;

        // Read projected values
        let mut projected = Vec::with_capacity(components);
        let mut val_buf = [0u8; 4];

        for _ in 0..components {
            cursor.read_exact(&mut val_buf)?;
            projected.push(f32::from_le_bytes(val_buf));
        }

        let reconstructed = self.reconstruct(&projected);
        Ok(Vector::new(reconstructed))
    }

    fn compression_ratio(&self) -> f32 {
        // Compression ratio depends on dimensionality reduction
        if self.mean.is_empty() {
            1.0
        } else {
            self.components as f32 / self.mean.len() as f32
        }
    }
}

/// Product Quantization compressor for efficient vector compression
pub struct ProductQuantizer {
    subvectors: usize,
    codebook_size: usize,
    codebooks: Vec<Vec<Vec<f32>>>, // codebooks[subvector][centroid][dimension]
    subvector_dim: usize,
}

impl ProductQuantizer {
    pub fn new(subvectors: usize, codebook_size: usize) -> Self {
        Self {
            subvectors,
            codebook_size,
            codebooks: Vec::new(),
            subvector_dim: 0,
        }
    }

    pub fn train(&mut self, vectors: &[Vector]) -> Result<(), VectorError> {
        if vectors.is_empty() {
            return Err(VectorError::InvalidDimensions(
                "No training data provided for Product Quantization".to_string(),
            ));
        }

        // Get the vector dimension
        let vector_dim = vectors[0].dimensions;
        if vector_dim % self.subvectors != 0 {
            return Err(VectorError::InvalidDimensions(format!(
                "Vector dimension {} is not divisible by number of subvectors {}",
                vector_dim, self.subvectors
            )));
        }

        self.subvector_dim = vector_dim / self.subvectors;
        self.codebooks = Vec::with_capacity(self.subvectors);

        // Convert vectors to f32
        let training_data: Result<Vec<Vec<f32>>, _> = vectors
            .iter()
            .map(|v| match &v.values {
                VectorData::F32(vals) => Ok(vals.clone()),
                VectorData::F64(vals) => Ok(vals.iter().map(|&x| x as f32).collect()),
                _ => Err(VectorError::UnsupportedOperation(
                    "Product quantization only supports float vectors".to_string(),
                )),
            })
            .collect();

        let training_data = training_data?;

        // Train a codebook for each subvector
        for subvec_idx in 0..self.subvectors {
            let start_dim = subvec_idx * self.subvector_dim;
            let end_dim = start_dim + self.subvector_dim;

            // Extract subvectors
            let subvectors: Vec<Vec<f32>> = training_data
                .iter()
                .map(|v| v[start_dim..end_dim].to_vec())
                .collect();

            // Train codebook using k-means
            let codebook = self.train_codebook(&subvectors)?;
            self.codebooks.push(codebook);
        }

        Ok(())
    }

    fn train_codebook(&self, subvectors: &[Vec<f32>]) -> Result<Vec<Vec<f32>>, VectorError> {
        use scirs2_core::random::Random;
        let mut rng = Random::seed(42);

        if subvectors.is_empty() {
            return Err(VectorError::InvalidDimensions(
                "No subvectors to train codebook".to_string(),
            ));
        }

        let dim = subvectors[0].len();
        let mut centroids = Vec::with_capacity(self.codebook_size);

        // Initialize centroids randomly
        for _ in 0..self.codebook_size {
            let mut centroid = vec![0.0; dim];
            for val in &mut centroid {
                *val = rng.gen_range(-1.0..1.0);
            }
            centroids.push(centroid);
        }

        // K-means iterations
        for _ in 0..10 {
            // Fixed number of iterations for simplicity
            let mut assignments = vec![0; subvectors.len()];

            // Assignment step
            for (i, subvec) in subvectors.iter().enumerate() {
                let mut best_dist = f32::INFINITY;
                let mut best_centroid = 0;

                for (j, centroid) in centroids.iter().enumerate() {
                    let dist = euclidean_distance(subvec, centroid);
                    if dist < best_dist {
                        best_dist = dist;
                        best_centroid = j;
                    }
                }
                assignments[i] = best_centroid;
            }

            // Update step
            for (j, centroid) in centroids.iter_mut().enumerate() {
                let assigned_points: Vec<&Vec<f32>> = subvectors
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| assignments[*i] == j)
                    .map(|(_, v)| v)
                    .collect();

                if !assigned_points.is_empty() {
                    for (d, centroid_val) in centroid.iter_mut().enumerate() {
                        *centroid_val = assigned_points.iter().map(|p| p[d]).sum::<f32>()
                            / assigned_points.len() as f32;
                    }
                }
            }
        }

        Ok(centroids)
    }

    fn quantize_vector(&self, vector: &[f32]) -> Result<Vec<u8>, VectorError> {
        if vector.len() != self.subvectors * self.subvector_dim {
            return Err(VectorError::InvalidDimensions(format!(
                "Vector dimension {} doesn't match expected {}",
                vector.len(),
                self.subvectors * self.subvector_dim
            )));
        }

        let mut codes = Vec::with_capacity(self.subvectors);

        for subvec_idx in 0..self.subvectors {
            let start_dim = subvec_idx * self.subvector_dim;
            let end_dim = start_dim + self.subvector_dim;
            let subvector = &vector[start_dim..end_dim];

            let codebook = &self.codebooks[subvec_idx];
            let mut best_dist = f32::INFINITY;
            let mut best_code = 0u8;

            for (code, centroid) in codebook.iter().enumerate() {
                let dist = euclidean_distance(subvector, centroid);
                if dist < best_dist {
                    best_dist = dist;
                    best_code = code as u8;
                }
            }

            codes.push(best_code);
        }

        Ok(codes)
    }

    fn dequantize_codes(&self, codes: &[u8]) -> Result<Vec<f32>, VectorError> {
        if codes.len() != self.subvectors {
            return Err(VectorError::InvalidDimensions(format!(
                "Code length {} doesn't match expected {}",
                codes.len(),
                self.subvectors
            )));
        }

        let mut reconstructed = Vec::with_capacity(self.subvectors * self.subvector_dim);

        for (subvec_idx, &code) in codes.iter().enumerate() {
            let codebook = &self.codebooks[subvec_idx];
            if (code as usize) < codebook.len() {
                reconstructed.extend_from_slice(&codebook[code as usize]);
            } else {
                return Err(VectorError::InvalidDimensions(format!(
                    "Invalid code {} for codebook of size {}",
                    code,
                    codebook.len()
                )));
            }
        }

        Ok(reconstructed)
    }
}

// Helper function for euclidean distance
fn euclidean_distance(a: &[f32], b: &[f32]) -> f32 {
    a.iter()
        .zip(b.iter())
        .map(|(&x, &y)| (x - y).powi(2))
        .sum::<f32>()
        .sqrt()
}

impl VectorCompressor for ProductQuantizer {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError> {
        let values = match &vector.values {
            VectorData::F32(v) => v.clone(),
            VectorData::F64(v) => v.iter().map(|&x| x as f32).collect(),
            _ => {
                return Err(VectorError::UnsupportedOperation(
                    "Product quantization only supports float vectors".to_string(),
                ))
            }
        };

        let codes = self.quantize_vector(&values)?;

        let mut compressed = Vec::new();
        // Write header with metadata
        compressed.write_all(&(self.subvectors as u32).to_le_bytes())?;
        compressed.write_all(&(self.codebook_size as u32).to_le_bytes())?;
        compressed.write_all(&(self.subvector_dim as u32).to_le_bytes())?;

        // Write quantization codes
        compressed.extend_from_slice(&codes);

        Ok(compressed)
    }

    fn decompress(&self, data: &[u8], _dimensions: usize) -> Result<Vector, VectorError> {
        if data.len() < 12 {
            // 3 * 4 bytes for header
            return Err(VectorError::InvalidData(
                "Invalid compressed data format".to_string(),
            ));
        }

        // Read header
        let subvectors = u32::from_le_bytes([data[0], data[1], data[2], data[3]]) as usize;
        let codebook_size = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as usize;
        let subvector_dim = u32::from_le_bytes([data[8], data[9], data[10], data[11]]) as usize;

        if subvectors != self.subvectors
            || codebook_size != self.codebook_size
            || subvector_dim != self.subvector_dim
        {
            return Err(VectorError::InvalidData(
                "Metadata mismatch in compressed data".to_string(),
            ));
        }

        let codes = &data[12..];
        if codes.len() != subvectors {
            return Err(VectorError::InvalidData("Invalid code length".to_string()));
        }

        let values = self.dequantize_codes(codes)?;
        Ok(Vector::new(values))
    }

    fn compression_ratio(&self) -> f32 {
        // Original: 32 bits per float, compressed: 8 bits per subvector
        (8.0 * self.subvectors as f32) / (32.0 * self.subvectors as f32 * self.subvector_dim as f32)
    }
}

/// Vector characteristics analysis for adaptive compression selection
#[derive(Debug, Clone)]
pub struct VectorAnalysis {
    pub sparsity: f32,               // Percentage of near-zero values
    pub range: f32,                  // max - min
    pub mean: f32,                   // Average value
    pub std_dev: f32,                // Standard deviation
    pub entropy: f32,                // Shannon entropy (approximation)
    pub dominant_patterns: Vec<f32>, // Most common value patterns
    pub recommended_method: CompressionMethod,
    pub expected_ratio: f32, // Expected compression ratio
}

impl VectorAnalysis {
    pub fn analyze(vectors: &[Vector], quality: &AdaptiveQuality) -> Result<Self, VectorError> {
        if vectors.is_empty() {
            return Err(VectorError::InvalidDimensions(
                "No vectors to analyze".to_string(),
            ));
        }

        // Convert all vectors to f32 for analysis
        let mut all_values = Vec::new();
        let mut dimensions = 0;

        for vector in vectors {
            let values = match &vector.values {
                VectorData::F32(v) => v.clone(),
                VectorData::F64(v) => v.iter().map(|&x| x as f32).collect(),
                VectorData::F16(v) => v.iter().map(|&x| f16::from_bits(x).to_f32()).collect(),
                VectorData::I8(v) => v.iter().map(|&x| x as f32).collect(),
                VectorData::Binary(_) => {
                    return Ok(Self::binary_analysis(vectors.len()));
                }
            };
            if dimensions == 0 {
                dimensions = values.len();
            }
            all_values.extend(values);
        }

        if all_values.is_empty() {
            return Err(VectorError::InvalidDimensions(
                "No values to analyze".to_string(),
            ));
        }

        // Calculate basic statistics
        let min_val = all_values.iter().copied().fold(f32::INFINITY, f32::min);
        let max_val = all_values.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let range = max_val - min_val;
        let mean = all_values.iter().sum::<f32>() / all_values.len() as f32;

        let variance =
            all_values.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / all_values.len() as f32;
        let std_dev = variance.sqrt();

        // Calculate sparsity (percentage of values close to zero)
        let epsilon = std_dev * 0.01; // 1% of std deviation
        let near_zero_count = all_values.iter().filter(|&&x| x.abs() < epsilon).count();
        let sparsity = near_zero_count as f32 / all_values.len() as f32;

        // Approximate entropy calculation (simplified)
        let entropy = Self::calculate_entropy(&all_values);

        // Find dominant patterns
        let dominant_patterns = Self::find_dominant_patterns(&all_values);

        // Select best compression method based on analysis
        let (recommended_method, expected_ratio) =
            Self::select_optimal_method(sparsity, range, std_dev, entropy, dimensions, quality);

        Ok(Self {
            sparsity,
            range,
            mean,
            std_dev,
            entropy,
            dominant_patterns,
            recommended_method,
            expected_ratio,
        })
    }

    fn binary_analysis(_vector_count: usize) -> Self {
        Self {
            sparsity: 0.0,
            range: 1.0,
            mean: 0.5,
            std_dev: 0.5,
            entropy: 1.0,
            dominant_patterns: vec![0.0, 1.0],
            recommended_method: CompressionMethod::Zstd { level: 1 },
            expected_ratio: 0.125, // Binary data compresses very well
        }
    }

    fn calculate_entropy(values: &[f32]) -> f32 {
        // Simplified entropy calculation using histogram
        let mut histogram = std::collections::HashMap::new();
        let bins = 64; // Quantize to 64 bins for entropy calculation

        if values.is_empty() {
            return 0.0;
        }

        let min_val = values.iter().copied().fold(f32::INFINITY, f32::min);
        let max_val = values.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let range = max_val - min_val;

        if range == 0.0 {
            return 0.0; // All values are the same
        }

        for &value in values {
            let bin = ((value - min_val) / range * (bins - 1) as f32) as usize;
            let bin = bin.min(bins - 1);
            *histogram.entry(bin).or_insert(0) += 1;
        }

        let total = values.len() as f32;
        let mut entropy = 0.0;

        for count in histogram.values() {
            let probability = *count as f32 / total;
            if probability > 0.0 {
                entropy -= probability * probability.log2();
            }
        }

        entropy
    }

    fn find_dominant_patterns(values: &[f32]) -> Vec<f32> {
        // Find the most common values (simplified implementation)
        let mut value_counts = std::collections::HashMap::new();

        // Quantize values to find patterns
        for &value in values {
            let quantized = (value * 1000.0).round() / 1000.0; // 3 decimal places
            *value_counts.entry(quantized.to_bits()).or_insert(0) += 1;
        }

        let mut patterns: Vec<_> = value_counts.into_iter().collect();
        patterns.sort_by_key(|b| std::cmp::Reverse(b.1)); // Sort by frequency

        patterns
            .into_iter()
            .take(5) // Top 5 patterns
            .map(|(bits, _)| f32::from_bits(bits))
            .collect()
    }

    fn select_optimal_method(
        sparsity: f32,
        range: f32,
        std_dev: f32,
        entropy: f32,
        dimensions: usize,
        quality: &AdaptiveQuality,
    ) -> (CompressionMethod, f32) {
        // Decision tree for optimal compression method selection

        // High sparsity suggests good compression with general methods
        if sparsity > 0.7 {
            return match quality {
                AdaptiveQuality::Fast => (CompressionMethod::Zstd { level: 1 }, 0.3),
                AdaptiveQuality::Balanced => (CompressionMethod::Zstd { level: 6 }, 0.2),
                AdaptiveQuality::BestRatio => (CompressionMethod::Zstd { level: 19 }, 0.15),
            };
        }

        // Low entropy (repetitive data) compresses well
        if entropy < 2.0 {
            return match quality {
                AdaptiveQuality::Fast => (CompressionMethod::Zstd { level: 3 }, 0.4),
                AdaptiveQuality::Balanced => (CompressionMethod::Zstd { level: 9 }, 0.3),
                AdaptiveQuality::BestRatio => (CompressionMethod::Zstd { level: 22 }, 0.2),
            };
        }

        // Small range suggests quantization will work well
        if range < 2.0 && std_dev < 0.5 {
            return match quality {
                AdaptiveQuality::Fast => (CompressionMethod::Quantization { bits: 8 }, 0.25),
                AdaptiveQuality::Balanced => (CompressionMethod::Quantization { bits: 6 }, 0.1875),
                AdaptiveQuality::BestRatio => (CompressionMethod::Quantization { bits: 4 }, 0.125),
            };
        }

        // High dimensional data might benefit from PCA
        if dimensions > 128 {
            let components = match quality {
                AdaptiveQuality::Fast => dimensions * 7 / 10, // 70% of dimensions
                AdaptiveQuality::Balanced => dimensions / 2,  // 50% of dimensions
                AdaptiveQuality::BestRatio => dimensions / 3, // 33% of dimensions
            };
            return (
                CompressionMethod::Pca { components },
                components as f32 / dimensions as f32,
            );
        }

        // Default to moderate Zstd compression
        match quality {
            AdaptiveQuality::Fast => (CompressionMethod::Zstd { level: 3 }, 0.6),
            AdaptiveQuality::Balanced => (CompressionMethod::Zstd { level: 6 }, 0.5),
            AdaptiveQuality::BestRatio => (CompressionMethod::Zstd { level: 12 }, 0.4),
        }
    }
}

/// Adaptive compressor that automatically selects the best compression method
pub struct AdaptiveCompressor {
    quality_level: AdaptiveQuality,
    analysis_samples: usize,
    current_method: Option<Box<dyn VectorCompressor>>,
    analysis_cache: Option<VectorAnalysis>,
    performance_metrics: CompressionMetrics,
}

#[derive(Debug, Clone)]
pub struct CompressionMetrics {
    pub vectors_compressed: usize,
    pub total_original_size: usize,
    pub total_compressed_size: usize,
    pub compression_time_ms: f64,
    pub decompression_time_ms: f64,
    pub current_ratio: f32,
    pub method_switches: usize,
}

impl Default for CompressionMetrics {
    fn default() -> Self {
        Self {
            vectors_compressed: 0,
            total_original_size: 0,
            total_compressed_size: 0,
            compression_time_ms: 0.0,
            decompression_time_ms: 0.0,
            current_ratio: 1.0,
            method_switches: 0,
        }
    }
}

impl AdaptiveCompressor {
    pub fn new(quality_level: AdaptiveQuality, analysis_samples: usize) -> Self {
        Self {
            quality_level,
            analysis_samples: analysis_samples.max(10), // Minimum 10 samples
            current_method: None,
            analysis_cache: None,
            performance_metrics: CompressionMetrics::default(),
        }
    }

    pub fn with_fast_quality() -> Self {
        Self::new(AdaptiveQuality::Fast, 50)
    }

    pub fn with_balanced_quality() -> Self {
        Self::new(AdaptiveQuality::Balanced, 100)
    }

    pub fn with_best_ratio() -> Self {
        Self::new(AdaptiveQuality::BestRatio, 200)
    }

    /// Analyze sample vectors and optimize compression method
    pub fn optimize_for_vectors(&mut self, sample_vectors: &[Vector]) -> Result<(), VectorError> {
        if sample_vectors.is_empty() {
            return Ok(());
        }

        let start_time = std::time::Instant::now();

        // Take a sample for analysis
        let samples_to_analyze = sample_vectors.len().min(self.analysis_samples);
        let analysis_vectors = &sample_vectors[..samples_to_analyze];

        let analysis = VectorAnalysis::analyze(analysis_vectors, &self.quality_level)?;

        // Create new compressor if method changed
        let should_switch = match (&self.current_method, &self.analysis_cache) {
            (Some(_), Some(cached)) => {
                // Check if the recommended method is significantly different
                !methods_equivalent(&cached.recommended_method, &analysis.recommended_method)
            }
            _ => true, // No current method or analysis
        };

        if should_switch {
            self.current_method = Some(create_compressor(&analysis.recommended_method));
            self.performance_metrics.method_switches += 1;
        }

        // Train the compressor if it supports training (after potential switch)
        if self.current_method.is_some() {
            // This is a simplified training approach since we can't access both compressor and self
            // In a real implementation, we would restructure to avoid this borrowing issue
        }

        self.analysis_cache = Some(analysis);

        let analysis_time = start_time.elapsed().as_secs_f64() * 1000.0;
        tracing::debug!("Adaptive compression analysis took {:.2}ms", analysis_time);

        Ok(())
    }

    fn train_compressor(
        &self,
        _compressor: &mut dyn VectorCompressor,
        _vectors: &[Vector],
    ) -> Result<(), VectorError> {
        // This is a bit hacky since we need to downcast to train specific compressor types
        // In a real implementation, we'd want a training trait
        Ok(())
    }

    pub fn get_metrics(&self) -> &CompressionMetrics {
        &self.performance_metrics
    }

    pub fn get_analysis(&self) -> Option<&VectorAnalysis> {
        self.analysis_cache.as_ref()
    }

    /// Adaptively re-analyze and potentially switch compression method
    pub fn adaptive_reanalysis(&mut self, recent_vectors: &[Vector]) -> Result<bool, VectorError> {
        if recent_vectors.len() < self.analysis_samples / 4 {
            return Ok(false); // Not enough data for meaningful analysis
        }

        let old_method = self
            .analysis_cache
            .as_ref()
            .map(|a| a.recommended_method.clone());

        self.optimize_for_vectors(recent_vectors)?;

        let method_changed = match (old_method, &self.analysis_cache) {
            (Some(old), Some(new)) => !methods_equivalent(&old, &new.recommended_method),
            _ => false,
        };

        Ok(method_changed)
    }
}

impl VectorCompressor for AdaptiveCompressor {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError> {
        if let Some(compressor) = &self.current_method {
            let start = std::time::Instant::now();
            let result = compressor.compress(vector);
            let _compression_time = start.elapsed().as_secs_f64() * 1000.0;

            // Update metrics (note: this requires mutable access, so we can't update here)
            // In a real implementation, we'd use interior mutability or restructure

            result
        } else {
            // Fallback to no compression
            let no_op = NoOpCompressor;
            no_op.compress(vector)
        }
    }

    fn decompress(&self, data: &[u8], dimensions: usize) -> Result<Vector, VectorError> {
        if let Some(compressor) = &self.current_method {
            let start = std::time::Instant::now();
            let result = compressor.decompress(data, dimensions);
            let _decompression_time = start.elapsed().as_secs_f64() * 1000.0;

            // Update metrics (note: this requires mutable access)

            result
        } else {
            // Fallback to no compression
            let no_op = NoOpCompressor;
            no_op.decompress(data, dimensions)
        }
    }

    fn compression_ratio(&self) -> f32 {
        if let Some(compressor) = &self.current_method {
            compressor.compression_ratio()
        } else {
            1.0
        }
    }
}

fn methods_equivalent(method1: &CompressionMethod, method2: &CompressionMethod) -> bool {
    match (method1, method2) {
        (CompressionMethod::None, CompressionMethod::None) => true,
        (CompressionMethod::Zstd { level: l1 }, CompressionMethod::Zstd { level: l2 }) => {
            (l1 - l2).abs() <= 2 // Allow small level differences
        }
        (
            CompressionMethod::Quantization { bits: b1 },
            CompressionMethod::Quantization { bits: b2 },
        ) => b1 == b2,
        (CompressionMethod::Pca { components: c1 }, CompressionMethod::Pca { components: c2 }) => {
            ((*c1 as i32) - (*c2 as i32)).abs() <= (*c1 as i32) / 10 // Allow 10% difference
        }
        _ => false,
    }
}

pub fn create_compressor(method: &CompressionMethod) -> Box<dyn VectorCompressor> {
    match method {
        CompressionMethod::None => Box::new(NoOpCompressor),
        CompressionMethod::Zstd { level } => Box::new(ZstdCompressor::new(*level)),
        CompressionMethod::Quantization { bits } => Box::new(ScalarQuantizer::new(*bits)),
        CompressionMethod::Pca { components } => Box::new(PcaCompressor::new(*components)),
        CompressionMethod::ProductQuantization {
            subvectors,
            codebook_size,
        } => Box::new(ProductQuantizer::new(*subvectors, *codebook_size)),
        CompressionMethod::Adaptive {
            quality_level,
            analysis_samples,
        } => Box::new(AdaptiveCompressor::new(
            quality_level.clone(),
            *analysis_samples,
        )),
    }
}

struct NoOpCompressor;

impl VectorCompressor for NoOpCompressor {
    fn compress(&self, vector: &Vector) -> Result<Vec<u8>, VectorError> {
        vector_to_bytes(vector)
    }

    fn decompress(&self, data: &[u8], dimensions: usize) -> Result<Vector, VectorError> {
        bytes_to_vector(data, dimensions)
    }

    fn compression_ratio(&self) -> f32 {
        1.0
    }
}

fn vector_to_bytes(vector: &Vector) -> Result<Vec<u8>, VectorError> {
    let mut bytes = Vec::new();

    // Write type indicator
    let type_byte = match &vector.values {
        VectorData::F32(_) => 0u8,
        VectorData::F64(_) => 1u8,
        VectorData::F16(_) => 2u8,
        VectorData::I8(_) => 3u8,
        VectorData::Binary(_) => 4u8,
    };
    bytes.push(type_byte);

    match &vector.values {
        VectorData::F32(v) => {
            for val in v {
                bytes.write_all(&val.to_le_bytes())?;
            }
        }
        VectorData::F64(v) => {
            for val in v {
                bytes.write_all(&val.to_le_bytes())?;
            }
        }
        VectorData::F16(v) => {
            for val in v {
                bytes.write_all(&val.to_le_bytes())?;
            }
        }
        VectorData::I8(v) => {
            for &val in v {
                bytes.push(val as u8);
            }
        }
        VectorData::Binary(v) => {
            bytes.extend_from_slice(v);
        }
    }

    Ok(bytes)
}

fn bytes_to_vector(data: &[u8], dimensions: usize) -> Result<Vector, VectorError> {
    if data.is_empty() {
        return Err(VectorError::InvalidDimensions("Empty data".to_string()));
    }

    let type_byte = data[0];
    let data = &data[1..];

    match type_byte {
        0 => {
            // F32
            let mut values = Vec::with_capacity(dimensions);
            let mut cursor = std::io::Cursor::new(data);
            let mut buf = [0u8; 4];

            for _ in 0..dimensions {
                cursor.read_exact(&mut buf)?;
                values.push(f32::from_le_bytes(buf));
            }
            Ok(Vector::new(values))
        }
        1 => {
            // F64
            let mut values = Vec::with_capacity(dimensions);
            let mut cursor = std::io::Cursor::new(data);
            let mut buf = [0u8; 8];

            for _ in 0..dimensions {
                cursor.read_exact(&mut buf)?;
                values.push(f64::from_le_bytes(buf));
            }
            Ok(Vector::f64(values))
        }
        2 => {
            // F16
            let mut values = Vec::with_capacity(dimensions);
            let mut cursor = std::io::Cursor::new(data);
            let mut buf = [0u8; 2];

            for _ in 0..dimensions {
                cursor.read_exact(&mut buf)?;
                values.push(u16::from_le_bytes(buf));
            }
            Ok(Vector::f16(values))
        }
        3 => {
            // I8
            Ok(Vector::i8(
                data[..dimensions].iter().map(|&b| b as i8).collect(),
            ))
        }
        4 => {
            // Binary
            Ok(Vector::binary(data[..dimensions].to_vec()))
        }
        _ => Err(VectorError::InvalidData(format!(
            "Unknown vector type: {type_byte}"
        ))),
    }
}

#[cfg(test)]
mod tests {
    type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
    use super::*;

    #[test]
    fn test_zstd_compression() -> Result<()> {
        let vector = Vector::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let compressor = ZstdCompressor::new(3);

        let compressed = compressor.compress(&vector)?;
        let decompressed = compressor.decompress(&compressed, 5)?;

        let orig = vector.as_f32();
        let dec = decompressed.as_f32();
        assert_eq!(orig.len(), dec.len());
        for (a, b) in orig.iter().zip(dec.iter()) {
            assert!((a - b).abs() < 1e-6);
        }
        Ok(())
    }

    #[test]
    fn test_scalar_quantization() -> Result<()> {
        let vector = Vector::new(vec![0.1, 0.5, 0.9, 0.3, 0.7]);
        let mut quantizer = ScalarQuantizer::new(8);
        quantizer.train(std::slice::from_ref(&vector))?;

        let compressed = quantizer.compress(&vector)?;
        let decompressed = quantizer.decompress(&compressed, 5)?;

        // Check compression ratio
        assert!(compressed.len() < 20); // Should be much smaller than original

        let orig = vector.as_f32();
        let dec = decompressed.as_f32();
        assert_eq!(orig.len(), dec.len());
        // With 8-bit quantization, expect some loss
        for (a, b) in orig.iter().zip(dec.iter()) {
            assert!((a - b).abs() < 0.01);
        }
        Ok(())
    }

    #[test]
    fn test_pca_compression() -> Result<()> {
        let vectors = vec![
            Vector::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]),
            Vector::new(vec![2.0, 3.0, 4.0, 5.0, 6.0]),
            Vector::new(vec![3.0, 4.0, 5.0, 6.0, 7.0]),
        ];

        let mut pca = PcaCompressor::new(3);
        pca.train(&vectors)?;

        let compressed = pca.compress(&vectors[0])?;
        let decompressed = pca.decompress(&compressed, 5)?;

        let dec = decompressed.as_f32();
        assert_eq!(dec.len(), 5);
        Ok(())
    }

    #[test]
    fn test_adaptive_compression_sparse_data() -> Result<()> {
        // Test with sparse data (should select Zstd with high level)
        let vectors = vec![
            Vector::new(vec![0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0]),
            Vector::new(vec![0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0]),
            Vector::new(vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0]),
        ];

        let mut adaptive = AdaptiveCompressor::with_balanced_quality();
        adaptive.optimize_for_vectors(&vectors)?;

        let analysis = adaptive
            .get_analysis()
            .ok_or("get_analysis returned None")?;
        assert!(analysis.sparsity > 0.5); // Should detect high sparsity

        // Test compression
        let compressed = adaptive.compress(&vectors[0])?;
        let decompressed = adaptive.decompress(&compressed, 10)?;

        let orig = vectors[0].as_f32();
        let dec = decompressed.as_f32();
        assert_eq!(orig.len(), dec.len());
        Ok(())
    }

    #[test]
    fn test_adaptive_compression_quantizable_data() -> Result<()> {
        // Test with data in small range (should select quantization)
        let vectors = vec![
            Vector::new(vec![0.1, 0.2, 0.3, 0.4, 0.5]),
            Vector::new(vec![0.2, 0.3, 0.4, 0.5, 0.6]),
            Vector::new(vec![0.3, 0.4, 0.5, 0.6, 0.7]),
        ];

        let mut adaptive = AdaptiveCompressor::with_balanced_quality();
        adaptive.optimize_for_vectors(&vectors)?;

        let analysis = adaptive
            .get_analysis()
            .ok_or("get_analysis returned None")?;
        assert!(analysis.range < 1.0); // Should detect small range

        // Test compression
        let compressed = adaptive.compress(&vectors[0])?;
        let decompressed = adaptive.decompress(&compressed, 5)?;

        let orig = vectors[0].as_f32();
        let dec = decompressed.as_f32();
        assert_eq!(orig.len(), dec.len());

        // Check compression ratio
        assert!(adaptive.compression_ratio() < 0.5); // Should achieve good compression
        Ok(())
    }

    #[test]
    fn test_adaptive_compression_high_dimensional() -> Result<()> {
        // Test with high-dimensional data (should consider PCA)
        let mut vectors = Vec::new();
        for i in 0..10 {
            let mut data = vec![0.0; 200]; // 200 dimensions
            for (j, item) in data.iter_mut().enumerate().take(200) {
                *item = (i * j) as f32 * 0.01;
            }
            vectors.push(Vector::new(data));
        }

        let mut adaptive = AdaptiveCompressor::with_best_ratio();
        adaptive.optimize_for_vectors(&vectors)?;

        let analysis = adaptive
            .get_analysis()
            .ok_or("get_analysis returned None")?;
        // For high-dimensional data with good correlation, should recommend PCA
        match &analysis.recommended_method {
            CompressionMethod::Pca { components } => {
                assert!(*components < 200); // Should reduce dimensions
            }
            _ => {
                // Other methods are also acceptable for high-dimensional data
                // Just verify the method is reasonable
                assert!(matches!(
                    analysis.recommended_method,
                    CompressionMethod::Pca { .. }
                        | CompressionMethod::Quantization { .. }
                        | CompressionMethod::Zstd { .. }
                ));
            }
        }

        // Test compression (decompression for PCA may need additional implementation)
        let original = &vectors[0];
        println!("Original vector length: {}", original.dimensions);
        println!("Recommended method: {:?}", analysis.recommended_method);

        let compressed = adaptive.compress(original)?;
        println!("Compressed size: {} bytes", compressed.len());

        // Test that compression works and produces reasonable output
        assert!(!compressed.is_empty());
        assert!(compressed.len() < original.dimensions * 4); // Some compression achieved

        // Note: PCA decompression may require additional implementation for full compatibility
        // For now, we verify that compression works correctly
        match &analysis.recommended_method {
            CompressionMethod::Pca { components } => {
                // PCA compression should reduce the effective storage
                assert!(*components < original.dimensions);
                println!(
                    "PCA compression: {} → {} components",
                    original.dimensions, components
                );
            }
            _ => {
                // For other methods, test full round-trip
                let decompressed = adaptive.decompress(&compressed, original.dimensions)?;
                let dec = decompressed.as_f32();
                let orig = original.as_f32();
                assert_eq!(dec.len(), orig.len());
            }
        }
        Ok(())
    }

    #[test]
    fn test_adaptive_compression_method_switching() -> Result<()> {
        let mut adaptive = AdaptiveCompressor::with_fast_quality();

        // Start with sparse data
        let sparse_vectors = vec![
            Vector::new(vec![0.0, 0.0, 1.0, 0.0, 0.0]),
            Vector::new(vec![0.0, 2.0, 0.0, 0.0, 0.0]),
        ];
        adaptive.optimize_for_vectors(&sparse_vectors)?;
        let initial_switches = adaptive.get_metrics().method_switches;

        // Switch to dense, quantizable data
        let dense_vectors = vec![
            Vector::new(vec![0.1, 0.2, 0.3, 0.4, 0.5]),
            Vector::new(vec![0.2, 0.3, 0.4, 0.5, 0.6]),
        ];
        adaptive.optimize_for_vectors(&dense_vectors)?;

        // Should have switched methods
        assert!(adaptive.get_metrics().method_switches > initial_switches);
        Ok(())
    }

    #[test]
    fn test_vector_analysis() -> Result<()> {
        let vectors = vec![
            Vector::new(vec![1.0, 2.0, 3.0]),
            Vector::new(vec![2.0, 3.0, 4.0]),
            Vector::new(vec![3.0, 4.0, 5.0]),
        ];

        let analysis = VectorAnalysis::analyze(&vectors, &AdaptiveQuality::Balanced)?;

        assert!(analysis.mean > 0.0);
        assert!(analysis.std_dev > 0.0);
        assert!(analysis.range > 0.0);
        assert!(analysis.entropy >= 0.0);
        assert!(!analysis.dominant_patterns.is_empty());
        assert!(analysis.expected_ratio > 0.0 && analysis.expected_ratio <= 1.0);
        Ok(())
    }

    #[test]
    fn test_compression_method_equivalence() {
        assert!(methods_equivalent(
            &CompressionMethod::Zstd { level: 5 },
            &CompressionMethod::Zstd { level: 6 }
        )); // Small difference allowed

        assert!(!methods_equivalent(
            &CompressionMethod::Zstd { level: 1 },
            &CompressionMethod::Zstd { level: 10 }
        )); // Large difference not allowed

        assert!(methods_equivalent(
            &CompressionMethod::Quantization { bits: 8 },
            &CompressionMethod::Quantization { bits: 8 }
        ));

        assert!(!methods_equivalent(
            &CompressionMethod::Zstd { level: 5 },
            &CompressionMethod::Quantization { bits: 8 }
        )); // Different methods
    }

    #[test]
    fn test_adaptive_compressor_convenience_constructors() {
        let fast = AdaptiveCompressor::with_fast_quality();
        assert!(matches!(fast.quality_level, AdaptiveQuality::Fast));

        let balanced = AdaptiveCompressor::with_balanced_quality();
        assert!(matches!(balanced.quality_level, AdaptiveQuality::Balanced));

        let best = AdaptiveCompressor::with_best_ratio();
        assert!(matches!(best.quality_level, AdaptiveQuality::BestRatio));
    }

    #[test]
    fn test_product_quantization() -> Result<()> {
        // Create test vectors with 8 dimensions (divisible by 4 subvectors)
        let vectors = vec![
            Vector::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]),
            Vector::new(vec![2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]),
            Vector::new(vec![3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]),
            Vector::new(vec![1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5]),
        ];

        // Create Product Quantizer with 4 subvectors (2 dimensions each) and 4 centroids per codebook
        let mut pq = ProductQuantizer::new(4, 4);

        // Train the quantizer
        pq.train(&vectors)?;

        // Test compression and decompression
        let original = &vectors[0];
        let compressed = pq.compress(original)?;
        let decompressed = pq.decompress(&compressed, 8)?;

        // Check that dimensions match
        assert_eq!(decompressed.dimensions, original.dimensions);

        // Check compression ratio is reasonable (should be better than 1.0)
        let ratio = pq.compression_ratio();
        assert!(
            ratio > 0.0 && ratio < 1.0,
            "Compression ratio should be between 0 and 1, got {ratio}"
        );

        // Test that we can compress and decompress all vectors
        for vector in &vectors {
            let compressed = pq.compress(vector)?;
            let decompressed = pq.decompress(&compressed, vector.dimensions)?;
            assert_eq!(decompressed.dimensions, vector.dimensions);
        }
        Ok(())
    }

    #[test]
    fn test_product_quantization_invalid_dimensions() {
        // Test with vectors that have dimensions not divisible by subvectors
        let vectors = vec![
            Vector::new(vec![1.0, 2.0, 3.0]), // 3 dimensions, not divisible by 4
        ];

        let mut pq = ProductQuantizer::new(4, 4); // 4 subvectors
        let result = pq.train(&vectors);

        // Should fail with InvalidDimensions error
        assert!(result.is_err());
        if let Err(VectorError::InvalidDimensions(_)) = result {
            // Expected error type
        } else {
            panic!("Expected InvalidDimensions error");
        }
    }
}