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
use crate::imp::structs::linked_m::{LinkedMap};
use crate::imp::structs::rust_list::MutItem;
use std::marker::PhantomData;
use crate::imp::structs::list_def_obj::ListDefObj;
use crate::imp::intf::mitem::MItemPtr;
use crate::imp::structs::root_def_obj::RootDefObj;
use crate::imp::structs::linked_map_unsafe_iter::LinkedMapUnsafeIter;

/// This uses pointers so every method is basically unsafe.
/// You can get this ptr, and create an immutable reference,
/// and modify the referent through this pointer,
/// and access the immutable reference afterwards.
/// Anything can happen with the access.
///
/// Getting data through this pointer while a mutable reference is alive
/// is also an undefined behavior.
///
/// Pointers can outlive their referents, and access dropped items. It's also UB.
///
/// Because references and pointers are not exposed,
/// creating contradict references is basically impossible.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MListPtr<V : From<MItemPtr>>{
    map : *const LinkedMap<MutItem>,
    list_def : *const ListDefObj,
    root_def : *const RootDefObj,
    phantom : PhantomData<*const V>,
}

impl<V : From<MItemPtr>> MListPtr<V>{
    /// &LinkedMap<MutItem>から得たポインタを通して書き換えるとUBなので、
    /// 書き換える場合&mut LinkedMapからポインタを取得し、*constに入れる
    /// Const版とMut版に分けて安全に書くのも出来なくはない気がするが大変すぎるのでこのようになっている
    pub fn new(map : *const LinkedMap<MutItem>, list_def : *const ListDefObj, root_def : *const RootDefObj)
        -> MListPtr<V>{ MListPtr { map, list_def, root_def, phantom : PhantomData } }

    fn from(&self, item : *const MutItem) -> V{
        V::from(MItemPtr::new(item, self.list_def, self.root_def))
    }

    fn map(&self) -> &LinkedMap<MutItem>{ unsafe{ &*self.map }}
    fn map_mut<'a>(&mut self) -> &'a mut LinkedMap<MutItem>{ unsafe{ &mut *(self.map as *mut LinkedMap<MutItem>) }}
    //fn def_ref<'a>(&mut self) -> &'a ListDefObj{ unsafe{ &*self.list_def } }

    pub fn first_mut(&mut self) -> Option<V> {
        let map = self.map_mut();
        map.first_mut().map(|r| self.from(r))
    }

    pub fn first_const(&self) -> Option<V> {
        let map = self.map();
        map.first().map(|r| self.from(r))
    }
    pub fn first_id(&self) -> Option<u64> {
        self.map().first_id()
    }
    pub fn last_mut(&mut self) -> Option<V> {
        self.map_mut().last_mut().map(|r| self.from(r))
    }

    pub fn last_const(&self) -> Option<V> {
        self.map().last().map(|r| self.from(r))
    }

    pub fn last_id(&self) -> Option<u64> {
        self.map().last_id()
    }
    pub fn get_item_mut(&mut self, id : u64) -> Option<V>{
        self.map_mut().get_item_mut(id).map(|b| self.from(b))
    }

    pub fn get_item_const(&self, id : u64) -> Option<V>{
        self.map().get_item(id).map(|b| self.from(b))
    }

    pub fn next_id(&self) -> u64{
        self.map().next_id()
    }

    pub fn contains_key(&self, key : u64) -> bool{
        self.map().contains_key(key)
    }
    pub fn len(&self) -> usize{
        self.map().len()
    }
    pub fn is_empty(&self) -> bool {
        self.map().is_empty()
    }

    pub fn insert(&mut self) -> V{
        self.insert_last()
    }

    pub fn insert_last(&mut self) -> V{
        let map = self.map_mut();
        let id = map.insert_last(MutItem::default());
        self.get_item_mut(id).unwrap()
    }
    pub fn insert_first(&mut self) -> V{
        let map = self.map_mut();
        let id = map.insert_first(MutItem::default());
        self.get_item_mut(id).unwrap()
    }

    /// Anything can happen when a removed item is accessed, so be careful
    pub unsafe fn remove(&mut self, id : u64) -> bool {
        self.map_mut().remove(id)
    }
    /// Anything can happen when a removed item is accessed, so be careful
    pub unsafe fn remove_first(&mut self) -> bool{
        self.map_mut().remove_first()
    }
    /// Anything can happen when a removed item is accessed, so be careful
    pub unsafe fn remove_last(&mut self) -> bool{
        self.map_mut().remove_last()
    }

    pub fn move_to_first(&mut self, id : u64) -> bool {
        self.map_mut().move_to_first(id)
    }

    pub fn move_to_last(&mut self, id : u64) -> bool {
        self.map_mut().move_to_last(id)
    }

    pub fn move_to_prev(&mut self, next_items_id : u64, id : u64) -> bool{
        self.map_mut().move_to_prev(next_items_id, id)
    }

    pub fn move_to_next(&mut self, prev_items_id : u64, id : u64) -> bool{
        self.map_mut().move_to_next(prev_items_id, id)
    }

    pub fn iter_const(&self) -> MListPtrIter<V> {
        let iter  = unsafe{ self.map().iter_unsafe_const() };
        MListPtrIter::new(iter, self.list_def, self.root_def)
    }
    pub fn iter_mut(&mut self) -> MListPtrIter<V> {
        let iter = unsafe{ self.map_mut().iter_unsafe_mut() };
        MListPtrIter::new(iter, self.list_def, self.root_def)
    }

    pub fn iter_from_last_const(&self) -> MListPtrIter<V> {
        let iter = unsafe{ self.map().iter_from_last_unsafe_const() };
        MListPtrIter::new(iter, self.list_def, self.root_def)
    }
    pub fn iter_from_last_mut(&mut self) -> MListPtrIter<V>{
        let iter = unsafe{ self.map_mut().iter_from_last_unsafe_mut() };
        MListPtrIter::new(iter, self.list_def, self.root_def)
    }

    pub fn iter_from_id_const(&self, id : u64) -> Option<MListPtrIter<V>> {
        let iter = unsafe{ self.map().iter_from_id_unsafe_const(id) };
        iter.map(|iter| MListPtrIter::new(iter, self.list_def, self.root_def))
    }
    pub fn iter_from_id_mut(&mut self, id : u64)-> Option<MListPtrIter<V>> {
        let iter = unsafe{ self.map_mut().iter_from_id_unsafe_mut(id) };
        iter.map(|iter| MListPtrIter::new(iter, self.list_def, self.root_def))
    }
}

#[derive(Debug)]
pub struct MListPtrIter<V : From<MItemPtr>>{
    iter : LinkedMapUnsafeIter<MutItem>,
    list_def : *const ListDefObj,
    root_def : *const RootDefObj,
    phantom : PhantomData<*mut V>,
}
impl<V : From<MItemPtr>> Iterator for MListPtrIter<V>{
    type Item = (u64, V);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next_mut().map(|(k,v)| (*k, V::from(MItemPtr::new(v, self.list_def, self.root_def))))
    }
}
impl<V : From<MItemPtr>> MListPtrIter<V>{
    pub(crate) fn new(iter : LinkedMapUnsafeIter<MutItem>, list_def : *const ListDefObj, root_def : *const RootDefObj) -> MListPtrIter<V>{
        MListPtrIter { iter, list_def, root_def, phantom : PhantomData }
    }

    fn from(&self, item : *const MutItem) -> V{
        V::from(MItemPtr::new(item, self.list_def, self.root_def))
    }
    ///現在のカーソルにあるアイテムを返し、カーソルを進める
    pub fn next_const(&mut self) -> Option<(u64, V)> {
        self.iter.next_const().map(|(k,v)| (*k, self.from(v)))
    }

    ///現在のカーソルにあるアイテムを返し、カーソルを進める。Mutate可能なMItemPtrを返すをラップして返す。
    /// ConstなMListPtrから得たMListPtrIterから呼び出して書き換えたらUB
    pub fn next_mut(&mut self) -> Option<(u64, V)> {
        self.iter.next_mut().map(|(k,v)| (*k, self.from(v)))
    }

    ///前に戻ることが出来る。そして元あった場所を削除し、それによって削除されたアイテムの次にあったアイテムが現在のカーソルの次にくるので、
    /// next2回でそれをとることも出来る。
    ///今ある場所をremoveしたらポインタが不正になって安全にnext/prevできない
    pub fn prev_const(&mut self) -> Option<(u64, V)> {
        self.iter.prev_const().map(|(k,v)| (*k, self.from(v)))
    }

    pub fn prev_mut(&mut self) -> Option<(u64, V)> {
        self.iter.prev_mut().map(|(k,v)| (*k, self.from(v)))
    }
    
    pub fn current_const(&mut self) -> Option<(u64, V)> {
        self.iter.current_const().map(|(k,v)| (*k,self.from(v)))
    }

    pub fn current_mut(&mut self) -> Option<(u64, V)> {
        self.iter.current_mut().map(|(k,v)| (*k,self.from(v)))
    }



    pub fn is_available(&self) -> bool {
        self.iter.is_available()
    }

    pub fn is_first(&self) -> bool {
        self.iter.is_first()
    }

    pub fn is_last(&self) -> bool {
        self.iter.is_last()
    }
}