-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize adjacency list data structure (#28)
* minor: sort should use the unstable version instead * use smaller data structure to store adjacency * bump version * remove unused lifetime annotations * addd unit test for List * minor
- Loading branch information
Showing
4 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "quranize" | ||
version = "0.10.1" | ||
version = "0.10.2" | ||
authors = ["Alfan Nur Fauzan <[email protected]>"] | ||
edition = "2021" | ||
description = "Encoding transliterations into Quran forms." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#[derive(Default)] | ||
pub enum List<T> { | ||
#[default] | ||
Empty, | ||
Cons(T, Box<List<T>>), | ||
} | ||
|
||
impl<T> List<T> { | ||
pub fn push(&mut self, e: T) { | ||
*self = List::Cons(e, Box::new(std::mem::take(self))); | ||
} | ||
|
||
pub fn iter(&self) -> Iter<T> { | ||
Iter { list: self } | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn len(&self) -> usize { | ||
self.iter().count() | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn is_empty(&self) -> bool { | ||
matches!(self, Self::Empty) | ||
} | ||
} | ||
|
||
pub struct Iter<'a, T> { | ||
list: &'a List<T>, | ||
} | ||
|
||
impl<'a, T> Iterator for Iter<'a, T> { | ||
type Item = &'a T; | ||
fn next(&mut self) -> Option<Self::Item> { | ||
match self.list { | ||
List::Cons(head, tail) => { | ||
self.list = tail; | ||
Some(head) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_list() { | ||
let mut list: List<u8> = Default::default(); | ||
assert!(list.is_empty()); | ||
list.push(1); | ||
list.push(2); | ||
assert_eq!(list.len(), 2); | ||
let mut iter = list.iter(); | ||
assert_eq!(iter.next(), Some(&2)); | ||
assert_eq!(iter.next(), Some(&1)); | ||
assert_eq!(iter.next(), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod list; | ||
pub use list::List; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters