X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fabt.h;h=f0a412773a0938b7348c75b1793e1e1f88ff8583;hb=e5a05f16e98783e2785107059de22a44f51c18ea;hp=24d7268a2d6543a383361c50914d796e52e7b6c1;hpb=8b71948cd57dbd2787cb4c50525b957e9be8a62b;p=pspp diff --git a/src/libpspp/abt.h b/src/libpspp/abt.h index 24d7268a2d..f0a412773a 100644 --- a/src/libpspp/abt.h +++ b/src/libpspp/abt.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2007, 2009 Free Software Foundation, Inc. + Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,10 +34,9 @@ The ABT data structure partially abstracts augmentation. The client passes in a "reaugmentation" function that accepts a - node and its left and right children. This function must - recalculate the node's augmentation data based on its own - contents and the contents of its children, and store the new - augmentation data in the node. + node. This function must recalculate the node's augmentation + data based on its own contents and the contents of its + children, and store the new augmentation data in the node. The ABT automatically calls the reaugmentation function whenever it can tell that a node's augmentation data might @@ -88,7 +87,7 @@ static struct element * node_to_element (const struct abt_node *node) { - return abt_data (node, struct element, node); + return ABT_DATA (node, struct element, node); } // Compares the DATA values in A and B and returns a @@ -104,19 +103,16 @@ } // Recalculates the count for NODE's subtree by adding up the - // counts for its LEFT and RIGHT child subtrees. + // counts for its left and right child subtrees. static void - reaugment_elements (struct abt_node *node_, - const struct abt_node *left, - const struct abt_node *right, - const void *aux) + reaugment_elements (struct abt_node *node_, const void *aux) { struct element *node = node_to_element (node_); node->count = 1; - if (left != NULL) - node->count += node_to_element (left)->count; - if (right != NULL) - node->count += node_to_element (right)->count; + if (node->node.down[0] != NULL) + node->count += node_to_element (node->node.down[0])->count; + if (node->node.down[1] != NULL) + node->count += node_to_element (node->node.down[1])->count; } // Finds and returns the element in ABT that is in the given @@ -125,7 +121,7 @@ find_by_position (struct abt *abt, int position) { struct abt_node *p; - for (p = abt->root; p != NULL; ) + for (p = abt->root; p != NULL;) { int p_pos = p->down[0] ? node_to_element (p->down[0])->count : 0; if (position == p_pos) @@ -149,15 +145,20 @@ code and links to other resources, such as the original AA tree paper. */ +#include #include -#include +#include "libpspp/cast.h" -/* Returns the data structure corresponding to the given NODE, - assuming that NODE is embedded as the given MEMBER name in - data type STRUCT. */ -#define abt_data(NODE, STRUCT, MEMBER) \ - (CHECK_POINTER_HAS_TYPE (NODE, struct abt_node *), \ - UP_CAST (NODE, STRUCT, MEMBER)) +/* Returns the data structure corresponding to the given NODE, assuming that + NODE is embedded as the given MEMBER name in data type STRUCT. NODE must + not be a null pointer. */ +#define ABT_DATA(NODE, STRUCT, MEMBER) \ + (CHECK_POINTER_HAS_TYPE (NODE, struct abt_node *), \ + UP_CAST (NODE, STRUCT, MEMBER)) + +/* Like ABT_DATA, except that a null NODE yields a null pointer result. */ +#define ABT_NULLABLE_DATA(NODE, STRUCT, MEMBER) \ + ((STRUCT *) abt_nullable_data__ (NODE, offsetof (STRUCT, MEMBER))) /* Node in an augmented binary tree. */ struct abt_node @@ -173,12 +174,10 @@ typedef int abt_compare_func (const struct abt_node *a, const struct abt_node *b, const void *aux); -/* Recalculates NODE's augmentation based on NODE's data and that - of its LEFT and RIGHT children, with the tree's AUX. */ -typedef void abt_reaugment_func (struct abt_node *node, - const struct abt_node *left, - const struct abt_node *right, - const void *aux); +/* Recalculates NODE's augmentation based on NODE's data and that of its left + and right children NODE->down[0] and NODE[1], respectively, with the tree's + AUX. */ +typedef void abt_reaugment_func (struct abt_node *node, const void *aux); /* An augmented binary tree. */ struct abt @@ -188,10 +187,14 @@ struct abt abt_reaugment_func *reaugment; /* To augment a node using its children. */ const void *aux; /* Auxiliary data. */ }; +#define ABT_INITIALIZER(COMPARE, REAUGMENT, AUX) \ + { .compare = COMPARE, .reaugment = REAUGMENT, .aux = AUX } void abt_init (struct abt *, abt_compare_func *, abt_reaugment_func *, const void *aux); +static inline bool abt_is_empty (const struct abt *); + struct abt_node *abt_insert (struct abt *, struct abt_node *); void abt_insert_after (struct abt *, const struct abt_node *, struct abt_node *); @@ -209,4 +212,42 @@ void abt_reaugmented (const struct abt *, struct abt_node *); struct abt_node *abt_changed (struct abt *, struct abt_node *); void abt_moved (struct abt *, struct abt_node *); +/* Convenience macros for iteration. + + These macros automatically use ABT_DATA to obtain the data elements that + encapsulate abt nodes, which often saves typing and can make code easier to + read. Refer to the large comment near the top of this file for an example. + + These macros evaluate their arguments many times. */ +#define ABT_FIRST(STRUCT, MEMBER, ABT) \ + ABT_NULLABLE_DATA (abt_first (ABT), STRUCT, MEMBER) +#define ABT_NEXT(DATA, STRUCT, MEMBER, ABT) \ + ABT_NULLABLE_DATA (abt_next (ABT, &(DATA)->MEMBER), STRUCT, MEMBER) +#define ABT_FOR_EACH(DATA, STRUCT, MEMBER, ABT) \ + for ((DATA) = ABT_FIRST (STRUCT, MEMBER, ABT); \ + (DATA) != NULL; \ + (DATA) = ABT_NEXT (DATA, STRUCT, MEMBER, ABT)) +#define ABT_FOR_EACH_SAFE(DATA, NEXT, STRUCT, MEMBER, ABT) \ + for ((DATA) = ABT_FIRST (STRUCT, MEMBER, ABT); \ + ((DATA) != NULL \ + ? ((NEXT) = ABT_NEXT (DATA, STRUCT, MEMBER, ABT), 1) \ + : 0); \ + (DATA) = (NEXT)) + +/* Returns true if ABT contains no nodes, false if ABT contains at least one + node. */ +static inline bool +abt_is_empty (const struct abt *abt) +{ + return abt->root == NULL; +} + +/* Helper for ABT_NULLABLE_DATA (to avoid evaluating its NODE argument more + than once). */ +static inline void * +abt_nullable_data__ (struct abt_node *node, size_t member_offset) +{ + return node != NULL ? (char *) node - member_offset : NULL; +} + #endif /* libpspp/abt.h */