C語言常用功能庫:Klib
Klib 是一個C語言常用功能庫,是一個輕量級和獨立的 Glib 版本。大多數組件都是獨立的外部庫,除了標準C庫,并且相互獨立。要使用這個庫的一個組成部分,你只需要幾個文件復制到你的源代碼樹,而不必擔心庫的依賴。 Klib求效率和小內存占用。有些部件,如khash.h,kbtree.h,ksort.h和kvec.h,都是相似的算法或數據結構中所有的編程語言的在速度和存儲器使用方面最有效的實現方式。
包括如下常用組件:
-
khash.h: generic hash table based on double hashing.
-
ksort.h: generic sort, including introsort, merge sort, heap sort, comb sort, Knuth shuffle and the k-small algorithm.
-
kseq.h: generic stream buffer and a FASTA/FASTQ format parser.
-
kvec.h: generic dynamic array.
-
klist.h: generic single-linked list and memory pool.
-
kstring.{h,c}: basic string library.
-
kmath.{h,c}: numerical routines including MT19937-64 pseudorandom generator, basic nonlinear programming and a few special math functions.
示例代碼:
#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; }
本文由用戶 f663x 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!