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

            glib庫(kù)中的哈希函數(shù)和比較函數(shù)

                   最近在項(xiàng)目中需要用到哈希表,要以ip地址構(gòu)造哈希函數(shù)和比較函數(shù)。就去網(wǎng)上找了下相關(guān)的資料,看了下glib中哈希表中的實(shí)現(xiàn)方式,雖然最終沒(méi)用這個(gè),但既然找了就順便記錄下來(lái),方便查閱。

            哈希表是一種提供key-value訪問(wèn)的數(shù)據(jù)結(jié)構(gòu),通過(guò)指定的key值可以快速的訪問(wèn)到與它相關(guān)聯(lián)的value值。hash表的一種典型用法就是字典,通過(guò)單詞的首字母能夠快速的找到單詞。關(guān)于哈希表的詳細(xì)介紹請(qǐng)查閱數(shù)據(jù)結(jié)構(gòu)的相關(guān)書籍,我這里只介紹glib庫(kù)中哈希表的哈希函數(shù)和比較函數(shù)。

            主要包括針對(duì)int, int64, double, string四種數(shù)據(jù)類型的處理。詳細(xì)請(qǐng)看下面的代碼。

             

            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 閱讀(3849) 評(píng)論(1)  編輯 收藏 引用 所屬分類: c/c++基礎(chǔ)知識(shí)

            FeedBack:
            # re: glib庫(kù)中的哈希函數(shù)和比較函數(shù)
            2010-07-06 22:39 | 飛鴿傳書
            這么好的東西,現(xiàn)在估計(jì)很少人用了,以前開(kāi)發(fā)飛鴿傳書(FreeEIM)項(xiàng)目的時(shí)候,有考慮過(guò)使用GLIB,后來(lái)實(shí)在是能力有限,未用上。我強(qiáng)烈支持有能力者用這個(gè)東西。  回復(fù)  更多評(píng)論
              

            <2008年2月>
            272829303112
            3456789
            10111213141516
            17181920212223
            2425262728291
            2345678

            常用鏈接

            留言簿(17)

            隨筆分類(138)

            隨筆檔案(137)

            網(wǎng)絡(luò)開(kāi)發(fā)

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 499069
            • 排名 - 36

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            精品久久久久中文字幕一区| 国内精品伊人久久久影院| 久久99国产综合精品| 久久w5ww成w人免费| 久久97久久97精品免视看秋霞| 久久久久亚洲?V成人无码| 国产免费久久精品99re丫y| 久久亚洲精品国产精品| 国内精品久久国产大陆| 伊人色综合久久天天网| 国产精品九九九久久九九| 四虎影视久久久免费| 久久免费高清视频| 午夜天堂精品久久久久| 婷婷久久综合九色综合绿巨人| 2021久久国自产拍精品| 久久人人爽人人爽人人爽| 久久精品国产欧美日韩| 99久久er这里只有精品18| 久久精品国产亚洲AV久| 精品国产91久久久久久久a| 国产成年无码久久久久毛片| 久久精品国产日本波多野结衣| 国产精品成人99久久久久 | 久久人妻少妇嫩草AV蜜桃| 欧美亚洲色综久久精品国产| 亚洲精品NV久久久久久久久久| 国产精品永久久久久久久久久| 久久精品国产69国产精品亚洲| 亚洲国产欧美国产综合久久| 久久久精品国产| 亚洲七七久久精品中文国产| 久久亚洲中文字幕精品一区四| 久久久久久国产精品无码下载| 成人久久久观看免费毛片| 久久er热视频在这里精品| 国产精品天天影视久久综合网| 久久超乳爆乳中文字幕| 久久男人Av资源网站无码软件| 久久99精品久久久久久动态图| 久久久精品国产sm调教网站|