拯救你的字串:精簡

來源:互聯網
上載者:User

原來的方法由於要支援播種不得不保留了幾個空函數,這其實是沒有必要的。

不過話要說回來,少了他們,筆者我又不得不多重載不少的函數,鬱悶阿。

算了吧,支援使用者,提供一個只能掛種子皮但是效率趨向完美的版本(不管你編譯器會不會對空函數inline)。代價是多寫了不少模板函數,和對葉子節點進行了封裝(原來沒有葉子類,現在有了葉子unary_tree_ref,但是葉子不會出現在樹上,效率是沒有損失的,注意觀察哦)。

#include <string><br />#include <time.h><br />#include <stdio.h></p><p>using namespace std;</p><p>#ifdef _MSC_VER<br />#if _MSC_VER <= 1200<br />#ifndef __INTEL_COMPILER<br />#define _MSVC_6<br />#pragma warning( disable : 4786)<br />#endif<br />#endif<br />#endif</p><p>namespace _et_private<br />{<br />template <typename T><br />struct unary_tree_ref<br />{<br /> typedef T dest_type;<br /> inline unary_tree_ref(const dest_type& arg):<br /> data(arg){<br /> }<br /> const dest_type& data;<br />};</p><p>#ifdef _MSVC_6</p><p>template <bool N><br />struct mpl_bool<br />{<br /> size_t sizer[size_t(N)+10];<br />};</p><p>template <typename T><br />mpl_bool<true> get_result(T*, typename T::dest_type*);</p><p>mpl_bool<false> get_result(...);</p><p>template <typename T><br />struct check<br />{<br /> enum {result = (sizeof(get_result((T*)(NULL),NULL)) == sizeof(mpl_bool<true>)) };<br />};<br />#endif</p><p>template <class D, class L, class R ><br />struct binary_tree_ref<br />{<br /> typedef D dest_type;<br /> typedef L left_type;<br /> typedef R right_type;</p><p> inline binary_tree_ref(const left_type& lhs, const right_type& rhs):<br /> ldata(lhs), rdata(rhs){<br /> }</p><p> const left_type& ldata;<br /> const right_type& rdata;</p><p>#ifndef _MSVC_6<br /> template <typename T><br /> inline static void op(const T& arg, dest_type& result){<br /> result += arg;<br /> }</p><p> template <typename T, typename M><br /> inline static void op(const binary_tree_ref<dest_type, T, M>& arg, dest_type& result ){<br /> binary_tree_ref<dest_type, T, M>::op(arg.ldata, result);<br /> binary_tree_ref<dest_type, T, M>::op(arg.rdata, result);<br /> }</p><p> inline operator dest_type() const{<br /> dest_type result;<br /> binary_tree_ref<dest_type, left_type, right_type>::op(ldata, result );<br /> binary_tree_ref<dest_type, left_type, right_type>::op(rdata, result );<br /> return result;<br /> }<br />#else //vc6 cannot choose the correct template function, so the 3rd argument is used to help it!</p><p> template <typename T><br /> inline static void op(const T& arg, dest_type& result, mpl_bool<false>* ){<br /> result += arg;<br /> }</p><p> template <typename T, typename M><br /> inline static void op(const binary_tree_ref<dest_type, T, M>& arg, dest_type& result, mpl_bool<true>* ){<br /> binary_tree_ref<dest_type, T, M>::op(arg.ldata, result, (mpl_bool<((bool)check<T>::result)>*)NULL);<br /> binary_tree_ref<dest_type, T, M>::op(arg.rdata, result, (mpl_bool<((bool)check<M>::result)>*)NULL);<br /> }</p><p> inline operator dest_type() const{<br /> dest_type result;<br /> binary_tree_ref<dest_type, left_type, right_type>::op(ldata, result, (mpl_bool<((bool)check<left_type>::result)>*)NULL );<br /> binary_tree_ref<dest_type, left_type, right_type>::op(rdata, result, (mpl_bool<((bool)check<right_type>::result)>*)NULL );<br /> return result;<br /> }<br />#endif<br />};</p><p>}</p><p>template <typename D, typename L, typename R, typename C><br />inline const _et_private::binary_tree_ref<D, _et_private::binary_tree_ref<D, L, R>, C ><br /> operator + (const _et_private::binary_tree_ref<D, L, R>& lhs, const C& rhs){<br /> return _et_private::binary_tree_ref<D, _et_private::binary_tree_ref<D, L, R>, C > (lhs, rhs);<br />}</p><p>template <typename D, typename L><br />inline const _et_private::binary_tree_ref<D, L, D><br /> operator + (const L& lhs, const _et_private::unary_tree_ref<D>& rhs){<br /> return _et_private::binary_tree_ref<D, L, D> (lhs, rhs.data);<br />}<br />template <typename D, typename R><br />inline const _et_private::binary_tree_ref<D, D, R><br /> operator + (const _et_private::unary_tree_ref<D>& lhs, const R& rhs){<br /> return _et_private::binary_tree_ref<D, D, R> (lhs.data, rhs);<br />} </p><p>#ifndef _MSVC_6</p><p>template <typename D, typename L, typename R, typename C><br />inline const _et_private::binary_tree_ref<D, C, _et_private::binary_tree_ref<D, L, R> ><br /> operator + (const C& lhs, const _et_private::binary_tree_ref<D, L, R>& rhs){<br /> return _et_private::binary_tree_ref<D, C, _et_private::binary_tree_ref<D, L, R> > (lhs, rhs);<br />}</p><p>template <typename D, typename L0, typename R0, typename L1, typename R1><br />inline const _et_private::binary_tree_ref<D, _et_private::binary_tree_ref<D, L0, R0>, _et_private::binary_tree_ref<D, L1, R1> ><br /> operator + (const _et_private::binary_tree_ref<D, L0, R0>& lhs, const _et_private::binary_tree_ref<D, L1, R1>& rhs){<br /> return _et_private::binary_tree_ref<D, _et_private::binary_tree_ref<D, L0, R0>, _et_private::binary_tree_ref<D, L1, R1> > (lhs, rhs);<br />}</p><p>#else</p><p>template <typename D, typename L, typename R, typename C><br />inline const _et_private::binary_tree_ref<D, C, _et_private::binary_tree_ref<D, L, R> ><br /> operator + (const C& lhs, _et_private::binary_tree_ref<D, L, R>& rhs){<br /> return _et_private::binary_tree_ref<D, C, _et_private::binary_tree_ref<D, L, R> > (lhs, rhs);<br />}</p><p>#endif</p><p>template <typename D><br />inline _et_private::unary_tree_ref<D><br /> any_seeder(const D& arg){<br /> return _et_private::unary_tree_ref<D> (arg);<br />}</p><p>void test()<br />{<br /> string str1 = "1234567890";<br /> string str2 = "12345678901234567890";<br /> string str3 = "123456789012345678901234567890";<br /> string strb;</p><p> int current = time(NULL);<br /> int i = 0;<br /> for(i = 0 ; i < 10000000; ++i)<br /> {<br /> strb = "12345678901234567890" + any_seeder(str1) + str2 + string("3 ") + str1 + str2 + str3 + (any_seeder(str1) + str2 + ' ') ;<br /> *strb.begin() = '0';<br /> }<br /> printf("%d/n", time(NULL) - current); </p><p> string().swap(strb);<br /> for(i = 0 ; i < 10000000; ++i)<br /> {<br /> strb = "12345678901234567890" + (str1) + str2 + string("3 ") + str1 + str2 + str3 + ((str1) + str2 + ' ') ;<br /> *strb.begin() = '0';<br /> }<br /> printf("%d/n", time(NULL) - current);<br />}</p><p>int main(int , char** )<br />{<br /> test();<br /> return 0;<br />}<br /> 

筆者現在要準備一個考試,“拯救”系列的更新幾乎就暫時到此為止。有問題的朋友敬請留言。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.