Klib是一个C通用库,是一个轻量级和独立的Glib版本。
包括如下常用组件:
khash.h:具有开放地址的通用哈希表。
kbtree.h:基于B树的通用搜索树。
kavl.h:通用侵入式AVL树。ksort.h:通用排序,包括itrosort,merge排序,heap排序,comb排序,Kuth随机播放和k-small算法。
kseq.h:通用流缓冲区和FASTA / FASTQ格式解析器。
kvec.h:通用动态数组。
klist.h:通用单链列表和内存池。
kstrig.{h,c}:基本的字符串库。
kmath.{h,c}:包括MT19937-64 伪随机数生成器,基本非线性编程和一些特殊数学函数的数值例程。
ketopt.h:具有类似getopt_log的API的可移植命令行参数解析器。示例代码:
#iclude "khash.h"KHASH_MAP_INIT_INT(m32, char) // istatiate structs ad methodsit mai() { it ret, is_missig; khit_t k; khash_t(m32) *h = kh_iit(m32); // allocate a hash table k = kh_put(m32, h, 5, &ret); // isert 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_missig = (k == kh_ed(h)); // test if the key is preset k = kh_get(m32, h, 5); kh_del(m32, h, k); // remove a key-value pair for (k = kh_begi(h); k != kh_ed(h); ++k) // traverse if (kh_exist(h, k)) // test if a bucket cotais data kh_value(h, k) = 1; kh_destroy(m32, h); // deallocate the hash table retur 0;}
评论