Skip to content

Commit 4a0895d

Browse files
committed
WHOLE: refactor into chapters
1 parent 0e8287c commit 4a0895d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+126
-131
lines changed

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ adivon = "*"
1818
byteorder = "*"
1919
mtl = "*"
2020

21+
22+
# [dev-dependencies.piston_window]
23+
# piston_window = "*"
24+
2125
[dev-dependencies.sdl2]
22-
sdl2 = "0.6.2"
26+
sdl2 = "0.9"
2327

2428
[dev-dependencies.sdl2_gfx]
2529
sdl2_gfx = "0.6.2"

benches/geometric_search.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ extern crate algs4;
88
use test::Bencher;
99
use rand::{thread_rng, Rng};
1010

11-
use algs4::geometric_search::primitive::{Point2D, PointSet, RectHV};
12-
use algs4::symbol_tables::ST;
11+
use algs4::searching::ST;
12+
use algs4::fundamentals::primitive::{Point2D, PointSet, RectHV};
1313
use algs4::geometric_search::kd_tree::KdTree;
1414

1515
const SIZE: usize = 1000;

benches/sorting.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ extern crate algs4;
77

88
use test::Bencher;
99
use rand::{thread_rng, Rng};
10-
use algs4::elementary_sorts::*;
11-
use algs4::mergesort::*;
12-
use algs4::quicksort::*;
13-
use algs4::priority_queues::heapsort::heap_sort;
14-
10+
use algs4::sorting::*;
11+
use algs4::sorting::quicksort::{quick_sort_3way, quick_sort_orig};
12+
use algs4::fundamentals::knuth_shuffle;
1513

1614
static SIZE: usize = 1000;
1715
// for small array

benches/union_find.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ extern crate algs4;
77

88
use test::{black_box, Bencher};
99

10-
use algs4::union_find::UF;
11-
use algs4::union_find::quick_find;
12-
use algs4::union_find::quick_union;
13-
use algs4::union_find::weighted_quick_union;
14-
use algs4::union_find::improved;
10+
use algs4::fundamentals::union_find::*;
1511

1612
const NUM_OF_OBJECTS: usize = 100;
1713
const NUM_OF_OPERATIONS: usize = 500;

examples/8puzzle.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::io;
66
use std::fmt;
77
use std::cmp::Ordering;
88

9-
use algs4::priority_queues::MinPQ;
10-
use algs4::priority_queues::binary_heaps::BinaryHeapMinPQ;
9+
use algs4::sorting::priority_queues::MinPQ;
10+
use algs4::sorting::priority_queues::binary_heaps::BinaryHeapMinPQ;
1111

1212
#[derive(Debug, Clone, PartialEq, Eq)]
1313
pub struct Board {

examples/bouncing_balls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use sdl2::event::Event;
99
use sdl2_gfx::primitives::DrawRenderer;
1010

1111
use rand::{thread_rng, Rng};
12-
use algs4::priority_queues::event_driven_simulation::{Particle, CollisionSystem};
12+
use algs4::sorting::priority_queues::event_driven_simulation::{Particle, CollisionSystem};
1313

1414

1515
const BALLS: usize = 100;

examples/collinear.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::io;
66
use std::fmt;
77
use std::cmp::Ordering;
88

9-
use algs4::quicksort::quick_sort;
10-
use algs4::mergesort::comparator::Comparator;
11-
use algs4::mergesort::comparator::insertion_sort;
9+
use algs4::sorting::quick_sort;
10+
use algs4::sorting::comparator::Comparator;
11+
use algs4::sorting::comparator::insertion_sort;
1212

1313
#[derive(Copy, Clone)]
1414
pub struct Point {

examples/kdtree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ extern crate rand;
55
use std::io::prelude::*;
66
use std::io;
77

8-
use algs4::geometric_search::primitive::{Point2D, PointSet, RectHV};
9-
use algs4::symbol_tables::ST;
8+
use algs4::fundamentals::primitive::{Point2D, PointSet, RectHV};
9+
use algs4::searching::ST;
1010
// FIXME: fails under input1M.txt
1111
use algs4::geometric_search::kd_tree::KdTree;
1212

examples/orthogonal_line_segment_intersection.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::cmp::Ordering;
55
use rand::{Rng, Rand};
66
// use rand::thread_rng;
77

8-
use algs4::quicksort::quick_sort;
9-
use algs4::symbol_tables::ST;
10-
use algs4::symbol_tables::binary_search_tree::BST;
8+
use algs4::sorting::quick_sort;
9+
use algs4::searching::ST;
10+
use algs4::searching::binary_search_tree::BST;
1111
use algs4::geometric_search::RangeSearch1D;
1212

1313
use self::Event::*;

examples/percolation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::iter;
99
use test::stats::Stats;
1010
use rand::{thread_rng, Rng};
1111

12-
use algs4::union_find::UF;
13-
use algs4::union_find::weighted_quick_union::UnionFind;
12+
use algs4::fundamentals::union_find::UF;
13+
use algs4::fundamentals::union_find::weighted_quick_union::UnionFind;
1414

1515
/// a percolation system using an N-by-N grid of sites
1616
pub struct Percolation {

examples/subset.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::env;
44
use std::io::prelude::*;
55
use std::io;
66

7-
use algs4::stacks_and_queues::RandomizedQueue;
8-
use algs4::stacks_and_queues::resizing_array_randomized_queue::ResizingArrayRandomizedQueue;
7+
use algs4::fundamentals::stacks_and_queues::RandomizedQueue;
8+
use algs4::fundamentals::stacks_and_queues::resizing_array_randomized_queue::ResizingArrayRandomizedQueue;
99

1010

1111
fn main() {

examples/union_find.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ extern crate algs4;
44
use std::io::prelude::*;
55
use std::io;
66

7-
use algs4::union_find::UF;
8-
use algs4::union_find::quick_find;
9-
use algs4::union_find::quick_union;
10-
use algs4::union_find::weighted_quick_union;
11-
use algs4::union_find::improved;
7+
use algs4::fundamentals::union_find::UF;
8+
use algs4::fundamentals::union_find::quick_find;
9+
use algs4::fundamentals::union_find::quick_union;
10+
use algs4::fundamentals::union_find::weighted_quick_union;
11+
use algs4::fundamentals::union_find::improved;
1212

1313
fn main() {
1414
let mut lines = io::BufReader::new(io::stdin()).lines();
File renamed without changes.
File renamed without changes.

src/context/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod linear_programming;
2+
pub mod maximum_flow;
3+
pub mod suffix_arrays;
File renamed without changes.

src/convex_hull.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cmp::Ordering;
22
use std::mem;
3-
use super::mergesort::comparator::Comparator;
3+
use super::sorting::comparator::Comparator;
44

55
#[allow(non_snake_case)]
66
pub struct Point2D {

src/fundamentals/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use rand::{thread_rng, Rng};
2+
3+
/// Rearranges an array of objects in uniformly random order
4+
pub fn knuth_shuffle<T>(a: &mut [T]) {
5+
let mut rng = thread_rng();
6+
let n = a.len();
7+
8+
for i in 0 .. n {
9+
let r = rng.gen_range(0, i+1);
10+
a.swap(i, r);
11+
}
12+
}
13+
14+
#[test]
15+
fn test_knuth_shuffle() {
16+
let array = thread_rng().gen_iter().take(10).collect::<Vec<f64>>();
17+
let mut new_array = array.clone();
18+
knuth_shuffle(&mut new_array);
19+
assert!(array != new_array);
20+
}
21+
22+
23+
pub mod primitive;
24+
pub mod stacks_and_queues;
25+
pub mod union_find;

src/geometric_search/primitive.rs renamed to src/fundamentals/primitive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::vec::IntoIter;
33
use std::f64;
44
use rand::{Rand, Rng};
55
use std::borrow::Borrow;
6-
use super::super::symbol_tables::ST;
7-
use super::super::balanced_search_trees::RedBlackBST;
6+
use super::super::searching::ST;
7+
use super::super::searching::red_black_tree::RedBlackBST;
88

99
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
1010
pub struct Point2D {

src/stacks_and_queues/resizing_array_stack.rs renamed to src/fundamentals/stacks_and_queues/resizing_array_stack.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
137137

138138
fn next(&mut self) -> Option<&'a T> {
139139
unsafe {
140-
if self.ptr < self.end {
140+
if self.ptr <= self.end {
141141
None
142142
} else {
143143
self.ptr = self.ptr.offset(-1);
File renamed without changes.
File renamed without changes.

src/geometric_search/interval_search_tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cmp::Ordering;
33
use std::fmt;
44
use std::iter;
55
// use std::vec::IntoIter;
6-
use super::super::symbol_tables::ST;
6+
use super::super::searching::ST;
77

88

99
pub struct Node<K: PartialOrd + Copy, V> {

src/geometric_search/kd_tree.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::vec::IntoIter;
55
use std::cmp::Ordering;
66
use std::borrow::Borrow;
77

8-
use super::super::symbol_tables::ST;
9-
use super::super::stacks_and_queues::Queue;
10-
use super::super::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
11-
use super::primitive::{Point2D, RectHV};
8+
use super::super::searching::ST;
9+
use super::super::fundamentals::stacks_and_queues::Queue;
10+
use super::super::fundamentals::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
11+
use super::super::fundamentals::primitive::{Point2D, RectHV};
1212

1313

1414
pub trait Point: Copy {

src/geometric_search/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use super::symbol_tables::{ST, OrderedST};
2-
use super::symbol_tables::binary_search_tree::{BST, Node};
1+
use super::searching::{ST, OrderedST};
2+
use super::searching::binary_search_tree::{BST, Node};
33

4-
pub mod primitive;
4+
// pub mod primitive;
55

66
pub mod kd_tree;
77

src/directed_graphs/mod.rs renamed to src/graphs/directed_graphs/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::iter;
2-
use super::stacks_and_queues::bag::Bag;
3-
use super::stacks_and_queues::{Stack, Queue};
4-
use super::stacks_and_queues::linked_stack;
5-
use super::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
2+
use super::super::fundamentals::stacks_and_queues::bag::Bag;
3+
use super::super::fundamentals::stacks_and_queues::{Stack, Queue};
4+
use super::super::fundamentals::stacks_and_queues::linked_stack;
5+
use super::super::fundamentals::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
66

77
#[derive(Clone, Debug)]
88
pub struct Digraph {

src/minimum_spanning_trees/mod.rs renamed to src/graphs/minimum_spanning_trees/mod.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ use std::cmp;
44
use std::f64;
55

66
use adivon::priority_queue::IndexMinPQ;
7-
use super::stacks_and_queues::bag::Bag;
8-
use super::stacks_and_queues::Queue;
9-
use super::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
10-
use super::priority_queues::MinPQ;
11-
use super::priority_queues::binary_heaps::BinaryHeapMinPQ;
12-
use super::union_find::UF;
13-
use super::union_find::weighted_quick_union::UnionFind;
7+
use adivon::{Bag, Queue, MinPQ};
8+
use adivon::UnionFind;
149

1510
/// a weighted edge
1611
#[derive(Clone, Copy)]
@@ -182,21 +177,21 @@ fn test_edge_weighted_graph() {
182177
pub struct KruskalMST<'a> {
183178
graph: &'a EdgeWeightedGraph,
184179
weight: f64,
185-
mst: ResizingArrayQueue<Edge>
180+
mst: Queue<Edge>
186181
}
187182

188183
impl<'a> KruskalMST<'a> {
189184
fn new<'b>(graph: &'b EdgeWeightedGraph) -> KruskalMST<'b> {
190185
let n = graph.v();
191186
let mut weight = 0f64;
192-
let mut mst = ResizingArrayQueue::<Edge>::new();
193-
let mut pq = BinaryHeapMinPQ::<Edge>::new();
187+
let mut mst = Queue::<Edge>::new();
188+
let mut pq = MinPQ::<Edge>::new();
194189
for e in graph.edges() {
195190
pq.insert(e);
196191
}
197192
let mut uf = UnionFind::new(n);
198193

199-
while !pq.is_empty() && mst.size() < n - 1 {
194+
while !pq.is_empty() && mst.len() < n - 1 {
200195
let e = pq.del_min().unwrap();
201196
let v = e.either();
202197
let w = e.other(v);
@@ -229,17 +224,17 @@ impl EdgeWeightedGraph {
229224
pub struct LazyPrimMST<'a> {
230225
graph: &'a EdgeWeightedGraph,
231226
weight: f64,
232-
mst: ResizingArrayQueue<Edge>,
227+
mst: Queue<Edge>,
233228
marked: Vec<bool>,
234-
pq: BinaryHeapMinPQ<Edge>
229+
pq: MinPQ<Edge>
235230
}
236231

237232
impl<'a> LazyPrimMST<'a> {
238233
fn new<'b>(graph: &'b EdgeWeightedGraph) -> LazyPrimMST<'b> {
239234
let n = graph.v();
240235
let marked = iter::repeat(false).take(n).collect();
241-
let pq = BinaryHeapMinPQ::new();
242-
let mst = ResizingArrayQueue::new();
236+
let pq = MinPQ::new();
237+
let mst = Queue::new();
243238

244239
let mut ret = LazyPrimMST {
245240
graph: graph,
@@ -365,7 +360,7 @@ impl<'a> PrimMST<'a> {
365360
}
366361

367362
pub fn edges(&self) -> Vec<Edge> {
368-
let mut mst = ResizingArrayQueue::new();
363+
let mut mst = Queue::new();
369364
for e in self.edge_to.iter() {
370365
e.map(|e| mst.enqueue(e.clone()));
371366
}

src/graphs/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod undirected_graphs;
2+
pub mod directed_graphs;
3+
4+
pub mod minimum_spanning_trees;
File renamed without changes.

src/undirected_graphs/mod.rs renamed to src/graphs/undirected_graphs/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::iter;
2-
use super::stacks_and_queues::bag::Bag;
3-
use super::stacks_and_queues::{Stack, Queue};
4-
use super::stacks_and_queues::linked_stack;
5-
use super::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
2+
use super::super::fundamentals::stacks_and_queues::bag::Bag;
3+
use super::super::fundamentals::stacks_and_queues::{Stack, Queue};
4+
use super::super::fundamentals::stacks_and_queues::linked_stack;
5+
use super::super::fundamentals::stacks_and_queues::resizing_array_queue::ResizingArrayQueue;
66

77
#[derive(Clone, Debug)]
88
pub struct Graph {

src/lib.rs

+7-28
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,20 @@ extern crate adivon;
66
#[macro_use]
77
extern crate mtl;
88

9-
// part1
10-
pub mod union_find;
9+
pub mod fundamentals;
1110

12-
pub mod stacks_and_queues;
13-
pub mod elementary_sorts;
11+
pub mod sorting;
1412

15-
pub mod mergesort;
16-
pub mod quicksort;
13+
pub mod searching;
1714

18-
pub mod convex_hull;
15+
pub mod graphs;
1916

20-
pub mod priority_queues;
17+
pub mod strings;
2118

22-
pub mod symbol_tables;
19+
pub mod convex_hull;
2320

24-
pub mod balanced_search_trees;
2521
pub mod geometric_search;
2622

27-
pub mod hash_tables;
2823
pub mod searching_application;
2924

30-
// part2
31-
pub mod undirected_graphs;
32-
pub mod directed_graphs;
33-
34-
pub mod minimum_spanning_trees;
35-
pub mod shortest_paths;
36-
37-
pub mod maximum_flow;
38-
pub mod radix_sorts;
39-
40-
pub mod tries;
41-
pub mod substring_search;
42-
43-
pub mod regular_expressions;
44-
pub mod data_compression;
45-
46-
pub mod linear_programming;
25+
pub mod context;
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)