···
use std::collections::VecDeque;
···
children: (DnaRef, DnaRef, DnaRef),
···
-
pub fn depth(&self) -> usize {
···
let (a, b) = &node.children;
-
(x, Self::concat(y, b.clone()))
let (x, y) = b.split(n - a.len());
-
(Self::concat(a.clone(), x), y)
Dna::ThreeNode(node) => {
let (a, b, c) = &node.children;
-
(x, Self::concat(Self::concat(y, b.clone()), c.clone()))
-
(a.clone(), Self::concat(b.clone(), c.clone()))
} else if n - a.len() < b.len() {
let (x, y) = b.split(n - a.len());
-
(Self::concat(a.clone(), x), Self::concat(y, c.clone()))
} else if n == a.len() + b.len() {
-
(Self::concat(a.clone(), b.clone()), c.clone())
let (x, y) = c.split(n - a.len() - b.len());
-
(Self::concat(a.clone(), Self::concat(b.clone(), x)), y)
-
fn concat_helper(lhs: DnaRef, rhs: DnaRef) -> ConcatState {
-
if lhs.depth() == rhs.depth() {
-
ConcatState::Two(lhs, rhs)
-
} else if lhs.depth() < rhs.depth() {
-
Dna::Empty => unreachable!(),
-
Dna::Leaf(_) => unreachable!(),
-
Dna::TwoNode(node) => {
-
let (a, b) = &node.children;
-
match Self::concat_helper(lhs, a.clone()) {
-
ConcatState::One(x) => {
-
ConcatState::One(Self::from_two_children(x, b.clone()))
-
ConcatState::Two(x, y) => {
-
ConcatState::One(Self::from_three_children(x, y, b.clone()))
-
Dna::ThreeNode(node) => {
-
let (a, b, c) = &node.children;
-
match Self::concat_helper(lhs, a.clone()) {
-
ConcatState::One(x) => {
-
ConcatState::One(Self::from_three_children(x, b.clone(), c.clone()))
-
ConcatState::Two(x, y) => ConcatState::Two(
-
Self::from_two_children(x, y),
-
Self::from_two_children(b.clone(), c.clone()),
-
Dna::Empty => unreachable!(),
-
Dna::Leaf(_) => unreachable!(),
-
Dna::TwoNode(node) => {
-
let (a, b) = &node.children;
-
match Self::concat_helper(b.clone(), rhs) {
-
ConcatState::One(x) => {
-
ConcatState::One(Self::from_two_children(a.clone(), x))
-
ConcatState::Two(x, y) => {
-
ConcatState::One(Self::from_three_children(a.clone(), x, y))
-
Dna::ThreeNode(node) => {
-
let (a, b, c) = &node.children;
-
match Self::concat_helper(c.clone(), rhs) {
-
ConcatState::One(x) => {
-
ConcatState::One(Self::from_three_children(a.clone(), b.clone(), x))
-
ConcatState::Two(x, y) => ConcatState::Two(
-
Self::from_two_children(a.clone(), b.clone()),
-
Self::from_two_children(x, y),
-
// Use Add trait instead
-
pub fn concat(lhs: DnaRef, rhs: DnaRef) -> Self {
-
match Self::concat_helper(lhs, rhs) {
-
ConcatState::One(a) => a,
-
ConcatState::Two(a, b) => Self::from_two_children(a, b),
pub fn iter<'a>(&'a self) -> DnaIterator<'a> {
let mut stack = Vec::new();
···
···
let lhs = DnaRef::from_string("ABCD");
let rhs = DnaRef::from_string("WXYZ");
-
DnaRef::concat(lhs, rhs).iter().collect::<String>(),
···
use std::collections::VecDeque;
···
children: (DnaRef, DnaRef, DnaRef),
···
+
fn depth(&self) -> usize {
···
let (a, b) = &node.children;
let (x, y) = b.split(n - a.len());
Dna::ThreeNode(node) => {
let (a, b, c) = &node.children;
+
(x, y + b.clone() + c.clone())
+
(a.clone(), b.clone() + c.clone())
} else if n - a.len() < b.len() {
let (x, y) = b.split(n - a.len());
+
(a.clone() + x, y + c.clone())
} else if n == a.len() + b.len() {
+
(a.clone() + b.clone(), c.clone())
let (x, y) = c.split(n - a.len() - b.len());
+
(a.clone() + b.clone() + x, y)
pub fn iter<'a>(&'a self) -> DnaIterator<'a> {
let mut stack = Vec::new();
···
+
fn add_helper(lhs: DnaRef, rhs: DnaRef) -> AddState {
+
if lhs.depth() == rhs.depth() {
+
AddState::Two(lhs, rhs)
+
} else if lhs.depth() < rhs.depth() {
+
Dna::Empty => unreachable!(),
+
Dna::Leaf(_) => unreachable!(),
+
Dna::TwoNode(node) => {
+
let (a, b) = &node.children;
+
match add_helper(lhs, a.clone()) {
+
AddState::One(DnaRef::from_two_children(x, b.clone()))
+
AddState::Two(x, y) => {
+
AddState::One(DnaRef::from_three_children(x, y, b.clone()))
+
Dna::ThreeNode(node) => {
+
let (a, b, c) = &node.children;
+
match add_helper(lhs, a.clone()) {
+
AddState::One(DnaRef::from_three_children(x, b.clone(), c.clone()))
+
AddState::Two(x, y) => AddState::Two(
+
DnaRef::from_two_children(x, y),
+
DnaRef::from_two_children(b.clone(), c.clone()),
+
Dna::Empty => unreachable!(),
+
Dna::Leaf(_) => unreachable!(),
+
Dna::TwoNode(node) => {
+
let (a, b) = &node.children;
+
match add_helper(b.clone(), rhs) {
+
AddState::One(DnaRef::from_two_children(a.clone(), x))
+
AddState::Two(x, y) => {
+
AddState::One(DnaRef::from_three_children(a.clone(), x, y))
+
Dna::ThreeNode(node) => {
+
let (a, b, c) = &node.children;
+
match add_helper(c.clone(), rhs) {
+
AddState::One(DnaRef::from_three_children(a.clone(), b.clone(), x))
+
AddState::Two(x, y) => AddState::Two(
+
DnaRef::from_two_children(a.clone(), b.clone()),
+
DnaRef::from_two_children(x, y),
+
fn add(self, other: Self) -> Self {
+
match add_helper(self, other) {
+
AddState::Two(a, b) => Self::from_two_children(a, b),
···
let lhs = DnaRef::from_string("ABCD");
let rhs = DnaRef::from_string("WXYZ");
+
(lhs + rhs).iter().collect::<String>(),