8000 lists changes · ratulb/rust_programming@0a7be1c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a7be1c

Browse files
committed
lists changes

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#![forbid(unsafe_code)]
2+
use minivec::MiniVec;
23 8000
use std::cell::{Ref, RefCell, RefMut};
34
use std::cmp::Ordering;
45
use std::fmt::{Debug, Error, Formatter};
56
use std::ops::{Add, Deref, DerefMut};
67
use std::rc::Rc;
7-
use minivec::MiniVec;
88

99
type Cell<T> = Rc<RefCell<Node<T>>>;
1010
type Link<T> = Option<Cell<T>>;
@@ -285,12 +285,7 @@ impl<T: Default> LinkedList<T> {
285285
result
286286
}
287287

288-
fn partition_at_tail(
289-
&self,
290-
high: usize,
291-
ascending: bool,
292-
cells: &MiniVec<Cell<T>>,
293-
) -> usize
288+
fn partition_at_tail(&self, high: usize, ascending: bool, cells: &MiniVec<Cell<T>>) -> usize
294289
where
295290
T: PartialOrd,
296291
{
@@ -307,7 +302,10 @@ impl<T: Default> LinkedList<T> {
307302
};
308303
if less_or_eq_or_greater_or_eq {
309304
if i != j {
310-
std::mem::swap(&mut cells[i].borrow_mut().elem, &mut cells[j].borrow_mut().elem);
305+
std::mem::swap(
306+
&mut cells[i].borrow_mut().elem,
307+
&mut cells[j].borrow_mut().elem,
308+
);
311309
}
312310
i += 1;
313311
}
@@ -459,6 +457,23 @@ impl<T: Default> LinkedList<T> {
459457
self.head = previous;
460458
}
461459

460+
pub fn reverse_recursively(&mut self) {
461+
if self.len() < 2 {
462+
return;
463+
}
464+
let cell = self.head.take();
465+
Self::reverse_recursively_helper(cell);
466+
}
467+
468+
fn reverse_recursively_helper(cell: Option<Cell<T>>) {
469+
if let Some(cell) = cell {
470+
let has_next = cell.borrow().next.is_some();
471+
if has_next {
472+
Self::reverse_recursively_helper(cell.borrow_mut().next.take());
473+
}
474+
}
475+
}
476+
462477
///
463478
///Append another list to this
464479
///
@@ -765,7 +780,7 @@ impl<T: Default> LinkedList<T> {
765780
self.head.as_mut().map(|node| MutT(node.borrow_mut()))
766781
}
767782
///Quick sort - slow. Usage is advisable when list size is small
768-
< 8000 /td>783+
769784
pub fn quicksort(&self, ascending: bool)
770785
where
771786
T: PartialOrd,
@@ -775,13 +790,8 @@ impl<T: Default> LinkedList<T> {
775790
self.quicklysort(ascending, 0, self.len - 1, &minivec);
776791
}
777792

778-
fn quicklysort(
779-
&self,
780-
ascending: bool,
781-
start: usize,
782-
end: usize,
783-
cells: &MiniVec<Cell<T>>,
784-
) where
793+
fn quicklysort(&self, ascending: bool, start: usize, end: usize, cells: &MiniVec<Cell<T>>)
794+
where
785795
T: PartialOrd,
786796
{
787797
if start < end {