elid 0.4.24

Embedding Locality IDentifier - encode embeddings into sortable string IDs for vector search without vector stores, plus fast string similarity algorithms
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
//! Core data types for ELID encoding and decoding
//!
//! This module defines the fundamental types used throughout the ELID embedding system:
//! - [`Elid`]: The output identifier (base32hex-encoded string)
//! - [`Profile`]: Encoding strategy configuration
//! - [`ProfileInfo`]: Metadata extracted from ID headers
//! - [`Embedding`]: Input vector representation
//! - [`VectorPrecision`]: Precision options for full vector encoding
//! - [`DimensionMode`]: Dimension handling modes for projection/reduction

use super::encoding::decode_sortable;
use super::error::ElidError;
use serde::{Deserialize, Serialize};
use std::fmt;

// ============================================================================
// VectorPrecision - Precision options for full vector encoding
// ============================================================================

/// Precision options for full vector encoding
///
/// Controls how many bits are used to represent each dimension value.
/// Higher precision means more accurate reconstruction but larger output.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum VectorPrecision {
    /// Full 32-bit float (lossless, 4 bytes per dimension)
    #[default]
    Full32,
    /// 16-bit half-precision float (2 bytes per dimension)
    Half16,
    /// 8-bit quantized (1 byte per dimension, ~1% error)
    Quant8,
    /// Custom bit depth (1-32 bits per dimension)
    Bits {
        /// Number of bits per dimension (1-32)
        bits: u8,
    },
}

impl VectorPrecision {
    /// Get the number of bits used per dimension
    #[must_use]
    pub fn bits_per_dim(&self) -> u8 {
        match self {
            VectorPrecision::Full32 => 32,
            VectorPrecision::Half16 => 16,
            VectorPrecision::Quant8 => 8,
            VectorPrecision::Bits { bits } => *bits,
        }
    }

    /// Validate the precision settings
    pub fn validate(&self) -> Result<(), ElidError> {
        match self {
            VectorPrecision::Bits { bits } if *bits == 0 || *bits > 32 => Err(
                ElidError::InvalidPrecision(format!("Bits must be 1-32, got {}", bits)),
            ),
            _ => Ok(()),
        }
    }
}

// ============================================================================
// DimensionMode - Dimension handling modes
// ============================================================================

/// Dimension handling mode for full vector encoding
///
/// Controls whether to preserve original dimensions, reduce them,
/// or project to a common space for cross-dimensional comparison.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "snake_case")]
pub enum DimensionMode {
    /// Preserve all original dimensions (no projection)
    #[default]
    Preserve,
    /// Reduce to target dimensions using random projection
    Reduce {
        /// Target number of dimensions (must be < original)
        target_dims: u16,
    },
    /// Project to common space for cross-dimensional comparison
    ///
    /// This allows comparing vectors of different original dimensions
    /// by projecting them to the same intermediate space.
    Common {
        /// Common dimension space (all vectors projected to this)
        dims: u16,
    },
}

impl DimensionMode {
    /// Get the output dimension count for a given input dimension
    #[must_use]
    pub fn output_dims(&self, input_dims: u16) -> u16 {
        match self {
            DimensionMode::Preserve => input_dims,
            DimensionMode::Reduce { target_dims } => *target_dims,
            DimensionMode::Common { dims } => *dims,
        }
    }

    /// Validate the dimension mode against input dimensions
    pub fn validate(&self, input_dims: u16) -> Result<(), ElidError> {
        match self {
            DimensionMode::Preserve => Ok(()),
            DimensionMode::Reduce { target_dims } => {
                if *target_dims == 0 {
                    Err(ElidError::InvalidDimension {
                        got: 0,
                        expected_range: (1, input_dims as usize),
                    })
                } else if *target_dims >= input_dims {
                    Err(ElidError::ProjectionError(format!(
                        "Target dims {} must be less than input dims {}",
                        target_dims, input_dims
                    )))
                } else {
                    Ok(())
                }
            }
            DimensionMode::Common { dims } => {
                if *dims == 0 {
                    Err(ElidError::InvalidDimension {
                        got: 0,
                        expected_range: (1, 2048),
                    })
                } else {
                    Ok(())
                }
            }
        }
    }
}

// ============================================================================
// Elid - The primary output type
// ============================================================================

/// An ELID string (base32hex-encoded, lexicographically sortable)
///
/// This is the primary output type representing an encoded embedding ID.
/// The string contains only base32hex characters (0-9, a-v) and maintains
/// lexicographic ordering that matches the binary ordering of underlying bytes.
///
/// # Examples
///
/// ```rust,ignore
/// let id = Elid::from_string("0123456789abcdefghijk".to_string())?;
/// assert_eq!(id.as_str(), "0123456789abcdefghijk");
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Elid(String);

impl Elid {
    /// Create from validated base32hex string
    ///
    /// # Errors
    ///
    /// Returns [`ElidError::InvalidEncoding`] if the string contains characters
    /// outside the base32hex alphabet (0-9, a-v).
    pub fn from_string(s: String) -> Result<Self, ElidError> {
        // Validate: only 0-9, a-v characters
        if s.chars().all(|c| matches!(c, '0'..='9' | 'a'..='v')) {
            Ok(Elid(s))
        } else {
            Err(ElidError::InvalidEncoding)
        }
    }

    /// Get the underlying string (zero-copy)
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Decode to raw bytes (big-endian)
    ///
    /// # Errors
    ///
    /// Returns [`ElidError::InvalidEncoding`] if the base32hex decoding fails.
    pub fn to_bytes(&self) -> Result<Vec<u8>, ElidError> {
        decode_sortable(self.as_str())
    }

    /// Extract profile information from header (first 2 bytes)
    ///
    /// # Errors
    ///
    /// Returns an error if decoding fails or if the header is invalid.
    pub fn profile(&self) -> Result<ProfileInfo, ElidError> {
        let bytes = self.to_bytes()?;
        if bytes.len() < 2 {
            return Err(ElidError::InvalidHeader);
        }
        ProfileInfo::from_header(&bytes[0..2])
    }
}

impl fmt::Debug for Elid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Elid(\"{}\")", self.0)
    }
}

impl fmt::Display for Elid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

// ============================================================================
// Profile - Encoding strategy configuration
// ============================================================================

/// Encoding profile determining ID generation strategy
///
/// Profiles define how embeddings are transformed into compact identifiers.
/// Different profiles offer trade-offs between speed, locality preservation,
/// and compression.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Profile {
    /// 128-bit SimHash (cosine similarity via Hamming distance)
    ///
    /// Uses signed random projections to create a locality-sensitive hash.
    /// Fast encoding, supports similarity search via Hamming distance.
    Mini128 {
        /// Seed for deterministic random projections
        seed: u64,
    },

    /// Morton curve encoding (sortable locality)
    ///
    /// Interleaves quantized dimension bits for Z-order curve traversal.
    /// Fast encoding with good locality preservation.
    Morton10x10 {
        /// Number of dimensions to encode (1-32)
        dims: u8,
        /// Bits per dimension (1-16)
        bits_per_dim: u8,
        /// Rotation/PCA transform ID (optional, for v0.2+)
        #[serde(skip_serializing_if = "Option::is_none")]
        transform_id: Option<u16>,
    },

    /// Hilbert curve encoding (higher quality, slower)
    ///
    /// Maps to Hilbert space-filling curve for better locality preservation
    /// than Morton encoding. Slower to compute but better for range queries.
    Hilbert10x10 {
        /// Number of dimensions to encode (1-32)
        dims: u8,
        /// Bits per dimension (1-16)
        bits_per_dim: u8,
        /// Rotation/PCA transform ID (optional, for v0.2+)
        #[serde(skip_serializing_if = "Option::is_none")]
        transform_id: Option<u16>,
    },

    /// Full vector encoding (reversible, supports various precisions)
    ///
    /// Encodes the full embedding vector with configurable precision and
    /// optional dimension reduction. Supports lossless and lossy modes.
    ///
    /// Key features:
    /// - Reversible: Can decode back to original embedding
    /// - Precision options: Full32, Half16, Quant8, or custom bits
    /// - Dimension reduction: Random projection for size reduction
    /// - Cross-dimensional comparison: Project different-sized vectors to common space
    FullVector {
        /// Precision for each dimension value
        precision: VectorPrecision,
        /// How to handle dimensions (preserve, reduce, or common space)
        dimensions: DimensionMode,
        /// Seed for deterministic random projections (used for dimension reduction)
        seed: u64,
    },
}

impl Profile {
    /// Expected output length in bits (excluding header)
    ///
    /// For FullVector, this is approximate and depends on original dimensions.
    /// Use `bit_length_for_dims` for FullVector with known dimensions.
    #[must_use]
    pub fn bit_length(&self) -> usize {
        match self {
            Profile::Mini128 { .. } => 128,
            Profile::Morton10x10 {
                dims, bits_per_dim, ..
            }
            | Profile::Hilbert10x10 {
                dims, bits_per_dim, ..
            } => (*dims as usize) * (*bits_per_dim as usize),
            Profile::FullVector {
                precision,
                dimensions,
                ..
            } => {
                // Default estimate for 768-dim embeddings
                let output_dims = dimensions.output_dims(768);
                (output_dims as usize) * (precision.bits_per_dim() as usize)
            }
        }
    }

    /// Expected output length in bits for a given input dimension count
    ///
    /// More accurate than `bit_length()` for FullVector profiles.
    #[must_use]
    pub fn bit_length_for_dims(&self, input_dims: u16) -> usize {
        match self {
            Profile::Mini128 { .. } => 128,
            Profile::Morton10x10 {
                dims, bits_per_dim, ..
            }
            | Profile::Hilbert10x10 {
                dims, bits_per_dim, ..
            } => (*dims as usize) * (*bits_per_dim as usize),
            Profile::FullVector {
                precision,
                dimensions,
                ..
            } => {
                let output_dims = dimensions.output_dims(input_dims);
                (output_dims as usize) * (precision.bits_per_dim() as usize)
            }
        }
    }

    /// Expected base32hex string length (rounded up)
    ///
    /// Base32hex uses 5 bits per character, so string length is
    /// `ceil(bit_length / 5)`.
    #[must_use]
    pub fn string_length(&self) -> usize {
        // Add header bits (16 bits for basic header, up to 96 for extended)
        let header_bits = match self {
            Profile::FullVector { .. } => 96, // Extended header for full vector
            _ => 16,
        };
        (self.bit_length() + header_bits).div_ceil(5)
    }

    /// Expected base32hex string length for given input dimensions
    #[must_use]
    pub fn string_length_for_dims(&self, input_dims: u16) -> usize {
        let header_bits = match self {
            Profile::FullVector { .. } => 96, // Extended header for full vector
            _ => 16,
        };
        (self.bit_length_for_dims(input_dims) + header_bits).div_ceil(5)
    }

    /// Profile type ID (for header encoding)
    ///
    /// Returns the numeric identifier used in the ID header byte:
    /// - `0x01`: Mini128
    /// - `0x02`: Morton10x10
    /// - `0x03`: Hilbert10x10
    /// - `0x04`: FullVector
    #[must_use]
    pub fn type_id(&self) -> u8 {
        match self {
            Profile::Mini128 { .. } => 0x01,
            Profile::Morton10x10 { .. } => 0x02,
            Profile::Hilbert10x10 { .. } => 0x03,
            Profile::FullVector { .. } => 0x04,
        }
    }

    /// Check if this profile supports decoding back to the original embedding
    #[must_use]
    pub fn is_reversible(&self) -> bool {
        matches!(self, Profile::FullVector { .. })
    }

    // ========================================================================
    // Convenience Constructors
    // ========================================================================

    /// Create a lossless full vector profile (Full32 precision, preserve all dims)
    ///
    /// This produces the largest output but allows exact reconstruction
    /// of the original embedding.
    #[must_use]
    pub fn lossless() -> Self {
        Profile::FullVector {
            precision: VectorPrecision::Full32,
            dimensions: DimensionMode::Preserve,
            seed: 0x454c4944_46554c4c, // "ELIDFULL"
        }
    }

    /// Create a compressed profile with specified retention percentage
    ///
    /// The retention percentage (0.0-1.0) controls how much information is preserved:
    /// - 1.0 = lossless (Full32 precision, all dimensions)
    /// - 0.5 = half precision and/or half dimensions
    /// - 0.25 = quarter precision and/or quarter dimensions
    ///
    /// The algorithm optimizes for dimension reduction first (which preserves
    /// more geometric relationships) before reducing precision.
    ///
    /// # Parameters
    ///
    /// - `retention_pct`: Information retention (0.0-1.0)
    /// - `original_dims`: Original embedding dimension count
    ///
    /// # Returns
    ///
    /// A FullVector profile configured for the target retention
    #[must_use]
    pub fn compressed(retention_pct: f32, original_dims: u16) -> Self {
        let retention = retention_pct.clamp(0.01, 1.0);

        // Full lossless = 32 bits * original_dims
        let full_bits = 32.0 * original_dims as f32;
        let target_bits = full_bits * retention;

        // Strategy: prefer dimension reduction over precision reduction
        // because it preserves more geometric relationships

        // Start with Full32 and reduce dimensions
        let full32_target_dims = (target_bits / 32.0).round() as u16;
        if full32_target_dims >= original_dims {
            // No reduction needed
            return Profile::lossless();
        }

        if full32_target_dims >= original_dims / 4 {
            // Use Full32 with dimension reduction
            return Profile::FullVector {
                precision: VectorPrecision::Full32,
                dimensions: DimensionMode::Reduce {
                    target_dims: full32_target_dims.max(1),
                },
                seed: 0x454c4944_434f4d50, // "ELIDCOMP"
            };
        }

        // Try Half16 with dimension reduction
        let half16_target_dims = (target_bits / 16.0).round() as u16;
        if half16_target_dims >= original_dims / 4 {
            return Profile::FullVector {
                precision: VectorPrecision::Half16,
                dimensions: if half16_target_dims >= original_dims {
                    DimensionMode::Preserve
                } else {
                    DimensionMode::Reduce {
                        target_dims: half16_target_dims.max(1),
                    }
                },
                seed: 0x454c4944_434f4d50,
            };
        }

        // Try Quant8 with dimension reduction
        let quant8_target_dims = (target_bits / 8.0).round() as u16;
        Profile::FullVector {
            precision: VectorPrecision::Quant8,
            dimensions: if quant8_target_dims >= original_dims {
                DimensionMode::Preserve
            } else {
                DimensionMode::Reduce {
                    target_dims: quant8_target_dims.max(1),
                }
            },
            seed: 0x454c4944_434f4d50,
        }
    }

    /// Create a profile optimized for a maximum output string length
    ///
    /// Calculates the optimal precision and dimension settings to fit
    /// within the specified character limit while maximizing fidelity.
    ///
    /// # Parameters
    ///
    /// - `max_chars`: Maximum output string length in characters
    /// - `original_dims`: Original embedding dimension count
    ///
    /// # Returns
    ///
    /// A FullVector profile configured for the target length
    #[must_use]
    pub fn max_length(max_chars: usize, original_dims: u16) -> Self {
        // Base32hex: 5 bits per character
        // Header: 12 bytes (96 bits = ~20 chars)
        let header_chars = 20;
        let payload_chars = max_chars.saturating_sub(header_chars);
        let payload_bits = payload_chars * 5;

        if payload_bits == 0 {
            // Minimum viable encoding
            return Profile::FullVector {
                precision: VectorPrecision::Bits { bits: 1 },
                dimensions: DimensionMode::Reduce { target_dims: 1 },
                seed: 0x454c4944_4d41584c, // "ELIDMAXL"
            };
        }

        // Calculate what we can fit
        let bits_per_dim_full32 = payload_bits / original_dims as usize;

        if bits_per_dim_full32 >= 32 {
            // Can fit full lossless
            return Profile::lossless();
        }

        // Try different precision levels
        let precisions = [
            (VectorPrecision::Full32, 32),
            (VectorPrecision::Half16, 16),
            (VectorPrecision::Quant8, 8),
            (VectorPrecision::Bits { bits: 4 }, 4),
            (VectorPrecision::Bits { bits: 2 }, 2),
            (VectorPrecision::Bits { bits: 1 }, 1),
        ];

        for (precision, bits) in precisions {
            let dims_that_fit = payload_bits / bits;
            if dims_that_fit >= original_dims as usize {
                // All dimensions fit at this precision
                return Profile::FullVector {
                    precision,
                    dimensions: DimensionMode::Preserve,
                    seed: 0x454c4944_4d41584c,
                };
            } else if dims_that_fit >= 16 {
                // Reasonable number of dimensions at this precision
                return Profile::FullVector {
                    precision,
                    dimensions: DimensionMode::Reduce {
                        target_dims: dims_that_fit as u16,
                    },
                    seed: 0x454c4944_4d41584c,
                };
            }
        }

        // Fallback to minimum
        Profile::FullVector {
            precision: VectorPrecision::Bits { bits: 1 },
            dimensions: DimensionMode::Reduce {
                target_dims: (payload_bits as u16).max(1),
            },
            seed: 0x454c4944_4d41584c,
        }
    }

    /// Create a profile for cross-dimensional comparison
    ///
    /// Projects all vectors to a common dimension space, allowing comparison
    /// between embeddings of different original dimensions (e.g., 256d vs 768d).
    ///
    /// # Parameters
    ///
    /// - `common_dims`: Target dimension space (vectors will be projected here)
    /// - `precision`: Precision for the projected values (default: Half16)
    ///
    /// # Returns
    ///
    /// A FullVector profile configured for cross-dimensional comparison
    #[must_use]
    pub fn cross_dimensional(common_dims: u16) -> Self {
        Profile::FullVector {
            precision: VectorPrecision::Half16,
            dimensions: DimensionMode::Common { dims: common_dims },
            seed: 0x454c4944_58444949, // "ELIDXDIM"
        }
    }

    /// Create a cross-dimensional profile with custom precision
    #[must_use]
    pub fn cross_dimensional_with_precision(common_dims: u16, precision: VectorPrecision) -> Self {
        Profile::FullVector {
            precision,
            dimensions: DimensionMode::Common { dims: common_dims },
            seed: 0x454c4944_58444949,
        }
    }
}

impl Default for Profile {
    /// Default profile is Mini128 with standard seed
    ///
    /// The seed `0x454c4944_53494d48` is the ASCII encoding of "ELIDSIMH".
    fn default() -> Self {
        Profile::Mini128 {
            seed: 0x454c4944_53494d48, // "ELIDSIMH" in hex
        }
    }
}

// ============================================================================
// ProfileInfo - Metadata from ID headers
// ============================================================================

/// Profile information decoded from ELID header
///
/// The first 2 bytes of an ELID contain metadata about the encoding profile
/// used. Extended headers may include additional fields like transform IDs.
///
/// For FullVector profiles, the extended header contains:
/// - Bytes 2-3: Original dimension count (u16 big-endian)
/// - Byte 4: Precision type (0=Full32, 1=Half16, 2=Quant8, 3+=Bits(n-3))
/// - Byte 5: Dimension mode (0=Preserve, 1=Reduce, 2=Common)
/// - Bytes 6-7: Target/common dimensions if applicable (u16 big-endian)
/// - Bytes 8-11: Seed lower 32 bits (optional, for reproducibility)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProfileInfo {
    /// Version of ELID format (for backward compatibility)
    pub version: u8,

    /// Profile type ID (0x01=Mini128, 0x02=Morton, 0x03=Hilbert, 0x04=FullVector)
    pub profile_type: u8,

    /// Optional transform ID (for PCA/OPQ rotation)
    pub transform_id: Option<u16>,

    /// Optional model ID (for tracking which embedding model was used)
    pub model_id: Option<u16>,

    /// Original dimension count (FullVector only)
    pub original_dims: Option<u16>,

    /// Precision type for FullVector
    pub precision: Option<VectorPrecision>,

    /// Dimension mode for FullVector
    pub dimension_mode: Option<DimensionMode>,

    /// Seed for deterministic operations
    pub seed: Option<u64>,
}

impl ProfileInfo {
    /// Decode from ID header bytes
    ///
    /// The header format is:
    /// - Byte 0: `(version << 4) | profile_type`
    /// - Byte 1: Reserved / flags
    /// - Bytes 2-3: Optional transform_id (big-endian u16) for non-FullVector
    ///
    /// For FullVector (type 0x04), extended header:
    /// - Bytes 2-3: Original dimensions (u16 big-endian)
    /// - Byte 4: Precision type
    /// - Byte 5: Dimension mode
    /// - Bytes 6-7: Target/common dimensions (u16 big-endian)
    /// - Bytes 8-11: Seed lower 32 bits
    ///
    /// # Errors
    ///
    /// Returns [`ElidError::InvalidHeader`] if the header is too short.
    pub fn from_header(header: &[u8]) -> Result<Self, ElidError> {
        if header.len() < 2 {
            return Err(ElidError::InvalidHeader);
        }

        let version = (header[0] & 0xF0) >> 4; // Upper 4 bits
        let profile_type = header[0] & 0x0F; // Lower 4 bits

        // FullVector has extended header
        if profile_type == 0x04 {
            return Self::from_full_vector_header(version, header);
        }

        // Extended header fields (optional, for v0.2+)
        let transform_id = if header.len() >= 4 {
            Some(u16::from_be_bytes([header[2], header[3]]))
        } else {
            None
        };

        Ok(ProfileInfo {
            version,
            profile_type,
            transform_id,
            model_id: None,
            original_dims: None,
            precision: None,
            dimension_mode: None,
            seed: None,
        })
    }

    /// Decode FullVector extended header
    fn from_full_vector_header(version: u8, header: &[u8]) -> Result<Self, ElidError> {
        // FullVector requires at least 12 bytes header
        if header.len() < 12 {
            return Err(ElidError::InvalidHeader);
        }

        // Bytes 2-3: Original dimensions
        let original_dims = u16::from_be_bytes([header[2], header[3]]);

        // Byte 4: Precision type
        let precision = match header[4] {
            0 => VectorPrecision::Full32,
            1 => VectorPrecision::Half16,
            2 => VectorPrecision::Quant8,
            n if (3..=35).contains(&n) => VectorPrecision::Bits { bits: n - 3 + 1 },
            _ => {
                return Err(ElidError::InvalidMetadata(
                    "Invalid precision type".to_string(),
                ))
            }
        };

        // Byte 5: Dimension mode
        let dim_mode_type = header[5];

        // Bytes 6-7: Target/common dimensions
        let target_dims = u16::from_be_bytes([header[6], header[7]]);

        let dimension_mode = match dim_mode_type {
            0 => DimensionMode::Preserve,
            1 => DimensionMode::Reduce { target_dims },
            2 => DimensionMode::Common { dims: target_dims },
            _ => {
                return Err(ElidError::InvalidMetadata(
                    "Invalid dimension mode".to_string(),
                ))
            }
        };

        // Bytes 8-11: Seed (lower 32 bits, extend to u64)
        let seed_low = u32::from_be_bytes([header[8], header[9], header[10], header[11]]);

        Ok(ProfileInfo {
            version,
            profile_type: 0x04,
            transform_id: None,
            model_id: None,
            original_dims: Some(original_dims),
            precision: Some(precision),
            dimension_mode: Some(dimension_mode),
            seed: Some(seed_low as u64),
        })
    }

    /// Encode to header bytes
    ///
    /// Creates a byte vector containing the encoded header. The format matches
    /// the one expected by [`from_header`](Self::from_header).
    #[must_use]
    pub fn to_header(&self) -> Vec<u8> {
        let mut bytes = vec![
            (self.version << 4) | (self.profile_type & 0x0F),
            0x00, // Reserved / flags
        ];

        // FullVector has extended header
        if self.profile_type == 0x04 {
            // Original dimensions (bytes 2-3)
            let orig_dims = self.original_dims.unwrap_or(0);
            bytes.extend_from_slice(&orig_dims.to_be_bytes());

            // Precision type (byte 4)
            let precision_byte = match self.precision {
                Some(VectorPrecision::Full32) => 0,
                Some(VectorPrecision::Half16) => 1,
                Some(VectorPrecision::Quant8) => 2,
                Some(VectorPrecision::Bits { bits }) => 3 + bits - 1,
                None => 0,
            };
            bytes.push(precision_byte);

            // Dimension mode (byte 5) and target dims (bytes 6-7)
            let (mode_byte, target_dims) = match self.dimension_mode {
                Some(DimensionMode::Preserve) => (0u8, 0u16),
                Some(DimensionMode::Reduce { target_dims }) => (1u8, target_dims),
                Some(DimensionMode::Common { dims }) => (2u8, dims),
                None => (0u8, 0u16),
            };
            bytes.push(mode_byte);
            bytes.extend_from_slice(&target_dims.to_be_bytes());

            // Seed lower 32 bits (bytes 8-11)
            let seed_low = (self.seed.unwrap_or(0) & 0xFFFF_FFFF) as u32;
            bytes.extend_from_slice(&seed_low.to_be_bytes());

            return bytes;
        }

        // Non-FullVector: optional transform_id
        if let Some(tid) = self.transform_id {
            bytes.extend_from_slice(&tid.to_be_bytes());
        }

        bytes
    }

    /// Create ProfileInfo from a FullVector profile
    #[must_use]
    pub fn from_full_vector(
        original_dims: u16,
        precision: VectorPrecision,
        dimensions: DimensionMode,
        seed: u64,
    ) -> Self {
        ProfileInfo {
            version: 0,
            profile_type: 0x04,
            transform_id: None,
            model_id: None,
            original_dims: Some(original_dims),
            precision: Some(precision),
            dimension_mode: Some(dimensions),
            seed: Some(seed),
        }
    }
}

// ============================================================================
// Embedding - Input vector representation
// ============================================================================

/// Embedding vector (input to ELID encoding)
///
/// Represents a high-dimensional vector from an ML model. Embeddings must
/// have dimensions between 64 and 2048, and all values must be finite.
///
/// # Examples
///
/// ```rust,ignore
/// let values = vec![0.1, 0.2, 0.3, 0.4];
/// let embedding = Embedding::new(values)?;
/// assert_eq!(embedding.dim(), 4);
/// ```
#[derive(Clone, Debug)]
pub struct Embedding {
    /// Vector components (f32 for compatibility with most ML frameworks)
    values: Vec<f32>,
}

impl Embedding {
    /// Create from f32 vector
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Dimension is outside the range [64, 2048]
    /// - Any value is NaN or infinite
    pub fn new(values: Vec<f32>) -> Result<Self, ElidError> {
        Self::validate(&values)?;
        Ok(Embedding { values })
    }

    /// Create from f64 vector (converts to f32)
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`new`](Self::new).
    pub fn from_f64(values: Vec<f64>) -> Result<Self, ElidError> {
        let values_f32: Vec<f32> = values.iter().map(|&v| v as f32).collect();
        Self::new(values_f32)
    }

    /// Validate embedding constraints
    fn validate(values: &[f32]) -> Result<(), ElidError> {
        // Check dimensionality
        if values.len() < 64 || values.len() > 2048 {
            return Err(ElidError::InvalidDimension {
                got: values.len(),
                expected_range: (64, 2048),
            });
        }

        // Check for NaN/Inf
        if values.iter().any(|v| !v.is_finite()) {
            return Err(ElidError::InvalidValue);
        }

        // Warn on all-zero (valid but potentially meaningless)
        if values.iter().all(|v| *v == 0.0) {
            // TODO: Add logging when log dependency is added
            // log::warn!("All-zero embedding detected");
        }

        Ok(())
    }

    /// Get values as slice (zero-copy)
    #[must_use]
    pub fn as_slice(&self) -> &[f32] {
        &self.values
    }

    /// Normalize to unit length (L2 norm)
    ///
    /// Divides each component by the L2 norm (Euclidean length) of the vector.
    /// If the norm is zero, the vector remains unchanged.
    pub fn normalize(&mut self) {
        let norm: f32 = self.values.iter().map(|v| v * v).sum::<f32>().sqrt();
        if norm > 0.0 {
            self.values.iter_mut().for_each(|v| *v /= norm);
        }
    }

    /// Dimensionality
    #[must_use]
    pub fn dim(&self) -> usize {
        self.values.len()
    }
}

// ============================================================================
// QuantizedCoords - Space-filling curve coordinates
// ============================================================================

/// Quantized coordinates for space-filling curve encoding
///
/// This type represents an embedding's dimensions quantized to a fixed bit-width
/// for use in Morton or Hilbert curve encoding. Each dimension is mapped from
/// the embedding's value range (assumed to be [-1, 1] after normalization) to
/// an integer range [0, 2^bits_per_dim - 1].
///
/// # Quantization Algorithm
///
/// 1. Normalize embedding value from [-1, 1] to [0, 1]:
///    ```text
///    normalized = (value + 1.0) / 2.0
///    ```
///
/// 2. Scale to integer range and clamp:
///    ```text
///    max_val = (1 << bits_per_dim) - 1
///    quantized = (normalized * max_val).clamp(0, max_val) as u16
///    ```
///
/// # Examples
///
/// ```rust,ignore
/// let embedding = Embedding::new(vec![0.0; 128])?;
/// let coords = QuantizedCoords::from_embedding(&embedding, 10, 10)?;
/// assert_eq!(coords.len(), 10);
/// assert_eq!(coords.bits_per_dim(), 10);
/// ```
#[derive(Clone, Debug)]
pub struct QuantizedCoords {
    /// Coordinate values (one per dimension)
    coords: Vec<u16>,
    /// Bits per dimension
    bits: u8,
}

impl QuantizedCoords {
    /// Create quantized coordinates from an embedding
    ///
    /// Takes the first `dims` dimensions from the embedding and quantizes each
    /// dimension from the embedding value range to [0, 2^bits_per_dim - 1].
    ///
    /// # Parameters
    ///
    /// - `embedding`: Input embedding vector
    /// - `dims`: Number of dimensions to use (must be <= embedding dimension)
    /// - `bits_per_dim`: Bits per dimension (1-16)
    ///
    /// # Quantization Mapping
    ///
    /// Assumes embedding values are in range [-1, 1] after normalization:
    /// - `-1.0` -> `0`
    /// - `0.0` -> `2^(bits_per_dim-1)` (midpoint)
    /// - `1.0` -> `2^bits_per_dim - 1` (max value)
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - `dims` is 0 or greater than 32
    /// - `bits_per_dim` is 0 or greater than 16
    /// - `dims` exceeds the embedding dimension
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let embedding = Embedding::new(vec![0.5; 256])?;
    /// let coords = QuantizedCoords::from_embedding(&embedding, 8, 12)?;
    /// assert_eq!(coords.len(), 8);
    /// ```
    pub fn from_embedding(
        embedding: &Embedding,
        dims: u8,
        bits_per_dim: u8,
    ) -> Result<Self, ElidError> {
        // Validate parameters
        if dims == 0 || dims > 32 {
            return Err(ElidError::InvalidDimension {
                got: dims as usize,
                expected_range: (1, 32),
            });
        }

        if bits_per_dim == 0 || bits_per_dim > 16 {
            return Err(ElidError::InvalidDimension {
                got: bits_per_dim as usize,
                expected_range: (1, 16),
            });
        }

        if (dims as usize) > embedding.dim() {
            return Err(ElidError::InvalidDimension {
                got: dims as usize,
                expected_range: (1, embedding.dim()),
            });
        }

        // Calculate max value for this bit width
        let max_val = ((1u32 << bits_per_dim) - 1) as f32;

        // Quantize each dimension
        let coords: Vec<u16> = embedding
            .as_slice()
            .iter()
            .take(dims as usize)
            .map(|&value| {
                // Map from [-1, 1] to [0, 1]
                let normalized = (value + 1.0) / 2.0;

                // Scale to [0, max_val] and clamp
                let scaled = normalized * max_val;
                let clamped = scaled.clamp(0.0, max_val);

                // Convert to integer
                clamped.round() as u16
            })
            .collect();

        Ok(QuantizedCoords {
            coords,
            bits: bits_per_dim,
        })
    }

    /// Get coordinate values as slice (zero-copy)
    ///
    /// Returns a reference to the underlying coordinate array without
    /// allocating a new vector.
    #[must_use]
    pub fn as_slice(&self) -> &[u16] {
        &self.coords
    }

    /// Get bits per dimension
    ///
    /// Returns the number of bits allocated for each coordinate dimension.
    #[must_use]
    pub fn bits_per_dim(&self) -> u8 {
        self.bits
    }

    /// Get number of dimensions
    ///
    /// Returns the total number of quantized coordinates.
    #[must_use]
    pub fn len(&self) -> usize {
        self.coords.len()
    }

    /// Check if empty (always false for valid instances)
    ///
    /// This method is provided for clippy::len_without_is_empty compliance.
    /// Valid `QuantizedCoords` instances are never empty due to validation
    /// in `from_embedding`.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.coords.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // Elid Tests
    // ========================================================================

    #[test]
    fn test_elid_valid_string() {
        let id = Elid::from_string("0123456789abcdefghijklmnopqrstuv".to_string());
        assert!(id.is_ok());
        assert_eq!(id.unwrap().as_str(), "0123456789abcdefghijklmnopqrstuv");
    }

    #[test]
    fn test_elid_invalid_chars() {
        // 'w' is not in base32hex alphabet (only 0-9, a-v)
        let id = Elid::from_string("0123456789abcdefghijklmnopqrstuvw".to_string());
        assert!(matches!(id, Err(ElidError::InvalidEncoding)));
    }

    #[test]
    fn test_elid_ordering() {
        let id1 = Elid::from_string("00000000".to_string()).unwrap();
        let id2 = Elid::from_string("00000001".to_string()).unwrap();
        assert!(id1 < id2);
    }

    // ========================================================================
    // Profile Tests
    // ========================================================================

    #[test]
    fn test_profile_default() {
        let profile = Profile::default();
        assert_eq!(
            profile,
            Profile::Mini128 {
                seed: 0x454c4944_53494d48
            }
        );
    }

    #[test]
    fn test_profile_bit_length() {
        let mini = Profile::Mini128 { seed: 0 };
        assert_eq!(mini.bit_length(), 128);

        let morton = Profile::Morton10x10 {
            dims: 10,
            bits_per_dim: 10,
            transform_id: None,
        };
        assert_eq!(morton.bit_length(), 100);

        let hilbert = Profile::Hilbert10x10 {
            dims: 8,
            bits_per_dim: 12,
            transform_id: None,
        };
        assert_eq!(hilbert.bit_length(), 96);
    }

    #[test]
    fn test_profile_string_length() {
        // string_length now includes header bits (16 bits = 2 bytes for non-FullVector)
        let mini = Profile::Mini128 { seed: 0 };
        // 128 bits payload + 16 bits header = 144 bits -> ceil(144/5) = 29
        assert_eq!(mini.string_length(), 29);

        let morton = Profile::Morton10x10 {
            dims: 10,
            bits_per_dim: 10,
            transform_id: None,
        };
        // 100 bits payload + 16 bits header = 116 bits -> ceil(116/5) = 24
        assert_eq!(morton.string_length(), 24);
    }

    #[test]
    fn test_profile_type_id() {
        assert_eq!(Profile::Mini128 { seed: 0 }.type_id(), 0x01);
        assert_eq!(
            Profile::Morton10x10 {
                dims: 10,
                bits_per_dim: 10,
                transform_id: None
            }
            .type_id(),
            0x02
        );
        assert_eq!(
            Profile::Hilbert10x10 {
                dims: 10,
                bits_per_dim: 10,
                transform_id: None
            }
            .type_id(),
            0x03
        );
    }

    // ========================================================================
    // ProfileInfo Tests
    // ========================================================================

    #[test]
    fn test_profile_info_basic_header() {
        let header = vec![0x01, 0x00]; // version=0, type=1 (Mini128)
        let info = ProfileInfo::from_header(&header).unwrap();
        assert_eq!(info.version, 0);
        assert_eq!(info.profile_type, 1);
        assert_eq!(info.transform_id, None);
    }

    #[test]
    fn test_profile_info_extended_header() {
        let header = vec![0x12, 0x00, 0x00, 0xFF]; // version=1, type=2, transform=255
        let info = ProfileInfo::from_header(&header).unwrap();
        assert_eq!(info.version, 1);
        assert_eq!(info.profile_type, 2);
        assert_eq!(info.transform_id, Some(255));
    }

    #[test]
    fn test_profile_info_to_header() {
        let info = ProfileInfo {
            version: 1,
            profile_type: 3,
            transform_id: Some(0x1234),
            model_id: None,
            original_dims: None,
            precision: None,
            dimension_mode: None,
            seed: None,
        };
        let header = info.to_header();
        assert_eq!(header[0], 0x13); // (1 << 4) | 3
        assert_eq!(header[1], 0x00);
        assert_eq!(header[2..4], [0x12, 0x34]);
    }

    #[test]
    fn test_profile_info_roundtrip() {
        let info = ProfileInfo {
            version: 2,
            profile_type: 1,
            transform_id: Some(42),
            model_id: None,
            original_dims: None,
            precision: None,
            dimension_mode: None,
            seed: None,
        };
        let header = info.to_header();
        let decoded = ProfileInfo::from_header(&header).unwrap();
        assert_eq!(decoded.version, info.version);
        assert_eq!(decoded.profile_type, info.profile_type);
        assert_eq!(decoded.transform_id, info.transform_id);
    }

    #[test]
    fn test_profile_info_full_vector_roundtrip() {
        let info = ProfileInfo::from_full_vector(
            768,
            VectorPrecision::Half16,
            DimensionMode::Reduce { target_dims: 256 },
            0x12345678,
        );
        let header = info.to_header();
        assert_eq!(header.len(), 12);

        let decoded = ProfileInfo::from_header(&header).unwrap();
        assert_eq!(decoded.version, 0);
        assert_eq!(decoded.profile_type, 0x04);
        assert_eq!(decoded.original_dims, Some(768));
        assert_eq!(decoded.precision, Some(VectorPrecision::Half16));
        assert_eq!(
            decoded.dimension_mode,
            Some(DimensionMode::Reduce { target_dims: 256 })
        );
        assert_eq!(decoded.seed, Some(0x12345678));
    }

    #[test]
    fn test_profile_info_invalid_header() {
        let header = vec![0x01]; // Too short
        assert!(matches!(
            ProfileInfo::from_header(&header),
            Err(ElidError::InvalidHeader)
        ));
    }

    #[test]
    fn test_profile_info_full_vector_short_header() {
        // FullVector (type 0x04) needs 12 bytes
        let header = vec![0x04, 0x00, 0x00, 0x00]; // Only 4 bytes
        assert!(matches!(
            ProfileInfo::from_header(&header),
            Err(ElidError::InvalidHeader)
        ));
    }

    // ========================================================================
    // Embedding Tests
    // ========================================================================

    #[test]
    fn test_embedding_valid() {
        let values = vec![0.1; 128];
        let embedding = Embedding::new(values);
        assert!(embedding.is_ok());
        assert_eq!(embedding.unwrap().dim(), 128);
    }

    #[test]
    fn test_embedding_too_small() {
        let values = vec![0.1; 32]; // < 64
        let embedding = Embedding::new(values);
        assert!(matches!(embedding, Err(ElidError::InvalidDimension { .. })));
    }

    #[test]
    fn test_embedding_too_large() {
        let values = vec![0.1; 4096]; // > 2048
        let embedding = Embedding::new(values);
        assert!(matches!(embedding, Err(ElidError::InvalidDimension { .. })));
    }

    #[test]
    fn test_embedding_nan() {
        let mut values = vec![0.1; 128];
        values[64] = f32::NAN;
        let embedding = Embedding::new(values);
        assert!(matches!(embedding, Err(ElidError::InvalidValue)));
    }

    #[test]
    fn test_embedding_inf() {
        let mut values = vec![0.1; 128];
        values[64] = f32::INFINITY;
        let embedding = Embedding::new(values);
        assert!(matches!(embedding, Err(ElidError::InvalidValue)));
    }

    #[test]
    fn test_embedding_from_f64() {
        let values = vec![0.1_f64; 128];
        let embedding = Embedding::from_f64(values);
        assert!(embedding.is_ok());
    }

    #[test]
    fn test_embedding_normalize() {
        let values = vec![3.0, 4.0].into_iter().cycle().take(128).collect();
        let mut embedding = Embedding::new(values).unwrap();
        embedding.normalize();

        let norm: f32 = embedding
            .as_slice()
            .iter()
            .map(|v| v * v)
            .sum::<f32>()
            .sqrt();
        assert!((norm - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_embedding_normalize_zero() {
        let values = vec![0.0; 128];
        let mut embedding = Embedding::new(values).unwrap();
        embedding.normalize();
        // Should not panic or change values
        assert!(embedding.as_slice().iter().all(|&v| v == 0.0));
    }
}

#[cfg(test)]
mod quantized_coords_tests {
    use super::*;

    // ========================================================================
    // QuantizedCoords Tests
    // ========================================================================

    #[test]
    fn test_quantized_coords_basic() {
        let values = vec![0.0; 128]; // All zeros (midpoint)
        let embedding = Embedding::new(values).unwrap();

        let coords = QuantizedCoords::from_embedding(&embedding, 10, 10).unwrap();

        assert_eq!(coords.len(), 10);
        assert_eq!(coords.bits_per_dim(), 10);
        assert!(!coords.is_empty());

        // 0.0 should map to midpoint: (0 + 1) / 2 * 1023 = 511.5 ~ 512
        let expected = 512u16;
        assert!(
            coords.as_slice().iter().all(|&c| c == expected),
            "All coords should be at midpoint, got {:?}",
            coords.as_slice()
        );
    }

    #[test]
    fn test_quantized_coords_min_value() {
        let values = vec![-1.0; 128]; // Minimum value
        let embedding = Embedding::new(values).unwrap();

        let coords = QuantizedCoords::from_embedding(&embedding, 8, 10).unwrap();

        // -1.0 should map to 0: (-1 + 1) / 2 * 1023 = 0
        assert!(
            coords.as_slice().iter().all(|&c| c == 0),
            "Min values should quantize to 0, got {:?}",
            coords.as_slice()
        );
    }

    #[test]
    fn test_quantized_coords_max_value() {
        let values = vec![1.0; 128]; // Maximum value
        let embedding = Embedding::new(values).unwrap();

        let coords = QuantizedCoords::from_embedding(&embedding, 8, 10).unwrap();

        // 1.0 should map to max: (1 + 1) / 2 * 1023 = 1023
        let expected = (1u16 << 10) - 1; // 1023
        assert!(
            coords.as_slice().iter().all(|&c| c == expected),
            "Max values should quantize to {}, got {:?}",
            expected,
            coords.as_slice()
        );
    }

    #[test]
    fn test_quantized_coords_zero_dims() {
        let values = vec![0.0; 128];
        let embedding = Embedding::new(values).unwrap();

        let result = QuantizedCoords::from_embedding(&embedding, 0, 10);
        assert!(
            matches!(result, Err(ElidError::InvalidDimension { .. })),
            "Should reject 0 dimensions"
        );
    }

    #[test]
    fn test_quantized_coords_too_many_dims() {
        let values = vec![0.0; 128];
        let embedding = Embedding::new(values).unwrap();

        let result = QuantizedCoords::from_embedding(&embedding, 33, 10);
        assert!(
            matches!(result, Err(ElidError::InvalidDimension { .. })),
            "Should reject > 32 dimensions"
        );
    }

    #[test]
    fn test_quantized_coords_zero_bits() {
        let values = vec![0.0; 128];
        let embedding = Embedding::new(values).unwrap();

        let result = QuantizedCoords::from_embedding(&embedding, 10, 0);
        assert!(
            matches!(result, Err(ElidError::InvalidDimension { .. })),
            "Should reject 0 bits per dimension"
        );
    }

    #[test]
    fn test_quantized_coords_too_many_bits() {
        let values = vec![0.0; 128];
        let embedding = Embedding::new(values).unwrap();

        let result = QuantizedCoords::from_embedding(&embedding, 10, 17);
        assert!(
            matches!(result, Err(ElidError::InvalidDimension { .. })),
            "Should reject > 16 bits per dimension"
        );
    }
}