Skip to content

Commit

Permalink
stack: Impl more traits
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Aug 1, 2024
1 parent 57afdbb commit 452347a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
26 changes: 26 additions & 0 deletions stack/src/array_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// in the LICENSE file.

use std::cmp::Ordering;
use std::fmt;
use std::fmt::Formatter;
use std::hash::{Hash, Hasher};

/// 使用数组实现静态栈结构
Expand Down Expand Up @@ -114,6 +116,12 @@ impl<T> FromIterator<T> for ArrayStack<T> {
}
}

impl<T: fmt::Debug> fmt::Debug for ArrayStack<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.buf, f)
}
}

#[cfg(test)]
mod tests {
use std::mem::size_of;
Expand Down Expand Up @@ -158,4 +166,22 @@ mod tests {
let ret = stack.pop();
assert_eq!(ret, None);
}

#[test]
fn test_iter() {
let numbers = [1, 1, 2, 3, 5, 8];
let mut stack = ArrayStack::from_iter(numbers);
assert_eq!(stack.len(), numbers.len());

let ret = stack.pop();
assert_eq!(ret, Some(8));
}

#[test]
fn test_eq() {
let numbers = [1, 1, 2, 3, 5, 8];
let stack1 = ArrayStack::from_iter(numbers);
let stack2 = ArrayStack::from_iter(numbers);
assert_eq!(stack1, stack2);
}
}
63 changes: 63 additions & 0 deletions stack/src/vec_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use std::cmp::Ordering;
use std::fmt;
use std::fmt::Formatter;
use std::hash::{Hash, Hasher};

pub struct VecStack<T: Sized>(Vec<T>);

impl<T> Default for VecStack<T> {
Expand Down Expand Up @@ -69,6 +74,46 @@ impl<T> VecStack<T> {
}
}


impl<T: PartialEq> PartialEq for VecStack<T> {
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(&self.0, &other.0)
}
}

impl<T: Eq> Eq for VecStack<T> {}

impl<T: PartialOrd> PartialOrd for VecStack<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
PartialOrd::partial_cmp(&self.0, &other.0)
}
}

impl<T: Ord> Ord for VecStack<T> {
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.0, &other.0)
}
}

impl<T: Hash> Hash for VecStack<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&self.0, state);
}
}

impl<T> FromIterator<T> for VecStack<T> {
fn from_iter<U: IntoIterator<Item=T>>(iter: U) -> Self {
let vec: Vec<T> = iter.into_iter().collect();
Self(vec)
}
}

impl<T: fmt::Debug> fmt::Debug for VecStack<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}

#[cfg(test)]
mod tests {
use crate::vec_stack::VecStack;
Expand Down Expand Up @@ -105,4 +150,22 @@ mod tests {
let ret = stack.pop();
assert_eq!(ret, None);
}

#[test]
fn test_iter() {
let numbers = [1, 1, 2, 3, 5, 8];
let mut stack = VecStack::from_iter(numbers);
assert_eq!(stack.len(), numbers.len());

let ret = stack.pop();
assert_eq!(ret, Some(8));
}

#[test]
fn test_eq() {
let numbers = [1, 1, 2, 3, 5, 8];
let stack1 = VecStack::from_iter(numbers);
let stack2 = VecStack::from_iter(numbers);
assert_eq!(stack1, stack2);
}
}

0 comments on commit 452347a

Please sign in to comment.