···
use std::collections::VecDeque;
···
children: (DnaRef, DnaRef, DnaRef),
35
-
Two(DnaRef, DnaRef),
···
79
-
pub fn depth(&self) -> usize {
75
+
fn depth(&self) -> usize {
···
let (a, b) = &node.children;
131
-
(x, Self::concat(y, b.clone()))
let (x, y) = b.split(n - a.len());
136
-
(Self::concat(a.clone(), x), y)
Dna::ThreeNode(node) => {
let (a, b, c) = &node.children;
143
-
(x, Self::concat(Self::concat(y, b.clone()), c.clone()))
139
+
(x, y + b.clone() + c.clone())
145
-
(a.clone(), Self::concat(b.clone(), c.clone()))
141
+
(a.clone(), b.clone() + c.clone())
} else if n - a.len() < b.len() {
let (x, y) = b.split(n - a.len());
148
-
(Self::concat(a.clone(), x), Self::concat(y, c.clone()))
144
+
(a.clone() + x, y + c.clone())
} else if n == a.len() + b.len() {
150
-
(Self::concat(a.clone(), b.clone()), c.clone())
146
+
(a.clone() + b.clone(), c.clone())
let (x, y) = c.split(n - a.len() - b.len());
153
-
(Self::concat(a.clone(), Self::concat(b.clone(), x)), y)
159
-
fn concat_helper(lhs: DnaRef, rhs: DnaRef) -> ConcatState {
160
-
if lhs.depth() == rhs.depth() {
161
-
ConcatState::Two(lhs, rhs)
162
-
} else if lhs.depth() < rhs.depth() {
164
-
Dna::Empty => unreachable!(),
165
-
Dna::Leaf(_) => unreachable!(),
166
-
Dna::TwoNode(node) => {
167
-
let (a, b) = &node.children;
168
-
match Self::concat_helper(lhs, a.clone()) {
169
-
ConcatState::One(x) => {
170
-
ConcatState::One(Self::from_two_children(x, b.clone()))
172
-
ConcatState::Two(x, y) => {
173
-
ConcatState::One(Self::from_three_children(x, y, b.clone()))
177
-
Dna::ThreeNode(node) => {
178
-
let (a, b, c) = &node.children;
179
-
match Self::concat_helper(lhs, a.clone()) {
180
-
ConcatState::One(x) => {
181
-
ConcatState::One(Self::from_three_children(x, b.clone(), c.clone()))
183
-
ConcatState::Two(x, y) => ConcatState::Two(
184
-
Self::from_two_children(x, y),
185
-
Self::from_two_children(b.clone(), c.clone()),
192
-
Dna::Empty => unreachable!(),
193
-
Dna::Leaf(_) => unreachable!(),
194
-
Dna::TwoNode(node) => {
195
-
let (a, b) = &node.children;
196
-
match Self::concat_helper(b.clone(), rhs) {
197
-
ConcatState::One(x) => {
198
-
ConcatState::One(Self::from_two_children(a.clone(), x))
200
-
ConcatState::Two(x, y) => {
201
-
ConcatState::One(Self::from_three_children(a.clone(), x, y))
205
-
Dna::ThreeNode(node) => {
206
-
let (a, b, c) = &node.children;
207
-
match Self::concat_helper(c.clone(), rhs) {
208
-
ConcatState::One(x) => {
209
-
ConcatState::One(Self::from_three_children(a.clone(), b.clone(), x))
211
-
ConcatState::Two(x, y) => ConcatState::Two(
212
-
Self::from_two_children(a.clone(), b.clone()),
213
-
Self::from_two_children(x, y),
149
+
(a.clone() + b.clone() + x, y)
221
-
// Use Add trait instead
222
-
pub fn concat(lhs: DnaRef, rhs: DnaRef) -> Self {
223
-
match Self::concat_helper(lhs, rhs) {
224
-
ConcatState::One(a) => a,
225
-
ConcatState::Two(a, b) => Self::from_two_children(a, b),
pub fn iter<'a>(&'a self) -> DnaIterator<'a> {
let mut stack = Vec::new();
···
268
+
Two(DnaRef, DnaRef),
271
+
fn add_helper(lhs: DnaRef, rhs: DnaRef) -> AddState {
272
+
if lhs.depth() == rhs.depth() {
273
+
AddState::Two(lhs, rhs)
274
+
} else if lhs.depth() < rhs.depth() {
276
+
Dna::Empty => unreachable!(),
277
+
Dna::Leaf(_) => unreachable!(),
278
+
Dna::TwoNode(node) => {
279
+
let (a, b) = &node.children;
280
+
match add_helper(lhs, a.clone()) {
281
+
AddState::One(x) => {
282
+
AddState::One(DnaRef::from_two_children(x, b.clone()))
284
+
AddState::Two(x, y) => {
285
+
AddState::One(DnaRef::from_three_children(x, y, b.clone()))
289
+
Dna::ThreeNode(node) => {
290
+
let (a, b, c) = &node.children;
291
+
match add_helper(lhs, a.clone()) {
292
+
AddState::One(x) => {
293
+
AddState::One(DnaRef::from_three_children(x, b.clone(), c.clone()))
295
+
AddState::Two(x, y) => AddState::Two(
296
+
DnaRef::from_two_children(x, y),
297
+
DnaRef::from_two_children(b.clone(), c.clone()),
304
+
Dna::Empty => unreachable!(),
305
+
Dna::Leaf(_) => unreachable!(),
306
+
Dna::TwoNode(node) => {
307
+
let (a, b) = &node.children;
308
+
match add_helper(b.clone(), rhs) {
309
+
AddState::One(x) => {
310
+
AddState::One(DnaRef::from_two_children(a.clone(), x))
312
+
AddState::Two(x, y) => {
313
+
AddState::One(DnaRef::from_three_children(a.clone(), x, y))
317
+
Dna::ThreeNode(node) => {
318
+
let (a, b, c) = &node.children;
319
+
match add_helper(c.clone(), rhs) {
320
+
AddState::One(x) => {
321
+
AddState::One(DnaRef::from_three_children(a.clone(), b.clone(), x))
323
+
AddState::Two(x, y) => AddState::Two(
324
+
DnaRef::from_two_children(a.clone(), b.clone()),
325
+
DnaRef::from_two_children(x, y),
333
+
impl Add for DnaRef {
334
+
type Output = Self;
336
+
fn add(self, other: Self) -> Self {
337
+
match add_helper(self, other) {
338
+
AddState::One(a) => a,
339
+
AddState::Two(a, b) => Self::from_two_children(a, b),
···
let lhs = DnaRef::from_string("ABCD");
let rhs = DnaRef::from_string("WXYZ");
370
-
DnaRef::concat(lhs, rhs).iter().collect::<String>(),
374
+
(lhs + rhs).iter().collect::<String>(),