Use a struct rather than a std::pair for InternedString DataAndRefCount

This commit is contained in:
Maxime Coste 2014-12-30 11:56:01 +00:00
parent d4a929b42e
commit 0493cf6d62
2 changed files with 12 additions and 12 deletions

View File

@ -13,16 +13,16 @@ InternedString StringRegistry::acquire(StringView str)
{ {
slot = m_free_slots.back(); slot = m_free_slots.back();
m_free_slots.pop_back(); m_free_slots.pop_back();
kak_assert(m_storage[slot].second == 0); kak_assert(m_storage[slot].refcount == 0);
m_storage[slot] = DataAndRefCount({str.begin(), str.end()}, 1); m_storage[slot] = DataAndRefCount{{str.begin(), str.end()}, 1};
} }
else else
{ {
slot = m_storage.size(); slot = m_storage.size();
m_storage.push_back(DataAndRefCount({str.begin(), str.end()}, 1)); m_storage.push_back({{str.begin(), str.end()}, 1});
} }
// Create a new string view that point to the storage data // Create a new string view that point to the storage data
StringView storage_view{m_storage[slot].first.data(), (int)m_storage[slot].first.size()}; StringView storage_view{m_storage[slot].data.data(), (int)m_storage[slot].data.size()};
m_slot_map[storage_view] = slot; m_slot_map[storage_view] = slot;
return InternedString{storage_view, slot}; return InternedString{storage_view, slot};
@ -30,24 +30,24 @@ InternedString StringRegistry::acquire(StringView str)
size_t slot = it->second; size_t slot = it->second;
auto& data = m_storage[slot]; auto& data = m_storage[slot];
++data.second; ++data.refcount;
return {{data.first.data(), (int)data.first.size()}, slot}; return {{data.data.data(), (int)data.data.size()}, slot};
} }
void StringRegistry::acquire(size_t slot) void StringRegistry::acquire(size_t slot)
{ {
kak_assert(slot < m_storage.size()); kak_assert(slot < m_storage.size());
kak_assert(m_storage[slot].second > 0); kak_assert(m_storage[slot].refcount > 0);
++m_storage[slot].second; ++m_storage[slot].refcount;
} }
void StringRegistry::release(size_t slot) noexcept void StringRegistry::release(size_t slot) noexcept
{ {
kak_assert(m_storage[slot].second > 0); kak_assert(m_storage[slot].refcount > 0);
if (--m_storage[slot].second == 0) if (--m_storage[slot].refcount == 0)
{ {
m_free_slots.push_back(slot); m_free_slots.push_back(slot);
std::vector<char>& data = m_storage[slot].first; std::vector<char>& data = m_storage[slot].data;
auto it = m_slot_map.find(StringView{data.data(), (int)data.size()}); auto it = m_slot_map.find(StringView{data.data(), (int)data.size()});
kak_assert(it != m_slot_map.end()); kak_assert(it != m_slot_map.end());
m_slot_map.erase(it); m_slot_map.erase(it);

View File

@ -21,7 +21,7 @@ private:
UnorderedMap<StringView, size_t> m_slot_map; UnorderedMap<StringView, size_t> m_slot_map;
std::vector<size_t> m_free_slots; std::vector<size_t> m_free_slots;
using DataAndRefCount = std::pair<std::vector<char>, int>; struct DataAndRefCount { std::vector<char> data; int refcount; };
std::vector<DataAndRefCount> m_storage; std::vector<DataAndRefCount> m_storage;
}; };