libk

A tiny C library that exports functions and data structures for use in small programs.

libk.tgz

Compile & Install

In the shell, in libk/, run

make; make install

The header files go to

/usr/local/include/kak

The static library libk.a goes to

/usr/local/lib/

Usage

This is an example, from libk/test.c, of using libk facilities.

In the shell, in libk/, run

make test
./test

test.c

#include <stdio.h>
#include <stdlib.h>
#include "kcommon.h"
#include "ktree.h"
#include "klist.h"
#include "khash.h"
#include "kpost.h"

#define NUMNODES 10

char s[NUMNODES][10] = {"f", "b", "g", "a", "d",
                        "i", "c", "e", "h", "h"};

char w[6][4] = {"cat", "mat", "bat", "hat", "rat", "sat"};

void testlist();
void testhash();
void testtree();

int main(void)
{
    printf("list:\n");
    testlist();
    printf("hash:\n");
    testhash();
    printf("tree:\n");
    testtree();

    return 0;
}

void testhash()
{
    int c;
    int NHASH = 3;
    Node *np;
    Hash *h;

    h = newhash(NHASH, cmpkpost, hashkpost);
    for (int i = 0; i < 6; i++)
        np = hlookup(h, newnode(newkpost(w[i], i)), 1);

    c = 0;

    printf("fprinthash:\n");
    fprinthash(stdout, h, fprintkpost);

    printf("fprinthashstats:\n");
    fprinthashstats(stdout, h);

    printf("hlookup: ");    
    np = hlookup(h, newnode(newkpost(w[3], 0)), 0);
    if (np != NULL)
        printkpost("{%s:%d} found\n", np->data);
    else
        printkpost("{%s:%d} not found\n", np->data);

    freehash(h, free);
}

void testtree()
{
    TNode *t, *np;
    t = NULL;
    for (int i = 0; i < NUMNODES; i++)
        t = nrinsert(t, newtnode(newkpost(s[i], i)), cmpkpost, kpost_match_handler);

    printf("tlookup:\n");
    np = tlookup(t, newtnode(newkpost("h", 1)), cmpkpost);
    if (np != NULL)
        printkpost("{%s:%d} found\n", np->data);

    printf("nrtlookup:\n");
    np = nrtlookup(t, newtnode(newkpost("h", 1)), cmpkpost);
    if (np != NULL)
        printkpost("{%s:%d} found\n", np->data);

    printf("inorder:\n");
    applyinorder(t, printkpost, "{%s:%d} ");
    printf("\n");

    printf("postorder:\n"); 
    applypostorder(t, printkpost, "{%s:%d} ");
    printf("\n");   
}

void testlist()
{   
    Node *lt, *n, *tmp;

    n = newnode((void *)newkpost(s[1], 1));
    lt = newnode((void *)newkpost(s[0], 0));
    for (int i = 1; i < 3; i++)
        lt = addfront(lt, newnode((void *)newkpost(s[i], i)));

    printf("fprintkpost:\n");
    apply(lt, fprintkpost, stdout);
    printf("\n");

    printf("printkpost:\n");
    apply(lt, printkpost, "{%s:%d} ");
    printf("\n");

    printf("addend {%s:%d}:\n", s[3], 3);
    addend(lt, newnode((void *)newkpost(s[3], 3)));
    apply(lt, printkpost, "{%s:%d} ");
    printf("\n");

    printf("lookup {%s:%d}:\n", ((KPost *)(n->data))->s,
    ((KPost *)(n->data))->f);
    tmp = lookup(lt, n, cmpkpost);
    if (tmp != NULL)
        printkpost("{%s:%d} found\n", tmp->data);
    else
        printkpost("{%s:%d} not found\n", tmp->data);

    printf("delnode {%s:%d}:\n", ((KPost *)(n->data))->s,
    ((KPost *)(n->data))->f);
    delnode(lt, n, cmpkpost, freekpost);
    apply(lt, printkpost, "{%s:%d} ");
    printf("\n");

    freelist(lt, NULL);
}