yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.6 KiB80 linesraw
1#ifndef SLANG_CORE_STRING_SLICE_INDEX_MAP_H
2#define SLANG_CORE_STRING_SLICE_INDEX_MAP_H
3
4#include "slang-basic.h"
5#include "slang-string-slice-pool.h"
6
7namespace Slang
8{
9
10/* Maps an UnownedStringSlice to an index. All substrings are held internally in a StringSlicePool,
11and so owned by the type.
12*/
13class StringSliceIndexMap
14{
15public:
16    /// An index that identifies a key value pair.
17    typedef Index CountIndex;
18
19    /// Adds a key, value pair. Returns the CountIndex of the pair.
20    /// If there is already a value stored for the key it is replaced.
21    CountIndex add(const UnownedStringSlice& key, Index valueIndex);
22
23    /// Finds or adds the slice. If the slice is added the defaultValueIndex is set.
24    /// If not the index associated with the slice remains the same.
25    /// Returns the CountIndex where the key,value pair are stored
26    CountIndex findOrAdd(const UnownedStringSlice& key, Index defaultValueIndex);
27
28    /// Gets the index associated with the key. Returns -1 if there is no associated index.
29    SLANG_FORCE_INLINE Index getValue(const UnownedStringSlice& key);
30
31    /// Get the amount of pairs in the map
32    Index getCount() const { return m_indexMap.getCount(); }
33
34    /// Get the slice and the index at the specified index
35    SLANG_INLINE KeyValuePair<UnownedStringSlice, Index> getAt(CountIndex countIndex) const;
36
37    /// Clear the contents of the map
38    void clear();
39
40    /// Get the key at the specified index
41    UnownedStringSlice getKeyAt(CountIndex index) const
42    {
43        return m_pool.getSlice(StringSlicePool::Handle(index));
44    }
45    /// Get the value at the specified index
46    Index& getValueAt(CountIndex index) { return m_indexMap[index]; }
47
48    /// Get the amount of key,value pairs
49    Index getCount() { return m_indexMap.getCount(); }
50
51    /// Ctor
52    StringSliceIndexMap()
53        : m_pool(StringSlicePool::Style::Empty)
54    {
55    }
56
57protected:
58    StringSlicePool m_pool; ///< Pool holds the substrings
59    List<Index> m_indexMap; ///< Maps a pool index to the output index
60};
61
62// ---------------------------------------------------------------------------
63Index StringSliceIndexMap::getValue(const UnownedStringSlice& key)
64{
65    const Index poolIndex = m_pool.findIndex(key);
66    return (poolIndex >= 0) ? m_indexMap[poolIndex] : -1;
67}
68
69// ---------------------------------------------------------------------------
70KeyValuePair<UnownedStringSlice, Index> StringSliceIndexMap::getAt(CountIndex countIndex) const
71{
72    KeyValuePair<UnownedStringSlice, Index> pair;
73    pair.key = m_pool.getSlice(StringSlicePool::Handle(countIndex));
74    pair.value = m_indexMap[countIndex];
75    return pair;
76}
77
78} // namespace Slang
79
80#endif