在2001年就拜讀了C++巨匠Andrei Alexandrescu的著作:
《A Policy-Based basic_string Implementation》。flex_string的精巧令人嘆服。其后閱讀《Modern C++ Design》,更是令人震撼,正如候捷譯序中所說:“讓我瞠目結(jié)舌,陷入沉思……與……呃……恍惚?!盠oki庫(kù)更是如獲至寶……
可惜在當(dāng)時(shí)在下載了flex_string的源碼以后,一經(jīng)測(cè)試,竟然幾個(gè)顯而易見大大的bug就放在那里……高手也是有偶然犯錯(cuò)的時(shí)候……
今年之所以無意中想起這件事,原因是同事問起:他有一個(gè)數(shù)據(jù)結(jié)構(gòu)需要用大量定長(zhǎng)的字符串作Key,但std::string比較浪費(fèi)空間,const char *既不安全也不好用,除了自己寫一個(gè)class還有沒有其他的好方法?這一下子讓我想起了flex_string的SmallStringOpt,簡(jiǎn)直就是度身訂做的解決方案,于是再找到
Code Snippets的地址,發(fā)現(xiàn)flex_string今年初換了新版本,簡(jiǎn)直欣喜若狂。特別是作者的這句話:“Harmut Kaizer reported that simply dropping in
flex_string in the Wave Boost Preprocessor improved its speed by 5-10%, depending on input.”? 有人做過白老鼠,這次應(yīng)該不會(huì)有什么bug了吧。于是下載編譯測(cè)試……
?
看到各種的Storage,讓我又想起另外一件事:有個(gè)程序員跟我說他的工程需要很多不定長(zhǎng)的字符串作為Key,僅僅作為一種只讀的字符串,不需要任何的變更,因此std::string的幾個(gè)指針變得“多余”,換句話說,他只想要一個(gè)“節(jié)省”而又“安全”的const char *。其實(shí)個(gè)人覺得SmallStringOpt, 3>之類的已經(jīng)足夠了,但別人堅(jiān)持“我就喜歡”,那有什么辦法呢。于是簡(jiǎn)單做一個(gè)SlimStringStorage給flex_string用用吧:
?
#include
template >
class SlimStringStorage
{
?// The "public" below exists because MSVC can't do template typedefs
public:
?static const E emptyString_;
?typedef typename A::size_type size_type;
private:
?E * pData_;
?void Init(size_type size)
?{
??if (pData_ != &emptyString_)
???free(pData_);
??if (size == 0)
???pData_ = const_cast(&emptyString_);
??else
??{
???pData_ = static_cast(malloc(sizeof(E) + size * sizeof(E)));
???if (!pData_) throw std::bad_alloc();
???pData_[size] = E();
??}
?}
public:
?typedef E value_type;
?typedef E* iterator;
?typedef const E* const_iterator;
?typedef A allocator_type;
?SlimStringStorage(const SlimStringStorage& rhs)
?{
??pData_ = const_cast(&emptyString_);
??const size_type sz = rhs.size();
??Init(sz);
??if (sz) flex_string_details::pod_copy(rhs.begin(), rhs.begin() + sz, begin());
?}
?SlimStringStorage(const SlimStringStorage& s,flex_string_details::Shallow)
?: pData_(s.pData_)
?{}
?SlimStringStorage(const A&)
??{ pData_ = const_cast(&emptyString_); }
?SlimStringStorage(const E* s, size_type len, const A&)
?{
??pData_ = const_cast(&emptyString_);
??Init(len);
??flex_string_details::pod_copy(s, s + len, begin());
?}
?SlimStringStorage(size_type len, E c, const A&)
?{
??pData_ = const_cast(&emptyString_);
??Init(len);
??flex_string_details::pod_fill(begin(), begin() + len, c);
?}
?SlimStringStorage& operator=(const SlimStringStorage& rhs)
?{
??const size_type sz = rhs.size();
??reserve(sz);
??if (sz)
??{
???flex_string_details::pod_copy(&*rhs.begin(), &*(rhs.begin() + sz), begin());
???pData_[sz] = E();
??}
??
??return *this;
?}
?~SlimStringStorage()
?{
??if (pData_ != &emptyString_) free(pData_);
?}
?iterator begin()
?{ return pData_; }
?const_iterator begin() const
?{ return pData_; }
?iterator end()
?{ return pData_ + size(); }
?const_iterator end() const
?{ return pData_ + size(); }
?size_type size() const;
?size_type max_size() const
?{ return size_t(-1) / sizeof(E) - sizeof(E *) - 1; }
?size_type capacity() const
?{ return size(); }
?void reserve(size_type res_arg)
?{
??if (pData_ == &emptyString_ || res_arg == 0)
??{
???Init(res_arg);
???if (res_arg)
????*pData_ = E();
??}
??else
??{
???const size_type sz = size();
???
???if (res_arg > sz)
???{
????void* p = realloc(pData_, sizeof(E) + res_arg * sizeof(E));
????if (!p) throw std::bad_alloc();
????if (p != pData_)
????{
?????pData_ = static_cast(p);
?????pData_[sz] = E();
????} // if (p != pData_)
????pData_[res_arg] = E();
???}
??}
?}
?void append(const E* s, size_type sz)
?{
??const size_type szOrg = size();
??const size_type neededCapacity = szOrg + sz;
??const iterator b = begin();
??static std::less_equal le;
??if (le(b, s) && le(s, pData_ + szOrg))
??{
???// aliased
???const size_type offset = s - b;
???reserve(neededCapacity);
???s = begin() + offset;
??} // if (le(b, s) && le(s, pData_ + szOrg))
??else
???reserve(neededCapacity);
??
??flex_string_details::pod_copy(s, s + sz, pData_ + szOrg);
?}
?template
??void append(InputIterator b, InputIterator e)
??{
???const size_type szOrg = size();
???const size_type neededCapacity = szOrg + std::distance(b,e);
???reserve(neededCapacity);
???
???for (E * p = pData_ + szOrg; b != e; ++b, ++p)
????*p = *b;
??}
?void resize(size_type newSize, E fill)
?{
??const size_type szOrg = size();
??const int delta = int(newSize - szOrg);
??if (delta == 0) return;
??reserve(newSize);
??if (delta > 0)
??{
???E* e = pData_ + szOrg;
???flex_string_details::pod_fill(e, e + delta, fill);
??} // if (delta > 0)
??else if (newSize)
???pData_[newSize] = E();
?}
?void swap(SlimStringStorage& rhs)
?{
??std::swap(pData_, rhs.pData_);
?}
?const E* c_str() const
?{
??return pData_;
?}
?const E* data() const
?{ return pData_; }
?A get_allocator() const
?{ return A(); }
};
template
typename SlimStringStorage::size_type? SlimStringStorage::size() const
{
?register const E * p = pData_;
?for (; *p; ++p);
?return static_cast(p - pData_);
}
template
const E SlimStringStorage::emptyString_ = E();
?
其實(shí)原理也很簡(jiǎn)單,Copy SimpleStringStorage的代碼改一下,只用一個(gè)指針就是了。
最后用flex_string的測(cè)試程序測(cè)試,不通過……后來才發(fā)現(xiàn)String result(random(0, maxSize), '\0');然后獲得result.size()的時(shí)候變成0。哦,那當(dāng)然了,少了一個(gè)指針指向字符串結(jié)尾,只能通過尋找'\0',當(dāng)然size不正確了,于是測(cè)試程序的幾處:
??? String result(random(0, maxSize), '\0');
??? int i = 0;
??? for (; i != result.size(); ++i)
改為:
?size_t nSize = random(0, maxSize);
?String result(nSize, '\0');
?int i = 0;
?for (; i != nSize; ++i)
測(cè)試通過了。不過用的時(shí)候就要注意了,用了SlimStringStorage的flex_string的resize的含義和普通的string不一樣了,不過也沒關(guān)系的。
?
另外一點(diǎn)想不通的就是flex_string為什么不加上:
template
??? flex_string(const flex_string & str, size_type pos,size_type n = npos, const A& a = A());
template
??? flex_string(const std::basic_string & str, size_type pos,size_type n = npos, const A& a = A());
template
??? flex_string& operator=(const flex_string & str);
template???
??? flex_string& operator=(const std::basic_string & str);
之類的函數(shù),使得std::string和不同的Storage的flex_string之間可以相互通用呢?高手可能自有高手的看法,有空的話自己慢慢加吧。