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
//! B-Tree implementations using the _allocated_ pattern for explicit allocator control.
//!
//! This crate provides two B-Tree map implementations designed to demonstrate
//! and compare memory usage patterns:
//!
//! - [`NaiveBTreeMap`] - A traditional B-Tree with uniform node structure
//! - [`CompressedBTreeMap`] - An optimized B-Tree using ~30% less memory
//!
//! # Quick Start
//!
//! ```
//! use allocated_btree::CompressedBTreeMap;
//!
//! let mut map = CompressedBTreeMap::new();
//! map.insert(1, "one")?;
//! map.insert(2, "two")?;
//! map.insert(3, "three")?;
//!
//! assert_eq!(map.get(&2), Some(&"two"));
//! assert_eq!(map.len(), 3);
//! # Ok::<(), allocated::AllocErrorWithLayout>(())
//! ```
//!
//! # Which Implementation to Use?
//!
//! **Use [`CompressedBTreeMap`]** (recommended) for:
//! - Production use where memory efficiency matters
//! - Large datasets where the ~30% memory savings are significant
//! - General-purpose ordered map needs
//!
//! **Use [`NaiveBTreeMap`]** for:
//! - Learning about B-Tree internals (simpler implementation)
//! - Comparing memory usage patterns
//! - Debugging and testing
//!
//! # The Allocated Pattern
//!
//! This crate follows the _allocated_ pattern, providing two types per implementation:
//!
//! ## Wrapper Types (Recommended)
//!
//! - [`NaiveBTreeMap<K, V, B, A>`] - Owns allocator, safe API
//! - [`CompressedBTreeMap<K, V, B, A>`] - Owns allocator, safe API
//!
//! These are ergonomic wrappers that own their allocator and provide safe methods:
//!
//! ```
//! use allocated_btree::NaiveBTreeMap;
//!
//! let mut map = NaiveBTreeMap::new();
//! map.insert(42, "answer")?; // No unsafe blocks needed!
//! # Ok::<(), allocated::AllocErrorWithLayout>(())
//! ```
//!
//! ## Allocated Types (Advanced)
//!
//! - [`AllocatedNaiveBTreeMap<K, V, B>`] - Low-level, requires manual allocator passing
//! - [`AllocatedCompressedBTreeMap<K, V, B>`] - Low-level, requires manual allocator passing
//!
//! These are for building composite data structures or when you need fine control:
//!
//! ```
//! use allocated_btree::AllocatedNaiveBTreeMap;
//! use allocated::CountingAllocator;
//!
//! let alloc = CountingAllocator::default();
//! let mut map = AllocatedNaiveBTreeMap::<u32, String>::new_in(&alloc)?;
//!
//! unsafe {
//! map.insert_in(&alloc, 1, "one".to_string())?;
//! }
//!
//! // Track memory usage
//! println!("Allocations: {}", alloc.n_allocations());
//! # Ok::<(), allocated::AllocErrorWithLayout>(())
//! ```
//!
//! # Memory Comparison
//!
//! The compressed implementation achieves ~30% memory savings by using specialized
//! node types:
//!
//! - **Leaf nodes**: Store only keys and values (no child pointers)
//! - **Interior nodes**: Store keys, values, and child pointers
//!
//! The naive implementation uses a single node type with child pointers always
//! allocated, even for leaf nodes.
//!
//! See `examples/memory-comparison.rs` for a detailed comparison across different
//! dataset sizes.
extern crate std;
extern crate alloc;
/// Naive B-Tree implementation using a uniform node structure.
///
/// This module provides [`btree::AllocatedBTreeMap`] and its wrapper
/// [`btree::NaiveBTreeMap`]. All nodes in this implementation use the same
/// structure regardless of whether they are leaf or interior nodes.
/// Compressed B-Tree implementation using specialized node types.
///
/// This module provides [`compressed::AllocatedBTreeMap`] and its wrapper
/// [`compressed::CompressedBTreeMap`]. This implementation uses ~30% less
/// memory than the naive variant by using different structures for leaf and interior nodes.
// Re-export allocated types for advanced use cases
pub use AllocatedBTreeMap as AllocatedNaiveBTreeMap;
pub use AllocatedBTreeMap as AllocatedCompressedBTreeMap;
// Re-export wrapper types (recommended for most use cases)
pub use NaiveBTreeMap;
pub use CompressedBTreeMap;