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
//! # hedel-rs
//! [](LICENSE-GPL) [](https://crates.io/crates/hedel-rs) [](https://docs.rs/hedel-rs)
//!
//! **A Hierarchical Doubly Linked List**
//!
//! Hedel-rs provides all you need to create your own abstraction over a
//! hierarchical doubly linked list in Rust, suitable choice for a DOM tree.
//! Designed for when you need a nested generation of nodes. ( e.g with macros ```node!(1, node!(2))``` )
//! Based on `Rc`, `Weak`, and a safe wrapper around `UnsafeCell` (`HedelCell`).
//!
//! If you are new to linked lists, consider reading [Learn Rust With Entirely Too Many Linked Lists](https://rust-unofficial.github.io/too-many-lists/)
//!
//! # Ideology
//!
//! Hedel isn't exactly a tree structure.
//!
//! - `List` is a wrap around its first node. There isn't any root. This allows for
//! sibling nodes at the root-level.
//! - `Node` is a pointer to its content and other pointers to allow navigation. Those pointers are:
//! `parent`, `child`, `prev` and `next`, where child is a pointer to its first child.
//! - Support for node generation using macros: you can use node!(1) and nest how many nodes you want.
//!
//! # Features
//!
//! - `HedelCell`: a cell structure safely relying on UnsafeCell, similar to `RefCell` but smaller in size.
//! - `Node`/`WeakNode`: to avoid memory-leaking we also provide a weak version of `Node`.
//! - Macros: generate nodes blazingly fast with node!() and list!()
//!
//! ```rust
//! use hedel_rs::prelude::*;
//! use hedel_rs::*;
//!
//! fn main() {
//! let node = node!(45);
//!
//! let my_node = node!("Parent",
//! node!("Child"),
//! node!("Child")
//! );
//!
//! let my_list = list!(
//! node!(2),
//! node!(3)
//! );
//! }
//! ```
//!
//! - Identify and compare: create your own identifier implementing the `CompareNode` trait.
//!
//! ```rust
//! use hedel_rs::prelude::*;
//! use hedel_rs::*;
//!
//! pub enum NumIdent {
//! Equal(i32),
//! BiggerThan(i32),
//! SmallerThan(i32)
//! }
//!
//! impl CompareNode<i32> for NumIdent {
//! fn compare(&self, node: &Node<i32>) -> bool {
//! match &self {
//! NumIdent::Equal(n) => {
//! as_content!(node, |content| {
//! return content == *n;
//! });
//! },
//! NumIdent::BiggerThan(n) => {
//! as_content!(node, |content| {
//! return content > *n;
//! });
//! },
//! NumIdent::SmallerThan(n) => {
//! as_content!(node, |content| {
//! return content < *n;
//! });
//! }
//! }
//! }
//! }
//!
//! fn main() {
//! let node = node!(3);
//! assert!(NumIdent::BiggerThan(2).compare(&node));
//! }
//! ```
//!
//! - Collect: iterate over the linked list and collect
//! only the nodes matching the identifier.
//! ```rust
//! use hedel_rs::prelude::*;
//! use hedel_rs::*;
//!
//! pub enum NumIdent {
//! Equal(i32),
//! BiggerThan(i32),
//! SmallerThan(i32)
//! }
//!
//! impl CompareNode<i32> for NumIdent {
//! fn compare(&self, node: &Node<i32>) -> bool {
//! match &self {
//! NumIdent::Equal(n) => {
//! as_content!(node, |content| {
//! return content == *n;
//! });
//! },
//! NumIdent::BiggerThan(n) => {
//! as_content!(node, |content| {
//! return content > *n;
//! });
//! },
//! NumIdent::SmallerThan(n) => {
//! as_content!(node, |content| {
//! return content < *n;
//! });
//! }
//! }
//! }
//! }
//!
//! fn main() {
//! let node = node!(1,
//! node!(2),
//! node!(3),
//! node!(4),
//! node!(5)
//! );
//!
//! let collection = node.collect_children(&NumIdent::BiggerThan(3));
//!
//! for node in collection.into_iter() {
//! println!("{}", node.to_content());
//! }
//! }
//! ```
//!
//! - Detach: detach the nodes matching an identifier in the linked list.
//! ```rust
//! use hedel_rs::prelude::*;
//! use hedel_rs::*;
//!
//! pub enum NumIdent {
//! Equal(i32),
//! BiggerThan(i32),
//! SmallerThan(i32)
//! }
//!
//! impl CompareNode<i32> for NumIdent {
//! fn compare(&self, node: &Node<i32>) -> bool {
//! match &self {
//! NumIdent::Equal(n) => {
//! as_content!(node, |content| {
//! return content == *n;
//! });
//! },
//! NumIdent::BiggerThan(n) => {
//! as_content!(node, |content| {
//! return content > *n;
//! });
//! },
//! NumIdent::SmallerThan(n) => {
//! as_content!(node, |content| {
//! return content < *n;
//! });
//! }
//! }
//! }
//! }
//!
//! fn main() {
//! let node = node!(1,
//! node!(2),
//! node!(3),
//! node!(4),
//! node!(5)
//! );
//!
//! let three = node.find_child(&NumIdent::Equal(3)).unwrap();
//! three.detach();
//!
//! assert!(node.find_child(&NumIdent::Equal(3)).is_none());
//! }
//! ```
//!
//! - Insert or Append: insert a node at any position in a linked list.
//! ```rust
//! use hedel_rs::prelude::*;
//! use hedel_rs::*;
//!
//! fn main() {
//! let node = node!(1,
//! node!(3),
//! node!(4),
//! node!(5)
//! );
//!
//! node.insert_child(0, node!(2));
//!
//! assert_eq!(node.child().unwrap().to_content(), 2);
//!
//! node.append_child(node!(6));
//!
//! assert_eq!(node.get_last_child().unwrap().to_content(), 6);
//! }
//!
//! ```
pub use ;
pub use ;