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
use std::hash::{Hash, Hasher, SipHasher}; pub fn name_hash(str_: &str) -> u64 { let mut hasher = SipHasher::new(); str_.hash(&mut hasher); hasher.finish() } pub trait NameTableIdx { fn to_index(&self) -> usize; } impl NameTableIdx for usize { fn to_index(&self) -> usize { *self } } pub trait NameTable { fn parent<'a>(&'a self) -> Option<&Box<NameTable + 'a>>; fn initial_local(&self) -> usize; fn len_local(&self) -> usize; fn at_local<'a>(&'a self, idx: usize) -> &'a str; fn find_local(&self, name: &str) -> Option<usize> { for i in 0..self.len_local() { if self.at_local(i) == name { return Some(i); } } return None; } fn len(&self) -> usize { return self.len_local() + self.parent().map_or(0usize, |parent_table| parent_table.len()); } fn at<'a>(&'a self, idx: usize) -> &'a str { let initial = self.initial_local(); if idx >= initial { return self.at_local(idx - initial); } else if let Some(parent_table) = self.parent().as_ref() { &parent_table.at(idx) } else { panic!("access out of bound"); } } fn find(&self, name: &str) -> Option<usize> { let initial = self.initial_local(); self.find_local(name).map(|idx| idx + initial).or_else(|| { if let Some(parent_table) = self.parent().as_ref() { return parent_table.find(name); } return None; }) } } pub struct StaticNameTable<'a> { initial_idx: usize, names: &'static str, name_offsets: &'static [usize], parent: Option<Box<NameTable + 'a>>, } impl<'x> NameTable for StaticNameTable<'x> { fn initial_local(&self) -> usize { self.initial_idx } fn len_local(&self) -> usize { self.name_offsets.len() - 1 } fn at_local<'a>(&'a self, idx: usize) -> &'a str { &self.names[self.name_offsets[idx]..self.name_offsets[idx + 1]] } fn parent<'a>(&'a self) -> Option<&Box<NameTable + 'a>> { self.parent.as_ref() } } impl<'x> StaticNameTable<'x> { pub fn new(names_: &'static str, name_offsets_: &'static [usize]) -> Self { return StaticNameTable { initial_idx: 0usize, names: names_, name_offsets: name_offsets_, parent: None, }; } pub fn new_upon<ParentTableType: 'x + NameTable>(names_: &'static str, name_offsets_: &'static [usize], parent: ParentTableType) -> Self { return StaticNameTable { initial_idx: parent.initial_local() + parent.len_local(), names: names_, name_offsets: name_offsets_, parent: Some(Box::new(parent)), }; } pub fn index<T: NameTableIdx>(&'x self, idx: T) -> &'x str { &self.at(idx.to_index()) } } pub struct DynamicNameTable<'a> { initial_idx: usize, names: Box<Vec<String>>, parent: Option<Box<NameTable + 'a>>, } impl<'x> NameTable for DynamicNameTable<'x> { fn initial_local(&self) -> usize { self.initial_idx } fn len_local(&self) -> usize { self.names.len() } fn at_local<'a>(&'a self, idx: usize) -> &'a str { self.names[idx].as_str() } fn parent<'a>(&'a self) -> Option<&Box<NameTable + 'a>> { self.parent.as_ref() } } impl<'x> DynamicNameTable<'x> { pub fn new() -> Self { return DynamicNameTable { initial_idx: 0usize, names: Box::new(Vec::new()), parent: None, }; } pub fn new_upon<ParentTableType: 'x + NameTable>(parent: ParentTableType) -> Self { return DynamicNameTable { initial_idx: parent.initial_local() + parent.len_local(), names: Box::new(Vec::new()), parent: Some(Box::new(parent)), }; } pub fn intern(&mut self, name: &str) -> usize { self.find(name) .or_else(|| { Some({ self.names.as_mut().push(name.to_owned()); self.initial_idx + self.names.len() - 1 }) }) .unwrap() } pub fn index<T: NameTableIdx>(&'x self, idx: T) -> &'x str { &self.at(idx.to_index()) } } pub struct StaticHashedNameTable<'a> { initial_idx: usize, names: &'static str, name_offsets: &'static [usize], hash_idxes: &'static [(u64, usize)], parent: Option<Box<NameTable + 'a>>, } impl<'x> StaticHashedNameTable<'x> { pub fn new(names_: &'static str, name_offsets_: &'static [usize], hash_idxes_: &'static [(u64, usize)]) -> Self { return StaticHashedNameTable { initial_idx: 0usize, names: names_, name_offsets: name_offsets_, hash_idxes: hash_idxes_, parent: None, }; } pub fn new_upon<ParentTableType: 'x + NameTable>(names_: &'static str, name_offsets_: &'static [usize], hash_idxes_: &'static [(u64, usize)], parent: ParentTableType) -> Self { return StaticHashedNameTable { initial_idx: parent.initial_local() + parent.len_local(), names: names_, name_offsets: name_offsets_, hash_idxes: hash_idxes_, parent: Some(Box::new(parent)), }; } pub fn index<T: NameTableIdx>(&'x self, idx: T) -> &'x str { &self.at(idx.to_index()) } fn hash_enabled(&self) -> bool { self.hash_idxes.len() != 0 } fn find_local_hashing(&self, name: &str) -> Option<usize> { let target = name_hash(name); let result = self.hash_idxes.binary_search_by(|&(a, _)| a.cmp(&target)); match result { Ok(val) => { if self.at_local(self.hash_idxes[val].1) == name { Some(self.hash_idxes[val].1) } else { None } } _ => None, } } fn find_local_fallback(&self, name: &str) -> Option<usize> { for i in 0..self.len_local() { if self.at_local(i) == name { return Some(i); } } return None; } } impl<'x> NameTable for StaticHashedNameTable<'x> { fn initial_local(&self) -> usize { self.initial_idx } fn len_local(&self) -> usize { self.name_offsets.len() - 1 } fn at_local<'a>(&'a self, idx: usize) -> &'a str { &self.names[self.name_offsets[idx]..self.name_offsets[idx + 1]] } fn parent<'a>(&'a self) -> Option<&Box<NameTable + 'a>> { self.parent.as_ref() } fn find_local(&self, name: &str) -> Option<usize> { if self.hash_enabled() { self.find_local_hashing(name) } else { self.find_local_fallback(name) } } }