• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            cyt
            在2001年就拜讀了C++巨匠Andrei Alexandrescu的著作:《A Policy-Based basic_string Implementation》。flex_string的精巧令人嘆服。其后閱讀《Modern C++ Design》,更是令人震撼,正如候捷譯序中所說:“讓我瞠目結舌,陷入沉思……與……呃……恍惚。”Loki庫更是如獲至寶……
            可惜在當時在下載了flex_string的源碼以后,一經測試,竟然幾個顯而易見大大的bug就放在那里……高手也是有偶然犯錯的時候……
            今年之所以無意中想起這件事,原因是同事問起:他有一個數據結構需要用大量定長的字符串作Key,但std::string比較浪費空間,const char *既不安全也不好用,除了自己寫一個class還有沒有其他的好方法?這一下子讓我想起了flex_string的SmallStringOpt,簡直就是度身訂做的解決方案,于是再找到Code Snippets的地址,發現flex_string今年初換了新版本,簡直欣喜若狂。特別是作者的這句話:“Harmut Kaizer reported that simply dropping in flex_string in the Wave Boost Preprocessor improved its speed by 5-10%, depending on input.”? 有人做過白老鼠,這次應該不會有什么bug了吧。于是下載編譯測試……
            ?
            看到各種的Storage,讓我又想起另外一件事:有個程序員跟我說他的工程需要很多不定長的字符串作為Key,僅僅作為一種只讀的字符串,不需要任何的變更,因此std::string的幾個指針變得“多余”,換句話說,他只想要一個“節省”而又“安全”的const char *。其實個人覺得SmallStringOpt, 3>之類的已經足夠了,但別人堅持“我就喜歡”,那有什么辦法呢。于是簡單做一個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();
            ?
            其實原理也很簡單,Copy SimpleStringStorage的代碼改一下,只用一個指針就是了。
            最后用flex_string的測試程序測試,不通過……后來才發現String result(random(0, maxSize), '\0');然后獲得result.size()的時候變成0。哦,那當然了,少了一個指針指向字符串結尾,只能通過尋找'\0',當然size不正確了,于是測試程序的幾處:
            ??? 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)
            測試通過了。不過用的時候就要注意了,用了SlimStringStorage的flex_string的resize的含義和普通的string不一樣了,不過也沒關系的。
            ?
            另外一點想不通的就是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);
            之類的函數,使得std::string和不同的Storage的flex_string之間可以相互通用呢?高手可能自有高手的看法,有空的話自己慢慢加吧。
            posted on 2005-10-08 14:41 cyt 閱讀(421) 評論(0)  編輯 收藏 引用
             
            午夜精品久久久久久毛片| 69国产成人综合久久精品| 精品免费久久久久久久| 一本综合久久国产二区| 国产AⅤ精品一区二区三区久久| 国产亚洲欧美精品久久久| 人妻精品久久无码区| 国产成人久久精品一区二区三区 | 奇米综合四色77777久久| 久久天天躁狠狠躁夜夜2020一 | 精品久久久中文字幕人妻| 午夜精品久久影院蜜桃| 亚洲人成无码网站久久99热国产 | 亚洲国产精品久久久久婷婷软件| 国产精品美女久久久久| 久久青青草原精品影院| 91精品国产色综久久| 一本大道久久a久久精品综合| 国产精品gz久久久| 久久中文字幕视频、最近更新 | 中文精品99久久国产| 伊人久久五月天| 久久久无码一区二区三区 | 国产精品久久久久9999高清| 免费观看成人久久网免费观看| 91麻精品国产91久久久久| 久久丝袜精品中文字幕| 亚洲AV无码久久精品狠狠爱浪潮| 久久ww精品w免费人成| 久久久久香蕉视频| 伊人色综合久久天天人手人婷 | 18禁黄久久久AAA片| 91精品国产乱码久久久久久 | 久久国产色av免费看| 免费观看成人久久网免费观看| 老司机午夜网站国内精品久久久久久久久| 2021最新久久久视精品爱| 欧美一区二区精品久久| 一本色综合网久久| 国内精品久久久久久久coent | 理论片午午伦夜理片久久|