#stack #scoped

scoped_stack

作用域堆栈数据结构

12个稳定版本

1.2.1 2023年9月12日
1.2.0 2023年9月10日
1.1.2 2023年8月18日
1.0.3 2022年12月10日

#661数据结构

Download history 59/week @ 2024-03-12 5/week @ 2024-04-02

84 每月下载量

MIT 许可证

11KB
216

scoped_stack

scoped_stack 是一个Rust库,提供可作用域的堆栈。每个 ScopedStack 有一个值的 HashMap 和一个子 ScopedStack。当作用域被推入时,会创建一个新的 ScopedStack 作为其父堆栈。当作用域被弹出时,最顶层的 ScopedStack 被移除。当值被推入时,它被添加到最顶层的 ScopedStack。当值被弹出时,它从具有该键的最顶层 ScopedStack 中移除。当查找值时,返回具有该键的最顶层 ScopedStack 的值。

示例

use scoped_stack::ScopedStack;

fn main() {
  let mut stack = ScopedStack::<&str, i32>::new();

  stack.insert("a", 1);
  stack.insert("b", 2);
  stack.insert("c", 3);

  assert_eq!(stack.get(&"a"), Some(&1));
  assert_eq!(stack.get(&"b"), Some(&2));
  assert_eq!(stack.get(&"c"), Some(&3));

  stack.push_scope();

  stack.insert("a", 4);
  stack.insert("b", 5);

  assert_eq!(stack.get(&"a"), Some(&4));
  assert_eq!(stack.get(&"b"), Some(&5));
  assert_eq!(stack.get(&"c"), Some(&3));

  stack.pop_scope();

  assert_eq!(stack.get(&"a"), Some(&1));
  assert_eq!(stack.get(&"b"), Some(&2));
  assert_eq!(stack.get(&"c"), Some(&3));
}

无运行时依赖