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
//! Keys and how they map to slot indices.
//!
//! Every paged-index collection stores at most one key per *slot*, and
//! [`Key::index`] names the slot. For plain integers the slot is the integer
//! itself. For generational ids (an index plus a version or generation
//! counter) the slot is the index part only, so successive generations of the
//! same id share a slot.
//!
//! Collections compare the stored key with `==` on every lookup, so a key whose
//! slot is held by a different key (for example an older generation) is not
//! found. Methods that insert document what happens when the slot is held by a
//! different key.
//!
//! # Implementing `Key` for an id type
//!
//! ```
//! use paged_collections::key::Key;
//!
//! /// 32-bit entity id: low 24 bits are the index, high 8 bits the generation.
//! #[derive(Clone, Copy, PartialEq, Eq, Debug)]
//! struct Entity(u32);
//!
//! impl Key for Entity {
//! fn index(self) -> usize {
//! (self.0 & 0x00ff_ffff) as usize
//! }
//! }
//!
//! let a = Entity(5);
//! let a_next_generation = Entity(5 | 1 << 24);
//! assert_eq!(a.index(), a_next_generation.index());
//! assert_ne!(a, a_next_generation);
//! ```
/// A value that names a slot in a paged collection.
///
/// # Contract
///
/// - `index` must be a pure function of the key: equal keys have equal
/// indices, and calling it twice gives the same result.
/// - `index` must not panic. Collections call it while they rewrite their
/// index (after removing or sorting), where a panic would leave them
/// inconsistent.
/// - Different keys may share an index (generational ids do); the collections
/// then treat them as competing for one slot.
/// - Memory use follows the indices in use, not the number of keys, so keys
/// should map to indices that cluster. A flat page directory allocates
/// `max_index / PAGE` directory entries; see [`crate::page`].
///
/// Breaking the contract cannot cause undefined behaviour, but collections
/// may then return wrong results or panic.
/// A [`Key`] that its slot index and a small *tag* determine, so a direct map
/// stores only the tag in each slot, not the whole key.
///
/// [`PagedDirectMap`](crate::direct_map::PagedDirectMap) keeps
/// `Option<(K::Tag, V)>` in each slot and rebuilds keys with
/// [`from_parts`](DirectKey::from_parts). For plain integers the index is the
/// whole key and the tag is `()`, so a `u32 -> u64` map uses 16-byte slots
/// instead of 24. A generational id's tag is its generation. A key that its
/// index does not help to rebuild can use `type Tag = Self`, which stores the
/// whole key as before.
///
/// # Contract
///
/// For all keys `a` and `b`:
///
/// - `a == b` exactly when `a.index() == b.index()` and `a.tag() == b.tag()`;
/// - `K::from_parts(a.index(), a.tag()) == a`.
///
/// Neither `tag` nor `from_parts` may panic: the map calls `from_parts` only
/// with the parts of a key it was given, while iterating or removing.
/// Breaking the contract cannot cause undefined behaviour, but maps may then
/// return wrong keys or results.
///
/// ```
/// use paged_collections::direct_map::PagedDirectMap;
/// use paged_collections::key::{DirectKey, Key};
///
/// /// 32-bit entity id: low 24 bits are the index, high 8 bits the generation.
/// #[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// struct Entity(u32);
///
/// impl Key for Entity {
/// fn index(self) -> usize {
/// (self.0 & 0x00ff_ffff) as usize
/// }
/// }
///
/// impl DirectKey for Entity {
/// type Tag = u8;
/// fn tag(self) -> u8 {
/// (self.0 >> 24) as u8
/// }
/// fn from_parts(index: usize, tag: u8) -> Self {
/// Entity(index as u32 | u32::from(tag) << 24)
/// }
/// }
///
/// let mut map: PagedDirectMap<Entity, &str> = PagedDirectMap::new();
/// map.insert(Entity(5 | 1 << 24), "second generation");
/// assert_eq!(map.keys().next(), Some(Entity(5 | 1 << 24)));
/// assert_eq!(map.get(Entity(5)), None);
/// ```
/// Integers are their own index: the tag is `()`, and `from_parts` narrows
/// an index that came from a key of the same type, so it loses nothing.
impl_direct_key_int!;
impl_direct_key_int!;
impl_direct_key_int!;
impl_key_lossless!;
// `u32` and `u64` convert to `usize` without loss only on targets whose
// pointers are at least that wide, so the impls are gated on pointer width.