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
mod allocator;
mod head;
mod iter;
mod node;
mod update;

use std::{cmp::Ordering, marker::PhantomData, num::NonZeroU32};

use allocator::DefaultAvltrieeAllocator;

pub use allocator::AvltrieeAllocator;
pub use iter::AvltrieeIter;
pub use node::AvltrieeNode;
pub use update::AvltrieeHolder;

#[derive(Debug)]
pub struct Found {
    row: Option<NonZeroU32>,
    ord: Ordering,
}
impl Found {
    pub fn row(&self) -> Option<NonZeroU32> {
        self.row
    }

    pub fn ord(&self) -> Ordering {
        self.ord
    }
}

pub struct Avltriee<T, A = DefaultAvltrieeAllocator<T>> {
    allocator: A,
    _marker: PhantomData<fn() -> T>,
}

impl<T: Default> Avltriee<T, DefaultAvltrieeAllocator<T>> {
    /// Creates the Avltriee with Default allocator.
    pub fn new() -> Self {
        Self {
            allocator: DefaultAvltrieeAllocator::new(),
            _marker: PhantomData,
        }
    }
}

impl<T, A: AvltrieeAllocator<T>> Avltriee<T, A> {
    /// Creates the Avltriee with [AvltrieeAllocator].
    pub fn with_allocator(allocator: A) -> Self {
        Self {
            allocator,
            _marker: PhantomData,
        }
    }

    /// Returns the node of the specified row.
    pub fn get(&self, row: NonZeroU32) -> Option<&AvltrieeNode<T>> {
        self.allocator
            .get(row)
            .and_then(|node| (node.height != 0).then_some(unsafe { self.get_unchecked(row) }))
    }

    pub unsafe fn get_unchecked(&self, row: NonZeroU32) -> &AvltrieeNode<T> {
        &*self.allocator.as_ptr().offset(row.get() as isize)
    }

    unsafe fn get_unchecked_mut(&mut self, row: NonZeroU32) -> &mut AvltrieeNode<T> {
        &mut *self.allocator.as_mut_ptr().offset(row.get() as isize)
    }

    /// Finds the edge of a node from the specified value.
    pub fn search<F: Fn(&T) -> Ordering>(&self, cmp: F) -> Found {
        let mut row = self.root();
        let mut ord = Ordering::Equal;
        while let Some(row_inner) = row {
            let node = unsafe { self.get_unchecked(row_inner) };
            ord = cmp(node);
            match ord {
                Ordering::Greater => {
                    if let Some(left) = node.left {
                        row = Some(left);
                    } else {
                        break;
                    }
                }
                Ordering::Equal => {
                    break;
                }
                Ordering::Less => {
                    if let Some(right) = node.right {
                        row = Some(right);
                    } else {
                        break;
                    }
                }
            }
        }
        Found { row, ord }
    }

    /// Checks whether the specified row is a node with a unique value.
    pub fn is_unique(&self, row: NonZeroU32) -> Option<(bool, &AvltrieeNode<T>)> {
        self.get(row).map(|node| {
            (
                node.same.is_none()
                    && node.parent.is_some_and(|parent| {
                        unsafe { self.get_unchecked(parent) }.same != Some(row)
                    }),
                node,
            )
        })
    }

    fn allocate(&mut self, rows: NonZeroU32)
    where
        T: Clone + Default,
    {
        self.allocator.resize(rows.get());
        self.set_rows_count(rows.get());
    }

    fn min(&self, t: Option<NonZeroU32>) -> Option<NonZeroU32> {
        let mut t = t;
        while let Some(t_inner) = t {
            let l = unsafe { self.get_unchecked(t_inner) }.left;
            if let Some(l) = l {
                t = Some(l);
            } else {
                break;
            }
        }
        t
    }

    fn max(&self, t: Option<NonZeroU32>) -> Option<NonZeroU32> {
        let mut t = t;
        while let Some(t_inner) = t {
            let r = unsafe { self.get_unchecked(t_inner) }.right;
            if let Some(r) = r {
                t = Some(r);
            } else {
                break;
            }
        }
        t
    }
}