summaryrefslogtreecommitdiffstats
path: root/source/core/slang-string-slice-pool.cpp
blob: 88963d68f914aad7b32e2e49f932791089e5e07e (plain)
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
#include "slang-string-slice-pool.h"

namespace Slang {

StringSlicePool::StringSlicePool() :
    m_arena(1024)
{
    clear();
}

void StringSlicePool::clear()
{
    m_slices.SetSize(1);
    m_slices[0] = UnownedStringSlice::fromLiteral("");

    m_map.Clear();
}

StringSlicePool::Handle StringSlicePool::add(const Slice& slice)
{
    const int* indexPtr = m_map.TryGetValue(slice);
    if (indexPtr)
    {
        return Handle(*indexPtr);
    }

    // Create a scoped copy
    UnownedStringSlice scopePath(m_arena.allocateString(slice.begin(), slice.size()), slice.size());

    const int index = int(m_slices.Count());

    m_slices.Add(scopePath);
    m_map.Add(scopePath, index);
    return Handle(index);
}

int StringSlicePool::findIndex(const Slice& slice) const
{
    const int* index = m_map.TryGetValue(slice);
    return index ? *index : -1;

}
} // namespace Slang