2015-04-17 07:45:02 +02:00
|
|
|
/*
|
|
|
|
This file is part of libcapwap.
|
|
|
|
|
|
|
|
libcapwap is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2015-05-01 20:13:49 +02:00
|
|
|
* @brief MAVL, Mini AVL Tree,
|
|
|
|
* Yet another AVL Tree implementation!
|
2015-04-17 07:45:02 +02:00
|
|
|
*/
|
|
|
|
|
2015-04-19 16:44:20 +02:00
|
|
|
|
|
|
|
/**
|
2016-03-27 11:53:33 +02:00
|
|
|
* @defgroup MAVL MAVL
|
2015-04-19 16:44:20 +02:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __MAVL_H
|
|
|
|
#define __MAVL_H
|
2015-04-17 07:45:02 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-03-27 11:53:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup MAV_CONSTANTS Constants
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-04-17 07:45:02 +02:00
|
|
|
/** 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. */
|
2015-04-19 16:44:20 +02:00
|
|
|
#define MAVL_MAX_DEPTH 32
|
2015-04-17 07:45:02 +02:00
|
|
|
|
2016-03-27 11:53:33 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2015-04-17 07:45:02 +02:00
|
|
|
/**
|
|
|
|
* Defines the structure of an AVL Node.
|
|
|
|
*/
|
|
|
|
struct mavlnode {
|
2016-03-06 11:07:49 +01:00
|
|
|
/** Pointer to data, thant belongs to the node */
|
2015-04-17 07:45:02 +02:00
|
|
|
void *data;
|
2016-03-06 11:07:49 +01:00
|
|
|
/** Pointer to left son*/
|
2015-04-17 07:45:02 +02:00
|
|
|
struct mavlnode *left;
|
2016-03-06 11:07:49 +01:00
|
|
|
/** Pointer to right son*/
|
2015-04-17 07:45:02 +02:00
|
|
|
struct mavlnode *right;
|
2016-03-06 11:07:49 +01:00
|
|
|
/** AVL balance */
|
2015-04-17 07:45:02 +02:00
|
|
|
int bal;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AVL Tree
|
|
|
|
*/
|
|
|
|
struct mavl {
|
2015-04-19 16:44:20 +02:00
|
|
|
/** Pointer to root node */
|
2015-04-17 07:45:02 +02:00
|
|
|
struct mavlnode *root;
|
2015-04-19 16:44:20 +02:00
|
|
|
/** Compare function */
|
2015-04-17 07:45:02 +02:00
|
|
|
int (*cmp) (const void *, const void *);
|
2015-04-19 16:44:20 +02:00
|
|
|
/** Delete element function */
|
2015-04-17 07:45:02 +02:00
|
|
|
void (*del) (void *);
|
2015-04-19 16:44:20 +02:00
|
|
|
/** Number of elements currently stored in the tree */
|
2015-04-17 07:45:02 +02:00
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
2015-04-19 16:44:20 +02:00
|
|
|
/**
|
|
|
|
* MAVL AVL Tree type
|
|
|
|
*/
|
2015-04-17 07:45:02 +02:00
|
|
|
typedef struct mavl * mavl_t;
|
|
|
|
|
|
|
|
|
2016-03-27 11:53:33 +02:00
|
|
|
/**
|
|
|
|
* @defgroup MAVL_FUNCTIONS Functions
|
|
|
|
* @{
|
|
|
|
*/
|
2015-04-17 07:45:02 +02:00
|
|
|
struct mavl *mavl_create(int (*cmp) (const void *, const void *),
|
|
|
|
void (*del) (void *));
|
2015-10-17 18:06:37 +02:00
|
|
|
|
2015-04-19 16:44:20 +02:00
|
|
|
void mavlnode_destroy(struct mavl *t, struct mavlnode *n);
|
2015-04-17 07:45:02 +02:00
|
|
|
|
|
|
|
void mavl_del_all(struct mavl *t);
|
|
|
|
void *mavl_del(struct mavl *t, void *data);
|
|
|
|
void *mavl_add(struct mavl *t, void *data);
|
|
|
|
void * mavl_get(struct mavl *t ,void *data);
|
|
|
|
struct mavlnode *mavl_get_node(struct mavl *t, void *data);
|
2015-05-01 12:48:48 +02:00
|
|
|
void mavl_merge(mavl_t m, mavl_t t);
|
|
|
|
|
2015-04-17 07:45:02 +02:00
|
|
|
|
|
|
|
extern int mavl_foreach_lr(struct mavlnode *n, int (*callback) (void *, void *),
|
|
|
|
void *cbpriv);
|
|
|
|
extern int mavl_foreach_rl(struct mavlnode *n, int (*callback) (void *, void *),
|
|
|
|
void *cbpriv);
|
|
|
|
int mavl_foreach_from_lr(struct mavl *t, struct mavlnode *n, void *data,
|
|
|
|
int (*callback) (void *, void *), void *cbpriv);
|
|
|
|
|
2016-03-27 11:53:33 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
2015-04-17 07:45:02 +02:00
|
|
|
|
|
|
|
//extern void mavl_foreach(struct mavl *t, int (*callback)(void *,void*),void *cbpriv,int dir);
|
|
|
|
|
|
|
|
static inline void *mavl_replace_data(struct mavl *t, void *data, int len)
|
|
|
|
{
|
|
|
|
void *df = mavl_get(t, data);
|
|
|
|
if (!df)
|
|
|
|
return NULL;
|
|
|
|
memcpy(df, data, len);
|
|
|
|
return df;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *mavl_replace(struct mavl *t,void *data){
|
|
|
|
struct mavlnode * node = mavl_get_node(t,data);
|
|
|
|
if (node){
|
|
|
|
t->del(node->data);
|
|
|
|
return node->data=data;
|
|
|
|
}
|
|
|
|
return mavl_add(t,data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void mavl_destroy(struct mavl *t)
|
|
|
|
{
|
|
|
|
mavl_del_all(t);
|
|
|
|
free (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define mavl_find(t,d) mavl_get(t,d)
|
|
|
|
#define mavl_insert(t,d) mavl_add(t,d)
|
|
|
|
//#define mavl_walk(t,dir) mavl_foreach(t,dir)
|
|
|
|
|
|
|
|
#define mavl_foreach_asc(t,cb,priv) mavl_foreach_lr((t)->root,cb,priv)
|
|
|
|
#define mavl_foreach_desc(t,cb,priv) mavl_foreach_rl((t)->root,cb,priv)
|
|
|
|
|
|
|
|
#define mavl_foreach_from_asc(t,d,cb,priv) mavl_foreach_from_lr(t,(t)->root,d,cb,priv);
|
|
|
|
|
|
|
|
|
|
|
|
struct mavliter{
|
2015-04-19 16:44:20 +02:00
|
|
|
struct mavlnode *stack[MAVL_MAX_DEPTH*2];
|
2015-04-17 07:45:02 +02:00
|
|
|
|
|
|
|
struct mavlnode *cur;
|
|
|
|
int stack_ptr;
|
|
|
|
struct mavlnode * root;
|
|
|
|
int (*cmp) (const void *, const void *);
|
|
|
|
|
|
|
|
};
|
|
|
|
typedef struct mavliter mavliter_t;
|
|
|
|
|
|
|
|
|
|
|
|
void * mavliter_next(mavliter_t *i);
|
|
|
|
|
|
|
|
static inline void * mavliter_seek_set(struct mavliter *i)
|
|
|
|
{
|
|
|
|
i->stack_ptr=0;
|
|
|
|
i->cur=i->root;
|
|
|
|
return mavliter_next(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init an AVL Tree Iterator.
|
|
|
|
*
|
|
|
|
* After initialization #mavliter_next would return the first element.
|
|
|
|
* The behavior of #mavliter_get would still be undefined.
|
|
|
|
* @param i AVL Iterator to initialize
|
|
|
|
* @param t correspondending AVL Tree
|
|
|
|
*
|
|
|
|
* @See mavliter_t,
|
|
|
|
*/
|
|
|
|
static inline void mavliter_init(mavliter_t *i, mavl_t t){
|
|
|
|
i->root = t->root;
|
|
|
|
i->stack_ptr=0;
|
|
|
|
i->cmp=t->cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the element, where AVL Iterator currently is positioned.
|
|
|
|
* @param i AVL Iterator
|
|
|
|
* @return element or NULL if not found.
|
|
|
|
*/
|
|
|
|
static inline void * mavliter_get(mavliter_t *i){
|
|
|
|
if(!i->cur)
|
|
|
|
return NULL;
|
|
|
|
return i->cur->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern void * mavliter_seek(mavliter_t *i,void *d);
|
|
|
|
|
2015-04-19 16:44:20 +02:00
|
|
|
/**
|
2016-03-19 12:56:15 +01:00
|
|
|
* Define a AVL Iterator varialble and accociate it with
|
2015-04-19 16:44:20 +02:00
|
|
|
* an AVL Tree.
|
2016-03-19 12:56:15 +01:00
|
|
|
* @param i Name of the variable to define
|
|
|
|
* @param t #mavl_t Tree to associate
|
2016-03-27 11:53:33 +02:00
|
|
|
* @see #mavliter_get #mavliter_foreach
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* \code
|
|
|
|
mavl_t datatree = mavl_create();
|
|
|
|
|
|
|
|
// fill up datatree with some values
|
|
|
|
|
|
|
|
MAVLITER_DEFINE (i,datatree);
|
|
|
|
mavliter_foreach(&i) {
|
|
|
|
// Get the current value
|
|
|
|
void * value = mavliter_get(&i);
|
|
|
|
|
|
|
|
}
|
|
|
|
* \endcode
|
2015-04-19 16:44:20 +02:00
|
|
|
*/
|
|
|
|
#define MAVLITER_DEFINE(i,t)\
|
2015-04-17 07:45:02 +02:00
|
|
|
mavliter_t i; mavliter_init(&i,t)
|
|
|
|
|
|
|
|
|
2015-05-04 07:42:44 +02:00
|
|
|
/**
|
|
|
|
* Iterate through all elements of an MAVL Object using a MAVL Iterator.
|
|
|
|
* @param i pointer to MAVL Interator object
|
|
|
|
*/
|
2015-04-17 07:45:02 +02:00
|
|
|
#define mavliter_foreach(i)\
|
|
|
|
for (mavliter_seek_set(i); NULL != mavliter_get(i); mavliter_next(i))
|
|
|
|
|
|
|
|
#define mavliter_foreach_from(i,from)\
|
|
|
|
for (mavliter_seek(i,from); NULL != mavliter_get(i); mavliter_next(i))
|
|
|
|
|
|
|
|
#define mavliter_foreach_asc(iter,val) \
|
|
|
|
while(NULL != (val = mavliter_next(iter)))
|
|
|
|
|
2015-04-19 23:27:44 +02:00
|
|
|
|
2016-03-05 08:50:22 +01:00
|
|
|
/**
|
|
|
|
* Get the number of elements stored in a mavl object
|
|
|
|
* @param m mavl object
|
|
|
|
* @return Number of elemenst
|
|
|
|
*/
|
|
|
|
#define mavl_get_count(m) (m->count)
|
2015-04-19 23:27:44 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
typedef mavl_t mavl_conststr_t;
|
|
|
|
extern mavl_conststr_t mavl_create_conststr();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 23:27:44 +02:00
|
|
|
/*
|
|
|
|
static inline void *mavl_replace_data(struct mavl *t, void *data, int len)
|
|
|
|
{
|
|
|
|
void *df = mavl_get(t, data);
|
|
|
|
if (!df)
|
|
|
|
return NULL;
|
|
|
|
memcpy(df, data, len);
|
|
|
|
return df;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *mavl_replace(struct mavl *t,void *data){
|
|
|
|
struct mavlnode * node = mavl_get_node(t,data);
|
|
|
|
if (node){
|
|
|
|
t->del(node->data);
|
|
|
|
return node->data=data;
|
|
|
|
}
|
|
|
|
return mavl_add(t,data);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-04-17 07:45:02 +02:00
|
|
|
|
2015-04-19 16:44:20 +02:00
|
|
|
/** @} */
|
2015-04-17 07:45:02 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|