39 #ifndef MEM_REDBLACK_H 40 #define MEM_REDBLACK_H 61 void init_rbt_mem_node(){
65 color = k_invalid_color;
68 bj_ostream& print(bj_ostream& os){
73 friend bj_ostream& operator << (bj_ostream& os, rbt_mem_node& nod){
80 class rbt_mem_node_handler {
82 typedef obj_t rbt_obj_t;
83 typedef rbt_mem_node<rbt_obj_t> rbt_node_t;
84 typedef rbt_node_t* rbt_nod_ref_t;
86 typedef comparison (*cmp_func_t)(obj_t
const & obj1, obj_t
const & obj2);
90 rbt_mem_node_handler(cmp_func_t cmp_fn) : cmp_func(cmp_fn){
91 REDBLACK_CK(cmp_fn != NULL_PT);
94 ~rbt_mem_node_handler(){
96 REDBLACK_CK(cmp_func == NULL_PT);
102 rbt_mem_node_handler& operator = (
103 rbt_mem_node_handler& other)
105 RBT_OBJECT_COPY_ERROR;
108 rbt_mem_node_handler(rbt_mem_node_handler& other){
109 RBT_OBJECT_COPY_ERROR;
112 rbt_nod_ref_t create_node(rbt_obj_t
const & obj1){
113 rbt_nod_ref_t nd = tpl_malloc<rbt_node_t>();
114 new (nd) rbt_node_t();
119 void destroy_all_nodes(redblack<rbt_mem_node_handler<obj_t> >& rbt);
121 comparison compare_node_objects(rbt_obj_t
const & obj1, rbt_obj_t
const & obj2){
122 REDBLACK_CK(cmp_func != NULL_PT);
123 comparison cc_val = (*cmp_func)(obj1, obj2);
126 rbt_nod_ref_t get_null_node_reference(){
129 rbt_nod_ref_t& get_right_node_reference_of(rbt_nod_ref_t
const & nd){
130 REDBLACK_CK(nd != NULL_PT);
133 rbt_nod_ref_t& get_left_node_reference_of(rbt_nod_ref_t
const & nd){
134 REDBLACK_CK(nd != NULL_PT);
137 rbt_nod_ref_t& get_parent_node_reference_of(rbt_nod_ref_t
const & nd){
138 REDBLACK_CK(nd != NULL_PT);
141 rbt_obj_t& get_object_of(rbt_nod_ref_t
const & nd){
142 REDBLACK_CK(nd != NULL_PT);
145 rbt_color_t get_color_of(rbt_nod_ref_t
const & nd){
146 REDBLACK_CK(nd != NULL_PT);
149 void set_color_of(rbt_nod_ref_t
const & nd, rbt_color_t col){
150 REDBLACK_CK(nd != NULL_PT);
154 void free_node(redblack<rbt_mem_node_handler<obj_t> >& rbt,
157 REDBLACK_CK(nd != NULL_PT);
158 REDBLACK_CK(rbt.is_alone(nd));
160 tpl_free<rbt_node_t>(nd);
164 template<
class obj_t>
166 rbt_mem_node_handler<obj_t>::destroy_all_nodes(redblack<rbt_mem_node_handler<obj_t> >& rbt){
167 while(! rbt.is_empty()){
168 rbt_nod_ref_t mm = rbt.get_min();
169 REDBLACK_CK(mm != NULL_PT);
170 rbt_nod_ref_t nd = rbt.rbt_remove(mm);
175 template<
class obj_t>
176 class mem_redblack :
public redblack<rbt_mem_node_handler<obj_t> > {
178 typedef typename rbt_mem_node_handler<obj_t>::rbt_nod_ref_t rbt_nod_ref_t;
179 typedef typename rbt_mem_node_handler<obj_t>::rbt_node_t rbt_node_t;
182 rbt_mem_node_handler<obj_t> node_hdlr;
184 typedef redblack<rbt_mem_node_handler<obj_t> > rbt_sup_t;
185 typedef redblack_iter<rbt_mem_node_handler<obj_t> > rbt_iter_t;
186 typedef comparison (*cmp_func_t)(obj_t
const & obj1, obj_t
const & obj2);
188 mem_redblack(cmp_func_t cmp_fn) : redblack<rbt_mem_node_handler<obj_t> >(node_hdlr),
194 rbt_sup_t::clear_redblack();
200 mem_redblack& operator = (mem_redblack& other)
202 RBT_OBJECT_COPY_ERROR;
205 mem_redblack(mem_redblack& other){
206 RBT_OBJECT_COPY_ERROR;
209 bool push(obj_t obj){
210 bool p_ok = (rbt_insert(obj) != rbt_sup_t::get_null());
215 rbt_nod_ref_t ref_obj = search(obj);
216 if(ref_obj == rbt_sup_t::get_null()){
219 cmp_func_t cmp_fn = node_hdlr.cmp_func;
222 REDBLACK_CK(cmp_fn(get_obj(ref_obj), obj) == 0);
223 rbt_nod_ref_t ref_pop = rbt_remove(ref_obj);
225 REDBLACK_CK(ref_pop != rbt_sup_t::get_null());
226 node_hdlr.free_node(*
this, ref_pop);
228 SLOW_REDBLACK_CK(check_min_max());
232 bj_ostream& print_row(bj_ostream& os){
233 rbt_iter_t iter1(*
this);
234 iter1.go_first_ref();
236 while(! iter1.in_null()){
237 os << iter1.get_obj() <<
" ";
244 bj_ostream& print_tree(bj_ostream& os){
245 return rbt_sup_t::print(os);
249 bj_ostream& operator << (bj_ostream& os, mem_redblack& rbt){
255 cmp_is_sub cmp_mem_redblack(mem_redblack<obj_t>& rbt1, mem_redblack<obj_t>& rbt2,
258 rbt_iter_t iter1(rbt1);
259 rbt_iter_t iter2(rbt2);
260 cmp_func_t cmp_fn = rbt1.node_hdlr.cmp_func;
261 REDBLACK_CK(cmp_fn == rbt2.node_hdlr.cmp_func);
263 return is_subset_cmp<obj_t, rbt_iter_t>(iter1,
264 iter2, cmp_fn, are_eq);
270 #endif // MEM_REDBLACK_H