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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
use crate::LruCache;
use crate::entry::EntryPtr;

use std::iter::FusedIterator;
use std::marker::PhantomData;

/// An iterator over references to the entries of an [LruCache] ordered from
/// least- to most-recently-used. This is obtained by calling [LruCache::iter].
pub struct Iter<'a, K, V> {
    next: EntryPtr<K, V>,
    next_back: EntryPtr<K, V>,
    lifetime: PhantomData<&'a ()>
}

impl<'a, K, V> Iter<'a, K, V> {
    pub(crate) fn new<S>(cache: &LruCache<K, V, S>) -> Iter<K, V> {
        if cache.is_empty() {
            Iter {
                next: unsafe { EntryPtr::null() },
                next_back: unsafe { EntryPtr::null() },
                lifetime: PhantomData
            }
        }
        else {
            Iter {
                next: cache.seal.get().prev,
                next_back: cache.seal.get().next,
                lifetime: PhantomData
            }
        }
    }
}

impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<(&'a K, &'a V)> {
        if self.next.is_null() {
            None
        }
        else {
            let entry = unsafe { self.next.get_extended() };

            if self.next == self.next_back {
                self.next = unsafe { EntryPtr::null() };
            }
            else {
                self.next = entry.prev;
            }

            unsafe { Some((entry.key(), entry.value())) }
        }
    }
}

impl<'a, K: 'a, V: 'a> DoubleEndedIterator for Iter<'a, K, V> {
    fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
        if self.next.is_null() {
            None
        }
        else {
            let entry = unsafe { self.next_back.get_extended() };

            if self.next_back == self.next {
                self.next = unsafe { EntryPtr::null() };
            }
            else {
                self.next_back = entry.next;
            }

            unsafe { Some((entry.key(), entry.value())) }
        }
    }
}

impl<'a, K: 'a, V: 'a> FusedIterator for Iter<'a, K, V> { }

/// An iterator over references to the keys of an [LruCache] ordered from
/// least- to most-recently-used. This is obtained by calling [LruCache::keys].
pub struct Keys<'a, K, V> {
    iter: Iter<'a, K, V>
}

impl<'a, K, V> Keys<'a, K, V> {
    pub(crate) fn new<S>(cache: &'a LruCache<K, V, S>) -> Keys<'a, K, V> {
        Keys {
            iter: Iter::new(cache)
        }
    }
}

impl<'a, K: 'a, V: 'a> Iterator for Keys<'a, K, V> {
    type Item = &'a K;

    fn next(&mut self) -> Option<&'a K> {
        self.iter.next().map(|(k, _)| k)
    }
}

impl<'a, K: 'a, V: 'a> DoubleEndedIterator for Keys<'a, K, V> {
    fn next_back(&mut self) -> Option<&'a K> {
        self.iter.next_back().map(|(k, _)| k)
    }
}

impl<'a, K: 'a, V: 'a> FusedIterator for Keys<'a, K, V> { }

/// An iterator over references to the values of an [LruCache] ordered from
/// least- to most-recently-used. This is obtained by calling
/// [LruCache::values].
pub struct Values<'a, K, V> {
    iter: Iter<'a, K, V>
}

impl<'a, K, V> Values<'a, K, V> {
    pub(crate) fn new<S>(cache: &'a LruCache<K, V, S>) -> Values<'a, K, V> {
        Values {
            iter: Iter::new(cache)
        }
    }
}

impl<'a, K: 'a, V: 'a> Iterator for Values<'a, K, V> {
    type Item = &'a V;

    fn next(&mut self) -> Option<&'a V> {
        self.iter.next().map(|(_, v)| v)
    }
}

impl<'a, K: 'a, V: 'a> DoubleEndedIterator for Values<'a, K, V> {
    fn next_back(&mut self) -> Option<&'a V> {
        self.iter.next_back().map(|(_, v)| v)
    }
}

impl<'a, K: 'a, V: 'a> FusedIterator for Values<'a, K, V> { }

struct TakingIterator<K, V> {
    next: EntryPtr<K, V>,
    next_back: EntryPtr<K, V>,
}

impl<K, V> TakingIterator<K, V> {
    fn new<S>(cache: &LruCache<K, V, S>) -> TakingIterator<K, V> {
        if cache.is_empty() {
            TakingIterator {
                next: unsafe { EntryPtr::null() },
                next_back: unsafe { EntryPtr::null() }
            }
        }
        else {
            TakingIterator {
                next: cache.seal.get().prev,
                next_back: cache.seal.get().next
            }
        }
    }
}

impl<K, V> Iterator for TakingIterator<K, V> {
    type Item = (K, V);

    fn next(&mut self) -> Option<(K, V)> {
        if self.next.is_null() {
            None
        }
        else {
            unsafe {
                let entry = self.next.read();

                if self.next == self.next_back {
                    self.next = EntryPtr::null();
                }
                else {
                    self.next = entry.prev;
                }

                Some(entry.into_key_value())
            }
        }
    }
}

impl<K, V> DoubleEndedIterator for TakingIterator<K, V> {
    fn next_back(&mut self) -> Option<(K, V)> {
        if self.next.is_null() {
            None
        }
        else {
            unsafe {
                let entry = self.next_back.read();

                if self.next_back == self.next {
                    self.next = EntryPtr::null();
                }
                else {
                    self.next_back = entry.next;
                }

                Some(entry.into_key_value())
            }
        }
    }
}

/// An iterator that drains key-value-pairs from an [LruCache] ordered from
/// least- to most-recently-used. This is obtained by calling
/// [LruCache::drain].
pub struct Drain<'a, K, V, S> {
    iterator: TakingIterator<K, V>,
    cache: &'a mut LruCache<K, V, S>
}

impl<'a, K, V, S> Drain<'a, K, V, S> {
    pub(crate) fn new(cache: &'a mut LruCache<K, V, S>) -> Drain<'a, K, V, S> {
        Drain {
            iterator: TakingIterator::new(cache),
            cache
        }
    }
}

impl<'a, K, V, S> Iterator for Drain<'a, K, V, S> {
    type Item = (K, V);

    fn next(&mut self) -> Option<(K, V)> {
        self.iterator.next()
    }
}

impl<'a, K, V, S> DoubleEndedIterator for Drain<'a, K, V, S> {
    fn next_back(&mut self) -> Option<(K, V)> {
        self.iterator.next_back()
    }
}

impl<'a, K, V, S> Drop for Drain<'a, K, V, S> {
    fn drop(&mut self) {
        // Drop all allocated memory of the remaining elements.

        for _ in self.by_ref() { }

        // Set the cache as empty.

        self.cache.seal.get_mut().next = self.cache.seal;
        self.cache.seal.get_mut().prev = self.cache.seal;

        self.cache.current_size = 0;
        self.cache.table.clear_no_drop();
    }
}

impl<'a, K, V, S> FusedIterator for Drain<'a, K, V, S> { }

/// An iterator that takes ownership of an [LruCache] and iterates over its
/// entries as key-value-pairs ordered from least- to most-recently-used. This
/// is obtained by calling [IntoIterator::into_iter] on the cache.
pub struct IntoIter<K, V, S> {
    iterator: TakingIterator<K, V>,
    cache: LruCache<K, V, S>
}

impl<K, V, S> IntoIter<K, V, S> {
    pub(crate) fn new(cache: LruCache<K, V, S>) -> IntoIter<K, V, S> {
        IntoIter {
            iterator: TakingIterator::new(&cache),
            cache
        }
    }
}

impl<K, V, S> Iterator for IntoIter<K, V, S> {
    type Item = (K, V);

    fn next(&mut self) -> Option<(K, V)> {
        self.iterator.next()
    }
}

impl<K, V, S> DoubleEndedIterator for IntoIter<K, V, S> {
    fn next_back(&mut self) -> Option<(K, V)> {
        self.iterator.next_back()
    }
}

impl<K, V, S> Drop for IntoIter<K, V, S> {
    fn drop(&mut self) {
        // Drop all allocated memory of the remaining elements.
        for _ in self.by_ref() { }

        // Clear items from the cache without dropping their memory.
        self.cache.table.clear_no_drop();
    }
}

/// An iterator that takes ownership of an [LruCache] and iterates over its
/// keys ordered from least- to most-recently-used. This is obtained by calling
/// [LruCache::into_keys].
pub struct IntoKeys<K, V, S> {
    into_iter: IntoIter<K, V, S>
}

impl<K, V, S> IntoKeys<K, V, S> {
    pub(crate) fn new(cache: LruCache<K, V, S>) -> IntoKeys<K, V, S> {
        IntoKeys {
            into_iter: IntoIter::new(cache)
        }
    }
}

impl<K, V, S> Iterator for IntoKeys<K, V, S>  {
    type Item = K;

    fn next(&mut self) -> Option<K> {
        self.into_iter.next().map(|(k, _)| k)
    }
}

impl<K, V, S> DoubleEndedIterator for IntoKeys<K, V, S> {
    fn next_back(&mut self) -> Option<K> {
        self.into_iter.next_back().map(|(k, _)| k)
    }
}

/// An iterator that takes ownership of an [LruCache] and iterates over its
/// values ordered from least- to most-recently-used. This is obtained by
/// calling [LruCache::into_values].
pub struct IntoValues<K, V, S> {
    into_iter: IntoIter<K, V, S>
}

impl<K, V, S> IntoValues<K, V, S> {
    pub(crate) fn new(cache: LruCache<K, V, S>) -> IntoValues<K, V, S> {
        IntoValues {
            into_iter: IntoIter::new(cache)
        }
    }
}

impl<K, V, S> Iterator for IntoValues<K, V, S>  {
    type Item = V;

    fn next(&mut self) -> Option<V> {
        self.into_iter.next().map(|(_, v)| v)
    }
}

impl<K, V, S> DoubleEndedIterator for IntoValues<K, V, S> {
    fn next_back(&mut self) -> Option<V> {
        self.into_iter.next_back().map(|(_, v)| v)
    }
}

#[cfg(test)]
mod tests {

    use std::sync::{Arc, Mutex};

    use crate::{HeapSize, LruCache};
    use crate::tests::{large_test_cache, singleton_test_cache};

    #[test]
    fn iter_works_for_larger_cache() {
        let cache = large_test_cache();
        let mut iter = cache.iter();

        assert_eq!(Some((&"hello", &"world")), iter.next());
        assert_eq!(Some((&"good morning", &"jupiter")), iter.next_back());
        assert_eq!(Some((&"hi", &"venus")), iter.next_back());
        assert_eq!(Some((&"ahoy", &"mars")), iter.next_back());
        assert_eq!(Some((&"greetings", &"moon")), iter.next());
        assert_eq!(None, iter.next_back());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn forward_iter_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut iter = cache.iter();

        assert_eq!(Some((&"hello", &"world")), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn backward_iter_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut iter = cache.iter();

        assert_eq!(Some((&"hello", &"world")), iter.next_back());
        assert_eq!(None, iter.next_back());
    }

    #[test]
    fn forward_keys_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut keys = cache.keys();

        assert_eq!(Some(&"hello"), keys.next());
        assert_eq!(None, keys.next());
    }

    #[test]
    fn backward_keys_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut keys = cache.keys();

        assert_eq!(Some(&"hello"), keys.next_back());
        assert_eq!(None, keys.next_back());
    }

    #[test]
    fn forward_values_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut values = cache.values();

        assert_eq!(Some(&"world"), values.next());
        assert_eq!(None, values.next());
    }

    #[test]
    fn backward_values_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut values = cache.values();

        assert_eq!(Some(&"world"), values.next_back());
        assert_eq!(None, values.next_back());
    }

    #[test]
    fn separated_iters_zip_to_pair_iter() {
        let cache = large_test_cache();
        let pair_iter_collected = cache.iter().collect::<Vec<_>>();
        let zip_iter_collected = cache.keys()
            .zip(cache.values())
            .collect::<Vec<_>>();

        assert_eq!(pair_iter_collected, zip_iter_collected);
    }

    #[test]
    fn separated_into_iters_zip_to_pair_into_iter() {
        let cache = large_test_cache();
        let pair_iter_collected =
            cache.clone().into_iter().collect::<Vec<_>>();
        let zip_iter_collected = cache.clone().into_keys()
            .zip(cache.into_values())
            .collect::<Vec<_>>();

        assert_eq!(pair_iter_collected, zip_iter_collected);
    }

    #[test]
    fn drain_clears_cache() {
        let mut cache = LruCache::new(1024);
        cache.insert("hello", "world").unwrap();
        cache.insert("greetings", "moon").unwrap();
        cache.drain().next();

        assert_eq!(0, cache.len());
        assert_eq!(0, cache.current_size());
        assert!(cache.peek_lru().is_none());
        assert!(cache.peek_mru().is_none());
        assert!(cache.get("hello").is_none());
    }

    fn test_owning_iterator<I>(mut iter: I)
    where
        I: Iterator<Item = (&'static str, &'static str)> + DoubleEndedIterator
    {
        assert_eq!(Some(("hello", "world")), iter.next());
        assert_eq!(Some(("good morning", "jupiter")), iter.next_back());
        assert_eq!(Some(("hi", "venus")), iter.next_back());
        assert_eq!(Some(("ahoy", "mars")), iter.next_back());
        assert_eq!(Some(("greetings", "moon")), iter.next());
        assert_eq!(None, iter.next_back());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn drain_returns_entries() {
        let mut cache = large_test_cache();
        let drain = cache.drain();
        test_owning_iterator(drain);
    }

    #[test]
    fn into_iter_returns_entries() {
        let cache = large_test_cache();
        let into_iter = cache.into_iter();
        test_owning_iterator(into_iter);
    }

    #[test]
    fn forward_into_iter_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut into_iter = cache.into_iter();

        assert_eq!(Some(("hello", "world")), into_iter.next());
        assert_eq!(None, into_iter.next());
    }

    #[test]
    fn backward_into_iter_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut into_iter = cache.into_iter();

        assert_eq!(Some(("hello", "world")), into_iter.next_back());
        assert_eq!(None, into_iter.next_back());
    }

    #[test]
    fn forward_into_keys_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut into_keys = cache.into_keys();

        assert_eq!(Some("hello"), into_keys.next());
        assert_eq!(None, into_keys.next());
    }

    #[test]
    fn backward_into_keys_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut into_keys = cache.into_keys();

        assert_eq!(Some("hello"), into_keys.next_back());
        assert_eq!(None, into_keys.next_back());
    }

    #[test]
    fn forward_into_values_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut into_values = cache.into_values();

        assert_eq!(Some("world"), into_values.next());
        assert_eq!(None, into_values.next());
    }

    #[test]
    fn backward_into_values_works_for_singleton_cache() {
        let cache = singleton_test_cache();
        let mut into_values = cache.into_values();

        assert_eq!(Some("world"), into_values.next_back());
        assert_eq!(None, into_values.next_back());
    }

    #[derive(Debug)]
    struct DropObserver {
        drop_counter: Arc<Mutex<u32>>
    }

    impl HeapSize for DropObserver {
        fn heap_size(&self) -> usize {
            0
        }
    }

    impl Drop for DropObserver {
        fn drop(&mut self) {
            *self.drop_counter.lock().unwrap() += 1;
        }
    }

    fn setup_cache_with_drop_counter() -> (LruCache<i32, DropObserver>, Arc<Mutex<u32>>) {
        let drop_counter = Arc::new(Mutex::new(0));
        let mut cache = LruCache::new(1024);
        cache.insert(0, DropObserver { drop_counter: Arc::clone(&drop_counter) })
            .unwrap();
        cache.insert(1, DropObserver { drop_counter: Arc::clone(&drop_counter) })
            .unwrap();

        (cache, drop_counter)
    }

    #[test]
    fn into_iter_drops_remaining_elements_if_dropped() {
        let (cache, drop_counter) = setup_cache_with_drop_counter();
        let mut into_iter = cache.into_iter();
        into_iter.next();

        assert_eq!(1, *drop_counter.lock().unwrap());

        drop(into_iter);

        assert_eq!(2, *drop_counter.lock().unwrap());
    }

    #[test]
    fn into_keys_drops_remaining_elements_if_dropped() {
        let (cache, drop_counter) = setup_cache_with_drop_counter();
        let mut into_keys = cache.into_keys();
        into_keys.next();

        assert_eq!(1, *drop_counter.lock().unwrap());

        drop(into_keys);

        assert_eq!(2, *drop_counter.lock().unwrap());
    }

    #[test]
    fn into_values_drops_remaining_elements_if_dropped() {
        let (cache, drop_counter) = setup_cache_with_drop_counter();
        let mut into_values = cache.into_values();
        into_values.next();

        assert_eq!(1, *drop_counter.lock().unwrap());

        drop(into_values);

        assert_eq!(2, *drop_counter.lock().unwrap());
    }

    #[test]
    fn drain_drops_remaining_elements_if_dropped() {
        let (mut cache, drop_counter) = setup_cache_with_drop_counter();
        let mut drain = cache.drain();
        drain.next();

        assert_eq!(1, *drop_counter.lock().unwrap());

        drop(drain);

        assert_eq!(2, *drop_counter.lock().unwrap());
        assert!(cache.is_empty());
    }

    fn assert_is_empty<T, I>(mut iterator: I)
    where
        I: Iterator<Item = T> + DoubleEndedIterator
    {
        assert!(iterator.next().is_none());
        assert!(iterator.next_back().is_none());
    }

    #[test]
    fn empty_cache_builds_empty_iterators() {
        let mut cache: LruCache<&str, &str> = LruCache::new(1024);

        assert_is_empty(cache.iter());
        assert_is_empty(cache.keys());
        assert_is_empty(cache.values());
        assert_is_empty(cache.drain());
        assert_is_empty(cache.clone().into_iter());
        assert_is_empty(cache.clone().into_keys());
        assert_is_empty(cache.into_values());
    }
}