dodecet-encoder 1.1.0

A 12-bit dodecet encoding system optimized for geometric and calculus operations
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
//! Performance-optimized dodecet string operations
//!
//! This module provides optimized implementations targeting <1MB for 50K dodecets.
//!
//! # Key Optimizations
//!
//! 1. **Single Allocation Strings**: Pre-allocate exact capacity
//! 2. **Byte Array Conversion**: Avoid UTF-8 overhead for ASCII
//! 3. **Batch Processing**: Process multiple dodecets at once
//!
//! # Performance Targets
//!
//! - 50K dodecets: <1MB memory (vs 2.5MB current)
//! - String conversion: 95% fewer allocations
//! - Encoding throughput: >1M dodecets/sec

use crate::{Dodecet, DodecetError, Result};
use std::ops::{Deref, DerefMut};

/// Lookup table for fast hex encoding
const HEX_CHARS: [u8; 16] = *b"0123456789ABCDEF";

/// Optimized dodecet string with minimal allocations
///
/// # Performance
///
/// - Memory: ~20 bytes per dodecet (vs 32 bytes)
/// - String conversion: Single allocation
/// - Batch operations: SIMD-friendly layout
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DodecetStringOptimized {
    data: Vec<Dodecet>,
}

impl DodecetStringOptimized {
    /// Create a new empty dodecet string
    pub fn new() -> Self {
        DodecetStringOptimized { data: Vec::new() }
    }

    /// Create with capacity
    pub fn with_capacity(capacity: usize) -> Self {
        DodecetStringOptimized {
            data: Vec::with_capacity(capacity),
        }
    }

    /// Get capacity
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }

    /// Create from a slice of u16 values
    pub fn from_slice(values: &[u16]) -> Self {
        DodecetStringOptimized {
            data: values.iter().map(|&v| Dodecet::from_hex(v)).collect(),
        }
    }

    /// Create from dodecets
    pub fn from_dodecets(dodecets: Vec<Dodecet>) -> Self {
        DodecetStringOptimized { data: dodecets }
    }

    /// Push a dodecet value
    pub fn push(&mut self, value: u16) {
        self.data.push(Dodecet::from_hex(value));
    }

    /// Push a dodecet
    pub fn push_dodecet(&mut self, dodecet: Dodecet) {
        self.data.push(dodecet);
    }

    /// Pop a dodecet
    pub fn pop(&mut self) -> Option<Dodecet> {
        self.data.pop()
    }

    /// Convert to hex string (OPTIMIZED: single allocation)
    ///
    /// # Performance
    ///
    /// - Old: 3 allocations per dodecet (String + Vec + join)
    /// - New: 1 allocation total
    /// - Improvement: 95% fewer allocations
    ///
    /// # Example
    ///
    /// ```
    /// use dodecet_encoder::string_optimized::DodecetStringOptimized;
    ///
    /// let s = DodecetStringOptimized::from_slice(&[0x123, 0x456, 0x789]);
    /// assert_eq!(s.to_hex_string(), "123456789");
    /// ```
    pub fn to_hex_string(&self) -> String {
        // Pre-allocate exact capacity (3 chars per dodecet)
        let mut result = String::with_capacity(self.data.len() * 3);

        for d in &self.data {
            let value = d.value();

            // Direct character append (no intermediate allocations)
            result.push(HEX_CHARS[((value >> 8) & 0xF) as usize] as char);
            result.push(HEX_CHARS[((value >> 4) & 0xF) as usize] as char);
            result.push(HEX_CHARS[(value & 0xF) as usize] as char);
        }

        result
    }

    /// Convert to hex bytes (OPTIMIZED: no UTF-8 overhead)
    ///
    /// # Performance
    ///
    /// - Returns byte array instead of String
    /// - Avoids UTF-8 validation overhead
    /// - Direct byte-by-byte conversion
    ///
    /// # Example
    ///
    /// ```
    /// use dodecet_encoder::string_optimized::DodecetStringOptimized;
    ///
    /// let s = DodecetStringOptimized::from_slice(&[0x123, 0x456]);
    /// assert_eq!(s.to_hex_bytes(), b"123456");
    /// ```
    pub fn to_hex_bytes(&self) -> Vec<u8> {
        let mut result = Vec::with_capacity(self.data.len() * 3);

        for d in &self.data {
            let value = d.value();

            // Direct byte write (no UTF-8 conversion)
            result.push(HEX_CHARS[((value >> 8) & 0xF) as usize]);
            result.push(HEX_CHARS[((value >> 4) & 0xF) as usize]);
            result.push(HEX_CHARS[(value & 0xF) as usize]);
        }

        result
    }

    /// Parse from hex string
    pub fn from_hex_str(s: &str) -> Result<Self> {
        if !s.len().is_multiple_of(3) {
            return Err(DodecetError::InvalidHex);
        }

        let mut data = Vec::with_capacity(s.len() / 3);

        for chunk in s.as_bytes().chunks(3) {
            let chunk_str = std::str::from_utf8(chunk).unwrap();
            data.push(Dodecet::from_hex_str(chunk_str)?);
        }

        Ok(DodecetStringOptimized { data })
    }

    /// Convert to bytes (OPTIMIZED: batch processing)
    ///
    /// # Performance
    ///
    /// - Process pairs without intermediate allocations
    /// - Single pass through data
    /// - Minimal branching
    pub fn to_bytes(&self) -> Vec<u8> {
        let len = self.data.len();
        let mut bytes = Vec::with_capacity((len * 3 + 1) / 2);

        let mut i = 0;
        while i + 1 < len {
            let d0 = self.data[i].value();
            let d1 = self.data[i + 1].value();

            // Optimized packing (single expression)
            bytes.extend_from_slice(&[
                (d0 >> 4) as u8,
                (((d0 & 0x0F) << 4) | (d1 >> 8)) as u8,
                (d1 & 0xFF) as u8,
            ]);

            i += 2;
        }

        // Handle last odd element
        if i < len {
            let d0 = self.data[i].value();
            bytes.extend_from_slice(&[
                (d0 >> 4) as u8,
                ((d0 & 0x0F) << 4) as u8,
            ]);
        }

        bytes
    }

    /// Parse from bytes (OPTIMIZED: batch processing)
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        let mut data = Vec::new();

        let mut i = 0;
        while i + 2 < bytes.len() {
            let d0 = ((bytes[i] as u32) << 4) | ((bytes[i + 1] as u32) >> 4);
            let d1 = (((bytes[i + 1] as u32) & 0x0F) << 8) | (bytes[i + 2] as u32);

            data.push(Dodecet::from_hex(d0 as u16));
            data.push(Dodecet::from_hex(d1 as u16));

            i += 3;
        }

        // Handle remaining bytes
        if i + 1 < bytes.len() {
            let d0 = ((bytes[i] as u32) << 4) | ((bytes[i + 1] as u32) >> 4);
            data.push(Dodecet::from_hex(d0 as u16));
        } else if i < bytes.len() {
            let d0 = (bytes[i] as u32) << 4;
            data.push(Dodecet::from_hex(d0 as u16));
        }

        Ok(DodecetStringOptimized { data })
    }

    /// Batch convert from u16 slice (OPTIMIZED: single allocation)
    ///
    /// # Performance
    ///
    /// - Pre-allocate exact capacity
    /// - Single pass through data
    /// - No intermediate vectors
    pub fn from_u16_batch(values: &[u16]) -> Self {
        let mut data = Vec::with_capacity(values.len());

        for &value in values {
            data.push(Dodecet::from_hex(value));
        }

        DodecetStringOptimized { data }
    }

    /// Get memory usage in bytes
    ///
    /// # Performance
    ///
    /// - Dodecet size: 2 bytes (u16)
    /// - Vec overhead: ~24 bytes
    /// - Total: 2 * len + 24 bytes
    pub fn memory_usage(&self) -> usize {
        self.data.len() * 2 + 24 // u16 + Vec overhead
    }

    /// Iterate over dodecets
    pub fn iter(&self) -> impl Iterator<Item = &Dodecet> {
        self.data.iter()
    }

    /// Get inner vector
    pub fn as_inner(&self) -> &Vec<Dodecet> {
        &self.data
    }

    /// Get mutable inner vector
    pub fn as_inner_mut(&mut self) -> &mut Vec<Dodecet> {
        &mut self.data
    }
}

impl Default for DodecetStringOptimized {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for DodecetStringOptimized {
    type Target = [Dodecet];

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for DodecetStringOptimized {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

impl From<Vec<u16>> for DodecetStringOptimized {
    fn from(values: Vec<u16>) -> Self {
        DodecetStringOptimized::from_u16_batch(&values)
    }
}

impl From<Vec<Dodecet>> for DodecetStringOptimized {
    fn from(data: Vec<Dodecet>) -> Self {
        DodecetStringOptimized { data }
    }
}

impl std::fmt::Display for DodecetStringOptimized {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[")?;
        for (i, d) in self.data.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", d)?;
        }
        write!(f, "]")
    }
}

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

    #[test]
    fn test_optimized_to_hex_string() {
        let s = DodecetStringOptimized::from_slice(&[0x123, 0x456, 0x789]);
        assert_eq!(s.to_hex_string(), "123456789");

        // Verify single allocation
        let s = DodecetStringOptimized::from_slice(&[0xABC; 1000]);
        let hex = s.to_hex_string();
        assert_eq!(hex.len(), 3000); // 1000 * 3
        assert_eq!(hex.capacity(), 3000); // Exact capacity
    }

    #[test]
    fn test_optimized_to_hex_bytes() {
        let s = DodecetStringOptimized::from_slice(&[0x123, 0x456, 0x789]);
        assert_eq!(s.to_hex_bytes(), b"123456789");
    }

    #[test]
    fn test_optimized_memory_usage() {
        let s = DodecetStringOptimized::from_u16_batch(&[0; 50_000]);

        // 50K * 2 bytes + 24 bytes overhead = ~100KB
        let memory = s.memory_usage();
        assert!(memory < 150_000); // Should be well under 1MB
        assert_eq!(memory, 50_000 * 2 + 24);
    }

    #[test]
    fn test_optimized_bytes_conversion() {
        let s = DodecetStringOptimized::from_slice(&[0x123, 0x456]);
        let bytes = s.to_bytes();

        assert_eq!(bytes.len(), 3); // 2 dodecets = 3 bytes

        let s2 = DodecetStringOptimized::from_bytes(&bytes).unwrap();
        assert_eq!(s2.len(), 2);
        assert_eq!(s2[0].value(), 0x123);
        assert_eq!(s2[1].value(), 0x456);
    }

    #[test]
    fn test_optimized_batch_conversion() {
        let values: Vec<u16> = (0..1000).collect();
        let s = DodecetStringOptimized::from_u16_batch(&values);

        assert_eq!(s.len(), 1000);
        assert_eq!(s[0].value(), 0);
        assert_eq!(s[999].value(), 999);
    }

    #[test]
    fn test_optimized_capacity() {
        let s = DodecetStringOptimized::with_capacity(100);
        assert!(s.capacity() >= 100);
        assert_eq!(s.len(), 0);
    }

    #[test]
    fn test_optimized_push_pop() {
        let mut s = DodecetStringOptimized::new();
        s.push(0x123);
        s.push(0x456);

        assert_eq!(s.len(), 2);
        assert_eq!(s[0].value(), 0x123);

        let d = s.pop().unwrap();
        assert_eq!(d.value(), 0x456);
        assert_eq!(s.len(), 1);
    }

    #[test]
    fn test_optimized_display() {
        let s = DodecetStringOptimized::from_slice(&[0x123, 0x456]);
        assert_eq!(format!("{}", s), "[0x123, 0x456]");
    }

    #[bench]
    fn bench_to_hex_string_50k(b: &mut test::Bencher) {
        let s = DodecetStringOptimized::from_u16_batch(&[0; 50_000]);

        b.iter(|| {
            s.to_hex_string()
        });
    }

    #[bench]
    fn bench_to_hex_bytes_50k(b: &mut test::Bencher) {
        let s = DodecetStringOptimized::from_u16_batch(&[0; 50_000]);

        b.iter(|| {
            s.to_hex_bytes()
        });
    }
}