···
use std::collections::VecDeque;
···
-
// Use Index trait instead
-
pub fn index(&self, n: usize) -> char {
-
debug_assert!(n < self.len());
-
Dna::Empty => unreachable!(),
-
Dna::TwoNode(node) => {
-
let (a, b) = &node.children;
-
Dna::ThreeNode(node) => {
-
let (a, b, c) = &node.children;
-
} else if n < a.len() + b.len() {
-
c.index(n - a.len() - b.len())
pub fn split(&self, n: usize) -> (DnaRef, DnaRef) {
debug_assert!(n <= self.len());
-
Dna::Empty => (Self::new(), Self::new()),
-
(Self::new(), Self::from_char(*c))
-
(Self::from_char(*c), Self::new())
···
(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) {
···
···
let dna: DnaRef = DnaRef::from_string(s);
for (i, c) in s.chars().enumerate() {
-
assert_eq!(dna.index(i), c);
···
let lhs = DnaRef::from_string("ABCD");
let rhs = DnaRef::from_string("WXYZ");
-
(lhs + rhs).iter().collect::<String>(),
···
use std::collections::VecDeque;
···
+
pub fn iter<'a>(&'a self) -> DnaIterator<'a> {
+
let mut stack = Vec::new();
pub fn split(&self, n: usize) -> (DnaRef, DnaRef) {
debug_assert!(n <= self.len());
+
Dna::Empty => (DnaRef::new(), DnaRef::new()),
+
(DnaRef::new(), DnaRef::from_char(*c))
+
(DnaRef::from_char(*c), DnaRef::new())
···
(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)
···
+
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(x) => 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(x) => 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) {
···
+
impl Index<usize> for DnaRef {
+
fn index(&self, n: usize) -> &Self::Output {
+
debug_assert!(n < self.len());
+
Dna::Empty => unreachable!(),
+
Dna::TwoNode(node) => {
+
let (a, b) = &node.children;
+
Dna::ThreeNode(node) => {
+
let (a, b, c) = &node.children;
+
} else if n < a.len() + b.len() {
+
c.index(n - a.len() - b.len())
···
let dna: DnaRef = DnaRef::from_string(s);
for (i, c) in s.chars().enumerate() {
···
let lhs = DnaRef::from_string("ABCD");
let rhs = DnaRef::from_string("WXYZ");
+
assert_eq!((lhs + rhs).iter().collect::<String>(), "ABCDWXYZ");