-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTokenPool.cpp
More file actions
73 lines (53 loc) · 1.64 KB
/
TokenPool.cpp
File metadata and controls
73 lines (53 loc) · 1.64 KB
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
//
// FormulaEngine Project
// By Mike Lewis - 2015
//
// Mapper for condensing strings to unique unsigned integer IDs
//
// This module implements a simple and minimal version of the token
// pool concept, based on the requirements detailed in TokenPool.h.
//
// Note that we choose to use monotonic increasing IDs based on
// indices into a vector; this is not as efficient as it could be,
// but it's effective and easy to think about. Most of the usage
// of this object is meant to occur at load time and/or duing a
// heavy debugging session, so performance is not yet a concern.
//
#include "Pch.h"
#include "TokenPool.h"
//
// Add a string into the pool and get its token back
//
unsigned TokenPool::AddToken (const char token[]) {
auto iter = m_fastLookup.find(token);
if (iter != m_fastLookup.end())
return iter->second;
m_pool.emplace_back(token);
unsigned id = static_cast<unsigned>(m_pool.size());
m_fastLookup.emplace(token, id);
return id;
}
unsigned TokenPool::AddToken (const std::string & token) {
auto iter = m_fastLookup.find(token);
if (iter != m_fastLookup.end())
return iter->second;
m_pool.emplace_back(token);
unsigned id = static_cast<unsigned>(m_pool.size());
m_fastLookup.emplace(token, id);
return id;
}
unsigned TokenPool::FindToken (const std::string & token) {
auto iter = m_fastLookup.find(token);
if (iter != m_fastLookup.end())
return iter->second;
return 0;
}
//
// Given a token, look up the original string for it
//
const std::string & TokenPool::GetStringFromToken (unsigned token) const {
assert(token != 0);
unsigned index = token - 1;
assert(index < m_pool.size());
return m_pool[index];
}