-
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.
- Loading branch information
Showing
5 changed files
with
49 additions
and
7 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
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
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,2 +1,5 @@ | ||
pub mod queue; | ||
pub use self::queue::Queue; | ||
|
||
pub mod stack; | ||
pub use self::stack::Stack; |
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,23 +1,27 @@ | ||
use std::collections::VecDeque; | ||
|
||
/// A one-ended queue. | ||
/// This is just a wrapper around deque so nobody pushes or pops the wrong end. | ||
/// A one-ended queue. See also [Stack]. | ||
/// This is just a wrapper around vec so nobody pushes or pops the wrong end. | ||
pub struct Queue<T> { | ||
deque: VecDeque<T>, | ||
items: Vec<T>, | ||
} | ||
|
||
impl<T> Queue<T> { | ||
pub fn new() -> Self { | ||
Queue { | ||
deque: VecDeque::with_capacity(16), | ||
items: Vec::with_capacity(16), | ||
} | ||
} | ||
|
||
pub fn push(&mut self, value: T) { | ||
self.deque.push_back(value) | ||
self.items.push(value) | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<T> { | ||
self.deque.pop_front() | ||
self.items.pop() | ||
} | ||
|
||
/// Moves all the elements from a vector into the queue. | ||
pub fn append(&mut self, mut other: Vec<T>) { | ||
self.items.append(&mut other); | ||
} | ||
} |
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,27 @@ | ||
use std::collections::VecDeque; | ||
|
||
/// A one-ended stack. See also [Queue]. | ||
/// This is just a wrapper around deque so nobody pushes or pops the wrong end. | ||
pub struct Stack<T> { | ||
items: VecDeque<T>, | ||
} | ||
|
||
impl<T> Stack<T> { | ||
pub fn new() -> Self { | ||
Stack { | ||
items: VecDeque::with_capacity(16), | ||
} | ||
} | ||
|
||
pub fn push(&mut self, value: T) { | ||
self.items.push_back(value) | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<T> { | ||
self.items.pop_back() | ||
} | ||
|
||
pub fn borrow_mut(&mut self) -> Option<&mut T> { | ||
self.items.back_mut() | ||
} | ||
} |