From: Heng Li Date: Thu, 17 Sep 2015 23:43:17 +0000 (-0400) Subject: simplified kbtree iter example X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=777ef8f8d58d432b4f2f5b42ac9962c8f35c83c5;p=klib.git simplified kbtree iter example --- diff --git a/index.html b/index.html index 6834bbc..521ac6a 100644 --- a/index.html +++ b/index.html @@ -193,6 +193,8 @@ Error message and password prompt
  • CSS
  • +
  • Draft of 'KBtree: generic ordered map'
  • +
  • Generic Programming in C
  • KBtree: generic ordered map
  • @@ -5767,7 +5769,7 @@ pre .xml .cdata {
    lh3
    -
    +
    
     
    @@ -5914,6 +5916,53 @@ code { color: #333333; }
    +
    +
    !!Synopsis
    +* Functionality: generic balanced search tree based on B-tree.
    +* Library source code: [[kbtree.h|https://github.com/attractivechaos/klib/blob/master/kbtree.h]]
    +* Dependencies: none
    +* Related articles: [[B-tree vs binary search tree|https://attractivechaos.wordpress.com/2008/09/24/b-tree-vs-binary-search-tree/]] and [[Another look at my old benchmark|https://attractivechaos.wordpress.com/2008/10/07/another-look-at-my-old-benchmark/]]
    +!!Example
    +!!!Example 1: count distinct words on the command line
    +```c
    +// gcc -O2 this_prog.c; ./a.out two one three two three three
    +#include <stdio.h>
    +#include "kbtree.h"
    +
    +typedef struct {
    +    char *key;
    +    int count;
    +} elem_t;
    +
    +#define elem_cmp(a, b) (strcmp((a).key, (b).key))
    +KBTREE_INIT(str, elem_t, elem_cmp)
    +
    +int main(int argc, char *argv[])
    +{
    +    kbtree_t(str) *b;
    +    elem_t *p, t;
    +    kbitr_t itr;
    +    int i;
    +    b = kb_init(str, KB_DEFAULT_SIZE);
    +    for (i = 1; i < argc; ++i) {
    +        // no need to allocate; just use pointer
    +        t.key = argv[i], t.count = 1;
    +        p = kb_getp(str, b, &t); // kb_get() also works
    +        // IMPORTANT: put() only works if key is absent
    +        if (!p) kb_putp(str, b, &t);
    +        else ++p->count;
    +    }
    +    // ordered tree traversal
    +    kb_itr_first(str, b, &itr); // get an iterator pointing to the first
    +    for (; kb_itr_valid(&itr); kb_itr_next(str, b, &itr)) { // move on
    +        p = &kb_itr_key(elem_t, &itr);
    +        printf("%d\t%s\n", p->count, p->key);
    +    }
    +    kb_destroy(str, b);
    +    return 0;
    +}
    +```
    +
    For the implementation of generic [[containers|http://en.wikipedia.org/wiki/Container_(abstract_data_type)]], klib extensively uses C
     macros. To use these data structures, we usually need to instantiate methods by
    @@ -6052,7 +6101,7 @@ as type-specific code. A generic library written with `void*` will not get such
     performance boost.
     
    -
    +
    !!Synopsis
     * Functionality: generic balanced search tree based on B-tree.
     * Library source code: [[kbtree.h|https://github.com/attractivechaos/klib/blob/master/kbtree.h]]
    @@ -6077,6 +6126,7 @@ int main(int argc, char *argv[])
     {
         kbtree_t(str) *b;
         elem_t *p, t;
    +    kbitr_t itr;
         int i;
         b = kb_init(str, KB_DEFAULT_SIZE);
         for (i = 1; i < argc; ++i) {
    @@ -6088,13 +6138,9 @@ int main(int argc, char *argv[])
             else ++p->count;
         }
         // ordered tree traversal
    -    if (kb_size(b) > 0) { // not working on empty trees
    -        kbitr_t itr;
    -        kb_itr_first(str, b, &itr); // get the first iterator
    -        do {
    -            p = &kb_itr_key(elem_t, &itr); // get pointer to the key
    -            printf("%d\t%s\n", p->count, p->key);
    -        } while (kb_itr_next(str, b, &itr)); // move on
    +    for (kb_itr_first(str, b, &itr); kb_itr_valid(&itr); kb_itr_next(str, b, &itr)) {
    +        p = &kb_itr_key(elem_t, &itr);
    +        printf("%d\t%s\n", p->count, p->key);
         }
         kb_destroy(str, b);
         return 0;