--- libaitio/inc/Attic/atree.h 2011/06/07 11:49:39 1.2 +++ libaitio/inc/Attic/atree.h 2012/08/29 13:51:29 1.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: atree.h,v 1.2 2011/06/07 11:49:39 misho Exp $ +* $Id: atree.h,v 1.4 2012/08/29 13:51:29 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -135,7 +135,7 @@ struct { \ SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ (head)->sph_root = tmp; \ } while (0) - + #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ SPLAY_LEFT(tmp, field) = (head)->sph_root; \ @@ -339,6 +339,13 @@ void name##_SPLAY_MINMAX(struct name *head, int __comp (x) != NULL; \ (x) = SPLAY_NEXT(name, head, x)) +#define SPLAY_FOREACH_SAFE(x, name, head, y) \ + for ((x) = SPLAY_MIN(name, head); \ + ((x) != NULL) && \ + ((y) = SPLAY_NEXT(name, head, x), (x) != NULL); \ + (x) = (y)) + + /* Macros that define a red-black tree */ #define RB_HEAD(name, type) \ struct name { \ @@ -381,7 +388,7 @@ struct { \ } while (0) #ifndef RB_AUGMENT -#define RB_AUGMENT(x) do {} while (0) +#define RB_AUGMENT(x) (void)(x) #endif #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ @@ -425,7 +432,7 @@ struct { \ } while (0) /* Generates prototypes and inline functions */ -#define RB_PROTOTYPE(name, type, field, cmp) \ +#define RB_PROTOTYPE(name, type, field, cmp) \ RB_PROTOTYPE_INTERNAL(name, type, field, cmp,) #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \ RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static) @@ -517,7 +524,7 @@ name##_RB_REMOVE_COLOR(struct name *head, struct type if (RB_RIGHT(tmp, field) == NULL || \ RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ struct type *oleft; \ - if ((oleft = RB_LEFT(tmp, field)))\ + if ((oleft = RB_LEFT(tmp, field))) \ RB_COLOR(oleft, field) = RB_BLACK;\ RB_COLOR(tmp, field) = RB_RED; \ RB_ROTATE_RIGHT(head, tmp, oleft, field);\ @@ -549,7 +556,7 @@ name##_RB_REMOVE_COLOR(struct name *head, struct type if (RB_LEFT(tmp, field) == NULL || \ RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ struct type *oright; \ - if ((oright = RB_RIGHT(tmp, field)))\ + if ((oright = RB_RIGHT(tmp, field))) \ RB_COLOR(oright, field) = RB_BLACK;\ RB_COLOR(tmp, field) = RB_RED; \ RB_ROTATE_LEFT(head, tmp, oright, field);\ @@ -808,28 +815,26 @@ name##_RB_MINMAX(struct name *head, int val) \ (x) = (y)) /* Macros that define a avl tree */ -#define AVL_MAX_HEIGHT 42 +#ifndef AVL_MAX_HEIGHT +#define AVL_MAX_HEIGHT 32 +#endif #define AVL_HEAD(name, type) \ struct name { \ - struct type *avlh_root; /* root of the tree */ \ - int avlh_height; \ - long avlh_count; \ + struct type *avlh_root; /* root of the tree */ \ } #define AVL_INITIALIZER(root) \ - { NULL, 0, 0 } + { NULL } -#define AVL_INIT(root, height) do { \ +#define AVL_INIT(root) do { \ (root)->avlh_root = NULL; \ - (root)->avlh_height = !height ? AVL_MAX_HEIGHT : height; \ - (root)->avlh_count = 0; \ } while (0) #define AVL_ENTRY(type) \ struct { \ - struct type *avle_left; /* left element */ \ - struct type *avle_right; /* right element */ \ - int avle_height; /* node color */ \ + struct type *avle_left; /* left element */ \ + struct type *avle_right; /* right element */ \ + int avle_height; /* node height */ \ } #define AVL_LEFT(elm, field) (elm)->field.avle_left @@ -837,8 +842,6 @@ struct { \ #define AVL_HEIGHT(elm, field) (elm)->field.avle_height #define AVL_ROOT(head) (head)->avlh_root #define AVL_EMPTY(head) (AVL_ROOT(head) == NULL) -#define AVL_ROOT_HEIGHT(head) (head)->avlh_height -#define AVL_ROOT_COUNT(head) (head)->avlh_count /* Generates prototypes and inline functions */ #define AVL_PROTOTYPE(name, type, field, cmp) \ @@ -1029,16 +1032,11 @@ attr struct type * \ name##_AVL_INSERT(struct name *head, struct type *elm) \ { \ struct type *temp, **pt; \ - struct type ***ancestors; \ + struct type **ancestors[AVL_MAX_HEIGHT] = { 0 }; \ register int i; \ int comp; \ \ - ancestors = (struct type ***) alloca(AVL_ROOT_HEIGHT(head) * sizeof(void*)); \ - if (!ancestors) \ - return (struct type *) -1; \ - else \ - memset(ancestors, 0, AVL_ROOT_HEIGHT(head) * sizeof(void*)); \ - for (i = 0, pt = &AVL_ROOT(head); i < AVL_ROOT_HEIGHT(head) && *pt; i++) { \ + for (i = 0, pt = &AVL_ROOT(head); i < AVL_MAX_HEIGHT && *pt; i++) { \ temp = *pt; \ ancestors[i] = pt; \ comp = (cmp)(temp, elm); \ @@ -1054,7 +1052,6 @@ name##_AVL_INSERT(struct name *head, struct type *elm) AVL_LEFT(elm, field) = AVL_RIGHT(elm, field) = (struct type *) NULL; \ AVL_HEIGHT(elm, field) = 1; \ \ - AVL_ROOT_COUNT(head)++; \ AVL_REBALANCE(ancestors, type, field, i); \ return (NULL); \ } \ @@ -1063,16 +1060,11 @@ attr struct type * \ name##_AVL_REMOVE(struct name *head, struct type *elm) \ { \ struct type *temp, *old, **dp, **pt; \ - struct type ***ancestors; \ + struct type **ancestors[AVL_MAX_HEIGHT] = { 0 }; \ register int i; \ int comp, delcnt; \ \ - ancestors = (struct type ***) alloca(AVL_ROOT_HEIGHT(head) * sizeof(void*)); \ - if (!ancestors) \ - return (NULL); \ - else \ - memset(ancestors, 0, AVL_ROOT_HEIGHT(head) * sizeof(void*)); \ - for (i = 0, pt = &AVL_ROOT(head); i < AVL_ROOT_HEIGHT(head); i++) { \ + for (i = 0, pt = &AVL_ROOT(head); i < AVL_MAX_HEIGHT; i++) { \ if (!*pt) \ return (NULL); \ else \ @@ -1094,7 +1086,7 @@ name##_AVL_REMOVE(struct name *head, struct type *elm) } else { \ delcnt = i; \ dp = pt; \ - for (pt = &AVL_LEFT(temp, field); i < AVL_ROOT_HEIGHT(head) && \ + for (pt = &AVL_LEFT(temp, field); i < AVL_MAX_HEIGHT && \ AVL_RIGHT(temp, field); i++) { \ temp = *pt; \ ancestors[i] = pt; \ @@ -1110,7 +1102,6 @@ name##_AVL_REMOVE(struct name *head, struct type *elm) ancestors[delcnt] = &AVL_LEFT(temp, field); \ } \ \ - AVL_ROOT_COUNT(head)--; \ AVL_REBALANCE(ancestors, type, field, i); \ return (old); \ }