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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::{
	collections::{vec_deque, VecDeque},
	fmt::Debug,
	hash::{Hash, Hasher},
	iter::Copied,
	ops::Index,
};

use crate::{
	remove_children_deque,
	sealed::{Idx, Sealed},
	unified::UnifiedNodeRepresentation,
	Arena,
	BaseNode,
	BranchNode,
	BranchNodeDeque,
	Node,
	Token,
};

/// A [node] that is _not_ split into separate [branch] and leaf [nodes].
///
/// This is the [deque] version, where [children] are represented as a [`VecDeque`]. In this
/// version, a node's [previous sibling][prev][(s)][preceding] and
/// [next sibling][next][(s)][following] are not available, but the [node] can be
/// [directly indexed], and children can be [detached], [removed], or [inserted] by index.
///
/// [`Data`] represents the [custom data] associated with the node.
///
/// # See also
/// For the non-[deque] version, see [`UnifiedNode`].
///
/// For a [node] that _is_ split into separate [branch] and leaf [nodes], see [`SplitNode`] and
/// [`SplitNodeDeque`].
///
/// [node]: Node
/// [nodes]: Node
/// [branch]: BranchNode
/// [custom data]: UnifiedNodeDeque::Data
///
/// [deque]: BranchNodeDeque
/// [children]: UnifiedNodeDeque::children
/// [directly indexed]: UnifiedNodeDeque::index
/// [detached]: UnifiedNodeDeque::detach
/// [removed]: UnifiedNodeDeque::remove
/// [inserted]: UnifiedNodeDeque::insert
///
/// [prev]: crate::LinkedNode::prev
/// [preceding]: crate::LinkedNode::preceding_siblings
/// [next]: crate::LinkedNode::next
/// [following]: crate::LinkedNode::following_siblings
#[derive(Debug)]
pub struct UnifiedNodeDeque<Data: Debug> {
	token: Token<Self>,

	parent: Option<Token<Self>>,
	children: VecDeque<Token<Self>>,

	data: Data,
}

impl<Data: Debug> PartialEq for UnifiedNodeDeque<Data> {
	#[inline(always)]
	fn eq(&self, other: &Self) -> bool {
		self.token == other.token
	}
}

impl<Data: Debug> Eq for UnifiedNodeDeque<Data> {}

impl<Data: Debug> Hash for UnifiedNodeDeque<Data> {
	#[inline(always)]
	fn hash<H: Hasher>(&self, state: &mut H) {
		self.token.hash(state);
	}
}

impl<Data: Debug> Sealed for UnifiedNodeDeque<Data> {}

impl<Data: Debug> Node for UnifiedNodeDeque<Data> {
	type Base = Self;
	type Token = Token<Self>;

	type Data = Data;
	type DataRef<'data> = &'data Data
	where
		Self: 'data;
	type DataRefMut<'data> = &'data mut Data
	where
		Self: 'data;

	fn new(arena: &mut Arena<Self::Base>, data: Self::Data) -> Self::Token
	where
		Self: Sized,
	{
		Token::new(arena.0.insert_with(|idx| Self {
			token: Token::new(idx),

			parent: None,
			children: VecDeque::new(),

			data,
		}))
	}

	#[inline(always)]
	fn token(&self) -> Self::Token {
		self.token
	}

	#[inline(always)]
	fn parent(&self) -> Option<Token<Self>> {
		self.parent
	}

	#[inline(always)]
	fn data(&self) -> &Self::Data {
		&self.data
	}

	#[inline(always)]
	fn data_mut(&mut self) -> &mut Self::Data {
		&mut self.data
	}
}

impl<Data: Debug> BaseNode for UnifiedNodeDeque<Data> {
	type Representation = UnifiedNodeRepresentation<Data>;

	type Branch = Self;
	type Leaf = Self;

	fn into_representation(self, arena: &mut Arena<Self>) -> Self::Representation {
		UnifiedNodeRepresentation {
			children: remove_children_deque(&self, arena),
			data: self.data,
		}
	}
}

impl<Data: Debug> BranchNode for UnifiedNodeDeque<Data> {
	type ChildrenIter<'branch> = Copied<vec_deque::Iter<'branch, Token<Self>>>
	where
		Self: 'branch;

	#[inline]
	fn first(&self) -> Option<Token<Self>> {
		match self.children.len() {
			0 => None,
			_ => Some(self.children[0]),
		}
	}

	#[inline]
	fn last(&self) -> Option<Token<Self>> {
		match self.children.len() {
			0 => None,
			len => Some(self.children[len - 1]),
		}
	}

	#[inline(always)]
	fn children<'branch>(&'branch self, _arena: &'branch Arena<Self::Base>) -> Self::ChildrenIter<'branch> {
		self.children.iter().copied()
	}

	#[inline(always)]
	fn is_empty(&self) -> bool {
		self.children.is_empty()
	}

	fn detach_front(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<Token<Self>> {
		let child = arena.0[token.idx()].children.pop_front();

		if let Some(child) = &child {
			let node = &mut arena.0[child.idx()];

			// Detach the node's parent.
			node.parent = None;
		}

		child
	}

	fn detach_back(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<Token<Self>> {
		let child = arena.0[token.idx()].children.pop_back();

		if let Some(child) = &child {
			let node = &mut arena.0[child.idx()];

			// Detach the node's parent.
			node.parent = None;
		}

		child
	}

	fn pop_front(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<UnifiedNodeRepresentation<Data>> {
		let child = arena.0[token.idx()].children.pop_front();

		child.map(|child| {
			arena
				.0
				.remove(child.idx())
				.expect("tried to remove child but there was no such node in the `arena`")
				.into_representation(arena)
		})
	}

	fn pop_back(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<UnifiedNodeRepresentation<Data>> {
		let child = arena.0[token.idx()].children.pop_back();

		child.map(|child| {
			arena
				.0
				.remove(child.idx())
				.expect("tried to remove child but there was no such node in the `arena`")
				.into_representation(arena)
		})
	}

	fn push_front(token: Self::Token, arena: &mut Arena<Self::Base>, new: Token<Self>) {
		// We're not pushing our own root...
		assert_ne!(
			arena.0[token.idx()].root(arena),
			new,
			"tried to push this branch's own root as a child"
		);
		// And we're not pushing a child that already has a parent...
		assert!(
			arena.0[new.idx()].parent.is_none(),
			"tried to push a child that already has a parent"
		);

		// Set the child's parent.
		arena.0[new.idx()].parent = Some(token);

		// Push the child's token.
		arena.0[token.idx()].children.push_front(new);
	}

	fn push_back(token: Self::Token, arena: &mut Arena<Self::Base>, new: Token<Self>) {
		// We're not pushing our own root...
		assert_ne!(
			arena.0[token.idx()].root(arena),
			new,
			"tried to push this branch's own root as a child"
		);
		// And we're not pushing a child that already has a parent...
		assert!(
			arena.0[new.idx()].parent.is_none(),
			"tried to push a child that already has a parent"
		);

		// Set the child's parent.
		arena.0[new.idx()].parent = Some(token);

		// Push the child's token.
		arena.0[token.idx()].children.push_back(new);
	}
}

impl<Data: Debug> Index<usize> for UnifiedNodeDeque<Data> {
	type Output = Token<Self>;

	fn index(&self, index: usize) -> &Self::Output {
		&self.children[index]
	}
}

impl<Data: Debug> BranchNodeDeque for UnifiedNodeDeque<Data> {
	#[inline(always)]
	fn len(&self) -> usize {
		self.children.len()
	}

	fn detach(token: Self::Token, arena: &mut Arena<Self>, index: usize) -> Token<Self> {
		let children = &mut arena.0[token.idx()].children;

		let child = children.remove(index).expect(&format!(
			"the given `index` ({index}) was out of bounds; there were only {} children",
			children.len()
		));

		// Detach the child's parent.
		arena.0[child.idx()].parent = None;

		child
	}

	fn remove(token: Self::Token, arena: &mut Arena<Self>, index: usize) -> UnifiedNodeRepresentation<Data> {
		let children = &mut arena.0[token.idx()].children;

		let child = children.remove(index).expect(&format!(
			"the given `index` ({index}) was out of bounds; there were only {} children",
			children.len()
		));

		arena
			.0
			.remove(child.idx())
			.expect("tried to remove child but there was no such node in `arena`")
			.into_representation(arena)
	}

	fn insert(token: Self::Token, arena: &mut Arena<Self::Base>, index: usize, new: Token<Self>) {
		// We're not inserting our own root...
		assert_ne!(
			arena.0[token.idx()].root(arena),
			new,
			"tried to insert this branch's own root as a child"
		);
		// And we're not inserting a child that already has a parent...
		assert!(
			arena.0[new.idx()].parent.is_none(),
			"tried to insert a child that already has a parent"
		);

		// Set the child's parent.
		arena.0[new.idx()].parent = Some(token);

		// Insert the child's token.
		arena.0[token.idx()].children.insert(index, new);
	}
}