diff --git a/include/ucore/buffer.h b/include/ucore/buffer.h index 87b0016..aab45a1 100644 --- a/include/ucore/buffer.h +++ b/include/ucore/buffer.h @@ -17,13 +17,13 @@ extern "C" { typedef struct GBuf GBuf; struct GBuf { /** Allocated buffer length.*/ - size_t len; + size_t len; /** Used space in the buffer */ - size_t used; + size_t used; /** Reference count */ size_t ref_cnt; /** The data */ - void *buf; + void *buf; }; /** Allocate a new GBuf. diff --git a/include/ucore/dbuf.h b/include/ucore/dbuf.h index f564c3e..4dc497c 100644 --- a/include/ucore/dbuf.h +++ b/include/ucore/dbuf.h @@ -40,13 +40,13 @@ extern "C" { typedef struct DBuf DBuf; struct DBuf { /** Start of user data */ - unsigned char *start; + unsigned char *start; /** End of user data */ - unsigned char *end; + unsigned char *end; /** Start of underlying buffer. Internal use.*/ - unsigned char *bufstart; + unsigned char *bufstart; /** End of underlying buffer. Internal use.*/ - unsigned char *bufend; + unsigned char *bufend; }; /** Free the internals of a DBuf, does not free buf itself. * Does nothing if buf == NULL diff --git a/include/ucore/heapsort.h b/include/ucore/heapsort.h index 38947c3..c5dbf01 100644 --- a/include/ucore/heapsort.h +++ b/include/ucore/heapsort.h @@ -12,7 +12,7 @@ typedef int (*uc_hs_cmp)(const void *, const void *); void uc_heapsort(void *base, size_t count, size_t width, - uc_hs_cmp cmp); + uc_hs_cmp cmp); #ifdef __cplusplus } diff --git a/include/ucore/math.h b/include/ucore/math.h index 4d9d6fc..b895c25 100644 --- a/include/ucore/math.h +++ b/include/ucore/math.h @@ -22,15 +22,15 @@ uc_phi_64(uint64_t N); static inline uint32_t uc_nextpow2(uint32_t x) { - x--; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - x++; - - return x; + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x++; + + return x; } #ifdef __cplusplus diff --git a/include/ucore/rbtree.h b/include/ucore/rbtree.h index 7b6e16e..7c706a4 100644 --- a/include/ucore/rbtree.h +++ b/include/ucore/rbtree.h @@ -7,23 +7,23 @@ extern "C" { typedef enum RBColor RBColor; enum RBColor { - Red, - Black + Red, + Black }; typedef struct RBNode RBNode; struct RBNode { - unsigned char color; //Red/Black - RBNode *parent; - RBNode *right; - RBNode *left; - void *data; + unsigned char color; //Red/Black + RBNode *parent; + RBNode *right; + RBNode *left; + void *data; }; typedef struct RBTree RBTree; struct RBTree { - RBNode *node; - int (*compare)(const RBNode *a, const RBNode *b); + RBNode *node; + int (*compare)(const RBNode *a, const RBNode *b); }; /** Remove a node from the tree. diff --git a/include/ucore/threadqueue.h b/include/ucore/threadqueue.h index 143db28..dd189d1 100644 --- a/include/ucore/threadqueue.h +++ b/include/ucore/threadqueue.h @@ -26,13 +26,13 @@ extern "C" { * */ struct uc_threadmsg{ - /** - * A message type the application can use to + /** + * A message type the application can use to * discriminate different messages. * A negative value will cause a uc_threadmsg to * be added to the head of a queue. - */ - long msgtype; + */ + long msgtype; /** * Internal pointer used by the uc_thread_queue. @@ -100,22 +100,22 @@ struct uc_threadqueue { * Number of elements in the queue. * Use #threadqueue_length to read it. */ - unsigned int num_elements; + unsigned int num_elements; /** Max number of elements this queue will hold */ unsigned int max_elements; /** * Mutex for the queue. */ - pthread_mutex_t mutex; + pthread_mutex_t mutex; /** * Condition variable for readers on the queue. */ - pthread_cond_t read_cond; + pthread_cond_t read_cond; /** * Condition variable for writers on the queue (if the queue is full). */ - pthread_cond_t write_cond; + pthread_cond_t write_cond; /** * Number of threads blocking on writing to the queue */ @@ -128,7 +128,7 @@ struct uc_threadqueue { /** * Internal pointers for the messages in the queue. */ - struct uc_threadmsg *first,**last; + struct uc_threadmsg *first,**last; }; /** @@ -192,7 +192,7 @@ static inline int uc_thread_queue_add(struct uc_threadqueue *queue, struct uc_th * * struct timespec is defined as: * @code - * struct timespec { + * struct timespec { * long tv_sec; // seconds * long tv_nsec; // nanoseconds * }; diff --git a/include/ucore/tree.h b/include/ucore/tree.h index d044f98..8c89893 100644 --- a/include/ucore/tree.h +++ b/include/ucore/tree.h @@ -1,5 +1,5 @@ -/* $NetBSD: tree.h,v 1.16 2008/03/21 13:07:15 ad Exp $ */ -/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */ +/* $NetBSD: tree.h,v 1.16 2008/03/21 13:07:15 ad Exp $ */ +/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */ /* * Copyright 2002 Niels Provos * All rights reserved. @@ -25,8 +25,8 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _SYS_TREE_H_ -#define _SYS_TREE_H_ +#ifndef _SYS_TREE_H_ +#define _SYS_TREE_H_ /* * This file defines data structures for different types of trees: @@ -46,696 +46,696 @@ * * A red-black tree is a binary search tree with the node color as an * extra attribute. It fulfills a set of conditions: - * - every search path from the root to a leaf consists of the - * same number of black nodes, - * - each red node (except for the root) has a black parent, - * - each leaf node is black. + * - every search path from the root to a leaf consists of the + * same number of black nodes, + * - each red node (except for the root) has a black parent, + * - each leaf node is black. * * Every operation on a red-black tree is bounded as O(lg n). * The maximum height of a red-black tree is 2lg (n+1). */ -#define SPLAY_HEAD(name, type) \ -struct name { \ - struct type *sph_root; /* root of the tree */ \ +#define SPLAY_HEAD(name, type) \ +struct name { \ + struct type *sph_root; /* root of the tree */ \ } -#define SPLAY_INITIALIZER(root) \ - { NULL } +#define SPLAY_INITIALIZER(root) \ + { NULL } -#define SPLAY_INIT(root) do { \ - (root)->sph_root = NULL; \ +#define SPLAY_INIT(root) do { \ + (root)->sph_root = NULL; \ } while (/*CONSTCOND*/ 0) -#define SPLAY_ENTRY(type) \ -struct { \ - struct type *spe_left; /* left element */ \ - struct type *spe_right; /* right element */ \ +#define SPLAY_ENTRY(type) \ +struct { \ + struct type *spe_left; /* left element */ \ + struct type *spe_right; /* right element */ \ } -#define SPLAY_LEFT(elm, field) (elm)->field.spe_left -#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right -#define SPLAY_ROOT(head) (head)->sph_root -#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) +#define SPLAY_LEFT(elm, field) (elm)->field.spe_left +#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right +#define SPLAY_ROOT(head) (head)->sph_root +#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ -#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ - SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ - SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ - (head)->sph_root = tmp; \ +#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ } while (/*CONSTCOND*/ 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; \ - (head)->sph_root = tmp; \ +#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; \ + (head)->sph_root = tmp; \ } while (/*CONSTCOND*/ 0) -#define SPLAY_LINKLEFT(head, tmp, field) do { \ - SPLAY_LEFT(tmp, field) = (head)->sph_root; \ - tmp = (head)->sph_root; \ - (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ +#define SPLAY_LINKLEFT(head, tmp, field) do { \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ } while (/*CONSTCOND*/ 0) -#define SPLAY_LINKRIGHT(head, tmp, field) do { \ - SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ - tmp = (head)->sph_root; \ - (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ +#define SPLAY_LINKRIGHT(head, tmp, field) do { \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ } while (/*CONSTCOND*/ 0) -#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ - SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ - SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ - SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ - SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ +#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ + SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ + SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ } while (/*CONSTCOND*/ 0) /* Generates prototypes and inline functions */ -#define SPLAY_PROTOTYPE(name, type, field, cmp) \ -void name##_SPLAY(struct name *, struct type *); \ -void name##_SPLAY_MINMAX(struct name *, int); \ -struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ -struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ - \ -/* Finds the node with the same key as elm */ \ -static __inline struct type * \ -name##_SPLAY_FIND(struct name *head, struct type *elm) \ -{ \ - if (SPLAY_EMPTY(head)) \ - return(NULL); \ - name##_SPLAY(head, elm); \ - if ((cmp)(elm, (head)->sph_root) == 0) \ - return (head->sph_root); \ - return (NULL); \ -} \ - \ -static __inline struct type * \ -name##_SPLAY_NEXT(struct name *head, struct type *elm) \ -{ \ - name##_SPLAY(head, elm); \ - if (SPLAY_RIGHT(elm, field) != NULL) { \ - elm = SPLAY_RIGHT(elm, field); \ - while (SPLAY_LEFT(elm, field) != NULL) { \ - elm = SPLAY_LEFT(elm, field); \ - } \ - } else \ - elm = NULL; \ - return (elm); \ -} \ - \ -static __inline struct type * \ -name##_SPLAY_MIN_MAX(struct name *head, int val) \ -{ \ - name##_SPLAY_MINMAX(head, val); \ - return (SPLAY_ROOT(head)); \ +#define SPLAY_PROTOTYPE(name, type, field, cmp) \ +void name##_SPLAY(struct name *, struct type *); \ +void name##_SPLAY_MINMAX(struct name *, int); \ +struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ +struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ + \ +/* Finds the node with the same key as elm */ \ +static __inline struct type * \ +name##_SPLAY_FIND(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) \ + return(NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) \ + return (head->sph_root); \ + return (NULL); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_NEXT(struct name *head, struct type *elm) \ +{ \ + name##_SPLAY(head, elm); \ + if (SPLAY_RIGHT(elm, field) != NULL) { \ + elm = SPLAY_RIGHT(elm, field); \ + while (SPLAY_LEFT(elm, field) != NULL) { \ + elm = SPLAY_LEFT(elm, field); \ + } \ + } else \ + elm = NULL; \ + return (elm); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_MIN_MAX(struct name *head, int val) \ +{ \ + name##_SPLAY_MINMAX(head, val); \ + return (SPLAY_ROOT(head)); \ } /* Main splay operation. * Moves node close to the key of elm to top */ -#define SPLAY_GENERATE(name, type, field, cmp) \ -struct type * \ -name##_SPLAY_INSERT(struct name *head, struct type *elm) \ -{ \ - if (SPLAY_EMPTY(head)) { \ - SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ - } else { \ - int __comp; \ - name##_SPLAY(head, elm); \ - __comp = (cmp)(elm, (head)->sph_root); \ - if(__comp < 0) { \ - SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ - SPLAY_RIGHT(elm, field) = (head)->sph_root; \ - SPLAY_LEFT((head)->sph_root, field) = NULL; \ - } else if (__comp > 0) { \ - SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ - SPLAY_LEFT(elm, field) = (head)->sph_root; \ - SPLAY_RIGHT((head)->sph_root, field) = NULL; \ - } else \ - return ((head)->sph_root); \ - } \ - (head)->sph_root = (elm); \ - return (NULL); \ -} \ - \ -struct type * \ -name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ -{ \ - struct type *__tmp; \ - if (SPLAY_EMPTY(head)) \ - return (NULL); \ - name##_SPLAY(head, elm); \ - if ((cmp)(elm, (head)->sph_root) == 0) { \ - if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ - (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ - } else { \ - __tmp = SPLAY_RIGHT((head)->sph_root, field); \ - (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ - name##_SPLAY(head, elm); \ - SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ - } \ - return (elm); \ - } \ - return (NULL); \ -} \ - \ -void \ -name##_SPLAY(struct name *head, struct type *elm) \ -{ \ - struct type __node, *__left, *__right, *__tmp; \ - int __comp; \ +#define SPLAY_GENERATE(name, type, field, cmp) \ +struct type * \ +name##_SPLAY_INSERT(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) { \ + SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ + } else { \ + int __comp; \ + name##_SPLAY(head, elm); \ + __comp = (cmp)(elm, (head)->sph_root); \ + if(__comp < 0) { \ + SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ + SPLAY_RIGHT(elm, field) = (head)->sph_root; \ + SPLAY_LEFT((head)->sph_root, field) = NULL; \ + } else if (__comp > 0) { \ + SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT(elm, field) = (head)->sph_root; \ + SPLAY_RIGHT((head)->sph_root, field) = NULL; \ + } else \ + return ((head)->sph_root); \ + } \ + (head)->sph_root = (elm); \ + return (NULL); \ +} \ + \ +struct type * \ +name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *__tmp; \ + if (SPLAY_EMPTY(head)) \ + return (NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) { \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ + } else { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ + name##_SPLAY(head, elm); \ + SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ + } \ + return (elm); \ + } \ + return (NULL); \ +} \ + \ +void \ +name##_SPLAY(struct name *head, struct type *elm) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ + int __comp; \ \ - SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ - __left = __right = &__node; \ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ \ - while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \ - if (__comp < 0) { \ - __tmp = SPLAY_LEFT((head)->sph_root, field); \ - if (__tmp == NULL) \ - break; \ - if ((cmp)(elm, __tmp) < 0){ \ - SPLAY_ROTATE_RIGHT(head, __tmp, field); \ - if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ - break; \ - } \ - SPLAY_LINKLEFT(head, __right, field); \ - } else if (__comp > 0) { \ - __tmp = SPLAY_RIGHT((head)->sph_root, field); \ - if (__tmp == NULL) \ - break; \ - if ((cmp)(elm, __tmp) > 0){ \ - SPLAY_ROTATE_LEFT(head, __tmp, field); \ - if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ - break; \ - } \ - SPLAY_LINKRIGHT(head, __left, field); \ - } \ - } \ - SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ -} \ - \ -/* Splay with either the minimum or the maximum element \ - * Used to find minimum or maximum element in tree. \ - */ \ + while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) > 0){ \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} \ + \ +/* Splay with either the minimum or the maximum element \ + * Used to find minimum or maximum element in tree. \ + */ \ void name##_SPLAY_MINMAX(struct name *head, int __comp) \ -{ \ - struct type __node, *__left, *__right, *__tmp; \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ \ - SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ - __left = __right = &__node; \ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ \ - while (1) { \ - if (__comp < 0) { \ - __tmp = SPLAY_LEFT((head)->sph_root, field); \ - if (__tmp == NULL) \ - break; \ - if (__comp < 0){ \ - SPLAY_ROTATE_RIGHT(head, __tmp, field); \ - if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ - break; \ - } \ - SPLAY_LINKLEFT(head, __right, field); \ - } else if (__comp > 0) { \ - __tmp = SPLAY_RIGHT((head)->sph_root, field); \ - if (__tmp == NULL) \ - break; \ - if (__comp > 0) { \ - SPLAY_ROTATE_LEFT(head, __tmp, field); \ - if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ - break; \ - } \ - SPLAY_LINKRIGHT(head, __left, field); \ - } \ - } \ - SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ + while (1) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp > 0) { \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ } -#define SPLAY_NEGINF -1 -#define SPLAY_INF 1 +#define SPLAY_NEGINF -1 +#define SPLAY_INF 1 -#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) -#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) -#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) -#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) -#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ - : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) -#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ - : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) +#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) +#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) +#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) +#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) +#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) +#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) -#define SPLAY_FOREACH(x, name, head) \ - for ((x) = SPLAY_MIN(name, head); \ - (x) != NULL; \ - (x) = SPLAY_NEXT(name, head, x)) +#define SPLAY_FOREACH(x, name, head) \ + for ((x) = SPLAY_MIN(name, head); \ + (x) != NULL; \ + (x) = SPLAY_NEXT(name, head, x)) /* Macros that define a red-black tree */ -#define RB_HEAD(name, type) \ -struct name { \ - struct type *rbh_root; /* root of the tree */ \ +#define RB_HEAD(name, type) \ +struct name { \ + struct type *rbh_root; /* root of the tree */ \ } -#define RB_INITIALIZER(root) \ - { NULL } +#define RB_INITIALIZER(root) \ + { NULL } -#define RB_INIT(root) do { \ - (root)->rbh_root = NULL; \ +#define RB_INIT(root) do { \ + (root)->rbh_root = NULL; \ } while (/*CONSTCOND*/ 0) -#define RB_BLACK 0 -#define RB_RED 1 -#define RB_ENTRY(type) \ -struct { \ - struct type *rbe_left; /* left element */ \ - struct type *rbe_right; /* right element */ \ - struct type *rbe_parent; /* parent element */ \ - int rbe_color; /* node color */ \ +#define RB_BLACK 0 +#define RB_RED 1 +#define RB_ENTRY(type) \ +struct { \ + struct type *rbe_left; /* left element */ \ + struct type *rbe_right; /* right element */ \ + struct type *rbe_parent; /* parent element */ \ + int rbe_color; /* node color */ \ } -#define RB_LEFT(elm, field) (elm)->field.rbe_left -#define RB_RIGHT(elm, field) (elm)->field.rbe_right -#define RB_PARENT(elm, field) (elm)->field.rbe_parent -#define RB_COLOR(elm, field) (elm)->field.rbe_color -#define RB_ROOT(head) (head)->rbh_root -#define RB_EMPTY(head) (RB_ROOT(head) == NULL) +#define RB_LEFT(elm, field) (elm)->field.rbe_left +#define RB_RIGHT(elm, field) (elm)->field.rbe_right +#define RB_PARENT(elm, field) (elm)->field.rbe_parent +#define RB_COLOR(elm, field) (elm)->field.rbe_color +#define RB_ROOT(head) (head)->rbh_root +#define RB_EMPTY(head) (RB_ROOT(head) == NULL) -#define RB_SET(elm, parent, field) do { \ - RB_PARENT(elm, field) = parent; \ - RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ - RB_COLOR(elm, field) = RB_RED; \ +#define RB_SET(elm, parent, field) do { \ + RB_PARENT(elm, field) = parent; \ + RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ + RB_COLOR(elm, field) = RB_RED; \ } while (/*CONSTCOND*/ 0) -#define RB_SET_BLACKRED(black, red, field) do { \ - RB_COLOR(black, field) = RB_BLACK; \ - RB_COLOR(red, field) = RB_RED; \ +#define RB_SET_BLACKRED(black, red, field) do { \ + RB_COLOR(black, field) = RB_BLACK; \ + RB_COLOR(red, field) = RB_RED; \ } while (/*CONSTCOND*/ 0) #ifndef RB_AUGMENT #define RB_AUGMENT(x) (void)(x) #endif -#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ - (tmp) = RB_RIGHT(elm, field); \ - if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ - RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ - } \ - RB_AUGMENT(elm); \ - if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ - if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ - RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ - else \ - RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ - } else \ - (head)->rbh_root = (tmp); \ - RB_LEFT(tmp, field) = (elm); \ - RB_PARENT(elm, field) = (tmp); \ - RB_AUGMENT(tmp); \ - if ((RB_PARENT(tmp, field))) \ - RB_AUGMENT(RB_PARENT(tmp, field)); \ +#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ + (tmp) = RB_RIGHT(elm, field); \ + if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ + RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_LEFT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ } while (/*CONSTCOND*/ 0) -#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ - (tmp) = RB_LEFT(elm, field); \ - if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ - RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ - } \ - RB_AUGMENT(elm); \ - if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ - if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ - RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ - else \ - RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ - } else \ - (head)->rbh_root = (tmp); \ - RB_RIGHT(tmp, field) = (elm); \ - RB_PARENT(elm, field) = (tmp); \ - RB_AUGMENT(tmp); \ - if ((RB_PARENT(tmp, field))) \ - RB_AUGMENT(RB_PARENT(tmp, field)); \ +#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ + (tmp) = RB_LEFT(elm, field); \ + if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ + RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_RIGHT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ } while (/*CONSTCOND*/ 0) /* Generates prototypes and inline functions */ -#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, __unused static) -#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ -attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \ +#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, __unused static) +#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ +attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \ attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\ -attr struct type *name##_RB_REMOVE(struct name *, struct type *); \ -attr struct type *name##_RB_INSERT(struct name *, struct type *); \ -attr struct type *name##_RB_FIND(struct name *, struct type *); \ -attr struct type *name##_RB_NFIND(struct name *, struct type *); \ -attr struct type *name##_RB_NEXT(struct type *); \ -attr struct type *name##_RB_PREV(struct type *); \ -attr struct type *name##_RB_MINMAX(struct name *, int); \ - \ +attr struct type *name##_RB_REMOVE(struct name *, struct type *); \ +attr struct type *name##_RB_INSERT(struct name *, struct type *); \ +attr struct type *name##_RB_FIND(struct name *, struct type *); \ +attr struct type *name##_RB_NFIND(struct name *, struct type *); \ +attr struct type *name##_RB_NEXT(struct type *); \ +attr struct type *name##_RB_PREV(struct type *); \ +attr struct type *name##_RB_MINMAX(struct name *, int); \ + \ /* Main rb operation. * Moves node close to the key of elm to top */ -#define RB_GENERATE(name, type, field, cmp) \ - RB_GENERATE_INTERNAL(name, type, field, cmp,) -#define RB_GENERATE_STATIC(name, type, field, cmp) \ - RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static) -#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \ -attr void \ -name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ -{ \ - struct type *parent, *gparent, *tmp; \ - while ((parent = RB_PARENT(elm, field)) != NULL && \ - RB_COLOR(parent, field) == RB_RED) { \ - gparent = RB_PARENT(parent, field); \ - if (parent == RB_LEFT(gparent, field)) { \ - tmp = RB_RIGHT(gparent, field); \ - if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ - RB_COLOR(tmp, field) = RB_BLACK; \ - RB_SET_BLACKRED(parent, gparent, field);\ - elm = gparent; \ - continue; \ - } \ - if (RB_RIGHT(parent, field) == elm) { \ - RB_ROTATE_LEFT(head, parent, tmp, field);\ - tmp = parent; \ - parent = elm; \ - elm = tmp; \ - } \ - RB_SET_BLACKRED(parent, gparent, field); \ - RB_ROTATE_RIGHT(head, gparent, tmp, field); \ - } else { \ - tmp = RB_LEFT(gparent, field); \ - if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ - RB_COLOR(tmp, field) = RB_BLACK; \ - RB_SET_BLACKRED(parent, gparent, field);\ - elm = gparent; \ - continue; \ - } \ - if (RB_LEFT(parent, field) == elm) { \ - RB_ROTATE_RIGHT(head, parent, tmp, field);\ - tmp = parent; \ - parent = elm; \ - elm = tmp; \ - } \ - RB_SET_BLACKRED(parent, gparent, field); \ - RB_ROTATE_LEFT(head, gparent, tmp, field); \ - } \ - } \ - RB_COLOR(head->rbh_root, field) = RB_BLACK; \ -} \ - \ -attr void \ +#define RB_GENERATE(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp,) +#define RB_GENERATE_STATIC(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static) +#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \ +attr void \ +name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ +{ \ + struct type *parent, *gparent, *tmp; \ + while ((parent = RB_PARENT(elm, field)) != NULL && \ + RB_COLOR(parent, field) == RB_RED) { \ + gparent = RB_PARENT(parent, field); \ + if (parent == RB_LEFT(gparent, field)) { \ + tmp = RB_RIGHT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_RIGHT(parent, field) == elm) { \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_RIGHT(head, gparent, tmp, field); \ + } else { \ + tmp = RB_LEFT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_LEFT(parent, field) == elm) { \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_LEFT(head, gparent, tmp, field); \ + } \ + } \ + RB_COLOR(head->rbh_root, field) = RB_BLACK; \ +} \ + \ +attr void \ name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \ -{ \ - struct type *tmp; \ - while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ - elm != RB_ROOT(head)) { \ - if (RB_LEFT(parent, field) == elm) { \ - tmp = RB_RIGHT(parent, field); \ - if (RB_COLOR(tmp, field) == RB_RED) { \ - RB_SET_BLACKRED(tmp, parent, field); \ - RB_ROTATE_LEFT(head, parent, tmp, field);\ - tmp = RB_RIGHT(parent, field); \ - } \ - if ((RB_LEFT(tmp, field) == NULL || \ - RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ - (RB_RIGHT(tmp, field) == NULL || \ - RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ - RB_COLOR(tmp, field) = RB_RED; \ - elm = parent; \ - parent = RB_PARENT(elm, field); \ - } else { \ - if (RB_RIGHT(tmp, field) == NULL || \ - RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ - struct type *oleft; \ - if ((oleft = RB_LEFT(tmp, field)) \ - != NULL) \ - RB_COLOR(oleft, field) = RB_BLACK;\ - RB_COLOR(tmp, field) = RB_RED; \ - RB_ROTATE_RIGHT(head, tmp, oleft, field);\ - tmp = RB_RIGHT(parent, field); \ - } \ - RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ - RB_COLOR(parent, field) = RB_BLACK; \ - if (RB_RIGHT(tmp, field)) \ - RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ - RB_ROTATE_LEFT(head, parent, tmp, field);\ - elm = RB_ROOT(head); \ - break; \ - } \ - } else { \ - tmp = RB_LEFT(parent, field); \ - if (RB_COLOR(tmp, field) == RB_RED) { \ - RB_SET_BLACKRED(tmp, parent, field); \ - RB_ROTATE_RIGHT(head, parent, tmp, field);\ - tmp = RB_LEFT(parent, field); \ - } \ - if ((RB_LEFT(tmp, field) == NULL || \ - RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ - (RB_RIGHT(tmp, field) == NULL || \ - RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ - RB_COLOR(tmp, field) = RB_RED; \ - elm = parent; \ - parent = RB_PARENT(elm, field); \ - } else { \ - if (RB_LEFT(tmp, field) == NULL || \ - RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ - struct type *oright; \ - if ((oright = RB_RIGHT(tmp, field)) \ - != NULL) \ - RB_COLOR(oright, field) = RB_BLACK;\ - RB_COLOR(tmp, field) = RB_RED; \ - RB_ROTATE_LEFT(head, tmp, oright, field);\ - tmp = RB_LEFT(parent, field); \ - } \ - RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ - RB_COLOR(parent, field) = RB_BLACK; \ - if (RB_LEFT(tmp, field)) \ - RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ - RB_ROTATE_RIGHT(head, parent, tmp, field);\ - elm = RB_ROOT(head); \ - break; \ - } \ - } \ - } \ - if (elm) \ - RB_COLOR(elm, field) = RB_BLACK; \ -} \ - \ -attr struct type * \ -name##_RB_REMOVE(struct name *head, struct type *elm) \ -{ \ - struct type *child, *parent, *old = elm; \ - int color; \ - if (RB_LEFT(elm, field) == NULL) \ - child = RB_RIGHT(elm, field); \ - else if (RB_RIGHT(elm, field) == NULL) \ - child = RB_LEFT(elm, field); \ - else { \ - struct type *left; \ - elm = RB_RIGHT(elm, field); \ - while ((left = RB_LEFT(elm, field)) != NULL) \ - elm = left; \ - child = RB_RIGHT(elm, field); \ - parent = RB_PARENT(elm, field); \ - color = RB_COLOR(elm, field); \ - if (child) \ - RB_PARENT(child, field) = parent; \ - if (parent) { \ - if (RB_LEFT(parent, field) == elm) \ - RB_LEFT(parent, field) = child; \ - else \ - RB_RIGHT(parent, field) = child; \ - RB_AUGMENT(parent); \ - } else \ - RB_ROOT(head) = child; \ - if (RB_PARENT(elm, field) == old) \ - parent = elm; \ - (elm)->field = (old)->field; \ - if (RB_PARENT(old, field)) { \ - if (RB_LEFT(RB_PARENT(old, field), field) == old)\ - RB_LEFT(RB_PARENT(old, field), field) = elm;\ - else \ - RB_RIGHT(RB_PARENT(old, field), field) = elm;\ - RB_AUGMENT(RB_PARENT(old, field)); \ - } else \ - RB_ROOT(head) = elm; \ - RB_PARENT(RB_LEFT(old, field), field) = elm; \ - if (RB_RIGHT(old, field)) \ - RB_PARENT(RB_RIGHT(old, field), field) = elm; \ - if (parent) { \ - left = parent; \ - do { \ - RB_AUGMENT(left); \ - } while ((left = RB_PARENT(left, field)) != NULL); \ - } \ - goto color; \ - } \ - parent = RB_PARENT(elm, field); \ - color = RB_COLOR(elm, field); \ - if (child) \ - RB_PARENT(child, field) = parent; \ - if (parent) { \ - if (RB_LEFT(parent, field) == elm) \ - RB_LEFT(parent, field) = child; \ - else \ - RB_RIGHT(parent, field) = child; \ - RB_AUGMENT(parent); \ - } else \ - RB_ROOT(head) = child; \ -color: \ - if (color == RB_BLACK) \ - name##_RB_REMOVE_COLOR(head, parent, child); \ - return (old); \ -} \ - \ -/* Inserts a node into the RB tree */ \ -attr struct type * \ -name##_RB_INSERT(struct name *head, struct type *elm) \ -{ \ - struct type *tmp; \ - struct type *parent = NULL; \ - int comp = 0; \ - tmp = RB_ROOT(head); \ - while (tmp) { \ - parent = tmp; \ - comp = (cmp)(elm, parent); \ - if (comp < 0) \ - tmp = RB_LEFT(tmp, field); \ - else if (comp > 0) \ - tmp = RB_RIGHT(tmp, field); \ - else \ - return (tmp); \ - } \ - RB_SET(elm, parent, field); \ - if (parent != NULL) { \ - if (comp < 0) \ - RB_LEFT(parent, field) = elm; \ - else \ - RB_RIGHT(parent, field) = elm; \ - RB_AUGMENT(parent); \ - } else \ - RB_ROOT(head) = elm; \ - name##_RB_INSERT_COLOR(head, elm); \ - return (NULL); \ -} \ - \ -/* Finds the node with the same key as elm */ \ -attr struct type * \ -name##_RB_FIND(struct name *head, struct type *elm) \ -{ \ - struct type *tmp = RB_ROOT(head); \ - int comp; \ - while (tmp) { \ - comp = cmp(elm, tmp); \ - if (comp < 0) \ - tmp = RB_LEFT(tmp, field); \ - else if (comp > 0) \ - tmp = RB_RIGHT(tmp, field); \ - else \ - return (tmp); \ - } \ - return (NULL); \ -} \ - \ -/* Finds the first node greater than or equal to the search key */ \ -attr struct type * \ -name##_RB_NFIND(struct name *head, struct type *elm) \ -{ \ - struct type *tmp = RB_ROOT(head); \ - struct type *res = NULL; \ - int comp; \ - while (tmp) { \ - comp = cmp(elm, tmp); \ - if (comp < 0) { \ - res = tmp; \ - tmp = RB_LEFT(tmp, field); \ - } \ - else if (comp > 0) \ - tmp = RB_RIGHT(tmp, field); \ - else \ - return (tmp); \ - } \ - return (res); \ -} \ - \ -/* ARGSUSED */ \ -attr struct type * \ -name##_RB_NEXT(struct type *elm) \ -{ \ - if (RB_RIGHT(elm, field)) { \ - elm = RB_RIGHT(elm, field); \ - while (RB_LEFT(elm, field)) \ - elm = RB_LEFT(elm, field); \ - } else { \ - if (RB_PARENT(elm, field) && \ - (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ - elm = RB_PARENT(elm, field); \ - else { \ - while (RB_PARENT(elm, field) && \ - (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ - elm = RB_PARENT(elm, field); \ - elm = RB_PARENT(elm, field); \ - } \ - } \ - return (elm); \ -} \ - \ -/* ARGSUSED */ \ -attr struct type * \ -name##_RB_PREV(struct type *elm) \ -{ \ - if (RB_LEFT(elm, field)) { \ - elm = RB_LEFT(elm, field); \ - while (RB_RIGHT(elm, field)) \ - elm = RB_RIGHT(elm, field); \ - } else { \ - if (RB_PARENT(elm, field) && \ - (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ - elm = RB_PARENT(elm, field); \ - else { \ - while (RB_PARENT(elm, field) && \ - (elm == RB_LEFT(RB_PARENT(elm, field), field)))\ - elm = RB_PARENT(elm, field); \ - elm = RB_PARENT(elm, field); \ - } \ - } \ - return (elm); \ -} \ - \ -attr struct type * \ -name##_RB_MINMAX(struct name *head, int val) \ -{ \ - struct type *tmp = RB_ROOT(head); \ - struct type *parent = NULL; \ - while (tmp) { \ - parent = tmp; \ - if (val < 0) \ - tmp = RB_LEFT(tmp, field); \ - else \ - tmp = RB_RIGHT(tmp, field); \ - } \ - return (parent); \ +{ \ + struct type *tmp; \ + while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ + elm != RB_ROOT(head)) { \ + if (RB_LEFT(parent, field) == elm) { \ + tmp = RB_RIGHT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ + struct type *oleft; \ + if ((oleft = RB_LEFT(tmp, field)) \ + != NULL) \ + RB_COLOR(oleft, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_RIGHT(head, tmp, oleft, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_RIGHT(tmp, field)) \ + RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } else { \ + tmp = RB_LEFT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ + struct type *oright; \ + if ((oright = RB_RIGHT(tmp, field)) \ + != NULL) \ + RB_COLOR(oright, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_LEFT(head, tmp, oright, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_LEFT(tmp, field)) \ + RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } \ + } \ + if (elm) \ + RB_COLOR(elm, field) = RB_BLACK; \ +} \ + \ +attr struct type * \ +name##_RB_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *child, *parent, *old = elm; \ + int color; \ + if (RB_LEFT(elm, field) == NULL) \ + child = RB_RIGHT(elm, field); \ + else if (RB_RIGHT(elm, field) == NULL) \ + child = RB_LEFT(elm, field); \ + else { \ + struct type *left; \ + elm = RB_RIGHT(elm, field); \ + while ((left = RB_LEFT(elm, field)) != NULL) \ + elm = left; \ + child = RB_RIGHT(elm, field); \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ + if (RB_PARENT(elm, field) == old) \ + parent = elm; \ + (elm)->field = (old)->field; \ + if (RB_PARENT(old, field)) { \ + if (RB_LEFT(RB_PARENT(old, field), field) == old)\ + RB_LEFT(RB_PARENT(old, field), field) = elm;\ + else \ + RB_RIGHT(RB_PARENT(old, field), field) = elm;\ + RB_AUGMENT(RB_PARENT(old, field)); \ + } else \ + RB_ROOT(head) = elm; \ + RB_PARENT(RB_LEFT(old, field), field) = elm; \ + if (RB_RIGHT(old, field)) \ + RB_PARENT(RB_RIGHT(old, field), field) = elm; \ + if (parent) { \ + left = parent; \ + do { \ + RB_AUGMENT(left); \ + } while ((left = RB_PARENT(left, field)) != NULL); \ + } \ + goto color; \ + } \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ +color: \ + if (color == RB_BLACK) \ + name##_RB_REMOVE_COLOR(head, parent, child); \ + return (old); \ +} \ + \ +/* Inserts a node into the RB tree */ \ +attr struct type * \ +name##_RB_INSERT(struct name *head, struct type *elm) \ +{ \ + struct type *tmp; \ + struct type *parent = NULL; \ + int comp = 0; \ + tmp = RB_ROOT(head); \ + while (tmp) { \ + parent = tmp; \ + comp = (cmp)(elm, parent); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + RB_SET(elm, parent, field); \ + if (parent != NULL) { \ + if (comp < 0) \ + RB_LEFT(parent, field) = elm; \ + else \ + RB_RIGHT(parent, field) = elm; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = elm; \ + name##_RB_INSERT_COLOR(head, elm); \ + return (NULL); \ +} \ + \ +/* Finds the node with the same key as elm */ \ +attr struct type * \ +name##_RB_FIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (NULL); \ +} \ + \ +/* Finds the first node greater than or equal to the search key */ \ +attr struct type * \ +name##_RB_NFIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *res = NULL; \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) { \ + res = tmp; \ + tmp = RB_LEFT(tmp, field); \ + } \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (res); \ +} \ + \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_NEXT(struct type *elm) \ +{ \ + if (RB_RIGHT(elm, field)) { \ + elm = RB_RIGHT(elm, field); \ + while (RB_LEFT(elm, field)) \ + elm = RB_LEFT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_PREV(struct type *elm) \ +{ \ + if (RB_LEFT(elm, field)) { \ + elm = RB_LEFT(elm, field); \ + while (RB_RIGHT(elm, field)) \ + elm = RB_RIGHT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +attr struct type * \ +name##_RB_MINMAX(struct name *head, int val) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *parent = NULL; \ + while (tmp) { \ + parent = tmp; \ + if (val < 0) \ + tmp = RB_LEFT(tmp, field); \ + else \ + tmp = RB_RIGHT(tmp, field); \ + } \ + return (parent); \ } -#define RB_NEGINF -1 -#define RB_INF 1 +#define RB_NEGINF -1 +#define RB_INF 1 -#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) -#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) -#define RB_FIND(name, x, y) name##_RB_FIND(x, y) -#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y) -#define RB_NEXT(name, x, y) name##_RB_NEXT(y) -#define RB_PREV(name, x, y) name##_RB_PREV(y) -#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) -#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) +#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) +#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) +#define RB_FIND(name, x, y) name##_RB_FIND(x, y) +#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y) +#define RB_NEXT(name, x, y) name##_RB_NEXT(y) +#define RB_PREV(name, x, y) name##_RB_PREV(y) +#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) +#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) -#define RB_FOREACH(x, name, head) \ - for ((x) = RB_MIN(name, head); \ - (x) != NULL; \ - (x) = name##_RB_NEXT(x)) +#define RB_FOREACH(x, name, head) \ + for ((x) = RB_MIN(name, head); \ + (x) != NULL; \ + (x) = name##_RB_NEXT(x)) -#define RB_FOREACH_REVERSE(x, name, head) \ - for ((x) = RB_MAX(name, head); \ - (x) != NULL; \ - (x) = name##_RB_PREV(x)) +#define RB_FOREACH_REVERSE(x, name, head) \ + for ((x) = RB_MAX(name, head); \ + (x) != NULL; \ + (x) = name##_RB_PREV(x)) -#endif /* _SYS_TREE_H_ */ +#endif /* _SYS_TREE_H_ */ diff --git a/src/bcd.c b/src/bcd.c index 6d5da81..4cfb9ff 100644 --- a/src/bcd.c +++ b/src/bcd.c @@ -31,7 +31,7 @@ uc_ascii2bcd(const char *ascii, unsigned char *bcdout, unsigned char filler) bcdp++; } - return bcdp - bcdout; + return bcdp - bcdout; } static const char hex[] = "0123456789ABCDEF"; @@ -48,6 +48,6 @@ uc_bcd2ascii(const unsigned char *bcd, size_t bcdlen, char *asciiout) asciiout[idx] = 0; - return idx; + return idx; } diff --git a/src/buffer.c b/src/buffer.c index 610f0ab..e8c96a3 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -6,23 +6,23 @@ GBuf* uc_new_gbuf(size_t initsz) -{ - GBuf *p; - p = malloc(sizeof *p); - if (p == NULL) - return NULL; +{ + GBuf *p; + p = malloc(sizeof *p); + if (p == NULL) + return NULL; - p->used = 0; - p->len = initsz; - p->buf = NULL; + p->used = 0; + p->len = initsz; + p->buf = NULL; p->ref_cnt = 1; - if (initsz != 0 && (p->buf = malloc(initsz)) == NULL) { - free(p); - return NULL; - } - - return p; + if (initsz != 0 && (p->buf = malloc(initsz)) == NULL) { + free(p); + return NULL; + } + + return p; } void @@ -45,17 +45,17 @@ uc_gbuf_unref(GBuf *buf) int uc_gbuf_append(GBuf *buf,const void *data,size_t len) { - unsigned char *gbuf; - - if (buf->len - buf->used < len) - if (uc_gbuf_grow(buf, len + len/2)) - return -1; + unsigned char *gbuf; + + if (buf->len - buf->used < len) + if (uc_gbuf_grow(buf, len + len/2)) + return -1; - gbuf = buf->buf; - memcpy(gbuf + buf->used, data, len); - buf->used += len; - - return 0; + gbuf = buf->buf; + memcpy(gbuf + buf->used, data, len); + buf->used += len; + + return 0; } int uc_gbuf_printf(GBuf *buf, const char *fmt, ...) @@ -102,18 +102,18 @@ int uc_gbuf_printf(GBuf *buf, const char *fmt, ...) int uc_gbuf_grow(GBuf *buf, size_t addlen) { - void *tmp; - size_t newsz; + void *tmp; + size_t newsz; - newsz = buf->len + addlen; + newsz = buf->len + addlen; - tmp = realloc(buf->buf,newsz); - if (tmp == NULL) - return -1; + tmp = realloc(buf->buf,newsz); + if (tmp == NULL) + return -1; - buf->buf = tmp; - buf->len = newsz; + buf->buf = tmp; + buf->len = newsz; - return 0; + return 0; } diff --git a/src/crc32.c b/src/crc32.c index f9ba84a..0282f72 100644 --- a/src/crc32.c +++ b/src/crc32.c @@ -57,15 +57,15 @@ init_crc32() #include int main() { - int i; - init_crc32(); - for(i = 0; i < 256; i++) { - printf("0x%X,",crc32_table[i]); - if(i%10 == 0) - putchar('\n'); - } + int i; + init_crc32(); + for(i = 0; i < 256; i++) { + printf("0x%X,",crc32_table[i]); + if(i%10 == 0) + putchar('\n'); + } - return 0; + return 0; } */ diff --git a/src/dayofweek.c b/src/dayofweek.c index 6f73fa6..f3505d1 100644 --- a/src/dayofweek.c +++ b/src/dayofweek.c @@ -1,18 +1,18 @@ int uc_day_of_week(int day, int month, int year) { - int cent; + int cent; - /* adjust months so February is the last one */ - month -= 2; - if (month < 1) { - month += 12; - --year; - } - /* split by century */ - cent = year / 100; - year %= 100; - return ((26 * month - 2) / 10 + day + year - + year / 4 + cent / 4 + 5 * cent) % 7; + /* adjust months so February is the last one */ + month -= 2; + if (month < 1) { + month += 12; + --year; + } + /* split by century */ + cent = year / 100; + year %= 100; + return ((26 * month - 2) / 10 + day + year + + year / 4 + cent / 4 + 5 * cent) % 7; } diff --git a/src/djbhash.c b/src/djbhash.c index b06db0c..83d6100 100644 --- a/src/djbhash.c +++ b/src/djbhash.c @@ -3,11 +3,11 @@ uint32_t uc_djbhash(const char *str) { - uint32_t hash = 5381; - int c; - while ((c = *str++)) - hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + uint32_t hash = 5381; + int c; + while ((c = *str++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ - return hash; + return hash; } diff --git a/src/elfhash.c b/src/elfhash.c index c2a7578..1d81d6e 100644 --- a/src/elfhash.c +++ b/src/elfhash.c @@ -3,18 +3,18 @@ uint32_t uc_elfhash(const char *str, uint32_t len) { - uint32_t hash = 0; - uint32_t x = 0; - uint32_t i = 0; + uint32_t hash = 0; + uint32_t x = 0; + uint32_t i = 0; - for (i = 0; i < len; str++, i++) { - hash = (hash << 4) + (*str); - if ((x = hash & 0xF0000000L) != 0) { - hash ^= (x >> 24); - hash &= ~x; - } - } + for (i = 0; i < len; str++, i++) { + hash = (hash << 4) + (*str); + if ((x = hash & 0xF0000000L) != 0) { + hash ^= (x >> 24); + hash &= ~x; + } + } - return hash; + return hash; } diff --git a/src/gcd32.c b/src/gcd32.c index 852aeaa..90e9987 100644 --- a/src/gcd32.c +++ b/src/gcd32.c @@ -3,11 +3,11 @@ uint32_t uc_gcd_32(uint32_t a, uint32_t b) { - while (b != 0) { - uint32_t t = b; - b = a%b; - a = t; - } + while (b != 0) { + uint32_t t = b; + b = a%b; + a = t; + } - return a; + return a; } diff --git a/src/gcd64.c b/src/gcd64.c index d20719a..47d1a13 100644 --- a/src/gcd64.c +++ b/src/gcd64.c @@ -3,11 +3,11 @@ uint64_t uc_gcd_64(uint64_t a, uint64_t b) { - while (b != 0) { - uint64_t t = b; - b = a%b; - a = t; - } + while (b != 0) { + uint64_t t = b; + b = a%b; + a = t; + } - return a; + return a; } diff --git a/src/heapsort.c b/src/heapsort.c index 683703d..e291c89 100644 --- a/src/heapsort.c +++ b/src/heapsort.c @@ -5,37 +5,37 @@ static void uc_sift(unsigned char *base, size_t start, size_t count, size_t width, uc_hs_cmp cmp) { - size_t root = start, child; + size_t root = start, child; - while ((root * 2 + 1) < count) { - child = root * 2 + 1; - if (child < (count - 1) - && cmp(&base[child * width], &base[(child + 1) * width]) < 0) - child++; + while ((root * 2 + 1) < count) { + child = root * 2 + 1; + if (child < (count - 1) + && cmp(&base[child * width], &base[(child + 1) * width]) < 0) + child++; - if (cmp(&base[root * width], &base[child * width]) < 0) { - memcpy(base + root * width, base + child * width, width); - root = child; - } else - return; - } + if (cmp(&base[root * width], &base[child * width]) < 0) { + memcpy(base + root * width, base + child * width, width); + root = child; + } else + return; + } } void uc_heapsort(void *base_, size_t count, size_t width, - uc_hs_cmp cmp) + uc_hs_cmp cmp) { - int start = count / 2 - 1, end = count - 1; - unsigned char *base = base_; + int start = count / 2 - 1, end = count - 1; + unsigned char *base = base_; - while (start >= 0) { - uc_sift(base, start, count, width, cmp); - start--; - } + while (start >= 0) { + uc_sift(base, start, count, width, cmp); + start--; + } - while (end > 0) { - memcpy(base + end * width, base, width); - uc_sift(base, 0, end, width, cmp); - end--; - } + while (end > 0) { + memcpy(base + end * width, base, width); + uc_sift(base, 0, end, width, cmp); + end--; + } } diff --git a/src/mersenne_twister.c b/src/mersenne_twister.c index 4e59b8a..201a3cf 100644 --- a/src/mersenne_twister.c +++ b/src/mersenne_twister.c @@ -6,71 +6,71 @@ /*Beware , pollution. But the names are from the spec */ enum { - N = MT_RAND_N, - w = 32, - M = 387, - R = 19, - U = 0x80000000, - LL= 0x7FFFFFFF, - a = 0x9908B0DF, - s = 7, - t = 15, - b = 0x9D2C5680, - c = 0xEFC60000, - l = 18, - u = 11, + N = MT_RAND_N, + w = 32, + M = 387, + R = 19, + U = 0x80000000, + LL= 0x7FFFFFFF, + a = 0x9908B0DF, + s = 7, + t = 15, + b = 0x9D2C5680, + c = 0xEFC60000, + l = 18, + u = 11, }; void mtsrand(int seed, MTRand *r) { - int j; - for (j = 0 ; j < N ; j++) { - r->x[j] = seed * ((j+1)<< 3)|0x1; - } + int j; + for (j = 0 ; j < N ; j++) { + r->x[j] = seed * ((j+1)<< 3)|0x1; + } r->i = 0; } unsigned int mtrand(MTRand *r) { - unsigned int y; - unsigned int ch[] = {0, a}; - - y = r->x[r->i] & U; - y |= r->x[r->i % N]; - y &= LL; - - r->x[r->i] = r->x[(r->i + M ) %N]; - r->x[r->i] ^= y >> 1; + unsigned int y; + unsigned int ch[] = {0, a}; + + y = r->x[r->i] & U; + y |= r->x[r->i % N]; + y &= LL; + + r->x[r->i] = r->x[(r->i + M ) %N]; + r->x[r->i] ^= y >> 1; - r->x[r->i] ^= ch[y&0x1]; - - y = r->x[r->i]; - y ^= y >> u; - y ^= (y << s) & b; - y ^= (y << t) & c; - y ^= y >> l; + r->x[r->i] ^= ch[y&0x1]; + + y = r->x[r->i]; + y ^= y >> u; + y ^= (y << s) & b; + y ^= (y << t) & c; + y ^= y >> l; - r->i = (r->i + 1) % N; + r->i = (r->i + 1) % N; - return y; -} + return y; +} #ifdef TEST_MT #include #include int main(int argc,char *argv[]) { MTRand r; - mtsrand(time(NULL),&r); - for (;;){ - unsigned int _y = mtrand(&r); + mtsrand(time(NULL),&r); + for (;;){ + unsigned int _y = mtrand(&r); - if (_y < 100) - printf("%u \n",_y); - } + if (_y < 100) + printf("%u \n",_y); + } - return 0; + return 0; } #endif diff --git a/src/phi32.c b/src/phi32.c index 653a354..c273261 100644 --- a/src/phi32.c +++ b/src/phi32.c @@ -7,14 +7,14 @@ phi(N) = how many numbers between 1 and N - 1 which are relatively prime to N. uint32_t uc_phi_32(uint32_t N) { - uint32_t phi = 1; - uint32_t i; + uint32_t phi = 1; + uint32_t i; for (i = 2 ; i < N ; ++i){ if (uc_gcd_32(i, N) == 1){ ++phi; - } - } + } + } - return phi; + return phi; } diff --git a/src/phi64.c b/src/phi64.c index 7fd63a3..6e55e04 100644 --- a/src/phi64.c +++ b/src/phi64.c @@ -7,14 +7,14 @@ phi(N) = how many numbers between 1 and N - 1 which are relatively prime to N. uint64_t uc_phi_64(uint64_t N) { - uint64_t phi = 1; - uint64_t i; + uint64_t phi = 1; + uint64_t i; for (i = 2 ; i < N ; ++i){ if (uc_gcd_64(i, N) == 1){ ++phi; - } - } + } + } - return phi; + return phi; } diff --git a/src/pjwhash.c b/src/pjwhash.c index 901f03f..a52a442 100644 --- a/src/pjwhash.c +++ b/src/pjwhash.c @@ -4,16 +4,16 @@ uint32_t uc_pjwhash(const char *str,size_t len) { - unsigned h = 0, g; + unsigned h = 0, g; - while (len--) { - h = (h << 4) + *str++; - if ((g = h & 0xf0000000)) { - h ^= (g >> 24); - h ^= g; - } - } + while (len--) { + h = (h << 4) + *str++; + if ((g = h & 0xf0000000)) { + h ^= (g >> 24); + h ^= g; + } + } - return h; + return h; } diff --git a/src/rbtree.c b/src/rbtree.c index ebd1b71..e4f76f2 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -5,290 +5,290 @@ static void rotateleft(RBTree *root, RBNode *node) { - RBNode *right = node->right; + RBNode *right = node->right; - node->right = right->left; - if (right->left) - right->left->parent = node; - right->left = node; + node->right = right->left; + if (right->left) + right->left->parent = node; + right->left = node; - right->parent = node->parent; - if (node->parent) { - if (node == node->parent->left) - node->parent->left = right; - else - node->parent->right = right; - } else - root->node = right; - node->parent = right; + right->parent = node->parent; + if (node->parent) { + if (node == node->parent->left) + node->parent->left = right; + else + node->parent->right = right; + } else + root->node = right; + node->parent = right; } static void rotateright(RBTree *root, RBNode *node) { - RBNode *left = node->left; + RBNode *left = node->left; - node->left = left->right; - if (left->right) - left->right->parent = node; - left->right = node; + node->left = left->right; + if (left->right) + left->right->parent = node; + left->right = node; - left->parent = node->parent; - if (node->parent) { - if (node == node->parent->right) - node->parent->right = left; - else - node->parent->left = left; - } else - root->node = left; - node->parent = left; + left->parent = node->parent; + if (node->parent) { + if (node == node->parent->right) + node->parent->right = left; + else + node->parent->left = left; + } else + root->node = left; + node->parent = left; } static void rb_insertcolor(RBTree *root, RBNode *node) { - RBNode *parent, *gparent; + RBNode *parent, *gparent; - while ((parent = node->parent) && parent->color == Red) { - gparent = parent->parent; + while ((parent = node->parent) && parent->color == Red) { + gparent = parent->parent; - if (parent == gparent->left) { - RBNode *uncle = gparent->right; - if (uncle && uncle->color == Red) { - uncle->color = Black; - parent->color = Black; - gparent->color = Red; - node = gparent; - continue; - } + if (parent == gparent->left) { + RBNode *uncle = gparent->right; + if (uncle && uncle->color == Red) { + uncle->color = Black; + parent->color = Black; + gparent->color = Red; + node = gparent; + continue; + } - if (parent->right == node) { - RBNode *tmp; - rotateleft(root, parent); - tmp = parent; - parent = node; - node = tmp; - } + if (parent->right == node) { + RBNode *tmp; + rotateleft(root, parent); + tmp = parent; + parent = node; + node = tmp; + } - parent->color = Black; - gparent->color = Red; - rotateright(root, gparent); - } + parent->color = Black; + gparent->color = Red; + rotateright(root, gparent); + } - else { - RBNode *uncle = gparent->left; - if (uncle && uncle->color == Red) { - uncle->color = Black; - parent->color = Black; - gparent->color = Red; - node = gparent; - continue; - } + else { + RBNode *uncle = gparent->left; + if (uncle && uncle->color == Red) { + uncle->color = Black; + parent->color = Black; + gparent->color = Red; + node = gparent; + continue; + } - if (parent->left == node) { - RBNode *tmp; - rotateright(root, parent); - tmp = parent; - parent = node; - node = tmp; - } + if (parent->left == node) { + RBNode *tmp; + rotateright(root, parent); + tmp = parent; + parent = node; + node = tmp; + } - parent->color = Black; - gparent->color = Red; - rotateleft(root, gparent); - } - } + parent->color = Black; + gparent->color = Red; + rotateleft(root, gparent); + } + } - root->node->color = Black; + root->node->color = Black; } static void removecolor(RBTree *root, RBNode *node, RBNode *parent) { - RBNode *other; + RBNode *other; - while ((!node || node->color == Black) && node != root->node) { - if (parent->left == node) { - other = parent->right; - if (other->color == Red) { - other->color = Black; - parent->color = Red; - rotateleft(root, parent); - other = parent->right; - } - if ((!other->left || other->left->color == Black) - && (!other->right - || other->right->color == Black)) { - other->color = Red; - node = parent; - parent = node->parent; - } else { - if (!other->right - || other->right->color == Black) { - RBNode *o_left; - if ((o_left = other->left)) - o_left->color = Black; - other->color = Red; - rotateright(root, other); - other = parent->right; - } - other->color = parent->color; - parent->color = Black; - if (other->right) - other->right->color = Black; - rotateleft(root, parent); - node = root->node; - break; - } - } + while ((!node || node->color == Black) && node != root->node) { + if (parent->left == node) { + other = parent->right; + if (other->color == Red) { + other->color = Black; + parent->color = Red; + rotateleft(root, parent); + other = parent->right; + } + if ((!other->left || other->left->color == Black) + && (!other->right + || other->right->color == Black)) { + other->color = Red; + node = parent; + parent = node->parent; + } else { + if (!other->right + || other->right->color == Black) { + RBNode *o_left; + if ((o_left = other->left)) + o_left->color = Black; + other->color = Red; + rotateright(root, other); + other = parent->right; + } + other->color = parent->color; + parent->color = Black; + if (other->right) + other->right->color = Black; + rotateleft(root, parent); + node = root->node; + break; + } + } - else { - other = parent->left; - if (other->color == Red) { - other->color = Black; - parent->color = Red; - rotateright(root, parent); - other = parent->left; - } - if ((!other->left || other->left->color == Black) - && (!other->right - || other->right->color == Black)) { - other->color = Red; - node = parent; - parent = node->parent; - } else { - if (!other->left - || other->left->color == Black) { - RBNode *o_right; - if ((o_right = other->right)) - o_right->color = Black; - other->color = Red; - rotateleft(root, other); - other = parent->left; - } - other->color = parent->color; - parent->color = Black; - if (other->left) - other->left->color = Black; - rotateright(root, parent); - node = root->node; - break; - } - } - } + else { + other = parent->left; + if (other->color == Red) { + other->color = Black; + parent->color = Red; + rotateright(root, parent); + other = parent->left; + } + if ((!other->left || other->left->color == Black) + && (!other->right + || other->right->color == Black)) { + other->color = Red; + node = parent; + parent = node->parent; + } else { + if (!other->left + || other->left->color == Black) { + RBNode *o_right; + if ((o_right = other->right)) + o_right->color = Black; + other->color = Red; + rotateleft(root, other); + other = parent->left; + } + other->color = parent->color; + parent->color = Black; + if (other->left) + other->left->color = Black; + rotateright(root, parent); + node = root->node; + break; + } + } + } - if (node) - node->color = Black; + if (node) + node->color = Black; } void uc_rb_remove(RBTree *root, RBNode *node) { - RBNode *child, *parent; - int color; + RBNode *child, *parent; + int color; - if (!node->left) - child = node->right; - else if (!node->right) - child = node->left; - else { - RBNode *old = node, *left; + if (!node->left) + child = node->right; + else if (!node->right) + child = node->left; + else { + RBNode *old = node, *left; - node = node->right; - while ((left = node->left)) - node = left; - child = node->right; - parent = node->parent; - color = node->color; + node = node->right; + while ((left = node->left)) + node = left; + child = node->right; + parent = node->parent; + color = node->color; - if (child) - child->parent = parent; - if (parent) { - if (parent->left == node) - parent->left = child; - else - parent->right = child; - } else - root->node = child; + if (child) + child->parent = parent; + if (parent) { + if (parent->left == node) + parent->left = child; + else + parent->right = child; + } else + root->node = child; - if (node->parent == old) - parent = node; - node->parent = old->parent; - node->color = old->color; - node->right = old->right; - node->left = old->left; + if (node->parent == old) + parent = node; + node->parent = old->parent; + node->color = old->color; + node->right = old->right; + node->left = old->left; - if (old->parent) { - if (old->parent->left == old) - old->parent->left = node; - else - old->parent->right = node; - } else - root->node = node; + if (old->parent) { + if (old->parent->left == old) + old->parent->left = node; + else + old->parent->right = node; + } else + root->node = node; - old->left->parent = node; - if (old->right) - old->right->parent = node; - goto finish; - } + old->left->parent = node; + if (old->right) + old->right->parent = node; + goto finish; + } - parent = node->parent; - color = node->color; + parent = node->parent; + color = node->color; - if (child) - child->parent = parent; - if (parent) { - if (parent->left == node) - parent->left = child; - else - parent->right = child; - } else - root->node = child; + if (child) + child->parent = parent; + if (parent) { + if (parent->left == node) + parent->left = child; + else + parent->right = child; + } else + root->node = child; finish: - if (color == Black) - removecolor(root, child, parent); + if (color == Black) + removecolor(root, child, parent); } RBNode* uc_rb_find(RBTree *root, const RBNode *val) { - RBNode *n = root->node; - while (n) { - int d = root->compare(val,n); - if (d < 0) - n = n->left; - else if (d > 0) - n = n->right; - else - return n; - } - return NULL; + RBNode *n = root->node; + while (n) { + int d = root->compare(val,n); + if (d < 0) + n = n->left; + else if (d > 0) + n = n->right; + else + return n; + } + return NULL; } RBNode* uc_rb_insert(RBTree *root, RBNode *child) { - RBNode **p = &root->node; - RBNode *parent = NULL; - int d; - while (*p) { - parent = *p; - d = root->compare(child, parent); - if (d < 0) - p = &(*p)->left; - else if (d > 0) - p = &(*p)->right; - else - return parent; - } + RBNode **p = &root->node; + RBNode *parent = NULL; + int d; + while (*p) { + parent = *p; + d = root->compare(child, parent); + if (d < 0) + p = &(*p)->left; + else if (d > 0) + p = &(*p)->right; + else + return parent; + } - child->parent = parent; - child->color = Red; - child->left = child->right = NULL; - *p = child; - rb_insertcolor(root, child); - return NULL; + child->parent = parent; + child->color = Red; + child->left = child->right = NULL; + *p = child; + rb_insertcolor(root, child); + return NULL; } RBNode *uc_rb_first(const RBTree *root) diff --git a/src/read_file.c b/src/read_file.c index 4d019a5..cf6c3f5 100644 --- a/src/read_file.c +++ b/src/read_file.c @@ -1,9 +1,9 @@ -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include "ucore/read_file.h" #define CHUNK_SZ 1024 @@ -11,34 +11,34 @@ char * uc_read_file(const char *f, size_t *length, size_t max) { - int fd; - char *s = NULL; - char *p; - struct stat statb; + int fd; + char *s = NULL; + char *p; + struct stat statb; size_t alloc_sz; *length = 0; - if ((fd = open(f, O_RDONLY)) == -1) - return NULL; + if ((fd = open(f, O_RDONLY)) == -1) + return NULL; - if (fstat(fd, &statb) == -1) { + if (fstat(fd, &statb) == -1) { goto out_close; } - if ((statb.st_mode & S_IFMT) == S_IFREG) { + if ((statb.st_mode & S_IFMT) == S_IFREG) { if ((size_t)statb.st_size > max) { errno = EMSGSIZE; goto out_close; } alloc_sz = (size_t) statb.st_size + 1; // + 1 to avoid one realloc - s = malloc((size_t) alloc_sz + 1); //+1 for nul terminator - } else { + s = malloc((size_t) alloc_sz + 1); //+1 for nul terminator + } else { alloc_sz = CHUNK_SZ; s = malloc(alloc_sz + 1); //+1 for nul terminator } - if (s == NULL) { + if (s == NULL) { goto out_close; } @@ -77,6 +77,6 @@ out_err_free: s = NULL; out_close: close(fd); - return s; + return s; } diff --git a/src/salloc.c b/src/salloc.c index 50858e9..6ec0767 100644 --- a/src/salloc.c +++ b/src/salloc.c @@ -6,8 +6,8 @@ typedef struct Chunk Chunk; struct Chunk { - Chunk *next; - size_t firstfree; //index to the first free byte in this chunk + Chunk *next; + size_t firstfree; //index to the first free byte in this chunk union data { //for alignment long long ll; void *p; @@ -19,99 +19,99 @@ struct Chunk { }; struct SAlloc { - size_t chunksz; //data size of each chunk - Chunk *first; //head of the chunk list - Chunk *current; + size_t chunksz; //data size of each chunk + Chunk *first; //head of the chunk list + Chunk *current; }; static Chunk * add_chunk(SAlloc *p) { - Chunk *newchunk; + Chunk *newchunk; - newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data))); - if (newchunk == NULL) - return NULL; + newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data))); + if (newchunk == NULL) + return NULL; - if (p->current != NULL) - p->current->next = newchunk; + if (p->current != NULL) + p->current->next = newchunk; - newchunk->next = NULL; - newchunk->firstfree = 0; + newchunk->next = NULL; + newchunk->firstfree = 0; - return newchunk; + return newchunk; } static Chunk * next_chunk(SAlloc *p) { - Chunk *newchunk; + Chunk *newchunk; - if (p->current->next) { - newchunk = p->current->next; - newchunk->firstfree = 0; - } else - newchunk = add_chunk(p); + if (p->current->next) { + newchunk = p->current->next; + newchunk->firstfree = 0; + } else + newchunk = add_chunk(p); - if (newchunk) - p->current = newchunk; + if (newchunk) + p->current = newchunk; - return newchunk; + return newchunk; } SAlloc * uc_new_salloc(size_t chunksz) { - SAlloc *p; - Chunk *first; - p = malloc(sizeof *p); - if (p == NULL) - return NULL; + SAlloc *p; + Chunk *first; + p = malloc(sizeof *p); + if (p == NULL) + return NULL; - p->chunksz = chunksz; - p->current = NULL; + p->chunksz = chunksz; + p->current = NULL; - if ((first = add_chunk(p)) == NULL) { - free(p); - return NULL; - } - - p->first = p->current = first; + if ((first = add_chunk(p)) == NULL) { + free(p); + return NULL; + } + + p->first = p->current = first; - return p; + return p; } void uc_reset_salloc(SAlloc *p) { - Chunk *tmp; + Chunk *tmp; - for(tmp = p->first; tmp; tmp = tmp->next) - tmp->firstfree = 0; + for(tmp = p->first; tmp; tmp = tmp->next) + tmp->firstfree = 0; - p->current = p->first; + p->current = p->first; } void * uc_s_alloc(SAlloc *p,size_t sz) { - Chunk *chunk; - void *newmem; - - chunk = p->current; - if (chunk->firstfree + sz > p->chunksz) { - if (sz > p->chunksz) - return NULL; + Chunk *chunk; + void *newmem; + + chunk = p->current; + if (chunk->firstfree + sz > p->chunksz) { + if (sz > p->chunksz) + return NULL; - chunk = next_chunk(p); - if (chunk == NULL) - return NULL; - } - - newmem = &chunk->data.data[chunk->firstfree]; - chunk->firstfree += sz; + chunk = next_chunk(p); + if (chunk == NULL) + return NULL; + } + + newmem = &chunk->data.data[chunk->firstfree]; + chunk->firstfree += sz; - return newmem; + return newmem; } void * @@ -132,15 +132,15 @@ free_chunks(Chunk *chunk) for (next = NULL; chunk != NULL; chunk = next) { next = chunk->next; - free(chunk); + free(chunk); } } void uc_free_salloc(SAlloc *p) { - free_chunks(p->first); - free(p); + free_chunks(p->first); + free(p); } diff --git a/src/sdmhash.c b/src/sdmhash.c index cd0f42d..da7d0fb 100644 --- a/src/sdmhash.c +++ b/src/sdmhash.c @@ -1,12 +1,12 @@ unsigned long uc_sdbmhash(const unsigned char *str) { - unsigned long hash = 0; - unsigned int c; + unsigned long hash = 0; + unsigned int c; - while ((c = *str++)) - hash = c + (hash << 6) + (hash << 16) - hash; + while ((c = *str++)) + hash = c + (hash << 6) + (hash << 16) - hash; - return hash; + return hash; } diff --git a/src/sprintb.c b/src/sprintb.c index 55fe576..b42e7e7 100644 --- a/src/sprintb.c +++ b/src/sprintb.c @@ -6,16 +6,16 @@ char* uc_sprintb(char *result, unsigned int value) { - char *s = result; - unsigned mask = ~((unsigned) (~0) >> 1); + char *s = result; + unsigned mask = ~((unsigned) (~0) >> 1); - if (s) { - while (mask) { - *s++ = (mask & value) ? '1' : '0'; - mask >>= 1; - } - *s = '\0'; - } + if (s) { + while (mask) { + *s++ = (mask & value) ? '1' : '0'; + mask >>= 1; + } + *s = '\0'; + } - return result; + return result; } diff --git a/src/sprintbc.c b/src/sprintbc.c index c4f035a..2a82a78 100644 --- a/src/sprintbc.c +++ b/src/sprintbc.c @@ -5,17 +5,17 @@ char* uc_sprintbc(char *result, unsigned char value) { - char *s = result; - unsigned char mask = ~((unsigned char) (~0U) >> 1U); + char *s = result; + unsigned char mask = ~((unsigned char) (~0U) >> 1U); - if (s) { - while (mask) { - *s++ = (mask & value) ? '1' : '0'; - mask >>= 1U; - } - *s = '\0'; - } + if (s) { + while (mask) { + *s++ = (mask & value) ? '1' : '0'; + mask >>= 1U; + } + *s = '\0'; + } - return result; + return result; } diff --git a/src/sprintbll.c b/src/sprintbll.c index 160bcd2..16cd31c 100644 --- a/src/sprintbll.c +++ b/src/sprintbll.c @@ -5,16 +5,16 @@ char* uc_sprintbll(char *result, unsigned long long value) { - char *s = result; - unsigned long long mask = ~((unsigned long long) (~0) >> 1); + char *s = result; + unsigned long long mask = ~((unsigned long long) (~0) >> 1); - if (s) { - while (mask) { - *s++ = (mask & value) ? '1' : '0'; - mask >>= 1; - } - *s = '\0'; - } + if (s) { + while (mask) { + *s++ = (mask & value) ? '1' : '0'; + mask >>= 1; + } + *s = '\0'; + } - return result; + return result; } diff --git a/src/sprintbs.c b/src/sprintbs.c index d4058dd..0ba3522 100644 --- a/src/sprintbs.c +++ b/src/sprintbs.c @@ -5,16 +5,16 @@ char* uc_sprintbs(char *result, unsigned short value) { - char *s = result; - unsigned short mask = ~((unsigned short) (~0) >> 1); + char *s = result; + unsigned short mask = ~((unsigned short) (~0) >> 1); - if (s) { - while (mask) { - *s++ = (mask & value) ? '1' : '0'; - mask >>= 1; - } - *s = '\0'; - } + if (s) { + while (mask) { + *s++ = (mask & value) ? '1' : '0'; + mask >>= 1; + } + *s = '\0'; + } - return result; + return result; } diff --git a/src/threadqueue.c b/src/threadqueue.c index 040b6a0..e7a13c1 100644 --- a/src/threadqueue.c +++ b/src/threadqueue.c @@ -114,13 +114,13 @@ int uc_thread_queue_tryadd(struct uc_threadqueue *queue, queue->num_elements++; //signal blocked readers - if (queue->num_read_waiters == 1) { + if (queue->num_read_waiters == 1) { //if there's just one thread waiting to pop elements //use _cond_signal. _cond_broadcast can be more expensive (os dependent) - pthread_cond_signal(&queue->read_cond); - } else if (queue->num_read_waiters > 1) { + pthread_cond_signal(&queue->read_cond); + } else if (queue->num_read_waiters > 1) { //signall all threads that there's items available. - pthread_cond_broadcast(&queue->read_cond); + pthread_cond_broadcast(&queue->read_cond); } @@ -183,13 +183,13 @@ int uc_thread_queue_tryget(struct uc_threadqueue *queue, queue->num_elements--; //signal blocked writers - if (queue->num_write_waiters == 1) { + if (queue->num_write_waiters == 1) { //if there's just one thread waiting to push elements //use _cond_signal. _cond_broadcast can be more expensive (os dependent) - pthread_cond_signal(&queue->write_cond); - } else if (queue->num_write_waiters > 1) { + pthread_cond_signal(&queue->write_cond); + } else if (queue->num_write_waiters > 1) { //signall all threads that there's items available. - pthread_cond_broadcast(&queue->write_cond); + pthread_cond_broadcast(&queue->write_cond); } pthread_mutex_unlock(&queue->mutex); @@ -301,13 +301,13 @@ int uc_thread_queue_clear(struct uc_threadqueue *queue, queue->last = &queue->first; //notify writes, there should be space now - if (queue->num_write_waiters == 1) { + if (queue->num_write_waiters == 1) { //if there's just one thread waiting to push elements //use _cond_signal. _cond_broadcast can be more expensive (os dependent) - pthread_cond_signal(&queue->write_cond); - } else if (queue->num_write_waiters > 1) { + pthread_cond_signal(&queue->write_cond); + } else if (queue->num_write_waiters > 1) { //signall all threads that there's items available. - pthread_cond_broadcast(&queue->write_cond); + pthread_cond_broadcast(&queue->write_cond); } pthread_mutex_unlock(&queue->mutex); diff --git a/src/tval.c b/src/tval.c index a2577e8..4cfab6e 100644 --- a/src/tval.c +++ b/src/tval.c @@ -3,29 +3,29 @@ struct timeval * uc_tvadd(struct timeval *dst, const struct timeval *a, const struct timeval *b) { - dst->tv_sec = a->tv_sec + b->tv_sec; - dst->tv_usec = a->tv_usec + b->tv_usec; + dst->tv_sec = a->tv_sec + b->tv_sec; + dst->tv_usec = a->tv_usec + b->tv_usec; - if (dst->tv_usec >= 1000000) { - dst->tv_sec++; + if (dst->tv_usec >= 1000000) { + dst->tv_sec++; dst->tv_usec -= 1000000; } - return dst; + return dst; } struct timeval * uc_tvsub(struct timeval *dst, const struct timeval *a, const struct timeval *b) { - dst->tv_sec = a->tv_sec - b->tv_sec; - dst->tv_usec = a->tv_usec - b->tv_usec; + dst->tv_sec = a->tv_sec - b->tv_sec; + dst->tv_usec = a->tv_usec - b->tv_usec; - if (dst->tv_usec < 0) { - dst->tv_sec--; + if (dst->tv_usec < 0) { + dst->tv_sec--; dst->tv_usec += 1000000; } - return dst; + return dst; } int @@ -36,9 +36,9 @@ uc_tvcmp(const struct timeval *a, const struct timeval *b) else if (a->tv_sec > b->tv_sec) return 1; - if (a->tv_usec < b->tv_usec) - return -1; - else if (a->tv_usec > b->tv_usec) + if (a->tv_usec < b->tv_usec) + return -1; + else if (a->tv_usec > b->tv_usec) return 1; return 0; @@ -47,9 +47,9 @@ uc_tvcmp(const struct timeval *a, const struct timeval *b) struct timeval * uc_ms2tv(struct timeval *dst, unsigned long long ms) { - dst->tv_sec = ms / 1000; - dst->tv_usec = ms % 1000 * 1000; + dst->tv_sec = ms / 1000; + dst->tv_usec = ms % 1000 * 1000; - return dst; + return dst; } diff --git a/test/phi.c b/test/phi.c index 3e9517b..648a18d 100644 --- a/test/phi.c +++ b/test/phi.c @@ -4,14 +4,14 @@ int main(int argc, char *argv[]) { - unsigned int a,N; - if (argc < 2) { - printf("Usage %s interger1\n",argv[0]); - return 1; - } - a = atoi(argv[1]); - N = uc_phi_32(a); - printf("phi of %u is %u\n",a,N); + unsigned int a,N; + if (argc < 2) { + printf("Usage %s interger1\n",argv[0]); + return 1; + } + a = atoi(argv[1]); + N = uc_phi_32(a); + printf("phi of %u is %u\n",a,N); - return 0; + return 0; }