Subversion Repositories PlanixRsrch.SVN

Rev

Rev 449 | Rev 464 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright 2019, The PLANIX Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _MAVL_H
#define _MAVL_H

#include <stdlib.h>

/** Maximum AVL tree depth.
    The number of nodes is calculated by 2^depth.
    So a value of 32 should be enough for around 4
    billion nodes. This value is only used within
    struct mavliter.
    */
#ifndef MAVL_MAX_DEPTH
#define MAVL_MAX_DEPTH  32
#endif

/**
 * Defines the structure of an AVL Node.
 */
struct mavlnode {
        struct mavlnode * s[2];         /* Pointer to left and right son.
                                           s[0] is the "left" son
                                           s[1] is the right son
                                        */
        int bal:3;                      /* AVL balance factor - 3 bits*/
};

/**
 * Definition of an AVL Tree
 */
struct mavl {
        struct mavlnode *root;          /* Pointer to root node */
        int (*cmp) (const void *, const void *);        /* Compare function */
        void (*del) (void *);           /* Delete element function */
        int count;                      /* Number of elements currently stored
                                        * in the tree */
        void *(*malloc) (size_t n);     /* used to allocate a new mavlnode */
        void (*free) (void *ptr);       /* used to free a mavlnode */

        /*
         * size of data appended to each mavlnode element. 
         * Used to allocate space in mavl_add.
         */
        size_t data_size;
};


void mavlnode_destroy(struct mavl *t, struct mavlnode *n);
struct mavlnode * mavlnode_create(struct mavl *t, const void *data);
struct mavlnode * mavlnode_get(struct mavl *t ,const void *data);

#define mavlnode_dataptr(node) \
        ((void*)(((char*)((void*)(node))) + sizeof(struct mavlnode)))

struct mavl *mavl_create(int (*cmp) (const void *, const void *),
                         void (*del) (void *), size_t data_size);

void *mavl_insert(struct mavl *t, const void *data, int *exists);
void mavl_del_all(struct mavl *t);
void *mavl_del(struct mavl *t, const void *data);
int mavl_get_depth(struct mavl *t);
int mavl_verify(struct mavl *t);
void mavl_print(struct mavl *t, 
                void (*pcb)(char*,struct mavlnode *), int width);
void * mavl_get(struct mavl *t ,const void *data);



void mavl_destroy(struct mavl *t);



struct mavliter {
        struct mavlnode *stack[MAVL_MAX_DEPTH * 2];
        struct mavlnode *cur;
        int stack_ptr;
        struct mavlnode *root;
        int (*cmp) (const void *, const void *);
};

void *mavliter_get(struct mavliter *i);
void mavliter_init(struct mavliter *i, struct mavl *t);
void *mavliter_next(struct mavliter *i);
void *mavliter_seek_set(struct mavliter *i);

#define MAVL_E_NOTFOUND 2
#define MAVL_E_NOMEM    3


#define mavliter_foreach(iterator)\
        for ((void)mavliter_seek_set(iterator); \
                        NULL != mavliter_get(iterator); \
                (void)mavliter_next(iterator))


void mavl_rot(struct mavlnode *n, struct mavlnode **parent, int dir);
void mavl_drot(struct mavlnode *n, struct mavlnode **parent, int dir);



#define MAVL_FIND_FIRST 1
#define MAVL_FIND_LAST  2
void * mavl_get_ext(struct mavl *t ,const void *search, int mode);
#define mavl_get_first(tree,search) mavl_get_ext(tree,search,MAVL_FIND_FIRST)
#define mavl_get_last(tree,search) mavl_get_ext(tree,search,MAVL_FIND_LAST)



#endif