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
use std::fmt::{self, write, Arguments, Debug};
use std::hash::{BuildHasher, Hash};
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};

pub use lru;
pub use once_cell;
pub use rustc_hash;

pub struct LruCache {
    inner: lru::LruCache<String, u8, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>,
}

impl Default for LruCache {
    fn default() -> Self {
        let build_hasher = std::hash::BuildHasherDefault::<rustc_hash::FxHasher>::default();
        Self {
            inner: lru::LruCache::with_hasher(
                std::num::NonZeroUsize::new(128).unwrap(),
                build_hasher,
            ),
        }
    }
}

impl Deref for LruCache {
    type Target = lru::LruCache<String, u8, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for LruCache {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

struct RawTable<T, const N: usize> {
    buckets: [Option<T>; N],
}

impl<T: Copy, const N: usize> RawTable<T, N> {
    const fn new() -> Self {
        Self { buckets: [None; N] }
    }

    fn get(&self, hash: u64, mut eq: impl FnMut(&T) -> bool) -> Option<&T> {
        let mut offset = 0;
        loop {
            let index = (hash as usize + offset) % N;
            let val = unsafe { self.buckets.get_unchecked(index).as_ref() };
            if let Some(val) = val {
                if eq(val) {
                    return Some(val);
                }
            } else {
                return None;
            }
            offset += 1;
        }
    }

    fn insert(&mut self, hash: u64, value: T) {
        let mut offset = 0;
        loop {
            let index = (hash as usize + offset) % N;
            let val = unsafe { self.buckets.get_unchecked_mut(index) };
            if val.is_none() {
                *val = Some(value);
                return;
            }
            offset += 1;
        }
    }
}

pub struct ConstLru<T, H, const N: usize, const N2: usize> {
    entries: MaybeUninit<[Node<T>; N]>,
    free_after: u8,
    first: Option<u8>,
    last: Option<u8>,
    table: RawTable<u8, N2>,
    hasher: H,
}

impl<T: Hash + PartialEq, H: BuildHasher + Default, const N: usize, const N2: usize> Default
    for ConstLru<T, H, N, N2>
{
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl<T: Hash + PartialEq + Debug, H: BuildHasher, const N: usize, const N2: usize> Debug
    for ConstLru<T, H, N, N2>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConstLru")
            .field("entries", unsafe { self.entries.assume_init_ref() })
            .field("free_after", &self.free_after)
            .field("first", &self.first)
            .field("last", &self.last)
            .finish()
    }
}

impl<T: Hash + PartialEq, H: BuildHasher, const N: usize, const N2: usize> ConstLru<T, H, N, N2> {
    pub const fn new(hasher: H) -> Self {
        assert!(N < u8::MAX as usize);
        assert!(N > 0);
        Self {
            entries: MaybeUninit::uninit(),
            free_after: 0,
            first: None,
            last: None,
            table: RawTable::new(),
            hasher,
        }
    }

    pub fn push(&mut self, val: T) -> (u8, bool) {
        let hash = self.hasher.hash_one(&val);
        if let Some(entry) = self.table.get(hash, |ptr| unsafe {
            self.entries
                .assume_init_ref()
                .get_unchecked(*ptr as usize)
                .data
                == val
        }) {
            unsafe {
                let raw_ptr = self.entries.as_mut_ptr();
                let node = (*raw_ptr).get_unchecked_mut(*entry as usize);
                let next = node.next;
                let prev = node.prev;
                if let Some(next) = next {
                    let next = (*raw_ptr).get_unchecked_mut(next as usize);
                    next.prev = prev;
                }
                if let Some(prev) = prev {
                    let prev = (*raw_ptr).get_unchecked_mut(prev as usize);
                    prev.prev = next;
                }
                if let Some(old_first) = self.first {
                    (*raw_ptr).get_unchecked_mut(old_first as usize).next = Some(*entry);
                }
                self.first = Some(*entry);
                (*entry, false)
            }
        } else {
            let idx = if self.free_after < N as u8 {
                unsafe {
                    *self
                        .entries
                        .assume_init_mut()
                        .get_unchecked_mut(self.free_after as usize) = Node {
                        data: val,
                        next: None,
                        prev: self.first,
                    };
                }
                if let Some(first) = self.first {
                    unsafe {
                        self.entries
                            .assume_init_mut()
                            .get_unchecked_mut(first as usize)
                            .next = Some(self.free_after);
                    }
                }
                self.first = Some(self.free_after);
                if self.last.is_none() {
                    self.last = self.first;
                }
                let idx = self.free_after;
                self.free_after += 1;
                self.table.insert(hash, idx);
                idx
            } else {
                let last = self.last.unwrap();
                let last_node = unsafe {
                    self.entries
                        .assume_init_mut()
                        .get_unchecked_mut(last as usize)
                };
                let new = Node {
                    data: val,
                    next: None,
                    prev: self.first,
                };
                self.last = last_node.next;
                *last_node = new;
                if let Some(last) = self.last {
                    unsafe {
                        self.entries
                            .assume_init_mut()
                            .get_unchecked_mut(last as usize)
                            .prev = None;
                    }
                }
                if let Some(first) = self.first {
                    unsafe {
                        self.entries
                            .assume_init_mut()
                            .get_unchecked_mut(first as usize)
                            .next = Some(last);
                    }
                }
                self.first = Some(last);
                last
            };
            (idx, true)
        }
    }
}

#[derive(Debug)]
struct Node<T> {
    data: T,
    next: Option<u8>,
    prev: Option<u8>,
}

#[test]
fn push_2() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::*;
    let hash_builder: BuildHasherDefault<DefaultHasher> = BuildHasherDefault::default();
    let mut lru: ConstLru<_, _, 2, 4> = ConstLru::new(hash_builder);
    assert_eq!(lru.push(0), (0, true));
    assert_eq!(lru.push(0), (0, false));
    assert_eq!(lru.push(1), (1, true));
    assert_eq!(lru.push(2), (0, true));
    assert_eq!(lru.push(3), (1, true));
    assert_eq!(lru.push(4), (0, true));
}

#[test]
fn push_100() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::*;
    let hash_builder: BuildHasherDefault<DefaultHasher> = BuildHasherDefault::default();
    let mut lru: ConstLru<_, _, 100, 200> = ConstLru::new(hash_builder);
    for i in 0..100 {
        assert_eq!(lru.push(i), (i as u8, true));
    }
    for i in 0..100 {
        assert_eq!(lru.push(i + 100), (i as u8, true));
    }
}

#[test]
fn sizes() {
    use std::mem::size_of;

    dbg!(size_of::<RawTable<u8, 0>>());
    dbg!(size_of::<RawTable<u8, 10>>());
    dbg!(size_of::<RawTable<u8, 128>>());
    dbg!(size_of::<Node<()>>());
    dbg!(size_of::<ConstLru<(), (), 0, 0>>());
    dbg!(size_of::<ConstLru<(), (), 10, 20>>());
    dbg!(size_of::<ConstLru<u8, (), 128, 256>>());
}

pub trait Writable {
    fn write(self, into: &mut Vec<u8>);
}

impl Writable for &str {
    fn write(self, into: &mut Vec<u8>) {
        unsafe {
            copy(self, into);
        }
    }
}

impl Writable for char {
    fn write(self, into: &mut Vec<u8>) {
        into.push(self as u8);
    }
}

impl Writable for String {
    fn write(self, into: &mut Vec<u8>) {
        unsafe {
            copy(self.as_str(), into);
        }
    }
}

impl Writable for &String {
    fn write(self, into: &mut Vec<u8>) {
        unsafe {
            copy(self.as_str(), into);
        }
    }
}

impl Writable for Arguments<'_> {
    fn write(self, into: &mut Vec<u8>) {
        struct Wrapper<'a>(&'a mut Vec<u8>);

        impl<'a> fmt::Write for Wrapper<'a> {
            fn write_str(&mut self, s: &str) -> fmt::Result {
                unsafe {
                    copy(s, self.0);
                }
                Ok(())
            }
            fn write_char(&mut self, c: char) -> fmt::Result {
                self.0.push(c as u8);
                Ok(())
            }
        }

        let _ = write(&mut Wrapper(into), self);
    }
}

unsafe fn copy(s: &str, buf: &mut Vec<u8>) {
    let old_len = buf.len();
    buf.reserve(s.len());
    let ptr = buf.as_mut_ptr().add(old_len);
    let bytes = s.as_bytes();
    let str_ptr = bytes.as_ptr();
    for o in 0..s.len() {
        *ptr.add(o) = *str_ptr.add(o);
    }
    buf.set_len(old_len + s.len());
}

impl<F> Writable for F
where
    F: FnOnce(&mut Vec<u8>),
{
    fn write(self, to: &mut Vec<u8>) {
        self(to);
    }
}

macro_rules! write_unsized {
    ($t: ty) => {
        impl Writable for $t {
            fn write(self, to: &mut Vec<u8>) {
                let mut n = self;
                let mut n2 = n;
                let mut num_digits = 0;
                while n2 > 0 {
                    n2 /= 10;
                    num_digits += 1;
                }
                let len = num_digits.max(1);
                to.reserve(len);
                let ptr = to.as_mut_ptr().cast::<u8>();
                let old_len = to.len();
                let mut i = len - 1;
                loop {
                    unsafe { ptr.add(old_len + i).write((n % 10) as u8 + b'0') }
                    n /= 10;

                    if n == 0 {
                        break;
                    } else {
                        i -= 1;
                    }
                }

                #[allow(clippy::uninit_vec)]
                unsafe {
                    to.set_len(old_len + (len - i));
                }
            }
        }
    };
}

macro_rules! write_sized {
    ($t: ty) => {
        impl Writable for $t {
            fn write(self, to: &mut Vec<u8>) {
                let neg = self < 0;
                let mut n = if neg {
                    match self.checked_abs() {
                        Some(n) => n,
                        None => <$t>::MAX / 2 + 1,
                    }
                } else {
                    self
                };
                let mut n2 = n;
                let mut num_digits = 0;
                while n2 > 0 {
                    n2 /= 10;
                    num_digits += 1;
                }
                num_digits = num_digits.max(1);
                let len = if neg { num_digits + 1 } else { num_digits };
                to.reserve(len);
                let ptr = to.as_mut_ptr().cast::<u8>();
                let old_len = to.len();
                let mut i = len - 1;
                loop {
                    unsafe { ptr.add(old_len + i).write((n % 10) as u8 + b'0') }
                    n /= 10;

                    if n == 0 {
                        break;
                    } else {
                        i -= 1;
                    }
                }

                if neg {
                    i -= 1;
                    unsafe { ptr.add(old_len + i).write(b'-') }
                }

                #[allow(clippy::uninit_vec)]
                unsafe {
                    to.set_len(old_len + (len - i));
                }
            }
        }
    };
}

write_unsized!(u8);
write_unsized!(u16);
write_unsized!(u32);
write_unsized!(u64);
write_unsized!(u128);
write_unsized!(usize);

write_sized!(i8);
write_sized!(i16);
write_sized!(i32);
write_sized!(i64);
write_sized!(i128);
write_sized!(isize);

#[allow(unused)]
fn to_string_testing<T: Writable>(t: T) -> String {
    let mut buf = Vec::new();
    t.write(&mut buf);
    unsafe { String::from_utf8_unchecked(buf) }
}

#[test]
fn fmt_nums() {
    assert_eq!(to_string_testing(0u8), "0");
    assert_eq!(to_string_testing(100u8), "100");
    assert_eq!(to_string_testing(0u16), "0");
    assert_eq!(to_string_testing(100u16), "100");
    assert_eq!(to_string_testing(0u32), "0");
    assert_eq!(to_string_testing(100u32), "100");
    assert_eq!(to_string_testing(0i8), "0");
    assert_eq!(to_string_testing(-100i8), "-100");
    assert_eq!(to_string_testing(100i8), "100");
    assert_eq!(to_string_testing(0i16), "0");
    assert_eq!(to_string_testing(-100i16), "-100");
    assert_eq!(to_string_testing(100i16), "100");
    assert_eq!(to_string_testing(0i32), "0");
    assert_eq!(to_string_testing(-100i32), "-100");
    assert_eq!(to_string_testing(100i32), "100");
}

#[test]
fn fmt_str() {
    assert_eq!(to_string_testing("hello"), "hello");
    assert_eq!(to_string_testing("hello world"), "hello world");
    let mut buf = Vec::new();
    "hello".write(&mut buf);
    " world".write(&mut buf);
    assert_eq!(unsafe { String::from_utf8_unchecked(buf) }, "hello world");
}

#[test]
fn fmt_args() {
    assert_eq!(to_string_testing(format_args!("hello")), "hello");
    assert_eq!(
        to_string_testing(format_args!("hello world")),
        "hello world"
    );
    let mut buf = Vec::new();
    format_args!("hello").write(&mut buf);
    format_args!(" world").write(&mut buf);
    assert_eq!(unsafe { String::from_utf8_unchecked(buf) }, "hello world");
}