lkml.org 
[lkml]   [2012]   [May]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH 1/3] rbtree: Add rb_insert(), rb_search(), etc.
Hello, Kent.

Please cc akpm.

On Fri, May 25, 2012 at 01:57:39PM -0700, Kent Overstreet wrote:
> Change-Id: I072d94a42b75454aa62be849c724980d27523b08
>
> Signed-off-by: Kent Overstreet <koverstreet@google.com>

Same complaints.

And function comments please.

> +static inline int _rb_insert(struct rb_root *root,
> + struct rb_node *new,
> + rb_cmp_t cmp)
> +{
> + struct rb_node **n = &root->rb_node, *parent = NULL;
> + int res;
> +
> + while (*n) {
> + parent = *n;
> + res = cmp(new, *n);
> + if (!res)
> + return -1;

-EINVAL?

> + n = res < 0
> + ? &(*n)->rb_left
> + : &(*n)->rb_right;

I don't know. I'm finding this formatting rather weird. If ?: has to
be used and line has to break, how about the following?

n = res < 0 ? A
: B;

Or how about using good'ol if else?

if (res < 0)
n = A;
else if (res > 0)
n = B;
else
return -EINVAL;

> + }
> +
> + rb_link_node(new, parent, n);
> + rb_insert_color(new, root);
> + return 0;
> +}
> +
> +static inline void _rb_insert_allow_dup(struct rb_root *root,
> + struct rb_node *new,
> + rb_cmp_t cmp)
> +{
> + struct rb_node **n = &root->rb_node, *parent = NULL;
> +
> + while (*n) {
> + parent = *n;
> + n = cmp(new, *n) < 0
> + ? &(*n)->rb_left
> + : &(*n)->rb_right;

Ditto.

> + }
> +
> + rb_link_node(new, parent, n);
> + rb_insert_color(new, root);
> +}
> +
> +static inline struct rb_node *_rb_search(struct rb_root *root,
> + struct rb_node *search,
> + rb_cmp_t cmp)
> +{
> + struct rb_node *n = root->rb_node;
> +
> + while (n) {
> + int res = cmp(search, n);
> + if (!res)
> + return n;
> +
> + n = res < 0
> + ? n->rb_left
> + : n->rb_right;
> + }
> +
> + return NULL;
> +}
> +
> +static inline struct rb_node *_rb_greater(struct rb_root *root,
> + struct rb_node *search,
> + rb_cmp_t cmp)
> +{
> + struct rb_node *n = root->rb_node;
> + struct rb_node *ret = NULL;
> +
> + while (n) {
> + int res = cmp(search, n);
> +
> + if (res < 0) {
> + ret = n;
> + n = n->rb_left;
> + } else {
> + n = n->rb_right;
> + }
> + }
> +
> + return ret;
> +}

What does _rb_greater() do? If these are gonna be inlined, can't we
mostly just have single lookup function which returns either the found
node or insertion position and let each wrapper deal with the
specifics?

Thanks.

--
tejun


\
 
 \ /
  Last update: 2012-05-29 02:01    [W:0.116 / U:0.488 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site