gavl 0.1.5

A fast implementation for a map and a set using an AVL tree
Documentation
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
use std::{
    cmp::{
        Ordering,
        max
    },
    ptr::NonNull,
};

use super::{
    structs::{
        Side,
        BinarySon,
    },
    into_precomputed,
    MapNode,
    Map,
    MapLink,
};

impl<KeyType:Ord, ContentType> Map<KeyType, ContentType>{



    pub(super) fn compute_balance_additive(&mut self, mut pivot:MapLink<KeyType, ContentType>) {
        let mut side_holder = MapNode::get_side(pivot);
        while let Some(side) = side_holder {           
            let pivot_ref = unsafe {pivot.as_ref()};
            let mut pivot_father = pivot_ref.father.unwrap();
            let pivot_father_mut = unsafe {pivot_father.as_mut()};
            let pivot_new_depth = MapNode::get_max_height(pivot)+1;
            
            if pivot_father_mut.depth[side] >= pivot_new_depth {
                break;
            }
            pivot_father_mut.depth[side] = pivot_new_depth;
            
            if let Some(balance_side) = pivot_father_mut.get_balance_factor() {
                
                match MapNode::get_deepest_son_side(pivot) {
                    gs_side if gs_side == balance_side => {
                        self.variable_rotate(pivot, balance_side.complement());
                    },
                    gs_side @ _ => {
                        let pivot_mut = unsafe{pivot.as_ref()};
                        let pivot_son = pivot_mut.son[gs_side].unwrap();
                        self.variable_rotate(pivot_son, balance_side);
                        self.variable_rotate(pivot_son, gs_side);
                    },
                }
            }
            
            pivot = pivot_father;
            side_holder = MapNode::get_side(pivot);
        }
    }



    pub(super) fn compute_balance_subtractive(&mut self, mut pivot:MapLink<KeyType, ContentType>) {
        let mut pivot_mut = unsafe{pivot.as_mut()};
        let mut last_side:Option<Side> = None;
        
        loop {
            if let Some(balance_side) = pivot_mut.get_balance_factor() {
                let pivot_ref_tmp = unsafe{pivot.as_ref()};
                let mut pivot_son_tmp = pivot_ref_tmp.son[balance_side].unwrap();
                let pivot_son_ref_tmp = unsafe{pivot_son_tmp.as_mut()};
                
                match MapNode::get_deepest_son_side(pivot_son_tmp) {
                    gs_side if gs_side == balance_side => {
                        self.variable_rotate(pivot_son_tmp, balance_side.complement());
                        
                    },
                    gs_side @ _ => {
                        let pivot_granson_tmp = pivot_son_ref_tmp.son[gs_side].unwrap();
                        self.variable_rotate(pivot_granson_tmp, balance_side);
                        self.variable_rotate(pivot_granson_tmp, gs_side);
                    },
                }
                pivot = pivot_mut.father.unwrap();
                pivot_mut = unsafe{pivot.as_mut()};
            }
            
            if let Some(old_side) = last_side {
                let test = pivot_mut.son[old_side].unwrap();
                let pivot_new_depth = MapNode::get_max_height(test)+1;
                if pivot_mut.depth[old_side] <= pivot_new_depth {
                    return;
                }
                pivot_mut.depth[old_side] = pivot_new_depth;
            }
            
            let next_pivot = match pivot_mut.father {
                Some(next_pivot) => next_pivot,
                None => {break;}
            };
            
            last_side = MapNode::get_side(pivot);
            pivot = next_pivot;
            pivot_mut = unsafe{pivot.as_mut()};
        }
    }



    pub(super) fn compute_subtraccion_pivot(&mut self, target:MapLink<KeyType, ContentType>) -> MapLink<KeyType, ContentType> {
        let tmp_holder = MapNode::get_replacement(target);
        match tmp_holder {
            Some(pivot) => {
                let pivot_ref = unsafe{pivot.as_ref()};
                let pivot_side = MapNode::get_side(pivot).expect("should be granted to have father");
                let holder = match pivot_ref.son[pivot_side.complement()] {
                    Some(mut pivot_son) => {
                        let side = MapNode::get_side(pivot).unwrap();
                        let pivot_son_mut = unsafe{pivot_son.as_mut()};
                        let father = pivot_ref.father;
                        let father_mut =  unsafe{father.unwrap().as_mut()};
                        
                        pivot_son_mut.father = father;
                        father_mut.son[side] = Some(pivot_son);
                        pivot_son
                    },
                    None => {
                        let mut father = pivot_ref.father.unwrap();
                        let father_mut = unsafe{father.as_mut()};
                        father_mut.son[pivot_side] = None;
                        father_mut.depth[pivot_side] = 0;
                        father
                    },
                };
                self.replace_node(target, pivot);
                holder
            },
            None => {
                let side = MapNode::get_side(target).expect("should be granted to have father");
                let target_ref = unsafe{target.as_ref()};
                let mut father = target_ref.father.unwrap();
                let father_mut = unsafe{father.as_mut()};
                father_mut.son[side] = None;
                father_mut.depth[side] = 0;
                father
            }
        }
    }
    
    
    fn variable_rotate(&mut self, pivot:MapLink<KeyType, ContentType>, side:Side) {
        match side {
            Side::Left => {
                self.rotate_left(pivot);
            },
            Side::Right => {
                self.rotate_right(pivot);
            }
        }
    }

    fn rotate_right(&mut self, mut pivot:MapLink<KeyType, ContentType>) {
        let pivot_mut = unsafe{ pivot.as_mut() };
        let mut pivot_father = pivot_mut.father.unwrap();
        let pivot_father_mut = unsafe{ pivot_father.as_mut() };
        
        pivot_mut.father = pivot_father_mut.father;
        match pivot_father_mut.father {
            None => {
                self.head = Some(pivot);
            },
            Some(mut granfather) => {
                let father_side = MapNode::get_side(pivot_father).unwrap();
                let granfather_mut = unsafe { granfather.as_mut() };
                granfather_mut.son[father_side] = Some(pivot);
            }
        }
        
        pivot_father_mut.depth[Side::Left] = pivot_mut.depth[Side::Right];
        pivot_father_mut.son[Side::Left] = pivot_mut.son[Side::Right];
        if let Some(mut pivot_son) = pivot_mut.son[Side::Right] {
            let pivot_son_mut = unsafe{ pivot_son.as_mut() };
            pivot_son_mut.father = Some(pivot_father);
        }
        
        pivot_father_mut.father = Some(pivot);
        pivot_mut.son[Side::Right] = Some(pivot_father);
        pivot_mut.depth[Side::Right] = MapNode::get_max_height(pivot_father)+1;
    }
    

    
    fn rotate_left(&mut self, mut pivot:MapLink<KeyType, ContentType>) {
        let pivot_mut = unsafe{ pivot.as_mut() };
        let mut pivot_father = pivot_mut.father.unwrap();
        let pivot_father_mut = unsafe{ pivot_father.as_mut() };
        
        pivot_mut.father = pivot_father_mut.father;
        match pivot_father_mut.father {
            None => {
                self.head = Some(pivot);
            },
            Some(mut granfather) => {
                let father_side = MapNode::get_side(pivot_father).unwrap();
                let granfather_mut = unsafe { granfather.as_mut() };
                granfather_mut.son[father_side] = Some(pivot);
            }
        }
        
        pivot_father_mut.depth[Side::Right] = pivot_mut.depth[Side::Left];
        pivot_father_mut.son[Side::Right] = pivot_mut.son[Side::Left];
        if let Some(mut pivot_son) = pivot_mut.son[Side::Left] {
            let pivot_son_mut = unsafe{ pivot_son.as_mut() };
            pivot_son_mut.father = Some(pivot_father);
        }
        
        pivot_father_mut.father = Some(pivot);
        pivot_mut.son[Side::Left] = Some(pivot_father);
        pivot_mut.depth[Side::Left] = MapNode::get_max_height(pivot_father)+1;
    }
    


    //makes source take the place of dest in the tree
    pub(crate) fn replace_node(&mut self, dest:MapLink<KeyType, ContentType>, mut src:MapLink<KeyType, ContentType>) {
        let dest_ref = unsafe{dest.as_ref()};
        let src_mut = unsafe{src.as_mut()};
        src_mut.son = dest_ref.son;
        src_mut.depth = dest_ref.depth;
        src_mut.father = dest_ref.father;
        
        for side in [Side::Left, Side::Right] {
            if let Some(mut son) = src_mut.son[side] {
                let son_mut = unsafe{son.as_mut()};
                son_mut.father = Some(src);
            }
        }
        
        match MapNode::get_side(dest) {
            None => {
                self.head = Some(src);
            },
            Some(side) => {
                let father_mut = unsafe{src_mut.father.unwrap().as_mut()};
                father_mut.son[side] = Some(src);
            }
        }
        
    }

    

}


impl<KeyType:Ord, ContentType> MapNode<KeyType, ContentType>{
    
    

    #[inline(always)]
    pub(super) fn new_map_link(key:KeyType, content:ContentType) -> MapLink<KeyType, ContentType> {
        NonNull::new(Box::into_raw(Box::new(MapNode{
            key,
            content,
            father:None,
            depth: BinarySon::default(),
            son: BinarySon::default(),
            metadata:into_precomputed::FeatureField::default(),
        }))).expect("system ran out of memory")
        
    }
    
    pub(super) fn find_node(key:&KeyType, mut pivot:MapLink<KeyType, ContentType>) -> Option<MapLink<KeyType, ContentType>> { 
        loop {
            let pivot_ref = unsafe{pivot.as_ref()};
            let side;
            match key.cmp(&pivot_ref.key) {
                order @ (Ordering::Less|Ordering::Greater) => {
                    side = Side::try_from(order).unwrap();
                    match pivot_ref.son[side] {
                        None => {
                            return None;
                        },
                        Some(next_pivot) => {
                            pivot = next_pivot;
                        },
                    }
                },
                Ordering::Equal => {
                    break;
                },
            }
        }
        Some(pivot)
    }
    
    

    #[inline(always)]
    pub(super) fn unpack_node(node:MapLink<KeyType, ContentType>) -> ContentType {
        //from NonNull to Box and returns content
        //make it easyer to count the amount of dropped nodes
        let holder = unsafe{Box::from_raw(node.as_ptr())};
        holder.content
    }
    
    
    
    #[inline(always)]
    pub(super) fn free_node(node:MapLink<KeyType, ContentType>) {
        //from NonNull to Box
        //make it easyer to count the amount of dropped nodes
        let _ = unsafe{Box::from_raw(node.as_ptr())};
    }
    
    
    
    pub(super) fn insert_node(mut pivot:MapLink<KeyType, ContentType>, mut node:MapLink<KeyType, ContentType>) -> Result<(), MapLink<KeyType, ContentType>>{
        loop{
            let key_order = unsafe{ node.as_ref().key.cmp(&pivot.as_ref().key) };
            
            let side = match Side::try_from(key_order) {
                Ok(side) => side,
                Err(_) => {
                    return Err(pivot);
                }
            };
            
            let pivot_mut = unsafe { pivot.as_mut() };
            match pivot_mut.son[side] {
                None => {
                    let node_mut = unsafe{ node.as_mut() };
                    node_mut.father = Some(pivot);
                    pivot_mut.son[side] = Some(node);
                    break;
                },
                Some(next_pivot_pointer) => {
                    pivot = next_pivot_pointer;
                }
            }
        }
        Ok(())
    }
    
    

    fn get_replacement(node:MapLink<KeyType, ContentType>) -> Option<MapLink<KeyType, ContentType>> {
        let node_mut = unsafe{node.as_ref()};
        for son in [Side::Left, Side::Right] {
            let son_complement = son.complement();
            if let Some(mut pivot) = node_mut.son[son] {
                loop {
                    let pivot_ref = unsafe{pivot.as_ref()};
                    match pivot_ref.son[son_complement] {
                        None => {break;},
                        Some(next_pivot) => {
                            pivot = next_pivot;
                        }
                    }
                }
                return Some(pivot);
            }
        }
        None
    }
    


    fn get_max_height(node:MapLink<KeyType, ContentType>)-> i32 {
        let node_ref = unsafe {node.as_ref()};
        max(node_ref.depth[Side::Left], node_ref.depth[Side::Right])
    }
    
    

    pub(super) fn get_side(node:MapLink<KeyType, ContentType>) -> Option<Side> {
        //one liner to get the reference of father or return None if node has no father
        let node_father_ref = unsafe{node.as_ref().father?.as_ref()};
        for side in [Side::Left, Side::Right] {
            if let Some(test) = node_father_ref.son[side] {
                if test == node {
                    return Some(side);
                }
            }
        }
        None
    }

    

    fn get_deepest_son_side(node:MapLink<KeyType, ContentType>) -> Side {
        let node_ref = unsafe{ node.as_ref() };
        let side_comparison = node_ref.depth[Side::Right].cmp(&node_ref.depth[Side::Left]);
        //should only arrive here when |balance_factor| >= 2 
        Side::try_from(side_comparison).expect("one should be bigger")
    }
    
    

    fn get_balance_factor(&self) -> Option<Side> {
        let balance_factor = self.depth[Side::Right] - self.depth[Side::Left];
        match balance_factor {
            -2 => Some(Side::Left),
            2 => Some(Side::Right),
            _ => None,
        }
    }
    


}