2014-07-11 22:12:11 +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/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Yet another avl tree implementation!
|
2014-07-11 22:12:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __AVLTREE_H
|
|
|
|
#define __AVLTREE_H
|
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
#include <stdlib.h>
|
2015-04-05 02:07:59 +02:00
|
|
|
#include <stdio.h>
|
2015-04-05 20:27:17 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-09 16:30:04 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#define AVLTREE_MAX_DEPTH 32
|
2015-04-05 02:07:59 +02:00
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
struct avlnode {
|
|
|
|
void *data;
|
|
|
|
struct avlnode *left;
|
|
|
|
struct avlnode *right;
|
2014-07-11 22:12:11 +02:00
|
|
|
int bal;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
struct avltree {
|
|
|
|
struct avlnode *root;
|
|
|
|
int (*cmp) (const void *, const void *);
|
|
|
|
void (*del) (void *);
|
2014-07-11 22:12:11 +02:00
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
2015-04-09 16:30:04 +02:00
|
|
|
typedef struct avltree * avltree_t;
|
|
|
|
|
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
void avlnode_destroy(struct avltree *t, struct avlnode *n);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
struct avltree *avltree_create(int (*cmp) (const void *, const void *),
|
|
|
|
void (*del) (void *));
|
|
|
|
//void avltree_destroy(struct avltree *t);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
void avltree_del_all(struct avltree *t);
|
2015-04-05 20:27:17 +02:00
|
|
|
void *avltree_del(struct avltree *t, void *data);
|
|
|
|
void *avltree_add(struct avltree *t, void *data);
|
2015-04-08 16:16:31 +02:00
|
|
|
void * avltree_get(struct avltree *t ,void *data);
|
2015-04-05 20:27:17 +02:00
|
|
|
struct avlnode *avltree_get_node(struct avltree *t, void *data);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
extern int avltree_foreach_lr(struct avlnode *n, int (*callback) (void *, void *),
|
|
|
|
void *cbpriv);
|
|
|
|
extern int avltree_foreach_rl(struct avlnode *n, int (*callback) (void *, void *),
|
|
|
|
void *cbpriv);
|
|
|
|
int avltree_foreach_from_lr(struct avltree *t, struct avlnode *n, void *data,
|
|
|
|
int (*callback) (void *, void *), void *cbpriv);
|
2015-04-05 02:07:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
//extern void avltree_foreach(struct avltree *t, int (*callback)(void *,void*),void *cbpriv,int dir);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-05 20:27:17 +02:00
|
|
|
static inline void *avltree_replace_data(struct avltree *t, void *data, int len)
|
|
|
|
{
|
|
|
|
void *df = avltree_get(t, data);
|
|
|
|
if (!df)
|
2015-04-05 03:31:50 +02:00
|
|
|
return NULL;
|
2015-04-05 20:27:17 +02:00
|
|
|
memcpy(df, data, len);
|
2015-04-05 03:31:50 +02:00
|
|
|
return df;
|
2015-04-05 02:07:59 +02:00
|
|
|
}
|
2015-04-05 20:27:17 +02:00
|
|
|
|
|
|
|
static inline void avltree_destroy(struct avltree *t)
|
|
|
|
{
|
|
|
|
avltree_del_all(t);
|
|
|
|
free (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 07:42:36 +02:00
|
|
|
|
2015-04-09 16:30:04 +02:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
#define avltree_find(t,d) avltree_get(t,d)
|
|
|
|
#define avltree_insert(t,d) avltree_add(t,d)
|
2015-04-05 02:07:59 +02:00
|
|
|
//#define avltree_walk(t,dir) avltree_foreach(t,dir)
|
|
|
|
|
|
|
|
#define avltree_foreach_asc(t,cb,priv) avltree_foreach_lr((t)->root,cb,priv)
|
|
|
|
#define avltree_foreach_desc(t,cb,priv) avltree_foreach_rl((t)->root,cb,priv)
|
|
|
|
|
|
|
|
#define avltree_foreach_from_asc(t,d,cb,priv) avltree_foreach_from_lr(t,(t)->root,d,cb,priv);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
2015-04-09 16:30:04 +02:00
|
|
|
struct avliter{
|
|
|
|
struct avlnode *stack[AVLTREE_MAX_DEPTH*2];
|
|
|
|
|
|
|
|
struct avlnode *cur;
|
|
|
|
int stack_ptr;
|
|
|
|
struct avlnode * root;
|
|
|
|
|
|
|
|
};
|
|
|
|
typedef struct avliter avliter_t;
|
|
|
|
|
|
|
|
|
|
|
|
void * avliter_next(avliter_t *i);
|
|
|
|
|
|
|
|
void * avliter_seek_set(struct avliter *i)
|
|
|
|
{
|
|
|
|
i->stack_ptr=0;
|
|
|
|
i->cur=i->root;
|
|
|
|
return avliter_next(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void avliter_init(avliter_t *i, avltree_t t){
|
|
|
|
i->root = t->root;
|
|
|
|
i->stack_ptr=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define avliter_foreach_asc(iter,val) \
|
|
|
|
while(NULL != (val = avliter_next(iter)))
|
|
|
|
|
|
|
|
static inline void * avliter_get(avliter_t *i){
|
|
|
|
if(!i->cur)
|
|
|
|
return NULL;
|
|
|
|
return i->cur->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
#endif
|
2015-04-09 16:30:04 +02:00
|
|
|
|
|
|
|
|