|  | 
| 2 | 2 | // SPDX-License-Identifier: Apache-2.0 | 
| 3 | 3 | 
 | 
| 4 | 4 | use std::collections::HashMap; | 
| 5 |  | -use tinybytes::{Bytes, BytesString}; | 
|  | 5 | + | 
|  | 6 | +use crate::span::SpanText; | 
| 6 | 7 | 
 | 
| 7 | 8 | /// This struct represents the shared dictionary used for interning all the strings belonging to a | 
| 8 | 9 | /// v05 trace chunk. | 
| 9 |  | -pub struct SharedDict { | 
|  | 10 | +pub struct SharedDict<T> { | 
| 10 | 11 |     /// Map strings with their index (O(1) retrieval complexity). | 
| 11 |  | -    string_map: HashMap<BytesString, usize>, | 
|  | 12 | +    string_map: HashMap<T, usize>, | 
| 12 | 13 |     /// Since the collection needs to be ordered an additional vector to keep the insertion order. | 
| 13 |  | -    dict: Vec<BytesString>, | 
|  | 14 | +    dict: Vec<T>, | 
| 14 | 15 | } | 
| 15 | 16 | 
 | 
| 16 |  | -impl SharedDict { | 
|  | 17 | +impl<T: SpanText> SharedDict<T> { | 
| 17 | 18 |     /// Gets the index of the interned string. If the string is not part of the dictionary it is | 
| 18 | 19 |     /// added and its corresponding index returned. | 
| 19 | 20 |     /// | 
| 20 | 21 |     /// # Arguments: | 
| 21 | 22 |     /// | 
| 22 | 23 |     /// * `str`: string to look up in the dictionary. | 
| 23 |  | -    pub fn get_or_insert(&mut self, str: &BytesString) -> Result<u32, std::num::TryFromIntError> { | 
| 24 |  | -        if let Some(index) = self.string_map.get(str) { | 
|  | 24 | +    pub fn get_or_insert(&mut self, s: &T) -> Result<u32, std::num::TryFromIntError> { | 
|  | 25 | +        if let Some(index) = self.string_map.get(s.borrow()) { | 
| 25 | 26 |             (*index).try_into() | 
| 26 | 27 |         } else { | 
| 27 | 28 |             let index = self.dict.len(); | 
| 28 |  | -            self.dict.push(str.clone()); | 
| 29 |  | -            self.string_map.insert(str.clone(), index); | 
|  | 29 | +            self.dict.push(s.clone()); | 
|  | 30 | +            self.string_map.insert(s.clone(), index); | 
| 30 | 31 |             index.try_into() | 
| 31 | 32 |         } | 
| 32 | 33 |     } | 
| 33 | 34 | 
 | 
| 34 | 35 |     /// Returns the dictionary. This method consumes the structure. | 
| 35 |  | -    pub fn dict(mut self) -> Vec<BytesString> { | 
|  | 36 | +    pub fn dict(mut self) -> Vec<T> { | 
| 36 | 37 |         std::mem::take(&mut self.dict) | 
| 37 | 38 |     } | 
| 38 | 39 | } | 
| 39 | 40 | 
 | 
| 40 |  | -impl Default for SharedDict { | 
|  | 41 | +impl<T: SpanText> Default for SharedDict<T> { | 
| 41 | 42 |     fn default() -> Self { | 
| 42 |  | -        let empty_str = unsafe { BytesString::from_bytes_unchecked(Bytes::from_static(b"")) }; | 
| 43 | 43 |         Self { | 
| 44 |  | -            string_map: HashMap::from([(empty_str.clone(), 0)]), | 
| 45 |  | -            dict: vec![empty_str.clone()], | 
|  | 44 | +            string_map: HashMap::from([(T::default(), 0)]), | 
|  | 45 | +            dict: vec![T::default()], | 
| 46 | 46 |         } | 
| 47 | 47 |     } | 
| 48 | 48 | } | 
| 49 | 49 | 
 | 
| 50 | 50 | #[cfg(test)] | 
| 51 | 51 | mod tests { | 
|  | 52 | +    use tinybytes::{Bytes, BytesString}; | 
|  | 53 | + | 
| 52 | 54 |     use super::*; | 
| 53 | 55 | 
 | 
| 54 | 56 |     #[test] | 
| 55 | 57 |     fn default_test() { | 
| 56 |  | -        let dict = SharedDict::default(); | 
|  | 58 | +        let dict: SharedDict<BytesString> = SharedDict::default(); | 
| 57 | 59 | 
 | 
| 58 | 60 |         assert_eq!(dict.string_map.len(), 1); | 
| 59 | 61 |         assert_eq!(dict.dict.len(), 1); | 
|  | 
0 commit comments