• <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>
            aurain
            技術文摘
            posts - 137,  comments - 268,  trackbacks - 0

            glib庫中的哈希函數和比較函數

                   最近在項目中需要用到哈希表,要以ip地址構造哈希函數和比較函數。就去網上找了下相關的資料,看了下glib中哈希表中的實現方式,雖然最終沒用這個,但既然找了就順便記錄下來,方便查閱。

            哈希表是一種提供key-value訪問的數據結構,通過指定的key值可以快速的訪問到與它相關聯的value值。hash表的一種典型用法就是字典,通過單詞的首字母能夠快速的找到單詞。關于哈希表的詳細介紹請查閱數據結構的相關書籍,我這里只介紹glib庫中哈希表的哈希函數和比較函數。

            主要包括針對int, int64, double, string四種數據類型的處理。詳細請看下面的代碼。

             

            typedef char   gchar;

            typedef short  gshort;

            typedef long   glong;

            typedef int    gint;

            typedef gint   gboolean;

             

            typedef unsigned char   guchar;

            typedef unsigned short  gushort;

            typedef unsigned long   gulong;

            typedef unsigned int    guint;

             

            typedef float   gfloat;

            typedef double  gdouble;

            /* Define min and max constants for the fixed size numerical types */

            #define G_MININT8 ((gint8)  0x80)

            #define G_MAXINT8 ((gint8)  0x7f)

            #define G_MAXUINT8   ((guint8) 0xff)

             

            #define G_MININT16   ((gint16)  0x8000)

            #define G_MAXINT16   ((gint16)  0x7fff)

            #define G_MAXUINT16  ((guint16) 0xffff)

             

            #define G_MININT32   ((gint32)  0x80000000)

            #define G_MAXINT32   ((gint32)  0x7fffffff)

            #define G_MAXUINT32  ((guint32) 0xffffffff)

             

            #define G_MININT64   ((gint64) G_GINT64_CONSTANT(0x8000000000000000))

            #define G_MAXINT64   G_GINT64_CONSTANT(0x7fffffffffffffff)

            #define G_MAXUINT64  G_GINT64_CONSTANT(0xffffffffffffffffU)

             

            typedef void* gpointer;

            typedef const void *gconstpointer;

             

            gboolean

            g_int_equal (gconstpointer v1,

                    gconstpointer v2)

            {

              return *((const gint*) v1) == *((const gint*) v2);

            }

             

            guint

            g_int_hash (gconstpointer v)

            {

              return *(const gint*) v;

            }

             

            gboolean

            g_int64_equal (gconstpointer v1,

                           gconstpointer v2)

            {

              return *((const gint64*) v1) == *((const gint64*) v2);

            }

             

            guint

            g_int64_hash (gconstpointer v)

            {

              return (guint) *(const gint64*) v;

            }

             

            gboolean

            g_double_equal (gconstpointer v1,

                            gconstpointer v2)

            {

              return *((const gdouble*) v1) == *((const gdouble*) v2);

            }

             

            guint

            g_double_hash (gconstpointer v)

            {

              return (guint) *(const gdouble*) v;

            }

             

            gboolean

            g_str_equal (gconstpointer v1,

                    gconstpointer v2)

            {

              const gchar *string1 = v1;

              const gchar *string2 = v2;

             

              return strcmp (string1, string2) == 0;

            }

             

            guint

            g_str_hash (gconstpointer v)

            {

              /* 31 bit hash function */

              const signed char *p = v;

              guint32 h = *p;

             

              if (h)

                for (p += 1; *p != '\0'; p++)

                  h = (h << 5) - h + *p;

             

              return h;

            }

             

            posted on 2010-07-06 17:43 閱讀(3842) 評論(1)  編輯 收藏 引用 所屬分類: c/c++基礎知識

            FeedBack:
            # re: glib庫中的哈希函數和比較函數
            2010-07-06 22:39 | 飛鴿傳書
            這么好的東西,現在估計很少人用了,以前開發飛鴿傳書(FreeEIM)項目的時候,有考慮過使用GLIB,后來實在是能力有限,未用上。我強烈支持有能力者用這個東西。  回復  更多評論
              

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(17)

            隨筆分類(138)

            隨筆檔案(137)

            網絡開發

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 497632
            • 排名 - 36

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            99久久香蕉国产线看观香| 久久夜色精品国产噜噜亚洲AV| 亚洲中文字幕无码久久精品1 | 久久91精品国产91久久户| 2019久久久高清456| 日日狠狠久久偷偷色综合免费| 国产精品亚洲综合专区片高清久久久| 99久久免费国产特黄| 东京热TOKYO综合久久精品 | 一本久道久久综合狠狠躁AV| 久久e热在这里只有国产中文精品99 | 久久综合狠狠色综合伊人| 亚洲狠狠久久综合一区77777| 91精品国产乱码久久久久久| 91精品国产高清91久久久久久 | 国产精品对白刺激久久久| 久久久久四虎国产精品| 91亚洲国产成人久久精品网址| 狠狠精品久久久无码中文字幕 | 久久久久亚洲精品无码蜜桃| 久久久久国产精品熟女影院| 国产午夜福利精品久久2021 | 久久国产精品免费一区| 人人狠狠综合久久亚洲| 久久亚洲精品成人AV| 久久免费精品视频| 亚洲国产一成久久精品国产成人综合 | 久久免费视频观看| 中文字幕久久亚洲一区| 久久精品水蜜桃av综合天堂| 国产精品成人99久久久久 | 91精品国产综合久久四虎久久无码一级| 国产精品无码久久综合网| 亚洲国产另类久久久精品黑人| 青草影院天堂男人久久| 色天使久久综合网天天| 久久99精品综合国产首页| 久久综合九色综合网站| 99久久国产综合精品成人影院| 久久亚洲AV成人无码| 精品99久久aaa一级毛片|