Skip to content

Commit d042ce2

Browse files
committed
add a snapshottable hashmap
1 parent 5cff88f commit d042ce2

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed

src/librustc_data_structures/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod bitvec;
4242
pub mod graph;
4343
pub mod ivar;
4444
pub mod obligation_forest;
45+
pub mod snapshot_map;
4546
pub mod snapshot_vec;
4647
pub mod transitive_relation;
4748
pub mod unify;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use fnv::FnvHashMap;
12+
use std::hash::Hash;
13+
use std::ops;
14+
15+
#[cfg(test)]
16+
mod test;
17+
18+
pub struct SnapshotMap<K, V>
19+
where K: Hash + Clone + Eq
20+
{
21+
map: FnvHashMap<K, V>,
22+
undo_log: Vec<UndoLog<K, V>>,
23+
}
24+
25+
pub struct Snapshot {
26+
len: usize
27+
}
28+
29+
enum UndoLog<K, V> {
30+
OpenSnapshot,
31+
CommittedSnapshot,
32+
Inserted(K),
33+
Overwrite(K, V),
34+
}
35+
36+
impl<K, V> SnapshotMap<K, V>
37+
where K: Hash + Clone + Eq
38+
{
39+
pub fn new() -> Self {
40+
SnapshotMap {
41+
map: FnvHashMap(),
42+
undo_log: vec![]
43+
}
44+
}
45+
46+
pub fn insert(&mut self, key: K, value: V) -> bool {
47+
match self.map.insert(key.clone(), value) {
48+
None => {
49+
if !self.undo_log.is_empty() {
50+
self.undo_log.push(UndoLog::Inserted(key));
51+
}
52+
true
53+
}
54+
Some(old_value) => {
55+
if !self.undo_log.is_empty() {
56+
self.undo_log.push(UndoLog::Overwrite(key, old_value));
57+
}
58+
false
59+
}
60+
}
61+
}
62+
63+
pub fn remove(&mut self, key: K) -> bool {
64+
match self.map.remove(&key) {
65+
Some(old_value) => {
66+
if !self.undo_log.is_empty() {
67+
self.undo_log.push(UndoLog::Overwrite(key, old_value));
68+
}
69+
true
70+
}
71+
None => {
72+
false
73+
}
74+
}
75+
}
76+
77+
pub fn get(&self, key: &K) -> Option<&V> {
78+
self.map.get(key)
79+
}
80+
81+
pub fn snapshot(&mut self) -> Snapshot {
82+
self.undo_log.push(UndoLog::OpenSnapshot);
83+
let len = self.undo_log.len() - 1;
84+
Snapshot { len: len }
85+
}
86+
87+
fn assert_open_snapshot(&self, snapshot: &Snapshot) {
88+
assert!(snapshot.len < self.undo_log.len());
89+
assert!(match self.undo_log[snapshot.len] {
90+
UndoLog::OpenSnapshot => true,
91+
_ => false
92+
});
93+
}
94+
95+
pub fn commit(&mut self, snapshot: Snapshot) {
96+
self.assert_open_snapshot(&snapshot);
97+
if snapshot.len == 0 {
98+
// The root snapshot.
99+
self.undo_log.truncate(0);
100+
} else {
101+
self.undo_log[snapshot.len] = UndoLog::CommittedSnapshot;
102+
}
103+
}
104+
105+
pub fn rollback_to(&mut self, snapshot: Snapshot) {
106+
self.assert_open_snapshot(&snapshot);
107+
while self.undo_log.len() > snapshot.len + 1 {
108+
match self.undo_log.pop().unwrap() {
109+
UndoLog::OpenSnapshot => {
110+
panic!("cannot rollback an uncommitted snapshot");
111+
}
112+
113+
UndoLog::CommittedSnapshot => { }
114+
115+
UndoLog::Inserted(key) => {
116+
self.map.remove(&key);
117+
}
118+
119+
UndoLog::Overwrite(key, old_value) => {
120+
self.map.insert(key, old_value);
121+
}
122+
}
123+
}
124+
125+
let v = self.undo_log.pop().unwrap();
126+
assert!(match v { UndoLog::OpenSnapshot => true, _ => false });
127+
assert!(self.undo_log.len() == snapshot.len);
128+
}
129+
}
130+
131+
impl<'k, K, V> ops::Index<&'k K> for SnapshotMap<K, V>
132+
where K: Hash + Clone + Eq
133+
{
134+
type Output = V;
135+
fn index(&self, key: &'k K) -> &V {
136+
&self.map[key]
137+
}
138+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use super::SnapshotMap;
12+
13+
#[test]
14+
fn basic() {
15+
let mut map = SnapshotMap::new();
16+
map.insert(22, "twenty-two");
17+
let snapshot = map.snapshot();
18+
map.insert(22, "thirty-three");
19+
assert_eq!(map[&22], "thirty-three");
20+
map.insert(44, "fourty-four");
21+
assert_eq!(map[&44], "fourty-four");
22+
assert_eq!(map.get(&33), None);
23+
map.rollback_to(snapshot);
24+
assert_eq!(map[&22], "twenty-two");
25+
assert_eq!(map.get(&33), None);
26+
assert_eq!(map.get(&44), None);
27+
}
28+
29+
#[test]
30+
#[should_panic]
31+
fn out_of_order() {
32+
let mut map = SnapshotMap::new();
33+
map.insert(22, "twenty-two");
34+
let snapshot1 = map.snapshot();
35+
let _snapshot2 = map.snapshot();
36+
map.rollback_to(snapshot1);
37+
}
38+
39+
#[test]
40+
fn nested_commit_then_rollback() {
41+
let mut map = SnapshotMap::new();
42+
map.insert(22, "twenty-two");
43+
let snapshot1 = map.snapshot();
44+
let snapshot2 = map.snapshot();
45+
map.insert(22, "thirty-three");
46+
map.commit(snapshot2);
47+
assert_eq!(map[&22], "thirty-three");
48+
map.rollback_to(snapshot1);
49+
assert_eq!(map[&22], "twenty-two");
50+
}

0 commit comments

Comments
 (0)