1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::collections::HashMap;
use std::hash::Hash;
use record::Commands;
use {Command, Error, Record, Stack};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Group<K: Hash + Eq, V> {
map: HashMap<K, V>,
active: Option<K>,
}
impl<K: Hash + Eq, V> Group<K, V> {
#[inline]
pub fn new() -> Group<K, V> {
Group {
map: HashMap::new(),
active: None,
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> Group<K, V> {
Group {
map: HashMap::with_capacity(capacity),
active: None,
}
}
#[inline]
pub fn capacity(&self) -> usize {
self.map.capacity()
}
#[inline]
pub fn len(&self) -> usize {
self.map.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
#[inline]
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
self.map.insert(k, v)
}
#[inline]
pub fn remove(&mut self, k: &K) -> Option<V> {
self.map.remove(k)
}
#[inline]
pub fn get(&self) -> Option<&V> {
self.active.as_ref().and_then(|active| self.map.get(active))
}
#[inline]
pub fn set<T: Into<Option<K>>>(&mut self, k: T) -> bool {
match k.into() {
Some(ref k) if !self.map.contains_key(k) => false,
k => {
self.active = k;
true
}
}
}
}
impl<K: Hash + Eq, R, C: Command<R>> Group<K, Stack<R, C>> {
#[inline]
pub fn push(&mut self, cmd: C) -> Option<Result<(), Error<R, C>>> {
let map = &mut self.map;
self.active
.as_ref()
.and_then(|active| map.get_mut(active))
.map(move |stack| stack.push(cmd))
}
#[inline]
pub fn pop(&mut self) -> Option<Result<C, Error<R, C>>> {
let map = &mut self.map;
self.active
.as_ref()
.and_then(|active| map.get_mut(active))
.and_then(|stack| stack.pop())
}
}
impl<'a, K: Hash + Eq, R, C: Command<R>> Group<K, Record<'a, R, C>> {
#[inline]
pub fn is_clean(&self) -> Option<bool> {
self.get().map(|record| record.is_clean())
}
#[inline]
pub fn is_dirty(&self) -> Option<bool> {
self.is_clean().map(|is_clean| !is_clean)
}
#[inline]
pub fn push(&mut self, cmd: C) -> Option<Result<Commands<C>, Error<R, C>>> {
let map = &mut self.map;
self.active
.as_ref()
.and_then(|active| map.get_mut(active))
.map(move |record| record.push(cmd))
}
#[inline]
pub fn redo(&mut self) -> Option<Result<(), C::Err>> {
let map = &mut self.map;
self.active
.as_ref()
.and_then(|active| map.get_mut(active))
.and_then(|record| record.redo())
}
#[inline]
pub fn undo(&mut self) -> Option<Result<(), C::Err>> {
let map = &mut self.map;
self.active
.as_ref()
.and_then(|active| map.get_mut(active))
.and_then(|record| record.undo())
}
}
impl<K: Hash + Eq, V> Default for Group<K, V> {
#[inline]
fn default() -> Group<K, V> {
Group::new()
}
}