/*
* Copyright (c) 2006 Vojtech Mencl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/** @addtogroup genericadt
* @{
*/
/** @file
*/
#ifndef KERN_EXTAVLTREE_H_
#define KERN_EXTAVLTREE_H_
#include <arch/types.h>
/*
* Makro for getting pointer to structure which enclosure extavltree structure.
*
* @param link Pointer to extavltree structure.
* @param type Name of outer structure.
* @param member Name of extavltree atribute in outer structure.
*/
#define extavltree_get_instance(link,type,member) \
((type *)(((uint8_t *)(link)) - ((uint8_t *)&(((type *)NULL)->member))))
typedef struct extavltree_node extavltree_node_t;
typedef struct extavltree extavltree_t;
/** Extended AVL tree node structure. */
struct extavltree_node
{
/**
* Pointer to left descendand of this node.
*
* All keys of nodes in the left subtree are less then key of this node.
*/
extavltree_node_t *lft;
/**
* Pointer to right descendand of this node.
*
* All keys of nodes in the right subtree are greater then key of this node.
*/
extavltree_node_t *rgt;
/** Pointer to parent node. Root node has NULL parent. */
extavltree_node_t *par;
/** Pointer to next node which has equal or the closest greater key or head. */
extavltree_node_t *next;
/** Pointer to previous node which has equal or the closest lesser key or head. */
extavltree_node_t *prev;
/** Height of left subtree. */
int16_t lft_height;
/** Height of right subtree. */
int16_t rgt_height;
/** Nodes key. */
uint64_t key;
};
/** Extended AVL tree structure. */
struct extavltree
{
/** Extended AVL root node pointer. */
extavltree_node_t *root;
/** Head of doubly linked list of nodes. */
extavltree_node_t head;
/**
* Base of tree is value that is smaller or equal then every value in tree
* (valid for positive keys otherwise ignore this atribute).
*
* Base is added to current key when new node is inserted into tree.
* Base is changed to the key of node which is deleted with function
* extavltree_delete_min.
*/
uint64_t base;
};
/** Initialize node.
*
* @param node Node which is initialized.
*/
static inline void extavltree_node_initialize(extavltree_node_t *node)
{
node->lft = NULL;
node->rgt = NULL;
node->lft_height = 0;
node->rgt_height = 0;
node->par = NULL;
node->next = node;
node->prev = node;
node->key = 0;
};
/** Create empty Extended AVL tree.
*
* @param t Extended AVL tree structure.
*/
static inline void extavltree_create (extavltree_t *t) //jen inicializace
{
t->root = NULL;
t->base = 0;
extavltree_node_initialize(&t->head);
};
extavltree_node_t *extavltree_search(extavltree_t *t, uint64_t key);
void extavltree_insert(extavltree_t *t, extavltree_node_t *newnode);
void extavltree_delete(extavltree_t *t, extavltree_node_t *node);
bool extavltree_delete_min(extavltree_t *t);
#endif
/** @}
*/