Rework RBTree to use RBNode in the compare function
This commit is contained in:
@@ -23,7 +23,7 @@ struct RBNode {
|
|||||||
typedef struct RBTree RBTree;
|
typedef struct RBTree RBTree;
|
||||||
struct RBTree {
|
struct RBTree {
|
||||||
RBNode *node;
|
RBNode *node;
|
||||||
int (*compare)(const void *a, const void *b);
|
int (*compare)(const RBNode *a, const RBNode *b);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Remove a node from the tree.
|
/** Remove a node from the tree.
|
||||||
@@ -35,14 +35,16 @@ void uc_rb_remove(RBTree *root, RBNode *node);
|
|||||||
|
|
||||||
/** Find a node in the tree.
|
/** Find a node in the tree.
|
||||||
*
|
*
|
||||||
* The value to find will be in a node->data pointer, the compare
|
* The value to find must be of the same type as the
|
||||||
|
* other nodes inserted into a tree. This will normally
|
||||||
|
* be a user defined struct with the RBNode as one of its members
|
||||||
* function of the tree is used to find the matching item.
|
* function of the tree is used to find the matching item.
|
||||||
*
|
*
|
||||||
* @param root Root of the tree
|
* @param root Root of the tree
|
||||||
* @param val The value to find.
|
* @param val The value to find.
|
||||||
* @return the found node
|
* @return the found node
|
||||||
*/
|
*/
|
||||||
RBNode *uc_rb_find(RBTree *root, const void *val);
|
RBNode *uc_rb_find(RBTree *root, const RBNode *val);
|
||||||
|
|
||||||
/** insert a new node
|
/** insert a new node
|
||||||
*
|
*
|
||||||
|
|||||||
+3
-3
@@ -251,11 +251,11 @@ uc_rb_remove(RBTree *root, RBNode *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
RBNode*
|
RBNode*
|
||||||
uc_rb_find(RBTree *root, const void *val)
|
uc_rb_find(RBTree *root, const RBNode *val)
|
||||||
{
|
{
|
||||||
RBNode *n = root->node;
|
RBNode *n = root->node;
|
||||||
while (n) {
|
while (n) {
|
||||||
int d = root->compare(val,n->data);
|
int d = root->compare(val,n);
|
||||||
if (d < 0)
|
if (d < 0)
|
||||||
n = n->left;
|
n = n->left;
|
||||||
else if (d > 0)
|
else if (d > 0)
|
||||||
@@ -274,7 +274,7 @@ uc_rb_insert(RBTree *root, RBNode *child)
|
|||||||
int d;
|
int d;
|
||||||
while (*p) {
|
while (*p) {
|
||||||
parent = *p;
|
parent = *p;
|
||||||
d = root->compare(child->data, parent->data);
|
d = root->compare(child, parent);
|
||||||
if (d < 0)
|
if (d < 0)
|
||||||
p = &(*p)->left;
|
p = &(*p)->left;
|
||||||
else if (d > 0)
|
else if (d > 0)
|
||||||
|
|||||||
+4
-3
@@ -1,12 +1,13 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include "ucore/timers.h"
|
#include "ucore/timers.h"
|
||||||
#include "ucore/clock.h"
|
#include "ucore/clock.h"
|
||||||
|
#include "ucore/utils.h"
|
||||||
|
|
||||||
|
|
||||||
static int timers_cmp(const void *a_, const void *b_)
|
static int timers_cmp(const RBNode *a_, const RBNode *b_)
|
||||||
{
|
{
|
||||||
const struct UCTimer *a = a_;
|
const struct UCTimer *a = CONST_CONTAINER_OF(a_, struct UCTimer, rb_node);
|
||||||
const struct UCTimer *b = b_;
|
const struct UCTimer *b = CONST_CONTAINER_OF(b_, struct UCTimer, rb_node);
|
||||||
|
|
||||||
if (timercmp(&a->timeout, &b->timeout, <)) {
|
if (timercmp(&a->timeout, &b->timeout, <)) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user