Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
symbol-table.hh
Go to the documentation of this file.
1#pragma once
3
4#include <unordered_map>
5
8
9namespace nix {
10
16class SymbolStr
17{
18 friend class SymbolTable;
19
20private:
21 const std::string * s;
22
23 explicit SymbolStr(const std::string & symbol): s(&symbol) {}
24
25public:
26 bool operator == (std::string_view s2) const
27 {
28 return *s == s2;
29 }
30
31 operator const std::string & () const
32 {
33 return *s;
34 }
35
36 operator const std::string_view () const
37 {
38 return *s;
39 }
40
41 friend std::ostream & operator <<(std::ostream & os, const SymbolStr & symbol);
42};
43
49class Symbol
50{
51 friend class SymbolTable;
52
53private:
54 uint32_t id;
55
56 explicit Symbol(uint32_t id): id(id) {}
57
58public:
59 Symbol() : id(0) {}
60
61 explicit operator bool() const { return id > 0; }
62
63 bool operator<(const Symbol other) const { return id < other.id; }
64 bool operator==(const Symbol other) const { return id == other.id; }
65 bool operator!=(const Symbol other) const { return id != other.id; }
66};
67
73{
74private:
75 std::unordered_map<std::string_view, std::pair<const std::string *, uint32_t>> symbols;
77
78public:
79
83 Symbol create(std::string_view s)
84 {
85 // Most symbols are looked up more than once, so we trade off insertion performance
86 // for lookup performance.
87 // TODO: could probably be done more efficiently with transparent Hash and Equals
88 // on the original implementation using unordered_set
89 // FIXME: make this thread-safe.
90 auto it = symbols.find(s);
91 if (it != symbols.end()) return Symbol(it->second.second + 1);
92
93 const auto & [rawSym, idx] = store.add(std::string(s));
94 symbols.emplace(rawSym, std::make_pair(&rawSym, idx));
95 return Symbol(idx + 1);
96 }
97
98 SymbolStr operator[](Symbol s) const
99 {
100 if (s.id == 0 || s.id > store.size())
101 abort();
102 return SymbolStr(store[s.id - 1]);
103 }
104
105 size_t size() const
106 {
107 return store.size();
108 }
109
110 size_t totalSize() const;
111
112 template<typename T>
113 void dump(T callback) const
114 {
115 store.forEach(callback);
116 }
117};
118
119}
Definition chunked-vector.hh:21
Definition symbol-table.hh:17
Definition symbol-table.hh:73
Symbol create(std::string_view s)
Definition symbol-table.hh:83
Definition symbol-table.hh:50