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
use bimap::BiMap;
use std::borrow::Borrow;
use std::cell::RefCell;
use std::fmt;
use std::hash::Hash;
use std::path::{Path, PathBuf};
#[derive(Default)]
pub struct GlobalTable<T, U>
where
T: Hash + Eq,
U: Hash + Eq,
{
table: BiMap<T, U>,
last: U,
}
impl<T, U> GlobalTable<T, U>
where
T: Hash + Eq,
U: Hash + Eq + Copy + Incrementable,
{
pub fn insert(&mut self, value: T) -> U {
if let Some(id) = self.table.get_by_left(&value) {
*id
} else {
let id = self.last;
self.table.insert(value, id);
self.last.inc();
id
}
}
pub fn get_value(&self, id: U) -> Option<&T> {
self.table.get_by_right(&id)
}
pub fn get_id<V: Borrow<T>>(&self, value: V) -> Option<U> {
self.table.get_by_left(value.borrow()).copied()
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct StrId(usize);
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PathId(usize);
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TokenId(usize);
pub trait Incrementable {
fn inc(&mut self);
}
impl Incrementable for StrId {
fn inc(&mut self) {
self.0 += 1;
}
}
impl Incrementable for PathId {
fn inc(&mut self) {
self.0 += 1;
}
}
impl fmt::Display for StrId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let text = get_str_value(*self).unwrap();
text.fmt(f)
}
}
impl fmt::Display for PathId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let text = format!("{}", get_path_value(*self).unwrap().to_string_lossy());
text.fmt(f)
}
}
impl fmt::Display for TokenId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
thread_local!(static STRING_TABLE: RefCell<GlobalTable<String, StrId>> = RefCell::new(GlobalTable::default()));
thread_local!(static PATHBUF_TABLE: RefCell<GlobalTable<PathBuf, PathId>> = RefCell::new(GlobalTable::default()));
thread_local!(static TOKEN_ID: RefCell<usize> = RefCell::new(0));
pub fn insert_str(value: &str) -> StrId {
STRING_TABLE.with(|f| f.borrow_mut().insert(value.to_owned()))
}
pub fn insert_path(value: &Path) -> PathId {
PATHBUF_TABLE.with(|f| f.borrow_mut().insert(value.to_owned()))
}
pub fn get_str_value(id: StrId) -> Option<String> {
STRING_TABLE.with(|f| f.borrow().get_value(id).map(|x| x.to_owned()))
}
pub fn get_path_value(id: PathId) -> Option<PathBuf> {
PATHBUF_TABLE.with(|f| f.borrow().get_value(id).map(|x| x.to_owned()))
}
pub fn get_str_id<T: Borrow<String>>(value: T) -> Option<StrId> {
STRING_TABLE.with(|f| f.borrow().get_id(value).map(|x| x.to_owned()))
}
pub fn get_path_id<T: Borrow<PathBuf>>(value: T) -> Option<PathId> {
PATHBUF_TABLE.with(|f| f.borrow().get_id(value).map(|x| x.to_owned()))
}
pub fn new_token_id() -> TokenId {
TOKEN_ID.with(|f| {
let mut ret = f.borrow_mut();
*ret += 1;
TokenId(*ret)
})
}