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
//! `Unique` association of names with unique id use std::sync::*; use clojure::lang::*; use clojure::rust::*; // use intertrait::cast::CastArc; use intertrait::cast::CastRef; use intertrait::*; use lazy_static::lazy_static; use crate::*; castable_to!(SUnique => [sync] IObject, Unique); /// # A keyword storage structure /// /// We will store all `String`s used as reference to objects as `usize`. /// * `usize` values are unique and immutable for every `String`. /// * `Strings` are added incrementally to the `vect` `ObjVector` and cannot /// be destroyed. /// /// As a `String` is added, it's index is added in the `map` `ObjHashMap`. /// /// # Examples pub struct SUnique { /// `SPersistentMap` of `name`: `String` -> `id`: `usize` pub map: Object, /// `SPersistentVector` `index`: `usize` -> `value`: `String` pub vect: Object, } unsafe impl Send for SUnique {} unsafe impl Sync for SUnique {} /// Protocole `Unique` pub trait Unique: IObject { /// Size of SStrVector fn len(&self) -> ObjResult<usize>; /// Gives name of index /// /// return None if doesn't exist fn get_name<'a>( &self, index: usize, ) -> ObjResult<String>; /// Gives index of name /// /// Create name and index is they doesn't exist fn get_or_make_index( &mut self, index: &str, ) -> ObjResult<usize>; /// Gives index of name /// /// return None if doesn't exist fn get_index( &mut self, name: &str, ) -> ObjResult<usize>; /// Tests if name exists fn test( &self, key: &str, ) -> ObjResult<bool>; } // `Implementation` of `Protocol` `Unique` for `SUnique` impl Unique for SUnique { /// Size of SStrVector fn len(&self) -> ObjResult<usize> { let v = self.get_vect().unwrap(); let v = v.cast::<Counted>().unwrap(); v.count() } /// Gives name of index /// /// return None if doesn't exist fn get_name( &self, key: usize, ) -> ObjResult<String> { let v = self.cast::<Indexed>().unwrap(); let res = v.nth_1(key).unwrap(); Ok(res.toString().to_string()) } /// Gives index of name /// /// Create name and index is they doesn't exist fn get_or_make_index( &mut self, name: &str, ) -> ObjResult<usize> { // let m = self.map.cast::<SPersistentHashMap>().unwrap(); // let v = self.vect.cast::<SPersistentVector>().unwrap(); // if let Some(o) = m.get(name) { // return *o; // } else { // let length = self.len().unwrap(); // v.push_back(String::from(name)); // *m = m.update(String::from(name), length); // let k = SUnique { // map: new_obj!(*m), // vect: new_obj!(*v), // }; // *self = k; // // return new index that was the length of the vector // return length; // } todo!() } /// Gives index of name /// /// return Error if doesn't exist fn get_index( &mut self, name: &str, ) -> ObjResult<usize> { // let m = self.map.cast::<SPersistentHashMap>().unwrap(); // let v = self.vect.cast::<SPersistentVector>().unwrap(); // let o = m.get(name).unwrap(); // Ok(o) todo!() } /// Tests if name exists fn test( &self, name: &str, ) -> ObjResult<bool> { // if let m = self.map.cast::<SPersistentHashMap>().unwrap() { // match m.get(name) { // | Some(_) => return Ok(true), // | None => return Ok(false), // } // } // Ok(false) todo!() } } impl IObject for SUnique { fn getClass<'a>(&self) -> &'a SClass { todo!() } fn hashCode(&self) -> usize { todo!() } fn equals( &self, other: &Object, ) -> bool { todo!() } fn toString(&self) -> String { todo!() } } impl SUnique { fn get_vect(&self) -> ObjResult<&SPersistentVector> { // let v = match self.vect.inner { // | Some(v) => v, // | _ => { // return err::<&SPersistentVector>( // "SUnique.vect not initialized", // ) // }, // }; // match v.cast::<SPersistentVector>() { // | Ok(c) => Ok(c.as_ref()), // | Err(e) => err_cast(&self.vect, "<&SPersistentVector>"), // } todo!() } } impl Default for SUnique { fn default() -> Self { SUnique { map: new_obj!(SPersistentHashMap::default()), vect: new_obj!(SPersistentVector::default()), } } } impl SUnique { pub fn new() -> Object { new_obj!(SUnique::default()) } } impl Drop for SUnique { fn drop(&mut self) { println!("Dropping Keyword state! -> {:?}", self.toString()); } } lazy_static! { /// Methods short names static ref METHODS: Object = Object { inner: None }; /// Members short names static ref MEMBERS: Object = Object { inner: None }; } #[test] fn test_keywords() { // // Initial state // println!( // "Init state len = {:?} state = {:?}", // SUnique::len(&CORE), // SUnique::get(&CORE) // ); // let e1 = SUnique::get(&CORE); // // Call init_static // println!( // "New state len = {:?} state = {:?}", // SUnique::len(&CORE), // SUnique::get(&CORE) // ); // let e2 = SUnique::get(&CORE); // // add first keyword // let o = SUnique::get_key(&String::from("essai"), &CORE); // println!( // "add essai len = {:?} state = {:?}", // SUnique::len(&CORE), // SUnique::get(&CORE) // ); // let e3 = SUnique::get(&CORE); // // add second keyword // SUnique::get_key(&"essai2".to_string(), &CORE); // println!( // "add essai2 len = {:?} state = {:?}", // SUnique::len(&CORE), // SUnique::get(&CORE) // ); // let e4 = SUnique::get(&CORE); // // display existing keywords // println!("Keyword 0 = \"{}\"", SUnique::get_id(0, &CORE)); // println!("Keyword 1 = \"{}\"", SUnique::get_id(1, &CORE)); // println!("Keyword 2 = \"{}\"", SUnique::get_id(2, &CORE)); // // Verify persistant state // println!("State 1 = {:?}", e1); // println!("State 2 = {:?}", e2); // println!("State 3 = {:?}", e3); // println!("State 4 = {:?}", e4); // assert_eq!(1, 1) // // display of droppings // // At the output state 1 and 2 are the same and so only one drop, // // event is there are two Arc, but it's a lone struct. // // State 3 is droped as it's the output of test function, and // // e3 is the only link to the state. // // As state 4 is linked in the KEYWORDS global variable, the drop // // is not displayed.. }