summaryrefslogtreecommitdiffstats
path: root/source/core/slang-string-slice-pool.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-12-04 12:38:38 -0500
committerGitHub <noreply@github.com>2019-12-04 12:38:38 -0500
commit5df582dd3229789364ae3fa75575fd978ca3282d (patch)
tree89f66f7c2427030b0e9a0ed0754fc380a5f4b21c /source/core/slang-string-slice-pool.cpp
parent9653dcc2c9d5d20d3d0e8918aaf1d5b09e963060 (diff)
Feature/string hash review (#1142)
* * Added ConstArrayView * Made StringSlicePool have styles * Remove point about strings not having terminating 0 (they do), and restriction around "" * spCalcStringHash -> spComputeStringHash * Small code improvements. Closer to coding conventions. * Fix small bug with Empty adding c string. * Fix typo in assert. * Fix ArrayView compiling issue on gcc/clang. * Remove tabs. * Improve comments around StringSlicePool. Simplify getting the added slices.
Diffstat (limited to 'source/core/slang-string-slice-pool.cpp')
-rw-r--r--source/core/slang-string-slice-pool.cpp78
1 files changed, 57 insertions, 21 deletions
diff --git a/source/core/slang-string-slice-pool.cpp b/source/core/slang-string-slice-pool.cpp
index e3d9d8809..7ea15e0d6 100644
--- a/source/core/slang-string-slice-pool.cpp
+++ b/source/core/slang-string-slice-pool.cpp
@@ -5,10 +5,10 @@ namespace Slang {
/* static */ const StringSlicePool::Handle StringSlicePool::kNullHandle;
/* static */ const StringSlicePool::Handle StringSlicePool::kEmptyHandle;
-/* static */const int StringSlicePool::kNumDefaultHandles;
+/* static */const Index StringSlicePool::kDefaultHandlesCount;
-
-StringSlicePool::StringSlicePool() :
+StringSlicePool::StringSlicePool(Style style) :
+ m_style(style),
m_arena(1024)
{
clear();
@@ -16,15 +16,29 @@ StringSlicePool::StringSlicePool() :
void StringSlicePool::clear()
{
- m_slices.setCount(2);
-
- m_slices[0] = UnownedStringSlice((const char*)nullptr, (const char*)nullptr);
- m_slices[1] = UnownedStringSlice::fromLiteral("");
-
- // Add the empty entry
- m_map.Add(m_slices[1], kEmptyHandle);
-
m_map.Clear();
+
+ switch (m_style)
+ {
+ case Style::Default:
+ {
+ // Add the defaults
+ m_slices.setCount(2);
+
+ m_slices[0] = UnownedStringSlice((const char*)nullptr, (const char*)nullptr);
+ m_slices[1] = UnownedStringSlice::fromLiteral("");
+
+ // Add the empty entry
+ m_map.Add(m_slices[1], kEmptyHandle);
+ break;
+ }
+ case Style::Empty:
+ {
+ // There are no defaults
+ m_slices.clear();
+ break;
+ }
+ }
}
StringSlicePool::Handle StringSlicePool::add(const Slice& slice)
@@ -47,31 +61,53 @@ StringSlicePool::Handle StringSlicePool::add(const Slice& slice)
StringSlicePool::Handle StringSlicePool::add(StringRepresentation* stringRep)
{
- if (stringRep == nullptr)
+ if (stringRep == nullptr && m_style == Style::Default)
{
return kNullHandle;
}
return add(StringRepresentation::asSlice(stringRep));
}
-
StringSlicePool::Handle StringSlicePool::add(const char* chars)
{
- if (!chars)
+ switch (m_style)
{
- return kNullHandle;
- }
- if (chars[0] == 0)
- {
- return kEmptyHandle;
+ case Style::Default:
+ {
+ if (!chars)
+ {
+ return kNullHandle;
+ }
+ if (chars[0] == 0)
+ {
+ return kEmptyHandle;
+ }
+ break;
+ }
+ case Style::Empty:
+ {
+ if (chars == nullptr)
+ {
+ SLANG_ASSERT(!"Empty style doesn't support nullptr");
+ // Return an invalid handle
+ return Handle(~HandleIntegral(0));
+ }
+ }
}
+
return add(UnownedStringSlice(chars));
}
-int StringSlicePool::findIndex(const Slice& slice) const
+Index StringSlicePool::findIndex(const Slice& slice) const
{
const Handle* handlePtr = m_map.TryGetValue(slice);
- return handlePtr ? int(*handlePtr) : -1;
+ return handlePtr ? Index(*handlePtr) : -1;
+}
+ConstArrayView<UnownedStringSlice> StringSlicePool::getAdded() const
+{
+ const Index firstIndex = getFirstAddedIndex();
+ return makeConstArrayView(m_slices.getBuffer() + firstIndex, m_slices.getCount() - firstIndex);
}
+
} // namespace Slang