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
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
# Dodecet Encoder Source Modules

This document provides an overview of all source modules in the dodecet-encoder library, their purposes, and relationships.

## Module Architecture

```mermaid
graph TB
    subgraph "Core Library (lib.rs)"
        LIB[lib.rs<br/>Public API exports]
    end

    subgraph "Foundation Modules"
        DODECET[dodecet.rs<br/>12-bit type]
        ARRAY[array.rs<br/>Fixed arrays]
        STRING[string.rs<br/>Dynamic strings]
        STRING_OPT[string_optimized.rs<br/>Optimized strings]
    end

    subgraph "Operations Modules"
        HEX[hex.rs<br/>Hex encoding]
        CALC[calculus.rs<br/>Numerical methods]
        SIMD[simd.rs<br/>Vector operations]
    end

    subgraph "Domain Modules"
        GEO[geometric.rs<br/>3D geometry]
        WASM[wasm.rs<br/>WebAssembly bindings]
    end

    LIB --> DODECET
    LIB --> ARRAY
    LIB --> STRING
    LIB --> HEX
    LIB --> CALC
    LIB --> SIMD
    LIB --> GEO
    LIB --> WASM

    DODECET --> ARRAY
    DODECET --> STRING
    STRING --> STRING_OPT

    GEO --> DODECET
    GEO --> ARRAY
    GEO --> SIMD

    WASM --> DODECET
    WASM --> GEO
    WASM --> HEX

    style LIB fill:#4CAF50
    style DODECET fill:#2196F3
    style GEO fill:#FF9800
    style WASM fill:#9C27B0
```

## Module Overview

### Core Modules

#### dodecet.rs
**Purpose**: Core 12-bit dodecet type implementation

**Key Types**:
- `Dodecet` - 12-bit value (0-4095)
- Stored as `u16` with 4 unused bits
- Hex-friendly (3 hex digits)

**Key Operations**:
- Creation: `from_hex()`, `new()`, `from_hex_str()`
- Conversion: `normalize()`, `as_signed()`, `to_hex_string()`
- Arithmetic: `wrapping_add()`, `wrapping_sub()`, `wrapping_mul()`
- Bitwise: AND, OR, XOR operations
- Nibble access: `nibble(0)`, `nibble(1)`, `nibble(2)`

**Dependencies**: None (foundational type)

#### array.rs
**Purpose**: Fixed-size stack-allocated dodecet arrays

**Key Types**:
- `DodecetArray<N>` - Compile-time sized array

**Key Operations**:
- Creation: `from_slice()`, `new()`
- Access: Index-based, iteration
- Aggregation: `sum()`, `product()`, `min()`, `max()`
- Conversion: `to_hex_string()`, `to_bytes()`

**Dependencies**: `dodecet.rs`

#### string.rs
**Purpose**: Dynamic heap-allocated dodecet sequences

**Key Types**:
- `DodecetString` - Growable vector of dodecets

**Key Operations**:
- Creation: `new()`, `with_capacity()`
- Mutation: `push()`, `pop()`, `extend()`
- Conversion: `to_hex_string()`, `to_bytes()`, `from_bytes()`
- Query: `len()`, `is_empty()`, `capacity()`

**Dependencies**: `dodecet.rs`

#### string_optimized.rs
**Purpose**: Performance-optimized string operations

**Key Features**:
- Batch operations
- Memory-efficient packing
- SIMD-friendly layouts

**Dependencies**: `dodecet.rs`, `string.rs`

### Operations Modules

#### hex.rs
**Purpose**: Hex encoding/decoding utilities

**Key Functions**:
- `format_spaced()` - Add spaces between dodecets
- `is_valid()` - Validate hex strings
- `to_bytes()` - Convert hex to bytes
- `from_bytes()` - Convert bytes to hex

**Key Features**:
- 3-char-per-dodecet validation
- Spaced formatting for readability
- Error handling for invalid input

**Dependencies**: None

#### calculus.rs
**Purpose**: Numerical methods and calculus operations

**Key Functions**:
- `derivative()` - Finite difference approximation
- `integral()` - Trapezoidal rule integration
- `gradient()` - Multivariate gradient
- `laplacian()` - Sum of second derivatives
- `gradient_descent()` - Optimization routine
- `encode_function()` - Create lookup table
- `decode_function()` - Interpolate from table

**Key Features**:
- Numerical approximations (not symbolic)
- Function encoding for fast lookup
- Optimization algorithms

**Dependencies**: None (uses f64 for calculations)

#### simd.rs
**Purpose**: SIMD-optimized vector operations

**Key Features**:
- Batch dodecet operations
- Platform-specific optimizations
- Cache-friendly memory layouts

**Dependencies**: `dodecet.rs`, `array.rs`

### Domain Modules

#### geometric.rs
**Purpose**: 3D geometry and vector math

**Key Types**:
- `Point3D` - 3D point (x, y, z)
- `Vector3D` - 3D vector (dx, dy, dz)
- `Transform3D` - 3x4 transformation matrix
- `Box3D` - Axis-aligned bounding box
- `Triangle` - 3-vertex triangle

**Key Operations**:
- Distance: `distance_to()`, `magnitude()`
- Vector math: `dot()`, `cross()`, `normalize()`
- Transformations: translation, rotation, scale
- Queries: contains, intersects, bounds

**Dependencies**: `dodecet.rs`, `array.rs`, `simd.rs`

#### wasm.rs
**Purpose**: WebAssembly bindings for browser use

**Key Types Exported**:
- `Point3D` - WASM-compatible 3D point
- `Vector3DWasm` - WASM-compatible vector
- `Transform3DWasm` - WASM-compatible transform

**Key Features**:
- `wasm-bindgen` integration
- JavaScript/TypeScript API
- Browser-compatible memory management

**Dependencies**: `dodecet.rs`, `geometric.rs`, `hex.rs`

## Module Size & Complexity

| Module | Lines | Purpose | Complexity |
|--------|-------|---------|------------|
| `dodecet.rs` | ~400 | Core type | Low |
| `array.rs` | ~300 | Fixed arrays | Low |
| `string.rs` | ~400 | Dynamic strings | Medium |
| `string_optimized.rs` | ~350 | Optimizations | Medium |
| `hex.rs` | ~250 | Hex utilities | Low |
| `calculus.rs` | ~400 | Numerical methods | High |
| `simd.rs` | ~250 | Vector ops | High |
| `geometric.rs` | ~600 | 3D geometry | Medium |
| `wasm.rs` | ~400 | WASM bindings | Medium |

## Data Flow Diagrams

### Dodecet Creation Flow

```mermaid
graph LR
    INPUT[Input Value] --> VALIDATE{Validate}
    VALIDATE -->|u16| FROM_U16[from_hex]
    VALIDATE -->|str| FROM_STR[from_hex_str]
    VALIDATE -->|number| CHECK{Check Range}

    CHECK -->|0-4095| NEW[new]
    CHECK -->|>4095| ERROR[Error]

    FROM_U16 --> DODECET[Dodecet]
    FROM_STR --> PARSE[Parse Hex]
    PARSE --> DODECET
    NEW --> DODECET

    style DODECET fill:#4CAF50
    style ERROR fill:#f44336
```

### Geometric Operations Flow

```mermaid
graph TB
    POINTS[Two Point3D] --> SUB[Subtract coordinates]
    SUB --> VECTOR[Vector3D]

    VECTOR --> DOT[dot product]
    VECTOR --> CROSS[cross product]
    VECTOR --> MAG[magnitude]

    DOT --> SCALAR[Scalar result]
    CROSS --> VECTOR2[New Vector3D]
    MAG --> DIST[Distance]

    style POINTS fill:#e1f5fe
    style VECTOR fill:#fff3e0
    style DIST fill:#f3e5f5
```

### WASM Binding Flow

```mermaid
sequenceDiagram
    participant JS as JavaScript
    participant WASM as wasm.rs
    participant GEO as geometric.rs
    participant DODECET as dodecet.rs

    JS->>WASM: new Point3D(x, y, z)
    WASM->>GEO: Point3D::new()
    GEO->>DODECET: Dodecet::new()
    DODECET-->>GEO: Dodecet instances
    GEO-->>WASM: Point3D
    WASM-->>JS: Point3D instance

    JS->>WASM: point.distanceTo(other)
    WASM->>GEO: distance_to()
    GEO->>GEO: Calculate sqrt(dx² + dy² + dz²)
    GEO-->>WASM: f64 distance
    WASM-->>JS: number
```

## Testing Strategy

### Unit Tests per Module

```mermaid
graph TB
    subgraph "Test Coverage"
        DODECET_TESTS[dodecet.rs<br/>40+ tests]
        ARRAY_TESTS[array.rs<br/>30+ tests]
        STRING_TESTS[string.rs<br/>35+ tests]
        HEX_TESTS[hex.rs<br/>25+ tests]
        CALC_TESTS[calculus.rs<br/>20+ tests]
        SIMD_TESTS[simd.rs<br/>15+ tests]
        GEO_TESTS[geometric.rs<br/>50+ tests]
        WASM_TESTS[wasm.rs<br/>30+ tests]
    end

    subgraph "Test Categories"
        UNIT[Unit Tests]
        INTG[Integration Tests]
        PROP[Property Tests]
        PERF[Performance Tests]
    end

    DODECET_TESTS --> UNIT
    ARRAY_TESTS --> UNIT
    STRING_TESTS --> UNIT
    HEX_TESTS --> UNIT

    CALC_TESTS --> INTG
    GEO_TESTS --> INTG
    WASM_TESTS --> INTG

    SIMD_TESTS --> PERF

    style UNIT fill:#e1f5fe
    style INTG fill:#fff3e0
    style PERF fill:#f3e5f5
```

## Build Configuration

### Feature Flags

```toml
[features]
default = ["geometric", "calculus"]
geometric = ["dep:geometric"]
calculus = ["dep:calculus"]
wasm = ["dep:wasm-bindgen"]
simd = ["dep:simd"]
```

### Conditional Compilation

```mermaid
graph TB
    subgraph "Platform Targets"
        NATIVE[Native Rust]
        WASM32[WASM32]
        SIMD[SIMD-enabled]
    end

    subgraph "Enabled Features"
        GEO[geometric.rs]
        CALC[calculus.rs]
        WASM[wasm.rs]
        SIMD_OPS[simd.rs]
    end

    NATIVE --> GEO
    NATIVE --> CALC
    NATIVE --> SIMD_OPS

    WASM32 --> GEO
    WASM32 --> WASM

    SIMD --> SIMD_OPS

    style NATIVE fill:#4CAF50
    style WASM32 fill:#2196F3
    style SIMD fill:#FF9800
```

## Performance Considerations

### Memory Layout

```mermaid
graph LR
    subgraph "Dodecet"
        U16[u16: 2 bytes]
        BITS[12 bits data]
        UNUSED[4 bits unused]
    end

    subgraph "DodecetArray<3>"
        D1[Dodecet 1]
        D2[Dodecet 2]
        D3[Dodecet 3]
        TOTAL[6 bytes total]
    end

    subgraph "Point3D"
        X[x: Dodecet]
        Y[y: Dodecet]
        Z[z: Dodecet]
        SIZE[6 bytes total]
    end

    U16 --> BITS
    U16 --> UNUSED

    D1 --> TOTAL
    D2 --> TOTAL
    D3 --> TOTAL

    X --> SIZE
    Y --> SIZE
    Z --> SIZE

    style BITS fill:#4CAF50
    style TOTAL fill:#2196F3
    style SIZE fill:#FF9800
```

### Optimization Strategies

1. **Inline Functions**: Small operations are inlined
2. **Const Generics**: Compile-time array sizing
3. **SIMD**: Vector operations where available
4. **Cache Locality**: Compact memory layout
5. **Zero-copy**: References over clones

## Contributing Guidelines

### Adding New Features

1. Choose appropriate module
2. Add comprehensive tests
3. Document with examples
4. Update this README
5. Run `cargo test` and `cargo clippy`

### Code Style

- Follow Rust naming conventions
- Use `cargo fmt` for formatting
- Add doc comments to public items
- Include usage examples in docs
- Keep functions focused and small

## Module Dependencies Summary

```
dodecet.rs (foundation)
├── array.rs
├── string.rs
│   └── string_optimized.rs
├── geometric.rs
│   └── simd.rs
└── wasm.rs
    ├── hex.rs
    └── geometric.rs
```

---

## General-Purpose Library

The dodecet-encoder library is designed as a **general-purpose 12-bit encoding library** suitable for any application requiring memory-efficient spatial encoding. While it's used by SuperInstance projects (constrainttheory, claw, spreadsheet-moment) as a reference implementation, these are just example use cases.

**Common Applications:**
- 3D graphics and game engines
- Embedded systems and IoT
- Scientific computing and simulations
- Network protocols and data compression
- Any domain where 12-bit precision and memory efficiency are valuable

---

**Last Updated**: 2026-03-19
**Version**: 1.0.0
**Status**: Production Ready