• <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>

            Heath's Blog

            There is no end, it is just the beginning! - A Game Developer's Notes

            實現(xiàn)一個以字符串為Key大小寫不敏感的Hash Map

                Hash Map有兩個關(guān)鍵方法:哈希值計算方法和比較方法。以STLPort為例,通過Hash Map的模版定義可以看出,其缺省的哈希Functor是hash,比較functor為equal_to:

            1. template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
            2. ??????????_STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Key>),
            3. ??????????_STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_STLP_CONST _Key, _Tp) >
            4. class hash_map

                在_hash_fun.h中有針對各種類型的哈希值計算特化版本,下面僅摘錄針對C字符串的代碼:

            1. inline size_t __stl_hash_string(const char* __s) {
            2. ??_STLP_FIX_LITERAL_BUG(__s)
            3. ??unsigned long __h = 0;
            4. ??for ( ; *__s; ++__s)
            5. ????__h = 5*__h + *__s;
            6.  
            7. ??return size_t(__h);
            8. }
            9.  
            10. _STLP_MOVE_TO_STD_NAMESPACE
            11.  
            12. _STLP_TEMPLATE_NULL
            13. struct hash<char*> {
            14. ??size_t operator()(const char* __s) const {
            15. ????_STLP_FIX_LITERAL_BUG(__s)
            16. ????return _STLP_PRIV __stl_hash_string(__s);
            17. ??}
            18. };
            19.  
            20. _STLP_TEMPLATE_NULL
            21. struct hash<const char*> {
            22. ??size_t operator()(const char* __s) const {
            23. ????_STLP_FIX_LITERAL_BUG(__s)
            24. ????return _STLP_PRIV __stl_hash_string(__s);
            25. ??}
            26. };

                __stl_hash_string實現(xiàn)了一個簡單的哈希算法,因為算法中使用了字符的ASCII碼,為了達(dá)到大小寫不敏感的目的,可將每個字符轉(zhuǎn)換成小寫/大寫來計算。對于hash functor,我們也需要一個string的特化版。

                在_function_base.h中定義了equal_to functor:

            1. template <class _Arg1, class _Arg2, class _Result>
            2. struct binary_function {
            3. ??typedef _Arg1 first_argument_type;
            4. ??typedef _Arg2 second_argument_type;
            5. ??typedef _Result result_type;
            6. #if !defined (__BORLANDC__) || (__BORLANDC__ < 0x580)
            7. protected:
            8. ??/* See unary_function comment. */
            9. ??~binary_function() {}
            10. #endif
            11. };
            12.  
            13. template <class _Tp>
            14. struct equal_to : public binary_function<_Tp, _Tp, bool> {
            15. ??bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
            16. };

                通過定制一個string版本的equal_to,使用stricmp進(jìn)行字符串比較。下面列出實現(xiàn)及測試代碼:

            Test Codes
            1. #include <hash_map>
            2. #include <string>
            3. #include <algorithm>
            4. #include <cctype>
            5.  
            6. inline size_t __stl_hash_string(const char* __s)
            7. {
            8. ????unsigned long __h = 0;
            9. ????for ( ; *__s; ++__s)
            10. ????????__h = 5*__h + tolower(*__s);
            11.  
            12. ????return size_t(__h);
            13. }
            14.  
            15. template<>
            16. struct stlport::hash<stlport::string>
            17. {
            18. ????size_t operator()(const stlport::string& __s) const
            19. ????{
            20. ????????return __stl_hash_string(__s.c_str());
            21. ????}
            22. };
            23.  
            24. template<>
            25. struct stlport::equal_to<stlport::string>
            26. ????: public stlport::binary_function<stlport::string , stlport::string , bool>
            27. {
            28. ????bool operator()(const stlport::string& __x, const stlport::string& __y) const
            29. ????{
            30. ????????return !_stricmp(__x.c_str() , __y.c_str());
            31. ????}
            32. };
            33.  
            34. int _tmain(int argc, _TCHAR* argv[])
            35. {
            36. ????stlport::hash_map<stlport::string , int> map;
            37.  
            38. ????map.insert(stlport::make_pair("Test" , 123));
            39.  
            40. ????stlport::hash_map<stlport::string , int>::iterator iter = map.find("teSt");
            41. ????if(iter != map.end())
            42. ????????printf("Found!\n");
            43.  
            44. ????return 0;
            45. }

            posted on 2011-07-13 23:36 Heath 閱讀(3827) 評論(0)  編輯 收藏 引用 所屬分類: Studying

            国产福利电影一区二区三区久久久久成人精品综合 | 怡红院日本一道日本久久| 香蕉久久一区二区不卡无毒影院| 久久电影网一区| 中文精品久久久久人妻| 99久久国产热无码精品免费| 精品无码久久久久久国产| 久久综合给合久久国产免费| 久久国产精品二国产精品| 久久久精品人妻一区二区三区四| 久久久精品日本一区二区三区| 久久国产亚洲精品无码| 久久天天躁夜夜躁狠狠| 国产—久久香蕉国产线看观看| 中文字幕精品久久久久人妻| 久久99精品国产99久久| 亚洲精品国精品久久99热一| 亚洲精品美女久久久久99小说| 岛国搬运www久久| 久久99国产亚洲高清观看首页| 亚洲av日韩精品久久久久久a | 久久久久久国产精品无码下载 | 色婷婷久久久SWAG精品| 久久国产香蕉视频| 亚洲国产精品久久久久久| 久久久久久九九99精品| 欧美黑人激情性久久| 亚洲伊人久久精品影院| 人妻无码精品久久亚瑟影视| 亚洲婷婷国产精品电影人久久| 精品久久久久久无码免费| 久久精品国产亚洲5555| 久久精品国产亚洲Aⅴ蜜臀色欲| 国产高潮国产高潮久久久91 | 国产精品99久久精品爆乳| 国产V综合V亚洲欧美久久| 成人久久综合网| 久久九九久精品国产免费直播| 无码乱码观看精品久久| 一日本道伊人久久综合影| 亚洲午夜久久久久久久久电影网|