Nix  2.93.0-dev
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 <list>
5#include <map>
6#include <unordered_map>
7
10
11namespace nix {
12
19{
20 friend class SymbolTable;
21
22private:
23 const std::string * s;
24
25 explicit SymbolStr(const std::string & symbol): s(&symbol) {}
26
27public:
28 bool operator == (std::string_view s2) const
29 {
30 return *s == s2;
31 }
32
33 operator const std::string & () const
34 {
35 return *s;
36 }
37
38 operator const std::string_view () const
39 {
40 return *s;
41 }
42
43 friend std::ostream & operator <<(std::ostream & os, const SymbolStr & symbol);
44};
45
51class Symbol
52{
53 friend class SymbolTable;
54
55private:
56 uint32_t id;
57
58 explicit Symbol(uint32_t id): id(id) {}
59
60public:
61 Symbol() : id(0) {}
62
63 explicit operator bool() const { return id > 0; }
64
65 bool operator<(const Symbol other) const { return id < other.id; }
66 bool operator==(const Symbol other) const { return id == other.id; }
67 bool operator!=(const Symbol other) const { return id != other.id; }
68};
69
75{
76private:
77 std::unordered_map<std::string_view, std::pair<const std::string *, uint32_t>> symbols;
79
80public:
81
85 Symbol create(std::string_view s)
86 {
87 // Most symbols are looked up more than once, so we trade off insertion performance
88 // for lookup performance.
89 // TODO: could probably be done more efficiently with transparent Hash and Equals
90 // on the original implementation using unordered_set
91 // FIXME: make this thread-safe.
92 auto it = symbols.find(s);
93 if (it != symbols.end()) return Symbol(it->second.second + 1);
94
95 const auto & [rawSym, idx] = store.add(std::string(s));
96 symbols.emplace(rawSym, std::make_pair(&rawSym, idx));
97 return Symbol(idx + 1);
98 }
99
100 SymbolStr operator[](Symbol s) const
101 {
102 if (s.id == 0 || s.id > store.size())
103 abort();
104 return SymbolStr(store[s.id - 1]);
105 }
106
107 size_t size() const
108 {
109 return store.size();
110 }
111
112 size_t totalSize() const;
113
114 template<typename T>
115 void dump(T callback) const
116 {
117 store.forEach(callback);
118 }
119};
120
121}
Definition chunked-vector.hh:21
Definition symbol-table.hh:19
Definition symbol-table.hh:75
Symbol create(std::string_view s)
Definition symbol-table.hh:85
Definition symbol-table.hh:52