summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
Diffstat (limited to 'source/core')
-rw-r--r--source/core/core.vcxproj5
-rw-r--r--source/core/core.vcxproj.filters27
-rw-r--r--source/core/slang-string-slice-pool.cpp43
-rw-r--r--source/core/slang-string-slice-pool.h50
4 files changed, 115 insertions, 10 deletions
diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj
index ec6195940..d1ed4715f 100644
--- a/source/core/core.vcxproj
+++ b/source/core/core.vcxproj
@@ -183,11 +183,13 @@
<ClInclude Include="platform.h" />
<ClInclude Include="secure-crt.h" />
<ClInclude Include="slang-byte-encode-util.h" />
+ <ClInclude Include="slang-cpu-defines.h" />
<ClInclude Include="slang-free-list.h" />
<ClInclude Include="slang-io.h" />
<ClInclude Include="slang-math.h" />
<ClInclude Include="slang-memory-arena.h" />
<ClInclude Include="slang-random-generator.h" />
+ <ClInclude Include="slang-string-slice-pool.h" />
<ClInclude Include="slang-string-util.h" />
<ClInclude Include="slang-string.h" />
<ClInclude Include="smart-pointer.h" />
@@ -203,6 +205,7 @@
<ClCompile Include="slang-io.cpp" />
<ClCompile Include="slang-memory-arena.cpp" />
<ClCompile Include="slang-random-generator.cpp" />
+ <ClCompile Include="slang-string-slice-pool.cpp" />
<ClCompile Include="slang-string-util.cpp" />
<ClCompile Include="slang-string.cpp" />
<ClCompile Include="stream.cpp" />
@@ -210,7 +213,7 @@
<ClCompile Include="token-reader.cpp" />
</ItemGroup>
<ItemGroup>
- <None Include="core.natvis" />
+ <Natvis Include="core.natvis" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
diff --git a/source/core/core.vcxproj.filters b/source/core/core.vcxproj.filters
index f9eeef43a..7936ca11b 100644
--- a/source/core/core.vcxproj.filters
+++ b/source/core/core.vcxproj.filters
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Header Files">
@@ -45,6 +45,12 @@
<ClInclude Include="secure-crt.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="slang-byte-encode-util.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="slang-cpu-defines.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
<ClInclude Include="slang-free-list.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -60,6 +66,9 @@
<ClInclude Include="slang-random-generator.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="slang-string-slice-pool.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
<ClInclude Include="slang-string-util.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -81,14 +90,14 @@
<ClInclude Include="type-traits.h">
<Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="slang-byte-encode-util.h">
- <Filter>Header Files</Filter>
- </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="platform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="slang-byte-encode-util.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="slang-free-list.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -101,6 +110,9 @@
<ClCompile Include="slang-random-generator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="slang-string-slice-pool.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="slang-string-util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -116,13 +128,10 @@
<ClCompile Include="token-reader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="slang-byte-encode-util.cpp">
- <Filter>Source Files</Filter>
- </ClCompile>
</ItemGroup>
<ItemGroup>
- <None Include="core.natvis">
+ <Natvis Include="core.natvis">
<Filter>Source Files</Filter>
- </None>
+ </Natvis>
</ItemGroup>
</Project> \ No newline at end of file
diff --git a/source/core/slang-string-slice-pool.cpp b/source/core/slang-string-slice-pool.cpp
new file mode 100644
index 000000000..88963d68f
--- /dev/null
+++ b/source/core/slang-string-slice-pool.cpp
@@ -0,0 +1,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
diff --git a/source/core/slang-string-slice-pool.h b/source/core/slang-string-slice-pool.h
new file mode 100644
index 000000000..e6846b3dd
--- /dev/null
+++ b/source/core/slang-string-slice-pool.h
@@ -0,0 +1,50 @@
+#ifndef SLANG_STRING_SLICE_POOL_H
+#define SLANG_STRING_SLICE_POOL_H
+
+#include "slang-string.h"
+
+#include "list.h"
+#include "slang-memory-arena.h"
+#include "dictionary.h"
+
+namespace Slang {
+
+class StringSlicePool
+{
+public:
+ /// Handle of 0 is null. If accessed will be returned as the empty string
+ enum class Handle : uint32_t;
+ typedef UnownedStringSlice Slice;
+
+ /// Returns the index of a slice, if contained, or -1 if not found
+ int findIndex(const Slice& slice) const;
+
+ /// True if has the slice
+ bool has(const Slice& slice) { return findIndex(slice) >= 0; }
+ /// Add a slice
+ Handle add(const Slice& slice);
+
+ /// Empty contents
+ void clear();
+
+ /// Get the slice from the handle
+ const UnownedStringSlice& getSlice(Handle handle) const { return m_slices[UInt(handle)]; }
+
+ /// Get all the slices
+ const List<UnownedStringSlice>& getSlices() const { return m_slices; }
+
+ /// Convert a handle to and index. (A handle is just an index!)
+ static int asIndex(Handle handle) { return int(handle); }
+
+ /// Ctor
+ StringSlicePool();
+
+protected:
+ List<UnownedStringSlice> m_slices;
+ Dictionary<UnownedStringSlice, int> m_map;
+ MemoryArena m_arena;
+};
+
+} // namespace Slang
+
+#endif // SLANG_STRING_SLICE_POOL_H