|
| 1 | +//! For very small Persistent maps; this is a persistent map implemented upon |
| 2 | +//! persistent lists, which already can share structure. In this case, each |
| 3 | +//! element of the list is a key-value pair (so this is very similar to implementing |
| 4 | +//! a persistent associative structure in Elisp with alists) |
| 5 | +//! |
| 6 | +//! (MapEntry :a 1) |
| 7 | +//! / \ |
| 8 | +//! / \ |
| 9 | +//!(MapEntry :b 2) (MapEntry :b 3) |
| 10 | +//! / \ |
| 11 | +//! a b |
| 12 | +//! ------------------- |
| 13 | +//! a => {:a 1 :b 2} |
| 14 | +//! b => {:a 1 :b 3} |
| 15 | +
|
| 16 | +use crate::maps::MapEntry; |
| 17 | +use crate::value::Value; |
| 18 | +use crate::symbol::Symbol; |
| 19 | + |
| 20 | +use std::collections::HashMap; |
| 21 | +use std::rc::Rc; |
| 22 | +use std::fmt; |
| 23 | +use std::iter::FromIterator; |
| 24 | +use std::convert::From; |
| 25 | + |
| 26 | + |
| 27 | +#[derive(Debug,Clone,PartialEq,Hash)] |
| 28 | +pub enum PersistentListMap { |
| 29 | + Map(Rc<PersistentListMap>,MapEntry), |
| 30 | + Empty |
| 31 | +} |
| 32 | +// Again, only using strange IBlah convention to reflect the Clojure base |
| 33 | +// @TODO really though .. just rethink this |
| 34 | +/// A PersistentListMap. |
| 35 | +pub trait IPersistentListMap { |
| 36 | + fn get(&self,key: &Rc<Value>) -> Rc<Value>; |
| 37 | + fn assoc(&self,key: Rc<Value>, value: Rc<Value>) -> Self; |
| 38 | +} |
| 39 | +impl IPersistentListMap for PersistentListMap { |
| 40 | + // @TODO make fn of ILookup |
| 41 | + fn get(&self,key: &Rc<Value>) -> Rc<Value> { |
| 42 | + match self { |
| 43 | + PersistentListMap::Map(parent,entry) => { |
| 44 | + if entry.key == *key { |
| 45 | + return Rc::clone(&entry.val); |
| 46 | + } |
| 47 | + parent.get(key) |
| 48 | + }, |
| 49 | + PersistentListMap::Empty => Rc::new(Value::Nil) |
| 50 | + } |
| 51 | + } |
| 52 | + fn assoc(&self,key: Rc<Value>, val: Rc<Value>) -> PersistentListMap { |
| 53 | + PersistentListMap::Map(Rc::new(self.clone()),MapEntry{key,val}) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl IPersistentListMap for Rc<PersistentListMap> { |
| 58 | + // @TODO make fn of ILookup |
| 59 | + fn get(&self,key: &Rc<Value>) -> Rc<Value> { |
| 60 | + match &**self { |
| 61 | + PersistentListMap::Map(parent,entry) => { |
| 62 | + if entry.key == *key { |
| 63 | + return Rc::clone(&entry.val); |
| 64 | + } |
| 65 | + parent.get(key) |
| 66 | + }, |
| 67 | + PersistentListMap::Empty => Rc::new(Value::Nil) |
| 68 | + } |
| 69 | + } |
| 70 | + fn assoc(&self,key: Rc<Value>, val: Rc<Value>) -> Rc<PersistentListMap> { |
| 71 | + Rc::new(PersistentListMap::Map(Rc::clone(self),MapEntry{key,val})) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// The purpose of these functions are no longer to implement conversion, |
| 76 | +// but to give us a cleaner way to invoke it |
| 77 | +pub trait ToPersistentListMap { |
| 78 | + fn into_list_map(self) -> PersistentListMap; |
| 79 | +} |
| 80 | +impl<T> ToPersistentListMap for T where |
| 81 | + T: Into<PersistentListMap> |
| 82 | +{ |
| 83 | + fn into_list_map(self) -> PersistentListMap { |
| 84 | + Into::<PersistentListMap>::into(self) |
| 85 | + } |
| 86 | +} |
| 87 | +impl From<Vec<MapEntry>> for PersistentListMap { |
| 88 | + fn from(item: Vec<MapEntry>) -> Self { |
| 89 | + item.into_iter().collect::<PersistentListMap>() |
| 90 | + } |
| 91 | +} |
| 92 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 93 | +// Iterating |
| 94 | +// |
| 95 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 96 | +pub struct PersistentListMapIter { |
| 97 | + node: Rc<PersistentListMap>, |
| 98 | + seen: HashMap<Rc<Value>,bool> |
| 99 | +} |
| 100 | +pub trait ToPersistentListMapIter { |
| 101 | + fn iter(&self) -> PersistentListMapIter; |
| 102 | +} |
| 103 | +impl Iterator for PersistentListMapIter { |
| 104 | + type Item = MapEntry; |
| 105 | + fn next(&mut self) -> Option<Self::Item> { |
| 106 | + match &*(Rc::clone(&self.node)) { |
| 107 | + PersistentListMap::Map(parent,mapentry) => { |
| 108 | + self.node = Rc::clone(parent); |
| 109 | + if self.seen.contains_key(&mapentry.key) { |
| 110 | + return self.next(); |
| 111 | + } |
| 112 | + self.seen.insert(mapentry.key.clone(),true); |
| 113 | + Some(mapentry.clone()) |
| 114 | + }, |
| 115 | + PersistentListMap::Empty => None |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl ToPersistentListMapIter for Rc<PersistentListMap> { |
| 121 | + fn iter(&self) -> PersistentListMapIter { |
| 122 | + PersistentListMapIter { |
| 123 | + node: Rc::clone(self), |
| 124 | + seen: HashMap::new() |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +impl ToPersistentListMapIter for PersistentListMap { |
| 129 | + fn iter(&self) -> PersistentListMapIter { |
| 130 | + Rc::new(self.clone()).iter() |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl FromIterator<MapEntry> for PersistentListMap { |
| 135 | + fn from_iter<I: IntoIterator<Item=MapEntry>>(iter: I) -> Self { |
| 136 | + let mut map_so_far = PersistentListMap::Empty; |
| 137 | + |
| 138 | + for i in iter { |
| 139 | + map_so_far = PersistentListMap::Map(Rc::new(map_so_far),i.clone()); |
| 140 | + } |
| 141 | + map_so_far |
| 142 | + } |
| 143 | +} |
| 144 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 145 | +// End Iteration |
| 146 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 147 | + |
| 148 | +impl fmt::Display for PersistentListMap { |
| 149 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 150 | + let mut as_str = String::from("{"); |
| 151 | + let mut first_loop = true; |
| 152 | + |
| 153 | + for mapentry in self.iter() { |
| 154 | + if !first_loop { |
| 155 | + as_str.push_str(", "); |
| 156 | + } |
| 157 | + first_loop = false; |
| 158 | + as_str.push_str(&format!("{} {}",mapentry.key.to_string_explicit(),mapentry.val.to_string_explicit())); |
| 159 | + } |
| 160 | + as_str.push_str("}"); |
| 161 | + |
| 162 | + write!(f, "{}",as_str) |
| 163 | + } |
| 164 | +} |
| 165 | +#[cfg(test)] |
| 166 | +mod tests { |
| 167 | + use crate::persistent_list_map::*; |
| 168 | + use crate::value::ToValue; |
| 169 | + #[test] |
| 170 | + fn test_persistent_list_map() |
| 171 | + { |
| 172 | + let empty = PersistentListMap::Empty; |
| 173 | + let map1 = vec![MapEntry { key: Symbol::intern("a").to_value(), val: 15_i32.to_rc_value()}, |
| 174 | + MapEntry { key: Symbol::intern("b").to_value(), val: "stuff".to_rc_value()}].into_iter().collect::<PersistentListMap>(); |
| 175 | + println!("{}",map1); |
| 176 | + let map2 = map1.assoc(Symbol::intern("c").to_value(),100_i32.to_rc_value()); |
| 177 | + println!("{}",map1); |
| 178 | + println!("{}",map2); |
| 179 | + let map3 = map1.assoc(Symbol::intern("a").to_value(),100_i32.to_rc_value()); |
| 180 | + println!("{}",map1); |
| 181 | + println!("{}",map2); |
| 182 | + println!("{}",map3); |
| 183 | + let map4 = map2.assoc(Symbol::intern("a").to_value(),100_i32.to_rc_value()); |
| 184 | + println!("{}",map1); |
| 185 | + println!("{}",map2); |
| 186 | + println!("{}",map3); |
| 187 | + println!("{}",map4); |
| 188 | + } |
| 189 | +} |
0 commit comments