1#![deny(missing_docs)]
17#![no_std]
18
19#[cfg(test)]
20extern crate alloc;
21
22#[macro_use]
23extern crate cranelift_entity as entity;
24use crate::entity::packed_option;
25
26use core::borrow::BorrowMut;
27use core::cmp::Ordering;
28
29mod map;
30mod node;
31mod path;
32mod pool;
33mod set;
34
35pub use self::map::{Map, MapCursor, MapForest, MapIter};
36pub use self::set::{Set, SetCursor, SetForest, SetIter};
37
38use self::node::NodeData;
39use self::path::Path;
40use self::pool::NodePool;
41
42const INNER_SIZE: usize = 8;
45
46const MAX_PATH: usize = 16;
50
51pub trait Comparator<K>
56where
57 K: Copy,
58{
59 fn cmp(&self, a: K, b: K) -> Ordering;
63
64 fn search(&self, k: K, s: &[K]) -> Result<usize, usize> {
71 s.binary_search_by(|x| self.cmp(*x, k))
72 }
73}
74
75impl<K> Comparator<K> for ()
77where
78 K: Copy + Ord,
79{
80 fn cmp(&self, a: K, b: K) -> Ordering {
81 a.cmp(&b)
82 }
83}
84
85trait Forest {
87 type Key: Copy;
89
90 type Value: Copy;
92
93 type LeafKeys: Copy + BorrowMut<[Self::Key]>;
95
96 type LeafValues: Copy + BorrowMut<[Self::Value]>;
98
99 fn splat_key(key: Self::Key) -> Self::LeafKeys;
101
102 fn splat_value(value: Self::Value) -> Self::LeafValues;
104}
105
106#[derive(Clone, Copy, PartialEq, Eq)]
108struct Node(u32);
109entity_impl!(Node, "node");
110
111#[derive(Clone, Copy)]
113struct SetValue();
114
115fn slice_insert<T: Copy>(s: &mut [T], i: usize, x: T) {
117 for j in (i + 1..s.len()).rev() {
118 s[j] = s[j - 1];
119 }
120 s[i] = x;
121}
122
123fn slice_shift<T: Copy>(s: &mut [T], n: usize) {
125 for j in 0..s.len() - n {
126 s[j] = s[j + n];
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133 use crate::entity::EntityRef;
134
135 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
137 pub struct Block(u32);
138 entity_impl!(Block, "block");
139
140 #[test]
141 fn comparator() {
142 let block1 = Block::new(1);
143 let block2 = Block::new(2);
144 let block3 = Block::new(3);
145 let block4 = Block::new(4);
146 let vals = [block1, block2, block4];
147 let comp = ();
148 assert_eq!(comp.search(block1, &vals), Ok(0));
149 assert_eq!(comp.search(block3, &vals), Err(2));
150 assert_eq!(comp.search(block4, &vals), Ok(2));
151 }
152
153 #[test]
154 fn slice_insertion() {
155 let mut a = ['a', 'b', 'c', 'd'];
156
157 slice_insert(&mut a[0..1], 0, 'e');
158 assert_eq!(a, ['e', 'b', 'c', 'd']);
159
160 slice_insert(&mut a, 0, 'a');
161 assert_eq!(a, ['a', 'e', 'b', 'c']);
162
163 slice_insert(&mut a, 3, 'g');
164 assert_eq!(a, ['a', 'e', 'b', 'g']);
165
166 slice_insert(&mut a, 1, 'h');
167 assert_eq!(a, ['a', 'h', 'e', 'b']);
168 }
169
170 #[test]
171 fn slice_shifting() {
172 let mut a = ['a', 'b', 'c', 'd'];
173
174 slice_shift(&mut a[0..1], 1);
175 assert_eq!(a, ['a', 'b', 'c', 'd']);
176
177 slice_shift(&mut a[1..], 1);
178 assert_eq!(a, ['a', 'c', 'd', 'd']);
179
180 slice_shift(&mut a, 2);
181 assert_eq!(a, ['d', 'd', 'd', 'd']);
182 }
183}