generational_arena_tree/split/
deque.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::{collections::vec_deque, fmt::Debug, iter::Copied, ops::Index};
6
7use super::*;
8use crate::{remove_children_deque, BranchNodeDeque};
9
10type BranchTokenDeque<BranchData, LeafData> = Token<BranchDeque<BranchData, LeafData>>;
11type LeafTokenDeque<BranchData, LeafData> = Token<LeafDeque<BranchData, LeafData>>;
12
13#[cfg_attrs]
14/// A [node] that is split into separate [branch] and [leaf] nodes.
15///
16/// This is the [deque] version, where [children] are represented as a [`VecDeque`]. In this
17/// version, a [node]'s [previous sibling][prev][(s)][preceding] and
18/// [next sibling][next][(s)][following] are not available, but [branches] can be
19/// [directly indexed], and [children] can be [detached], [removed], or [inserted] by index.
20///
21/// [node]: Node
22/// [branch]: BranchDeque
23/// [branches]: BranchDeque
24/// [leaf]: LeafDeque
25/// [leaves]: LeafDeque
26///
27/// [deque]: BranchNodeDeque
28/// [children]: BranchDeque::children
29///
30/// [prev]: LinkedNode::prev
31/// [preceding]: LinkedNode::preceding_siblings
32/// [next]: LinkedNode::next
33/// [following]: LinkedNode::following_siblings
34///
35/// [directly indexed]: Index
36/// [detached]: BranchDeque::detach
37/// [removed]: BranchDeque::remove
38/// [inserted]: BranchDeque::insert
39///
40/// `BranchData` represents the [custom data](BranchDeque::Data) associated with [branches], while
41/// `LeafData` represents the [custom data](LeafDeque::Data) associated with [leaves].
42///
43/// # See also
44/// For the non-[deque] version, see [`SplitNode`].
45#[configure(
46	feature = "unified",
47	/// For a [node] that _isn't_ split into separate branch and leaf nodes, see
48	#[configure(
49		not(feature = "deque"),
50		/// [`UnifiedNode`].
51		///
52	)]
53	#[configure(
54		feature = "deque",
55		/// [`UnifiedNode`] and [`UnifiedNodeDeque`].
56		///
57		/// [`UnifiedNodeDeque`]: crate::unified::UnifiedNodeDeque
58	)]
59	/// [`UnifiedNode`]: crate::unified::UnifiedNode
60)]
61#[derive(Debug)]
62#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
63pub enum SplitNodeDeque<BranchData: Debug, LeafData: Debug> {
64	Branch(BranchDeque<BranchData, LeafData>),
65	Leaf(LeafDeque<BranchData, LeafData>),
66}
67
68/// The [token] type referring to a [`SplitNodeDeque`].
69///
70/// [token]: NodeToken
71#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
72pub enum SplitTokenDeque<BranchData: Debug, LeafData: Debug> {
73	/// A [branch] node's [token].
74	///
75	/// [branch]: BranchDeque
76	/// [token]: NodeToken
77	Branch(BranchTokenDeque<BranchData, LeafData>),
78	/// A [leaf] node's [token].
79	///
80	/// [leaf]: LeafDeque
81	/// [token]: NodeToken
82	Leaf(LeafTokenDeque<BranchData, LeafData>),
83}
84
85/// The [node] representing [branches] in a [split node] tree.
86///
87/// [Branches] are [nodes] which may have [children], as opposed to [leaves], which may not have
88/// [children].
89///
90/// `BranchData` represents the [custom data] associated with branch nodes.
91///
92/// [node]: Node
93/// [nodes]: Node
94///
95/// [branches]: BranchNodeDeque
96/// [Branches]: BranchNodeDeque
97///
98/// [leaves]: LeafDeque
99///
100/// [children]: BranchNode::children
101/// [split node]: SplitNodeDeque
102#[derive(Debug)]
103#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
104pub struct BranchDeque<BranchData: Debug, LeafData: Debug> {
105	token: Token<Self>,
106
107	parent: Option<BranchTokenDeque<BranchData, LeafData>>,
108	children: VecDeque<SplitTokenDeque<BranchData, LeafData>>,
109
110	data: BranchData,
111}
112
113/// The [node] representing leaves in a [split node] tree.
114///
115/// Leaves are [nodes] which may not have [children], as opposed to [branches], which may have
116/// [children].
117///
118/// `LeafData` represents the [custom data] associated with leaf nodes.
119///
120/// [custom data]: Leaf::Data
121///
122/// [node]: Node
123/// [nodes]: Node
124///
125/// [branches]: BranchDeque
126/// [children]: BranchDeque::children
127///
128/// [split node]: SplitNodeDeque
129#[derive(Debug)]
130#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
131pub struct LeafDeque<BranchData: Debug, LeafData: Debug> {
132	token: Token<Self>,
133
134	parent: Option<BranchTokenDeque<BranchData, LeafData>>,
135
136	data: LeafData,
137}
138
139impl<BranchData, LeafData> Debug for SplitTokenDeque<BranchData, LeafData>
140where
141	BranchData: Debug,
142	LeafData: Debug,
143{
144	#[inline(always)]
145	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
146		match self {
147			Self::Branch(branch) => write!(f, "BranchDequeToken({:?})", branch.idx()),
148			Self::Leaf(leaf) => write!(f, "LeafDequeToken({:?})", leaf.idx()),
149		}
150	}
151}
152
153impl<BranchData, LeafData, I: Idx> PartialEq<I> for SplitTokenDeque<BranchData, LeafData>
154where
155	BranchData: Debug,
156	LeafData: Debug,
157{
158	#[inline(always)]
159	fn eq(&self, other: &I) -> bool {
160		self.idx() == other.idx()
161	}
162}
163
164impl<BranchData, LeafData> Eq for SplitTokenDeque<BranchData, LeafData>
165where
166	BranchData: Debug,
167	LeafData: Debug,
168{
169}
170
171impl<BranchData, LeafData> Hash for SplitTokenDeque<BranchData, LeafData>
172where
173	BranchData: Debug,
174	LeafData: Debug,
175{
176	#[inline(always)]
177	fn hash<H: Hasher>(&self, state: &mut H) {
178		match self {
179			Self::Branch(branch) => branch.hash(state),
180			Self::Leaf(leaf) => leaf.hash(state),
181		}
182	}
183}
184
185impl<BranchData, LeafData> Clone for SplitTokenDeque<BranchData, LeafData>
186where
187	BranchData: Debug,
188	LeafData: Debug,
189{
190	#[inline(always)]
191	fn clone(&self) -> Self {
192		*self
193	}
194}
195
196impl<BranchData, LeafData> Copy for SplitTokenDeque<BranchData, LeafData>
197where
198	BranchData: Debug,
199	LeafData: Debug,
200{
201}
202
203impl<BranchData, LeafData> SplitTokenDeque<BranchData, LeafData>
204where
205	BranchData: Debug,
206	LeafData: Debug,
207{
208	/// Creates a new `SplitTokenDeque` for a [branch] from the given `token`.
209	///
210	/// [branch]: Branch
211	#[inline(always)]
212	pub const fn new_branch(token: BranchTokenDeque<BranchData, LeafData>) -> Self {
213		Self::Branch(token)
214	}
215
216	/// Creates a new `SplitTokenDeque` for a [leaf] from the given `token`.
217	///
218	/// [leaf]: Leaf
219	#[inline(always)]
220	pub const fn new_leaf(token: LeafTokenDeque<BranchData, LeafData>) -> Self {
221		Self::Leaf(token)
222	}
223}
224
225impl<BranchData, LeafData> Idx for SplitTokenDeque<BranchData, LeafData>
226where
227	BranchData: Debug,
228	LeafData: Debug,
229{
230	#[inline(always)]
231	fn idx(&self) -> generational_arena::Index {
232		match self {
233			Self::Branch(branch) => branch.idx(),
234			Self::Leaf(leaf) => leaf.idx(),
235		}
236	}
237}
238
239impl<BranchData, LeafData> NodeToken<SplitNodeDeque<BranchData, LeafData>> for SplitTokenDeque<BranchData, LeafData>
240where
241	BranchData: Debug,
242	LeafData: Debug,
243{
244}
245
246impl<BranchData, LeafData, Other: Node> PartialEq<Other> for BranchDeque<BranchData, LeafData>
247where
248	BranchData: Debug,
249	LeafData: Debug,
250	<Self as Node>::Token: PartialEq<Other::Token>,
251{
252	fn eq(&self, other: &Other) -> bool {
253		self.token == other.token()
254	}
255}
256
257impl<BranchData, LeafData> Eq for BranchDeque<BranchData, LeafData>
258where
259	BranchData: Debug,
260	LeafData: Debug,
261{
262}
263
264impl<BranchData, LeafData> Hash for BranchDeque<BranchData, LeafData>
265where
266	BranchData: Debug,
267	LeafData: Debug,
268{
269	#[inline(always)]
270	fn hash<H: Hasher>(&self, state: &mut H) {
271		self.token.hash(state);
272	}
273}
274
275impl<BranchData, LeafData, Other: Node> PartialEq<Other> for LeafDeque<BranchData, LeafData>
276where
277	BranchData: Debug,
278	LeafData: Debug,
279	<Self as Node>::Token: PartialEq<Other::Token>,
280{
281	fn eq(&self, other: &Other) -> bool {
282		self.token == other.token()
283	}
284}
285
286impl<BranchData, LeafData> Eq for LeafDeque<BranchData, LeafData>
287where
288	BranchData: Debug,
289	LeafData: Debug,
290{
291}
292
293impl<BranchData, LeafData> Hash for LeafDeque<BranchData, LeafData>
294where
295	BranchData: Debug,
296	LeafData: Debug,
297{
298	#[inline(always)]
299	fn hash<H: Hasher>(&self, state: &mut H) {
300		self.token.hash(state);
301	}
302}
303
304impl<'node, BranchData, LeafData> TryFrom<&'node SplitNodeDeque<BranchData, LeafData>>
305	for &'node BranchDeque<BranchData, LeafData>
306where
307	BranchData: Debug,
308	LeafData: Debug,
309{
310	type Error = &'node SplitNodeDeque<BranchData, LeafData>;
311
312	#[inline(always)]
313	fn try_from(node: &'node SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
314		match node {
315			SplitNodeDeque::Branch(branch) => Ok(branch),
316			SplitNodeDeque::Leaf(_) => Err(node),
317		}
318	}
319}
320
321impl<'node, BranchData, LeafData> TryFrom<&'node mut SplitNodeDeque<BranchData, LeafData>>
322	for &'node mut BranchDeque<BranchData, LeafData>
323where
324	BranchData: Debug,
325	LeafData: Debug,
326{
327	type Error = &'node mut SplitNodeDeque<BranchData, LeafData>;
328
329	#[inline(always)]
330	fn try_from(node: &'node mut SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
331		match node {
332			SplitNodeDeque::Branch(branch) => Ok(branch),
333			SplitNodeDeque::Leaf(_) => Err(node),
334		}
335	}
336}
337
338impl<'node, BranchData, LeafData> TryFrom<&'node SplitNodeDeque<BranchData, LeafData>>
339	for &'node LeafDeque<BranchData, LeafData>
340where
341	BranchData: Debug,
342	LeafData: Debug,
343{
344	type Error = &'node SplitNodeDeque<BranchData, LeafData>;
345
346	#[inline(always)]
347	fn try_from(node: &'node SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
348		match node {
349			SplitNodeDeque::Branch(_) => Err(node),
350			SplitNodeDeque::Leaf(leaf) => Ok(leaf),
351		}
352	}
353}
354
355impl<'node, BranchData, LeafData> TryFrom<&'node mut SplitNodeDeque<BranchData, LeafData>>
356	for &'node mut LeafDeque<BranchData, LeafData>
357where
358	BranchData: Debug,
359	LeafData: Debug,
360{
361	type Error = &'node mut SplitNodeDeque<BranchData, LeafData>;
362
363	#[inline(always)]
364	fn try_from(node: &'node mut SplitNodeDeque<BranchData, LeafData>) -> Result<Self, Self::Error> {
365		match node {
366			SplitNodeDeque::Branch(_) => Err(node),
367			SplitNodeDeque::Leaf(leaf) => Ok(leaf),
368		}
369	}
370}
371
372impl<BranchData, LeafData> SplitNodeDeque<BranchData, LeafData>
373where
374	BranchData: Debug,
375	LeafData: Debug,
376{
377	#[inline(always)]
378	fn set_parent(&mut self, parent: Option<BranchTokenDeque<BranchData, LeafData>>) {
379		match self {
380			Self::Branch(branch) => branch.parent = parent,
381			Self::Leaf(leaf) => leaf.parent = parent,
382		}
383	}
384}
385
386impl<BranchData, LeafData> Sealed for SplitNodeDeque<BranchData, LeafData>
387where
388	BranchData: Debug,
389	LeafData: Debug,
390{
391}
392
393impl<BranchData, LeafData> Node for SplitNodeDeque<BranchData, LeafData>
394where
395	BranchData: Debug,
396	LeafData: Debug,
397{
398	type Base = Self;
399	type Token = SplitTokenDeque<BranchData, LeafData>;
400
401	type Data = SplitData<BranchData, LeafData>;
402	type DataRef<'data> = SplitData<&'data BranchData, &'data LeafData>
403	where
404		Self: 'data;
405	type DataRefMut<'data> = SplitData<&'data mut BranchData, &'data mut LeafData>
406	where
407		Self: 'data;
408
409	#[inline(always)]
410	fn new(arena: &mut Arena<Self>, data: Self::Data) -> Self::Token {
411		match data {
412			SplitData::Branch(data) => SplitTokenDeque::Branch(BranchDeque::new(arena, data)),
413			SplitData::Leaf(data) => SplitTokenDeque::Leaf(LeafDeque::new(arena, data)),
414		}
415	}
416
417	#[inline(always)]
418	fn token(&self) -> Self::Token {
419		match self {
420			Self::Branch(branch) => SplitTokenDeque::Branch(branch.token()),
421			Self::Leaf(leaf) => SplitTokenDeque::Leaf(leaf.token()),
422		}
423	}
424
425	#[inline(always)]
426	fn parent(&self) -> Option<<<Self::Base as BaseNode>::Branch as Node>::Token> {
427		match self {
428			Self::Branch(branch) => branch.parent(),
429			Self::Leaf(leaf) => leaf.parent(),
430		}
431	}
432
433	#[inline(always)]
434	fn data(&self) -> Self::DataRef<'_> {
435		match self {
436			Self::Branch(branch) => SplitData::Branch(branch.data()),
437			Self::Leaf(leaf) => SplitData::Leaf(leaf.data()),
438		}
439	}
440
441	#[inline(always)]
442	fn data_mut(&mut self) -> Self::DataRefMut<'_> {
443		match self {
444			Self::Branch(branch) => SplitData::Branch(branch.data_mut()),
445			Self::Leaf(leaf) => SplitData::Leaf(leaf.data_mut()),
446		}
447	}
448}
449
450impl<BranchData, LeafData> BaseNode for SplitNodeDeque<BranchData, LeafData>
451where
452	BranchData: Debug,
453	LeafData: Debug,
454{
455	type Representation = SplitNodeRepresentation<BranchData, LeafData>;
456
457	type Branch = BranchDeque<BranchData, LeafData>;
458	type Leaf = LeafDeque<BranchData, LeafData>;
459
460	fn into_representation(self, arena: &mut Arena<Self>) -> Self::Representation
461	where
462		Self: Sized,
463	{
464		match self {
465			Self::Branch(branch) => SplitNodeRepresentation::Branch {
466				children: remove_children_deque(&branch, arena),
467				data: branch.data,
468			},
469
470			Self::Leaf(leaf) => SplitNodeRepresentation::Leaf { data: leaf.data },
471		}
472	}
473}
474
475impl<BranchData, LeafData> Sealed for BranchDeque<BranchData, LeafData>
476where
477	BranchData: Debug,
478	LeafData: Debug,
479{
480}
481
482impl<BranchData, LeafData> Node for BranchDeque<BranchData, LeafData>
483where
484	BranchData: Debug,
485	LeafData: Debug,
486{
487	type Base = SplitNodeDeque<BranchData, LeafData>;
488	type Token = Token<Self>;
489
490	type Data = BranchData;
491	type DataRef<'data> = &'data BranchData
492	where
493		Self: 'data;
494	type DataRefMut<'data> = &'data mut BranchData
495	where
496		Self: 'data;
497
498	fn new(arena: &mut Arena<Self::Base>, data: Self::Data) -> Token<Self> {
499		Token::new(arena.0.insert_with(|idx| {
500			SplitNodeDeque::Branch(Self {
501				token: Token::new(idx),
502
503				parent: None,
504				children: VecDeque::new(),
505
506				data,
507			})
508		}))
509	}
510
511	#[inline(always)]
512	fn token(&self) -> Self::Token {
513		self.token
514	}
515
516	#[inline(always)]
517	fn parent(&self) -> Option<Token<Self>> {
518		self.parent
519	}
520
521	#[inline(always)]
522	fn data(&self) -> &BranchData {
523		&self.data
524	}
525
526	#[inline(always)]
527	fn data_mut(&mut self) -> &mut BranchData {
528		&mut self.data
529	}
530}
531
532impl<BranchData, LeafData> BranchNode for BranchDeque<BranchData, LeafData>
533where
534	BranchData: Debug,
535	LeafData: Debug,
536{
537	type ChildrenIter<'branch>
538	= Copied<vec_deque::Iter<'branch, <Self::Base as Node>::Token>>
539	where
540		Self: 'branch;
541
542	#[inline]
543	fn first(&self) -> Option<<Self::Base as Node>::Token> {
544		match self.children.len() {
545			0 => None,
546			_ => Some(self.children[0]),
547		}
548	}
549
550	#[inline]
551	fn last(&self) -> Option<<Self::Base as Node>::Token> {
552		match self.children.len() {
553			0 => None,
554			len => Some(self.children[len - 1]),
555		}
556	}
557
558	#[inline(always)]
559	fn children<'branch>(&'branch self, _arena: &'branch Arena<Self::Base>) -> Self::ChildrenIter<'branch> {
560		self.children.iter().copied()
561	}
562
563	#[inline(always)]
564	fn is_empty(&self) -> bool {
565		self.children.is_empty()
566	}
567
568	fn detach_front(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<<Self::Base as Node>::Token> {
569		let child = token_to_node!(ref mut, Self: token, arena).children.pop_front();
570
571		if let Some(child) = &child {
572			let node = &mut arena.0[child.idx()];
573
574			// Detach the node's parent.
575			node.set_parent(None);
576		}
577
578		child
579	}
580
581	fn detach_back(token: Self::Token, arena: &mut Arena<Self::Base>) -> Option<<Self::Base as Node>::Token> {
582		let child = token_to_node!(ref mut, Self: token, arena).children.pop_back();
583
584		if let Some(child) = &child {
585			let node = &mut arena.0[child.idx()];
586
587			// Detach the node's parent.
588			node.set_parent(None);
589		}
590
591		child
592	}
593
594	fn pop_front(
595		token: Self::Token,
596		arena: &mut Arena<Self::Base>,
597	) -> Option<SplitNodeRepresentation<BranchData, LeafData>> {
598		let child = token_to_node!(ref mut, Self: token, arena).children.pop_front();
599
600		child.map(|child| {
601			arena
602				.0
603				.remove(child.idx())
604				.expect("tried to remove child but there was no such node in the `arena`")
605				.into_representation(arena)
606		})
607	}
608
609	fn pop_back(
610		token: Self::Token,
611		arena: &mut Arena<Self::Base>,
612	) -> Option<SplitNodeRepresentation<BranchData, LeafData>> {
613		let child = token_to_node!(ref mut, Self: token, arena).children.pop_back();
614
615		child.map(|child| {
616			arena
617				.0
618				.remove(child.idx())
619				.expect("tried to remove child but there was no such node in the `arena`")
620				.into_representation(arena)
621		})
622	}
623
624	fn push_front(token: Self::Token, arena: &mut Arena<Self::Base>, new: <Self::Base as Node>::Token) {
625		// We're not pushing our own root...
626		assert_ne!(
627			arena.0[token.idx()].root(arena),
628			new,
629			"tried to push this branch's own root as a child"
630		);
631		// And we're not pushing a child that already has a parent...
632		assert!(
633			arena.0[new.idx()].parent().is_none(),
634			"tried to push a child that already has a parent"
635		);
636
637		// Set the child's parent.
638		arena.0[new.idx()].set_parent(Some(token));
639
640		// Push the child's token.
641		token_to_node!(ref mut, Self: token, arena).children.push_front(new);
642	}
643
644	fn push_back(token: Self::Token, arena: &mut Arena<Self::Base>, new: <Self::Base as Node>::Token) {
645		// We're not pushing our own root...
646		assert_ne!(
647			arena.0[token.idx()].root(arena),
648			new,
649			"tried to push this branch's own root as a child"
650		);
651		// And we're not pushing a child that already has a parent...
652		assert!(
653			arena.0[new.idx()].parent().is_none(),
654			"tried to push a child that already has a parent"
655		);
656
657		// Set the child's parent.
658		arena.0[new.idx()].set_parent(Some(token));
659
660		// Push the child's token.
661		token_to_node!(ref mut, Self: token, arena).children.push_back(new);
662	}
663}
664
665impl<BranchData, LeafData> Index<usize> for BranchDeque<BranchData, LeafData>
666where
667	BranchData: Debug,
668	LeafData: Debug,
669{
670	type Output = <<Self as Node>::Base as Node>::Token;
671
672	#[inline(always)]
673	fn index(&self, index: usize) -> &Self::Output {
674		&self.children[index]
675	}
676}
677
678impl<BranchData, LeafData> BranchNodeDeque for BranchDeque<BranchData, LeafData>
679where
680	BranchData: Debug,
681	LeafData: Debug,
682{
683	#[inline(always)]
684	fn len(&self) -> usize {
685		self.children.len()
686	}
687
688	fn detach(token: Self::Token, arena: &mut Arena<Self::Base>, index: usize) -> <Self::Base as Node>::Token {
689		let children = &mut token_to_node!(ref mut, Self: token, arena).children;
690
691		let child = children.remove(index).unwrap_or_else(|| {
692			panic!(
693				"the given `index` ({index}) was out of bounds; there were only {} children",
694				children.len()
695			)
696		});
697
698		// Detach the child's parent.
699		arena.0[child.idx()].set_parent(None);
700
701		child
702	}
703
704	fn remove(
705		token: Self::Token,
706		arena: &mut Arena<Self::Base>,
707		index: usize,
708	) -> SplitNodeRepresentation<BranchData, LeafData> {
709		let children = &mut token_to_node!(ref mut, Self: token, arena).children;
710
711		let child = children.remove(index).unwrap_or_else(|| {
712			panic!(
713				"the given `index` ({index}) was out of bounds; there were only {} children",
714				children.len()
715			)
716		});
717
718		arena
719			.0
720			.remove(child.idx())
721			.expect("tried to remove child but there was no such node in `arena`")
722			.into_representation(arena)
723	}
724
725	fn insert(token: Self::Token, arena: &mut Arena<Self::Base>, index: usize, new: <Self::Base as Node>::Token) {
726		// We're not inserting our own root...
727		assert_ne!(
728			arena.0[token.idx()].root(arena),
729			new,
730			"tried to insert this branch's own root as a child"
731		);
732		// And we're not inserting a child that already has a parent...
733		assert!(
734			arena.0[new.idx()].parent().is_none(),
735			"tried to insert a child that already has a parent"
736		);
737
738		// Set the child's parent.
739		arena.0[new.idx()].set_parent(Some(token));
740
741		// Insert the child's token.
742		token_to_node!(ref mut, Self: token, arena).children.insert(index, new);
743	}
744}
745
746impl<BranchData, LeafData> Sealed for LeafDeque<BranchData, LeafData>
747where
748	BranchData: Debug,
749	LeafData: Debug,
750{
751}
752
753impl<BranchData, LeafData> Node for LeafDeque<BranchData, LeafData>
754where
755	BranchData: Debug,
756	LeafData: Debug,
757{
758	type Base = SplitNodeDeque<BranchData, LeafData>;
759	type Token = Token<Self>;
760
761	type Data = LeafData;
762	type DataRef<'data> = &'data LeafData
763	where
764		Self: 'data;
765	type DataRefMut<'data> = &'data mut LeafData
766	where
767		Self: 'data;
768
769	fn new(arena: &mut Arena<Self::Base>, data: Self::Data) -> Self::Token {
770		Token::new(arena.0.insert_with(|idx| {
771			SplitNodeDeque::Leaf(Self {
772				token: Token::new(idx),
773
774				parent: None,
775
776				data,
777			})
778		}))
779	}
780
781	#[inline(always)]
782	fn token(&self) -> Self::Token {
783		self.token
784	}
785
786	#[inline(always)]
787	fn parent(&self) -> Option<Token<<Self::Base as BaseNode>::Branch>> {
788		self.parent
789	}
790
791	#[inline(always)]
792	fn data(&self) -> &LeafData {
793		&self.data
794	}
795
796	#[inline(always)]
797	fn data_mut(&mut self) -> &mut LeafData {
798		&mut self.data
799	}
800}