aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/data/slime/symbol_table.h
blob: 0eae65cead0e4ff5481b9599d6d0832c305827d1 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "symbol.h"
#include <vespa/vespalib/data/memory.h>
#include <vespa/vespalib/util/stash.h>
#include <vespa/vespalib/stllike/hash_map.h>

namespace vespalib::slime {

/**
 * Maps between strings and symbols.
 **/
class SymbolTable
{
private:
    struct hasher {
        size_t operator () (const Memory & lcm) const {
            return hashValue(lcm.data, lcm.size);
        }
    };
    using SymbolMap = hash_map<Memory, Symbol, hasher>;
    using SymbolVector = std::vector<Memory>;
    Stash        _stash;
    SymbolMap    _symbols;
    SymbolVector _names;

public:
    using UP = std::unique_ptr<SymbolTable>;
    SymbolTable() : SymbolTable(16) {}
    explicit SymbolTable(size_t expectedNumSymbols);
    SymbolTable(SymbolTable &&) noexcept = default;
    SymbolTable & operator=(SymbolTable &&) noexcept = default;
    ~SymbolTable();
    size_t symbols() const noexcept { return _names.size(); }
    Memory inspect(const Symbol &symbol) const {
        if (symbol.getValue() > _names.size()) {
            return Memory();
        }
        return _names[symbol.getValue()];
    }
    Symbol insert(const Memory &name);
    Symbol lookup(const Memory &name) const;
    void clear();
};

}