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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use crate::base::{
    drain::{DrainInner, HeaplessDrain},
    entry::{Entry, HeaplessEntry, OccupiedEntry, VacantEntry},
    iter::{IntoIterInner, IterInner, IterMutInner},
};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::Display;
use std::hash::Hash;
use std::hint::unreachable_unchecked;
use std::mem::ManuallyDrop;
use std::ptr;

pub(crate) mod drain;
pub(crate) mod entry;
#[cfg(feature = "extract_if")]
pub(crate) mod extract_if;
pub(crate) mod iter;

pub(crate) enum MapImpl<K, V, const N: usize> {
    Heapless(heapless::Vec<(K, V), N>),
    Spilled(HashMap<K, V>),
}

impl<K, V, const N: usize> MapImpl<K, V, N> {
    #[inline(always)]
    pub const fn new() -> Self {
        Self::Heapless(heapless::Vec::new())
    }

    #[inline(always)]
    pub const fn spilled(&self) -> bool {
        matches!(self, Self::Spilled(_))
    }

    #[inline(always)]
    pub fn capacity(&self) -> usize {
        match self {
            Self::Heapless(_) => N,
            Self::Spilled(m) => m.capacity(),
        }
    }

    #[inline]
    pub fn iter(&self) -> IterInner<'_, K, V, N> {
        match self {
            Self::Heapless(vec) => IterInner::Heapless { next: 0, vec },
            Self::Spilled(map) => IterInner::Spilled(map.iter()),
        }
    }

    #[inline]
    pub fn iter_mut(&mut self) -> IterMutInner<'_, K, V, N> {
        match self {
            Self::Heapless(vec) => IterMutInner::Heapless(vec.iter_mut()),
            Self::Spilled(map) => IterMutInner::Spilled(map.iter_mut()),
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        match self {
            Self::Heapless(vec) => vec.len(),
            Self::Spilled(map) => map.len(),
        }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Heapless(vec) => vec.is_empty(),
            Self::Spilled(m) => m.is_empty(),
        }
    }

    #[inline]
    pub fn drain(&mut self) -> DrainInner<'_, K, V, N> {
        match self {
            Self::Heapless(base) => DrainInner::Heapless(HeaplessDrain { base }),
            Self::Spilled(map) => DrainInner::Spilled(map.drain()),
        }
    }

    #[cfg(feature = "extract_if")]
    #[inline]
    pub fn extract_if<F>(&mut self, pred: F) -> extract_if::ExtractIfInner<'_, K, V, F, N>
    where
        F: FnMut(&K, &mut V) -> bool,
    {
        match self {
            Self::Heapless(base) => extract_if::ExtractIfInner::Heapless {
                base,
                next: 0,
                pred,
            },
            Self::Spilled(map) => extract_if::ExtractIfInner::Spilled(map.extract_if(pred)),
        }
    }

    #[inline]
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&K, &mut V) -> bool,
    {
        match self {
            Self::Heapless(vec) => {
                vec.retain_mut(|(k, v)| f(k, v));
            }
            Self::Spilled(map) => {
                map.retain(f);
            }
        }
    }

    #[inline]
    pub fn clear(&mut self) {
        match self {
            Self::Heapless(vec) => vec.clear(),
            Self::Spilled(m) => m.clear(),
        }
    }

    /// # Safety
    ///
    /// `MapImpl` must be in the `Heapless` variant.
    #[inline]
    unsafe fn into_heapless_unchecked(self) -> heapless::Vec<(K, V), N> {
        match self {
            Self::Heapless(v) => v,
            _ => unsafe { unreachable_unchecked() },
        }
    }

    /// # Safety
    ///
    /// `MapImpl` must be in the `Spilled` variant.
    #[inline]
    unsafe fn into_spilled_unchecked(self) -> HashMap<K, V> {
        match self {
            Self::Spilled(m) => m,
            _ => unsafe { unreachable_unchecked() },
        }
    }

    /// # Safety
    ///
    /// `MapImpl` must be in the `Heapless` variant.
    #[inline]
    unsafe fn as_heapless_unchecked(&self) -> &heapless::Vec<(K, V), N> {
        match self {
            Self::Heapless(m) => m,
            _ => unsafe { unreachable_unchecked() },
        }
    }

    /// # Safety
    ///
    /// `MapImpl` must be in the `Heapless` variant.
    #[inline]
    unsafe fn as_heapless_mut_unchecked(&mut self) -> &mut heapless::Vec<(K, V), N> {
        match self {
            Self::Heapless(m) => m,
            _ => unsafe { unreachable_unchecked() },
        }
    }

    // /// # Safety
    // ///
    // /// `MapImpl` must be in the `Spilled` variant.
    // #[inline]
    // unsafe fn as_spilled_unchecked(&self) -> &HashMap<K, V> {
    //     match self {
    //         Self::Spilled(m) => m,
    //         _ => unsafe { unreachable_unchecked() },
    //     }
    // }

    /// # Safety
    ///
    /// `MapImpl` must be in the `Spilled` variant.
    #[inline]
    unsafe fn as_spilled_mut_unchecked(&mut self) -> &mut HashMap<K, V> {
        match self {
            Self::Spilled(m) => m,
            _ => unsafe { unreachable_unchecked() },
        }
    }
}

impl<K, V, const N: usize> MapImpl<K, V, N>
where
    K: Eq + Hash,
{
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        if !self.spilled() && self.len() + additional > N {
            // Safety: we just checked the variant
            unsafe { self.try_spill(additional) }.unwrap();
        } else {
            // Safety: we just checked the variant
            unsafe {
                self.as_spilled_mut_unchecked().reserve(additional);
            }
        }
    }

    #[inline]
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        if !self.spilled() {
            if self.len() + additional > N {
                // Safety: we just checked the variant
                unsafe { self.try_spill(additional) }?;
            }
            // otherwise, we're good
        } else {
            // Safety: we just checked the variant
            unsafe {
                self.as_spilled_mut_unchecked().try_reserve(additional)?;
            }
        }
        Ok(())
    }

    #[inline]
    pub fn spill(&mut self) {
        if !self.spilled() {
            // Safety: we just checked the variant
            unsafe { self.try_spill(0) }.unwrap();
        }
    }

    pub fn shrink_into_heapless<const M: usize>(
        self,
    ) -> Result<MapImpl<K, V, M>, MapImpl<K, V, N>> {
        if self.len() > M {
            return Err(self);
        }

        let heapless = match self {
            MapImpl::Heapless(vec) => {
                let vec = ManuallyDrop::new(vec);
                let mut new = heapless::Vec::new();
                unsafe {
                    // Safety: vec won't be dropped, vec cannot overlap with new
                    ptr::copy_nonoverlapping(vec.as_ptr(), new.as_mut_ptr(), vec.len());
                    new.set_len(vec.len());
                }
                new
            }
            MapImpl::Spilled(map) => map.into_iter().collect::<heapless::Vec<(K, V), M>>(),
        };

        Ok(MapImpl::Heapless(heapless))
    }

    #[inline]
    pub fn shrink_to_fit(&mut self) {
        if let Self::Spilled(map) = self {
            map.shrink_to_fit()
        }
    }

    #[inline]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        if let Self::Spilled(map) = self {
            map.shrink_to(min_capacity)
        }
    }

    #[inline]
    pub fn entry(&mut self, key: K) -> Entry<'_, K, V, N> {
        match self {
            Self::Heapless(vec) => {
                if vec.is_empty() {
                    Entry::Vacant(VacantEntry::Heapless(HeaplessEntry {
                        key: Some(key),
                        inner: self,
                        index: 0,
                    }))
                } else if let Some(index) = vec.iter().position(|(k, _)| k == &key) {
                    Entry::Occupied(OccupiedEntry::Heapless(HeaplessEntry {
                        key: Some(key),
                        inner: self,
                        index,
                    }))
                } else {
                    let len = vec.len();
                    Entry::Vacant(VacantEntry::Heapless(HeaplessEntry {
                        key: Some(key),
                        inner: self,
                        index: len,
                    }))
                }
            }
            Self::Spilled(map) => match map.entry(key) {
                std::collections::hash_map::Entry::Occupied(entry) => {
                    Entry::Occupied(OccupiedEntry::Spilled(entry))
                }
                std::collections::hash_map::Entry::Vacant(entry) => {
                    Entry::Vacant(VacantEntry::Spilled(entry))
                }
            },
        }
    }

    #[inline]
    pub fn get<Q>(&self, k: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self.get_key_value(k) {
            Some((_, value)) => Some(value),
            None => None,
        }
    }

    #[inline]
    pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self {
            Self::Heapless(vec) => {
                if vec.is_empty() {
                    None
                } else {
                    match vec.iter().find(|(key, _)| key.borrow() == k) {
                        Some((key, value)) => Some((key, value)),
                        None => None,
                    }
                }
            }
            Self::Spilled(map) => map.get_key_value(k),
        }
    }

    #[cfg(feature = "many_mut")]
    #[inline]
    pub fn get_many_mut<Q, const M: usize>(&mut self, ks: [&Q; M]) -> Option<[&'_ mut V; M]>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self {
            Self::Heapless(vec) => {
                let is =
                    ks.map(|k| {
                        vec.iter().enumerate().find_map(|(i, (key, _))| {
                            if key.borrow() == k {
                                Some(i)
                            } else {
                                None
                            }
                        })
                    });
                if is.iter().any(|i| i.is_none()) {
                    return None;
                }
                let is = is.map(|i| unsafe { i.unwrap_unchecked() });
                Some(vec.get_many_mut(is).ok()?.map(|(_, v)| v))
            }
            Self::Spilled(map) => map.get_many_mut(ks),
        }
    }

    #[cfg(feature = "many_mut")]
    #[inline]
    pub unsafe fn get_many_unchecked_mut<Q, const M: usize>(
        &mut self,
        ks: [&Q; M],
    ) -> Option<[&'_ mut V; M]>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self {
            Self::Heapless(vec) => {
                let is =
                    ks.map(|k| {
                        vec.iter().enumerate().find_map(|(i, (key, _))| {
                            if key.borrow() == k {
                                Some(i)
                            } else {
                                None
                            }
                        })
                    });
                if is.iter().any(|i| i.is_none()) {
                    return None;
                }
                let is = is.map(|i| unsafe { i.unwrap_unchecked() });
                let es = unsafe { vec.get_many_unchecked_mut(is) };
                Some(es.map(|(_, v)| v))
            }
            Self::Spilled(map) => unsafe { map.get_many_unchecked_mut(ks) },
        }
    }

    #[inline]
    pub fn contains_key<Q>(&self, k: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.get(k).is_some()
    }

    #[inline]
    pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self {
            Self::Heapless(vec) => {
                if vec.is_empty() {
                    None
                } else {
                    match vec.iter_mut().find(|(key, _)| key.borrow() == k) {
                        Some((_, value)) => Some(value),
                        None => None,
                    }
                }
            }
            Self::Spilled(map) => map.get_mut(k),
        }
    }

    pub fn insert(&mut self, k: K, v: V) -> Option<V> {
        match self {
            Self::Heapless(vec) => {
                // Scan for equivalent key
                for (key, value) in vec.iter_mut() {
                    if key == &k {
                        return Some(std::mem::replace(value, v));
                    }
                }
                // No equivalent key found, insert new entry
                // find first None slot (previous removal)
                match vec.push((k, v)) {
                    Ok(()) => None,
                    Err((k, v)) => {
                        // No None slot found, spill to HashMap
                        // Safety: we just checked the variant
                        let map = unsafe { self.try_spill(1) };
                        map.unwrap().insert(k, v);
                        None
                    }
                }
            }
            Self::Spilled(m) => m.insert(k, v),
        }
    }

    #[inline]
    pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self.remove_entry(k) {
            Some((_, value)) => Some(value),
            None => None,
        }
    }

    #[inline]
    pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self {
            Self::Heapless(vec) => {
                // find index
                let index = vec.iter().position(|(key, _)| key.borrow() == k)?;
                // Safety: index is in bounds
                Some(unsafe { vec.swap_remove_unchecked(index) })
            }
            Self::Spilled(m) => m.remove_entry(k),
        }
    }

    #[inline]
    pub fn into_hashmap(mut self) -> HashMap<K, V> {
        if !self.spilled() {
            // Safety: we just checked the variant
            unsafe { self.try_spill(0) }.unwrap();
        }
        // Safety: we just spilled the map
        unsafe { self.into_spilled_unchecked() }
    }

    /// # Safety
    ///
    /// Must be in the `Heapless` variant.
    #[inline]
    unsafe fn try_spill(
        &mut self,
        additional: usize,
    ) -> Result<&mut HashMap<K, V>, TryReserveError> {
        let cap_needed = N
            .checked_add(additional)
            .ok_or(TryReserveError { kind: () })?;
        let mut map = HashMap::new();
        map.try_reserve(cap_needed)?;
        let vec = std::mem::replace(self, Self::Spilled(map));
        let (vec, map) = unsafe {
            // Safety: we just swapped the variant
            (
                vec.into_heapless_unchecked(),
                self.as_spilled_mut_unchecked(),
            )
        };
        map.extend(vec);
        Ok(map)
    }

    pub fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
        if let MapImpl::Spilled(map) = self {
            map.extend(iter);
            return;
        }
        for (k, v) in iter {
            self.insert(k, v);
        }
    }
}

impl<K, V, const N: usize> IntoIterator for MapImpl<K, V, N> {
    type Item = (K, V);
    type IntoIter = IntoIterInner<K, V, N>;

    #[inline]
    fn into_iter(self) -> IntoIterInner<K, V, N> {
        match self {
            MapImpl::Heapless(vec) => IntoIterInner::Heapless(vec),
            MapImpl::Spilled(map) => IntoIterInner::Spilled(map.into_iter()),
        }
    }
}

impl<K, V, const N: usize, const M: usize> From<[(K, V); N]> for MapImpl<K, V, M>
where
    K: Eq + Hash,
{
    fn from(arr: [(K, V); N]) -> Self {
        if N <= M {
            Self::Heapless(heapless::Vec::from_iter(arr))
        } else {
            Self::Spilled(HashMap::from(arr))
        }
    }
}

/// The error type for `try_reserve` methods.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TryReserveError {
    kind: (),
}

impl From<std::collections::TryReserveError> for TryReserveError {
    fn from(_: std::collections::TryReserveError) -> Self {
        Self { kind: () }
    }
}

impl Display for TryReserveError {
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        fmt.write_str("memory allocation failed")
    }
}

impl std::error::Error for TryReserveError {}