pumpkin_core/containers/
keyed_vec.rs1use std::marker::PhantomData;
2use std::ops::Index;
3use std::ops::IndexMut;
4
5#[derive(Debug, Hash, PartialEq, Eq)]
10pub struct KeyedVec<Key, Value> {
11 key: PhantomData<Key>,
13 elements: Vec<Value>,
15}
16
17impl<Key, Value: Clone> Clone for KeyedVec<Key, Value> {
18 fn clone(&self) -> Self {
19 Self {
20 key: PhantomData,
21 elements: self.elements.clone(),
22 }
23 }
24}
25
26impl<Key, Value> Default for KeyedVec<Key, Value> {
27 fn default() -> Self {
28 Self {
29 key: PhantomData,
30 elements: Vec::default(),
31 }
32 }
33}
34
35impl<Key, Value> KeyedVec<Key, Value> {
36 pub(crate) const fn new() -> Self {
37 Self {
38 key: PhantomData,
39 elements: Vec::new(),
40 }
41 }
42}
43
44impl<Key: StorageKey, Value> KeyedVec<Key, Value> {
45 pub fn get(&self, key: Key) -> Option<&Value> {
46 self.elements.get(key.index())
47 }
48
49 pub fn len(&self) -> usize {
50 self.elements.len()
51 }
52
53 pub fn is_empty(&self) -> bool {
54 self.elements.is_empty()
55 }
56
57 pub fn push(&mut self, value: Value) -> Key {
61 self.elements.push(value);
62
63 Key::create_from_index(self.elements.len() - 1)
64 }
65
66 pub fn new_slot(&mut self) -> Slot<'_, Key, Value> {
106 Slot { vec: self }
107 }
108
109 pub fn iter(&self) -> impl Iterator<Item = &'_ Value> {
111 self.elements.iter()
112 }
113
114 pub(crate) fn keys(&self) -> impl Iterator<Item = Key> {
115 (0..self.elements.len()).map(Key::create_from_index)
116 }
117
118 pub(crate) fn iter_mut(&mut self) -> impl Iterator<Item = &'_ mut Value> {
119 self.elements.iter_mut()
120 }
121
122 pub(crate) fn swap(&mut self, a: usize, b: usize) {
123 self.elements.swap(a, b)
124 }
125}
126
127impl<Key: StorageKey, Value: Clone> KeyedVec<Key, Value> {
128 pub fn accomodate(&mut self, key: Key, default_value: Value) {
129 if key.index() >= self.elements.len() {
130 self.elements.resize(key.index() + 1, default_value)
131 }
132 }
133
134 pub fn resize(&mut self, new_len: usize, value: Value) {
135 self.elements.resize(new_len, value)
136 }
137
138 pub fn clear(&mut self) {
139 self.elements.clear();
140 }
141}
142
143impl<Key: StorageKey, Value> Index<Key> for KeyedVec<Key, Value> {
144 type Output = Value;
145
146 fn index(&self, index: Key) -> &Self::Output {
147 &self.elements[index.index()]
148 }
149}
150
151impl<Key: StorageKey, Value> Index<&Key> for KeyedVec<Key, Value> {
152 type Output = Value;
153
154 fn index(&self, index: &Key) -> &Self::Output {
155 &self.elements[index.index()]
156 }
157}
158
159impl<Key: StorageKey, Value> IndexMut<Key> for KeyedVec<Key, Value> {
160 fn index_mut(&mut self, index: Key) -> &mut Self::Output {
161 &mut self.elements[index.index()]
162 }
163}
164
165impl StorageKey for usize {
166 fn index(&self) -> usize {
167 *self
168 }
169
170 fn create_from_index(index: usize) -> Self {
171 index
172 }
173}
174
175impl StorageKey for u32 {
176 fn index(&self) -> usize {
177 *self as usize
178 }
179
180 fn create_from_index(index: usize) -> Self {
181 index as u32
182 }
183}
184
185pub trait StorageKey: Clone {
187 fn index(&self) -> usize;
188
189 fn create_from_index(index: usize) -> Self;
190}
191
192#[derive(Debug)]
194pub struct Slot<'a, Key, Value> {
195 vec: &'a mut KeyedVec<Key, Value>,
196}
197
198impl<Key: StorageKey, Value> Slot<'_, Key, Value> {
199 pub fn key(&self) -> Key {
201 Key::create_from_index(self.vec.len())
202 }
203
204 pub fn populate(self, value: Value) -> Key {
206 self.vec.push(value)
207 }
208}