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
use std::{ collections::BinaryHeap, cmp::Ordering, ptr::NonNull };
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Id(pub u32);
impl PartialOrd for Id {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
other.0.partial_cmp(&self.0)
}
}
impl Ord for Id {
fn cmp(&self, other: &Id) -> Ordering {
match self.0.partial_cmp(&other.0).unwrap() {
Ordering::Greater => Ordering::Less,
Ordering::Less => Ordering::Greater,
Ordering::Equal => Ordering::Equal,
}
}
}
struct HeapParent<T, U> {
v: Vec<*mut HeapChild<T,U>>,
o: BinaryHeap<Id>,
d: T,
}
pub struct Parent<T, U: PseudoDrop<T = T>> {
heap: *mut HeapParent<T, U>
}
struct HeapChild<T, U>(
Option<(NonNull<HeapParent<T, U>>, U)>,
);
pub struct Child<T, U: PseudoDrop<T = T>> {
heap: *mut HeapChild<T, U>,
id: Id,
}
impl<T, U> Parent<T, U> where U: PseudoDrop<T = T> {
pub fn new(data: T) -> Self {
let heap_parent: Box<HeapParent<T, U>> = Box::new(HeapParent {
v: vec![],
o: BinaryHeap::new(),
d: data,
});
Parent {
heap: unsafe { ::std::mem::transmute(heap_parent) },
}
}
pub fn data<'a>(&'a self) -> &'a mut T {
unsafe { &mut (*self.heap).d }
}
}
impl<T, U> Drop for Parent<T, U> where U: PseudoDrop<T = T> {
fn drop(&mut self) {
for i in unsafe { (*self.heap).v.iter() } {
if unsafe { (**i).0.is_some() } {
unsafe {
(**i).0.as_mut().unwrap().1
.pdrop(&mut self.data());
}
unsafe { (**i).0 = None };
}
}
let _ = unsafe {
::std::mem::transmute::<_, Box<HeapParent<T, U>>>(self.heap)
};
}
}
impl<T, U> Child<T, U> where U: PseudoDrop<T = T> {
pub fn new(parent: &Parent<T, U>, data: U) -> Self {
let heap_child = Box::new(HeapChild(Some(
(NonNull::new(parent.heap).unwrap(), data)
)));
let heap_pointer = unsafe { ::std::mem::transmute(heap_child) };
let id = unsafe {
if let Some(id) = (*parent.heap).o.pop() {
(*parent.heap).v[id.0 as usize] = heap_pointer;
id
} else {
let a = (*parent.heap).v.len() as u32;
(*parent.heap).v.push(heap_pointer);
Id(a)
}
};
Child {
heap: heap_pointer,
id,
}
}
fn heap_parent<'a>(&'a self) -> &'a HeapParent<T, U> {
unsafe { (*self.heap).0.as_mut().unwrap().0.as_mut() }
}
fn heap_parent_mut<'a>(&'a mut self) -> &'a mut HeapParent<T, U> {
unsafe { (*self.heap).0.as_mut().unwrap().0.as_mut() }
}
pub fn parent<'a>(&'a self) -> &'a T {
&self.heap_parent().d
}
pub fn parent_mut<'a>(&'a mut self) -> &'a mut T {
&mut self.heap_parent_mut().d
}
pub fn data<'a>(&'a self) -> &'a U {
unsafe { &(*self.heap).0.as_mut().unwrap().1 }
}
pub fn data_mut<'a>(&'a mut self) -> &'a mut U {
unsafe { &mut (*self.heap).0.as_mut().unwrap().1 }
}
pub fn id(&self) -> Id {
self.id
}
}
impl<T, U> Drop for Child<T, U> where U: PseudoDrop<T = T> {
fn drop(&mut self) {
if unsafe { &(*self.heap).0 }.is_some() {
let id = self.id;
self.heap_parent_mut().o.push(id);
unsafe {
(*self.heap).0.as_mut().unwrap().1
.pdrop(&mut self.heap_parent_mut().d);
}
unsafe { (*self.heap).0 = None };
}
}
}
pub trait PseudoDrop {
type T;
fn pdrop(&mut self, parent: &mut Self::T);
}