summaryrefslogtreecommitdiff
path: root/src/common/intern-pool.hpp
blob: caa54e4d2711c9ef880e22c6020101e077e0ce10 (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
#ifndef INTERN_POOL_HPP
#define INTERN_POOL_HPP

#include <cassert>

#include <map>
#include <string>
#include <vector>

class InternPool
{
    std::map<std::string, size_t> known;
    std::vector<std::string> names;
public:
    size_t intern(const std::string& name)
    {
        auto pair = known.insert({name, known.size()});
        if (pair.second)
            names.push_back(name);
        assert (known.size() == names.size());
        return pair.first->second;
    }

    const std::string& outtern(size_t sz) const
    {
        return names[sz];
    }

    size_t size() const
    {
        return known.size();
    }
};

#endif //INTERN_POOL_HPP