All Projects → mirek → rb_tree

mirek / rb_tree

Licence: other
Red-black tree C implementation

Projects that are alternatives of or similar to rb tree

physfs-old
UNOFFICIAL Git mirror of PhysicsFS Mercurial repository. The official repository has also moved to GitHub; this one will no longer be updated. Official website:
Stars: ✭ 53 (+60.61%)
Mutual labels:  c-language
C-Programming
C Programming Experiments
Stars: ✭ 25 (-24.24%)
Mutual labels:  c-language
Fstar
A Proof-oriented Programming Language
Stars: ✭ 2,171 (+6478.79%)
Mutual labels:  c-language
Learn-to-program-with-C AR
ترجمة لدرس تعلّم البرمجة بلغة السي الخاص بموقع OpenClassrooms
Stars: ✭ 51 (+54.55%)
Mutual labels:  c-language
Plotty
C language compiler from scratch for a custom architecture, with virtual machine and all
Stars: ✭ 33 (+0%)
Mutual labels:  c-language
pkcs11-tools
A set of tools to manage objects on PKCS#11 crypotographic tokens. Compatible with any PKCS#11 library, including NSS.
Stars: ✭ 70 (+112.12%)
Mutual labels:  c-language
stress
Single-purpose tools to stress resources
Stars: ✭ 24 (-27.27%)
Mutual labels:  c-language
mescc
Mike's Enhanced Small C Compiler for Z80 and CP/M.
Stars: ✭ 23 (-30.3%)
Mutual labels:  c-language
30-seconds-of-c
🔌Curated collection of useful C Programming tutorials, snippets, and projects that you can understand in 30 seconds or less.
Stars: ✭ 29 (-12.12%)
Mutual labels:  c-language
Cuik
A Modern C11 compiler (STILL EARLY)
Stars: ✭ 102 (+209.09%)
Mutual labels:  c-language
the-c-programming-language-2nd-edition-solutions
Solutions to the exercises in the book "The C Programming Language" (2nd edition) by Brian W. Kernighan and Dennis M. Ritchie. This book is also referred to as K&R.
Stars: ✭ 245 (+642.42%)
Mutual labels:  c-language
async2.h
Stackful Async Subroutines for C. Brings async 2 C
Stars: ✭ 87 (+163.64%)
Mutual labels:  c-language

Red-black tree C implementation

Based on Julienne Walker's http://eternallyconfuzzled.com/ red-black tree implementation.

Usage

First, grab the source into your project, you'll need just those two:

wget https://raw.github.com/mirek/rb_tree/master/rb_tree.h
wget https://raw.github.com/mirek/rb_tree/master/rb_tree.c

Let's say you want to store struct iovec objects (defined in sys/uio.h) with the following definition:

struct iovec {
	  void  *iov_base; // [XSI] Base address of I/O memory region
  	size_t iov_len;  // [XSI] Size of region iov_base points to
};

You'll need to provide comparison callback for your object. Let's say you want to keep track of the length of the iovec buffers, your callback should look similar to:

int
my_cmp_cb (struct rb_tree *self, struct rb_node *node_a, struct rb_node *node_b) {
    struct iovec *a = (struct iovec *) node_a->value;
    struct iovec *b = (struct iovec *) node_b->value;
    return (a->iov_len > b->iov_len) - (a->iov_len < b->iov_len);
}

To create your red-black tree:

struct rb_tree *tree = rb_tree_create(my_cmp_cb);
if (tree) {
    
    // Use the tree here...
    for (int i = 0; i < 10; i++) {
        struct iovec *v = malloc(sizeof *v);
        v->iov_base = (void *) i;
        v->iov_len = i * i;
        
        // Default insert, which allocates internal rb_nodes for you.
        rb_tree_insert(tree, v);
    }
    
    // To f
    struct iovec *f = rb_tree_find(tree, & (struct iovec) { .iov_base = (void *) 7, .iov_len = 0 });
    if (r) {
        fprintf(stdout, "found iovec(.iov_base = %p, .iov_len = %zu)\n", f->iov_base, f->iov_len)
    } else {
        printf("not found\n");
    }

    // Dealloc call can take optional parameter to notify on each node
    // being deleted so you can free the node and/or your object:
    rb_tree_dealloc(tree, NULL);
}

Above example will leak because malloc is not balanced with free.

If you want to iterate over elements (here in reverse order):

struct rb_iter *iter = rb_iter_create();
if (iter) {
    for (struct iovec *v = rb_iter_last(iter, tree); v; v = rb_iter_prev(iter)) {
        printf("- %p %zu\n", v->iov_base, v->iov_len);
    }
    rb_iter_dealloc(iter);
}

License

None (Public Domain).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].