docchi_core/imp/structs/
linked_map_iter.rs

1use crate::imp::structs::linked_m::{LinkedMap, MutNode};
2use crate::imp::structs::linked_map_unsafe_iter::LinkedMapUnsafeIter;
3use std::marker::PhantomData;
4
5
6pub struct LinkedMapIter<'a, V>{
7    iter : LinkedMapUnsafeIter<V>,
8    phantom : PhantomData<&'a LinkedMap<V>>,
9}
10impl<'a, V> LinkedMapIter<'a, V>{
11    pub(crate) fn new(map : &'a LinkedMap<V>, node : *const MutNode<V>) -> LinkedMapIter<'a, V>{
12        //LinkedMapIterが有効な間に書き換えるとアウトだが、&が有効なはずなので大丈夫
13        LinkedMapIter{ iter : LinkedMapUnsafeIter::new(map, node), phantom : PhantomData::default() }
14    }
15
16    ///現在のカーソルにあるアイテムを返し、カーソルを進める
17    pub fn next(&mut self) -> Option<(&'a u64, &'a V)> {
18        self.iter.next_const()
19    }
20
21    ///前に戻ることが出来る。そして元あった場所を削除し、それによって削除されたアイテムの次にあったアイテムが現在のカーソルの次にくるので、
22    /// next2回でそれをとることも出来る。Cインターフェースやunsafe iterなら
23    ///今ある場所をremoveしたらポインタが不正になって安全にnext/prevできない
24    pub fn prev(&mut self) -> Option<(&'a u64, &'a V)> {
25        self.iter.prev_const()
26    }
27
28    pub fn current(&mut self) -> Option<(&'a u64, &'a V)> {
29        self.iter.current_const()
30    }
31
32    ///nextもprevも現在のカーソルにあるアイテムを返す
33    ///空であるか、最後/最初まで移動してアイテムが無くなったらfalse
34    pub fn is_available(&self) -> bool {
35        self.iter.is_available()
36    }
37
38    pub fn is_first(&self) -> bool {
39        self.iter.is_first()
40    }
41    pub fn is_last(&self) -> bool {
42        self.iter.is_first()
43    }
44}
45impl<'a,V> Iterator for LinkedMapIter<'a, V>{
46    type Item = (&'a u64, &'a V);
47
48    fn next(&mut self) -> Option<Self::Item> {
49        self.next()
50    }
51}
52impl<'a,V> IntoIterator for &'a LinkedMap<V>{
53    type Item = (&'a u64, &'a V);
54    type IntoIter = LinkedMapIter<'a, V>;
55
56    fn into_iter(self) -> Self::IntoIter {
57        self.iter()
58    }
59}