2 #if !defined(INCLUDED_POOLEDSTRING_H)
3 #define INCLUDED_POOLEDSTRING_H
6 #include "generic/static.h"
7 #include "string/string.h"
8 #include "container/hashtable.h"
9 #include "container/hashfunc.h"
11 /// \brief The string pool class.
12 class StringPool : public HashTable<char*, std::size_t, RawStringHash, RawStringEqual>
16 inline void StringPool_analyse(StringPool& pool)
18 typedef std::multimap<std::size_t, const char*> Ordered;
20 std::size_t total = 0;
21 std::size_t pooled = 0;
22 for(StringPool::iterator i = pool.begin(); i != pool.end(); ++i)
24 std::size_t size = string_length((*i).key) + 1;
25 total += size * (*i).value;
27 ordered.insert(Ordered::value_type((*i).value, (*i).key));
29 globalOutputStream() << "total: " << Unsigned(total) << " pooled:" << Unsigned(pooled) << "\n";
30 for(Ordered::iterator i = ordered.begin(); i != ordered.end(); ++i)
32 globalOutputStream() << (*i).second << " " << Unsigned((*i).first) << "\n";
37 /// \brief A string which can be copied with zero memory cost and minimal runtime cost.
39 /// \param PoolContext The string pool context to use.
40 template<typename PoolContext>
43 StringPool::iterator m_i;
44 static StringPool::iterator increment(StringPool::iterator i)
49 static StringPool::iterator insert(const char* string)
51 StringPool::iterator i = PoolContext::instance().find(const_cast<char*>(string));
52 if(i == PoolContext::instance().end())
54 return PoolContext::instance().insert(string_clone(string), 1);
58 static void erase(StringPool::iterator i)
62 char* string = (*i).key;
63 PoolContext::instance().erase(i);
64 string_release(string, string_length(string));
68 PooledString() : m_i(insert(""))
71 PooledString(const PooledString& other) : m_i(increment(other.m_i))
74 PooledString(const char* string) : m_i(insert(string))
81 PooledString& operator=(const PooledString& other)
83 PooledString tmp(other);
87 PooledString& operator=(const char* string)
89 PooledString tmp(string);
93 void swap(PooledString& other)
95 std::swap(m_i, other.m_i);
97 bool operator==(const PooledString& other) const
99 return m_i == other.m_i;
101 const char* c_str() const