libosmocore
0.9.3
Osmocom core library
|
00001 /* 00002 Red Black Trees 00003 (C) 1999 Andrea Arcangeli <andrea@suse.de> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 linux/include/linux/rbtree.h 00020 00021 To use rbtrees you'll have to implement your own insert and search cores. 00022 This will avoid us to use callbacks and to drop drammatically performances. 00023 I know it's not the cleaner way, but in C (not in C++) to get 00024 performances and genericity... 00025 00026 Some example of insert and search follows here. The search is a plain 00027 normal search over an ordered tree. The insert instead must be implemented 00028 int two steps: as first thing the code must insert the element in 00029 order as a red leaf in the tree, then the support library function 00030 rb_insert_color() must be called. Such function will do the 00031 not trivial work to rebalance the rbtree if necessary. 00032 00033 ----------------------------------------------------------------------- 00034 static inline struct page * rb_search_page_cache(struct inode * inode, 00035 unsigned long offset) 00036 { 00037 struct rb_node * n = inode->i_rb_page_cache.rb_node; 00038 struct page * page; 00039 00040 while (n) 00041 { 00042 page = rb_entry(n, struct page, rb_page_cache); 00043 00044 if (offset < page->offset) 00045 n = n->rb_left; 00046 else if (offset > page->offset) 00047 n = n->rb_right; 00048 else 00049 return page; 00050 } 00051 return NULL; 00052 } 00053 00054 static inline struct page * __rb_insert_page_cache(struct inode * inode, 00055 unsigned long offset, 00056 struct rb_node * node) 00057 { 00058 struct rb_node ** p = &inode->i_rb_page_cache.rb_node; 00059 struct rb_node * parent = NULL; 00060 struct page * page; 00061 00062 while (*p) 00063 { 00064 parent = *p; 00065 page = rb_entry(parent, struct page, rb_page_cache); 00066 00067 if (offset < page->offset) 00068 p = &(*p)->rb_left; 00069 else if (offset > page->offset) 00070 p = &(*p)->rb_right; 00071 else 00072 return page; 00073 } 00074 00075 rb_link_node(node, parent, p); 00076 00077 return NULL; 00078 } 00079 00080 static inline struct page * rb_insert_page_cache(struct inode * inode, 00081 unsigned long offset, 00082 struct rb_node * node) 00083 { 00084 struct page * ret; 00085 if ((ret = __rb_insert_page_cache(inode, offset, node))) 00086 goto out; 00087 rb_insert_color(node, &inode->i_rb_page_cache); 00088 out: 00089 return ret; 00090 } 00091 ----------------------------------------------------------------------- 00092 */ 00093 00094 #pragma once 00095 00096 #include <stdlib.h> 00097 00098 struct rb_node 00099 { 00100 unsigned long rb_parent_color; 00101 #define RB_RED 0 00102 #define RB_BLACK 1 00103 struct rb_node *rb_right; 00104 struct rb_node *rb_left; 00105 } __attribute__((aligned(sizeof(long)))); 00106 /* The alignment might seem pointless, but allegedly CRIS needs it */ 00107 00108 struct rb_root 00109 { 00110 struct rb_node *rb_node; 00111 }; 00112 00113 00114 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3)) 00115 #define rb_color(r) ((r)->rb_parent_color & 1) 00116 #define rb_is_red(r) (!rb_color(r)) 00117 #define rb_is_black(r) rb_color(r) 00118 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0) 00119 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0) 00120 00121 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) 00122 { 00123 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p; 00124 } 00125 static inline void rb_set_color(struct rb_node *rb, int color) 00126 { 00127 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color; 00128 } 00129 00130 #define RB_ROOT { NULL, } 00131 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 00132 00133 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) 00134 #define RB_EMPTY_NODE(node) (rb_parent(node) == node) 00135 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) 00136 00137 extern void rb_insert_color(struct rb_node *, struct rb_root *); 00138 extern void rb_erase(struct rb_node *, struct rb_root *); 00139 00140 /* Find logical next and previous nodes in a tree */ 00141 extern struct rb_node *rb_next(const struct rb_node *); 00142 extern struct rb_node *rb_prev(const struct rb_node *); 00143 extern struct rb_node *rb_first(const struct rb_root *); 00144 extern struct rb_node *rb_last(const struct rb_root *); 00145 00146 /* Fast replacement of a single node without remove/rebalance/add/rebalance */ 00147 extern void rb_replace_node(struct rb_node *victim, struct rb_node *_new, 00148 struct rb_root *root); 00149 00150 static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, 00151 struct rb_node ** rb_link) 00152 { 00153 node->rb_parent_color = (unsigned long )parent; 00154 node->rb_left = node->rb_right = NULL; 00155 00156 *rb_link = node; 00157 }