浅析工具库中字典的实现

408 阅读2分钟

这是我参与8月更文挑战的第 7天,活动详情查看:8月更文挑战

介绍一下TBOX和GLIB中的字典实现。

Tbox

dictionary

实现方式:

/// the dictionary item type
typedef struct __tb_oc_dictionary_item_t
{
    /// the key
    tb_char_t const*    key;
​
    /// the value
    tb_object_ref_t     val;
​
}tb_oc_dictionary_item_t;
​
// the dictionary type
typedef struct __tb_oc_dictionary_t
{
    // the object base
    tb_object_t         base;
​
    // the capacity size
    tb_size_t           size;
​
    // the object hash
    tb_hash_map_ref_t   hash;
​
    // increase refn?
    tb_bool_t           incr;
​
}tb_oc_dictionary_t;
​
​

使用场景

  • 要创建 dictionary,使用 tb_oc_dictionary_init()
  • 返回字典大小,使用tb_oc_dictionary_size()
  • 设置引用,使用tb_oc_dictionary_incr()
  • 创建迭代器,使用tb_oc_dictionary_itor();
  • 插入键和值,使用 tb_oc_dictionary_insert()
  • 若要查找与给定键对应的值,使用 tb_oc_dictionary_value ()
  • 若要删除键和值,使用 tb_oc_dictionary_remove ()

Glib

Hash Tables

GHashTable 提供键和值之间的关联,这些键和值经过优化后可以在分摊 o (1)中找到、插入或删除关联值。遍历每个元素的所有操作都需要 o (n)时间(列出所有键/值、表调整大小等)。

实现方式:

struct _GHashTable
{
  gsize            size;
  gint             mod;
  guint            mask;
  gint             nnodes;
  gint             noccupied;  /* nnodes + tombstones */
​
  guint            have_big_keys : 1;
  guint            have_big_values : 1;
​
  gpointer         keys;
  guint           *hashes;
  gpointer         values;
​
  GHashFunc        hash_func;
  GEqualFunc       key_equal_func;
  gatomicrefcount  ref_count;
#ifndef G_DISABLE_ASSERT
  /*
   * Tracks the structure of the hash table, not its contents: is only
   * incremented when a node is added or removed (is not incremented
   * when the key or data of a node is modified).
   */
  int              version;
#endif
  GDestroyNotify   key_destroy_func;
  GDestroyNotify   value_destroy_func;
};
​
typedef struct
{
  GHashTable  *hash_table;
  gpointer     dummy1;
  gpointer     dummy2;
  gint         position;
  gboolean     dummy3;
  gint         version;
} RealIter;
​
​
typedef struct _GHashTable  GHashTable;
​
typedef gboolean  (*GHRFunc)  (gpointer  key,
                               gpointer  value,
                               gpointer  user_data);
​
typedef struct _GHashTableIter GHashTableIter;
​
struct _GHashTableIter
{
  /*< private >*/
  gpointer      dummy1;
  gpointer      dummy2;
  gpointer      dummy3;
  int           dummy4;
  gboolean      dummy5;
  gpointer      dummy6;
};

使用场景

  • 要创建 GHashTable,使用 g_hash_table_new ()
  • 若要向 GHashTable 中插入键和值,使用 g_hash_table_insert ()
  • 若要查找与给定键对应的值,使用 g_hash_table_lookup ()g_hash_table_lookup_extended ()
  • 还可以使用 g_ hash_table_lookup_extended ()简单地检查哈希表中是否存在键。
  • 若要删除键和值,使用 g_hash_table_remove ()
  • 要为每个键和值对调用函数,可以使用 g_hash_table_foreach ()或使用迭代器迭代哈希表中的键/值对,请参见 GHashTableIter。没有定义哈希表的迭代顺序,您不能依赖于按照插入键/值的相同顺序对它们进行迭代。
  • 使用 ghash_table_destroy ()来销毁一个 GHashTable
  • 哈希表的一个常见用例是存储关于一组键的信息,而不将任何特定值与每个键关联。GHashTable 优化了这样做的一种方法: 如果您只存储 key == value 的键值对,那么 GHashTable 不会分配内存来存储这些值,如果您的集合很大,这可以节省相当多的空间。函数 g_hash_table_add ()g_hash_table_contains ()被设计成这样使用 GHashTable