C语言常用功能库:Klib

f663x 9年前

Klib 是一个C语言常用功能库,是一个轻量级和独立的 Glib 版本。大多数组件都是独立的外部库,除了标准C库,并且相互独立。要使用这个库的一个组成部分,你只需要几个文件复制到你的源代码树,而不必担心库的依赖。 Klib求效率和小内存占用。有些部件,如khash.h,kbtree.h,ksort.h和kvec.h,都是相似的算法或数据结构中所有的编程语言的在速度和存储器使用方面最有效的实现方式。

包括如下常用组件:

示例代码:

#include "khash.h"  KHASH_MAP_INIT_INT(m32, char)        // instantiate structs and methods  int main() {      int ret, is_missing;      khint_t k;      khash_t(m32) *h = kh_init(m32);  // allocate a hash table      k = kh_put(m32, h, 5, &ret);     // insert a key to the hash table      if (!ret) kh_del(m32, h, k);      kh_value(h, k) = 10;             // set the value      k = kh_get(m32, h, 10);          // query the hash table      is_missing = (k == kh_end(h));   // test if the key is present      k = kh_get(m32, h, 5);      kh_del(m32, h, k);               // remove a key-value pair      for (k = kh_begin(h); k != kh_end(h); ++k)  // traverse          if (kh_exist(h, k))          // test if a bucket contains data              kh_value(h, k) = 1;      kh_destroy(m32, h);              // deallocate the hash table      return 0;  }


项目主页:http://www.open-open.com/lib/view/home/1426332589451