-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpersistent_list_map.rs
316 lines (301 loc) · 9.71 KB
/
persistent_list_map.rs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! For very small Persistent maps; this is a persistent map implemented upon
//! persistent lists, which already can share structure. In this case, each
//! element of the list is a key-value pair (so this is very similar to implementing
//! a persistent associative structure in Elisp with alists)
//!
//! (MapEntry :a 1)
//! / \
//! / \
//!(MapEntry :b 2) (MapEntry :b 3)
//! / \
//! a b
//! -------------------
//! a => {:a 1 :b 2}
//! b => {:a 1 :b 3}
use crate::maps::MapEntry;
use crate::value::Value;
use crate::traits;
use std::collections::HashMap;
use std::convert::From;
use std::fmt;
use std::iter::FromIterator;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum PersistentListMap {
Map(Rc<PersistentListMap>, MapEntry),
Empty,
}
impl Eq for PersistentListMap {}
/// map_entry!("doc", "this is a docstring");
#[macro_export]
macro_rules! map_entry {
($key:expr, $value:expr) => {{
MapEntry {
key: Keyword::intern($key).to_rc_value(),
val: $value.to_rc_value(),
}
}};
}
/// persistent_list_map!(map_entry!("key1", "value1"), map_entry!("key2", "value2"));
#[macro_export]
macro_rules! persistent_list_map {
($($kv:expr),*) => {
{
let mut temp_vec = Vec::new();
$(
temp_vec.push($kv);
)*
temp_vec.into_iter().collect::<PersistentListMap>()
}
};
{$($key:expr => $val:expr),*} => {
{
let mut temp_vec = Vec::new();
$(
temp_vec.push(map_entry!($key,$val));
)*
temp_vec.into_iter().collect::<PersistentListMap>()
}
};
}
/// Just like conj in Clojure, conj allows you to conjoin a new mapentry onto a map
/// although currently, that is all it allows
/// conj!(base_meta(name, ns), map_entry!("key1", "value1"), map_entry!("key2", "value2"));
#[macro_export]
macro_rules! conj {
( $plistmap:expr, $($kv:expr), *) => {
{
let mut temp_plistmap_as_vec = $plistmap.clone().iter().collect::<Vec<MapEntry>>();
$(
temp_plistmap_as_vec.push($kv);
)*
temp_plistmap_as_vec.into_iter().collect::<PersistentListMap>()
}
};
}
/// merge!(persistent_list_map!{"a" => 1 "b" => 2}
/// persistent_list_map!{"b" => 3}
/// persistent_list_map!{"d" => 5 "e" => 7}
/// ), ;
#[macro_export]
macro_rules! merge {
( $plistmap:expr, $($map:expr), *) => {
{
let mut temp_plistmap_as_vec = $plistmap.clone().iter().collect::<Vec<MapEntry>>();
$(
let next_map_as_vec = $map.clone().iter().collect::<Vec<MapEntry>>();
temp_plistmap_as_vec.extend_from_slice(&next_map_as_vec);
)*
temp_plistmap_as_vec.into_iter().collect::<PersistentListMap>()
}
};
}
// @TODO put note on IBlah traits in doc
/// A PersistentListMap.
pub trait IPersistentMap {
fn get(&self, key: &Rc<Value>) -> Rc<Value>;
fn assoc(&self, key: Rc<Value>, value: Rc<Value>) -> Self;
fn contains_key(&self,key: &Rc<Value>) -> bool;
}
impl IPersistentMap for PersistentListMap {
// @TODO make fn of ILookup
fn get(&self, key: &Rc<Value>) -> Rc<Value> {
match self {
PersistentListMap::Map(parent, entry) => {
if entry.key == *key {
return Rc::clone(&entry.val);
}
parent.get(key)
}
PersistentListMap::Empty => Rc::new(Value::Nil),
}
}
fn assoc(&self, key: Rc<Value>, val: Rc<Value>) -> PersistentListMap {
PersistentListMap::Map(Rc::new(self.clone()), MapEntry { key, val })
}
fn contains_key(&self,key: &Rc<Value>) -> bool {
match self {
PersistentListMap::Map(parent, entry) => {
if entry.key == *key {
return true;
}
parent.contains_key(key)
},
PersistentListMap::Empty => false
}
}
}
impl IPersistentMap for Rc<PersistentListMap> {
// @TODO make fn of ILookup
fn get(&self, key: &Rc<Value>) -> Rc<Value> {
match &**self {
PersistentListMap::Map(parent, entry) => {
if entry.key == *key {
return Rc::clone(&entry.val);
}
parent.get(key)
}
PersistentListMap::Empty => Rc::new(Value::Nil),
}
}
fn assoc(&self, key: Rc<Value>, val: Rc<Value>) -> Rc<PersistentListMap> {
Rc::new(PersistentListMap::Map(
Rc::clone(self),
MapEntry { key, val },
))
}
fn contains_key(&self,key: &Rc<Value>) -> bool {
match &**self {
PersistentListMap::Map(parent, entry) => {
if entry.key == *key {
return true;
}
parent.contains_key(key)
},
PersistentListMap::Empty => false
}
}
}
// The purpose of these functions are no longer to implement conversion,
// but to give us a cleaner way to invoke it
pub trait ToPersistentListMap {
fn into_list_map(self) -> PersistentListMap;
}
impl<T> ToPersistentListMap for T
where
T: Into<PersistentListMap>,
{
fn into_list_map(self) -> PersistentListMap {
Into::<PersistentListMap>::into(self)
}
}
impl From<Vec<MapEntry>> for PersistentListMap {
fn from(item: Vec<MapEntry>) -> Self {
item.into_iter().collect::<PersistentListMap>()
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Iterating
//
////////////////////////////////////////////////////////////////////////////////////////////////////
pub struct PersistentListMapIter {
node: Rc<PersistentListMap>,
seen: HashMap<Rc<Value>, bool>,
}
pub trait ToPersistentListMapIter {
fn iter(&self) -> PersistentListMapIter;
}
impl Iterator for PersistentListMapIter {
type Item = MapEntry;
fn next(&mut self) -> Option<Self::Item> {
match &*(Rc::clone(&self.node)) {
PersistentListMap::Map(parent, mapentry) => {
self.node = Rc::clone(parent);
if self.seen.contains_key(&mapentry.key) {
return self.next();
}
self.seen.insert(mapentry.key.clone(), true);
Some(mapentry.clone())
}
PersistentListMap::Empty => None,
}
}
}
impl ToPersistentListMapIter for Rc<PersistentListMap> {
fn iter(&self) -> PersistentListMapIter {
PersistentListMapIter {
node: Rc::clone(self),
seen: HashMap::new(),
}
}
}
impl ToPersistentListMapIter for PersistentListMap {
fn iter(&self) -> PersistentListMapIter {
Rc::new(self.clone()).iter()
}
}
impl FromIterator<MapEntry> for PersistentListMap {
fn from_iter<I: IntoIterator<Item = MapEntry>>(iter: I) -> Self {
let mut map_so_far = PersistentListMap::Empty;
for i in iter {
map_so_far = PersistentListMap::Map(Rc::new(map_so_far), i.clone());
}
map_so_far
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// End Iteration
////////////////////////////////////////////////////////////////////////////////////////////////////
impl traits::IMeta for PersistentListMap {
fn meta(&self) -> PersistentListMap {
// @TODO implement
PersistentListMap::Empty
}
}
impl traits::IObj for PersistentListMap {
fn with_meta(&self,meta: PersistentListMap) -> PersistentListMap {
// @TODO implement
self.clone()
}
}
impl fmt::Display for PersistentListMap {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut as_str = String::from("{");
let mut first_loop = true;
for mapentry in self.iter() {
if !first_loop {
as_str.push_str(", ");
}
first_loop = false;
as_str.push_str(&format!(
"{} {}",
mapentry.key.to_string_explicit(),
mapentry.val.to_string_explicit()
));
}
as_str.push_str("}");
write!(f, "{}", as_str)
}
}
#[cfg(test)]
mod tests {
use crate::persistent_list_map::*;
use crate::symbol::Symbol;
use crate::keyword::Keyword;
use crate::value::ToValue;
#[test]
fn persistent_list_map() {
let map1 = vec![
MapEntry {
key: Symbol::intern("a").to_rc_value(),
val: 15_i32.to_rc_value(),
},
MapEntry {
key: Symbol::intern("b").to_rc_value(),
val: "stuff".to_rc_value(),
},
]
.into_iter()
.collect::<PersistentListMap>();
println!("{}", map1);
let map2 = map1.assoc(Symbol::intern("c").to_rc_value(), 100_i32.to_rc_value());
println!("{}", map1);
println!("{}", map2);
let map3 = map1.assoc(Symbol::intern("a").to_rc_value(), 100_i32.to_rc_value());
println!("{}", map1);
println!("{}", map2);
println!("{}", map3);
let map4 = map2.assoc(Symbol::intern("a").to_rc_value(), 100_i32.to_rc_value());
println!("{}", map1);
println!("{}", map2);
println!("{}", map3);
println!("{}", map4);
}
#[test]
fn contains_key() {
let map1 = persistent_list_map!{ "a" => 12, "b" => 13 };
assert!(map1.contains_key(&Keyword::intern("a").to_rc_value()));
assert!(map1.contains_key(&Keyword::intern("b").to_rc_value()));
assert!(!map1.contains_key(&Keyword::intern("c").to_rc_value()));
}
}