xref: /OK3568_Linux_fs/external/security/rk_tee_user/v2/ta/vector_util/c_tree.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *
3  * Copyright (c) 1994
4  * Hewlett-Packard Company
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Hewlett-Packard Company makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  *
15  * Copyright (c) 1996
16  * Silicon Graphics Computer Systems, Inc.
17  *
18  * Permission to use, copy, modify, distribute and sell this software
19  * and its documentation for any purpose is hereby granted without fee,
20  * provided that the above copyright notice appear in all copies and
21  * that both that copyright notice and this permission notice appear
22  * in supporting documentation.  Silicon Graphics makes no
23  * representations about the suitability of this software for any
24  * purpose.  It is provided "as is" without express or implied warranty.
25  */
26 
27 /*
28 Copyright (c) 2007 Lao wen bo
29 
30 This software is provided 'as-is', without any express or implied
31 warranty. In no event will the authors be held liable for any damages
32 arising from the use of this software.
33 
34 Permission is granted to anyone to use this software for any purpose,
35 including commercial applications, and to alter it and redistribute it
36 freely, subject to the following restrictions:
37 
38     1. The origin of this software must not be misrepresented; you must not
39     claim that you wrote the original software. If you use this software
40     in a product, an acknowledgment in the product documentation would be
41     appreciated but is not required.
42 
43     2. Altered source versions must be plainly marked as such, and must not be
44     misrepresented as being the original software.
45 
46     3. This notice may not be removed or altered from any source
47     distribution.
48 
49 	Lao wen bo
50 	viewpl(at)gmail.com
51  */
52 
53 #include "c_tree.h"
54 #include "c_vector.h"
55 #include "c_algorithm.h"
56 
57 static const _c_rb_tree_color_type _S_c_rb_tree_red = false;
58 static const _c_rb_tree_color_type _S_c_rb_tree_black = true;
59 
_S_minimum(_base_ptr val)60 static _base_ptr _S_minimum(_base_ptr val)
61 {
62     while(val->_A_left)
63         val = val->_A_left;
64     return val;
65 }
66 
_S_maximum(_base_ptr val)67 static _base_ptr _S_maximum(_base_ptr val)
68 {
69     while(val->_A_right)
70         val = val->_A_right;
71     return val;
72 }
73 
_A_iterator_increment(c_piterator thiz)74 static void _A_iterator_increment(c_piterator thiz)
75 {
76     if(((_base_ptr)(thiz->_i))->_A_right)
77     {
78         thiz->_i = ((_base_ptr)(thiz->_i))->_A_right;
79         while(((_base_ptr)(thiz->_i))->_A_left)
80             thiz->_i = ((_base_ptr)(thiz->_i))->_A_left;
81     }
82     else
83     {
84         _base_ptr val = ((_base_ptr)(thiz->_i))->_A_parent;
85         while(((_base_ptr)(thiz->_i)) == val->_A_right)
86         {
87             thiz->_i = val;
88             val = val->_A_parent;
89         }
90         if(((_base_ptr)(thiz->_i))->_A_right != val)
91             thiz->_i = val;
92     }
93 }
94 
_A_iterator_decrement(c_piterator thiz)95 static void _A_iterator_decrement(c_piterator thiz)
96 {
97     if((((_base_ptr)(thiz->_i))->_A_color == _S_c_rb_tree_red) &&
98        ((_base_ptr)(thiz->_i))->_A_parent->_A_parent == ((_base_ptr)(thiz->_i)))
99         thiz->_i = ((_base_ptr)(thiz->_i))->_A_right;
100     else if(((_base_ptr)(thiz->_i))->_A_left)
101     {
102         _base_ptr val = ((_base_ptr)(thiz->_i))->_A_left;
103         while(val->_A_right)
104             val = val->_A_right;
105         thiz->_i = val;
106     }
107     else
108     {
109         _base_ptr val = ((_base_ptr)(thiz->_i))->_A_parent;
110         while(((_base_ptr)(thiz->_i)) == val->_A_left)
111         {
112             thiz->_i = val;
113             val = val->_A_parent;
114         }
115         thiz->_i = val;
116     }
117 }
118 
_A_reverse_iterator_increment(c_preverse_iterator thiz)119 static void _A_reverse_iterator_increment(c_preverse_iterator thiz)
120 {
121 	c_iterator iter;
122 	iter._i = thiz->_i;
123 	_A_iterator_decrement(&iter);
124 	thiz->_i = iter._i;
125 }
126 
_A_reverse_iterator_decrement(c_preverse_iterator thiz)127 static void _A_reverse_iterator_decrement(c_preverse_iterator thiz)
128 {
129 	c_iterator iter;
130 	iter._i = thiz->_i;
131 	_A_iterator_increment(&iter);
132 	thiz->_i = iter._i;
133 }
134 
_c_rb_tree_iterator_assign(c_piterator thiz,const c_piterator val)135 static c_iterator _c_rb_tree_iterator_assign(c_piterator thiz, const c_piterator val)
136 {
137     if(thiz != val)
138         thiz->_i = val->_i;
139     return *thiz;
140 }
141 
_c_rb_tree_iterator_ref(c_piterator thiz)142 static value_type _c_rb_tree_iterator_ref(c_piterator thiz)
143 {
144     return ((_base_ptr)(thiz->_i))->_A_value_field;
145 }
146 
_c_rb_tree_iterator_ref_assign(c_piterator thiz,const value_type val)147 static value_type _c_rb_tree_iterator_ref_assign(c_piterator thiz, const value_type val)
148 {
149     return ((_base_ptr)(thiz->_i))->_A_value_field = val;
150 }
151 
_c_rb_tree_iterator_inc(c_piterator thiz)152 static c_iterator _c_rb_tree_iterator_inc(c_piterator thiz)
153 {
154     _A_iterator_increment(thiz);
155     return *thiz;
156 }
157 
_c_rb_tree_iterator_dec(c_piterator thiz)158 static c_iterator _c_rb_tree_iterator_dec(c_piterator thiz)
159 {
160     _A_iterator_decrement(thiz);
161     return *thiz;
162 }
163 
_c_rb_tree_iterator_equal(c_piterator thiz,const c_piterator val)164 static c_bool _c_rb_tree_iterator_equal(c_piterator thiz, const c_piterator val)
165 {
166 	return (thiz->_i == val->_i &&
167 				thiz->_pft == val->_pft);
168 }
169 
170 static c_iterator_ft _c_rb_tree_iter_ft =
171 {
172     _c_rb_tree_iterator_assign,
173     _c_rb_tree_iterator_ref,
174     _c_rb_tree_iterator_ref_assign,
175     _c_rb_tree_iterator_inc,
176     NULL, /* _c_rb_tree_iterator_inc_n */
177     _c_rb_tree_iterator_dec,
178     NULL, /* _c_rb_tree_iterator_dec_n */
179     NULL, /* _c_rb_tree_iterator_diff */
180     NULL, /* _c_rb_tree_iterator_at */
181     NULL, /* _c_rb_tree_iterator_positive_n */
182     NULL, /* _c_rb_tree_iterator_negative_n */
183     _c_rb_tree_iterator_equal,
184     NULL /* _c_rb_tree_iterator_less */
185 };
186 
_A_get_iterator(_base_ptr val)187 static c_iterator _A_get_iterator(_base_ptr val)
188 {
189 	c_iterator iter;
190 	iter._pft = &_c_rb_tree_iter_ft;
191 	iter._i = val;
192 	return iter;
193 }
194 
_c_rb_tree_reverse_iterator_assign(c_preverse_iterator thiz,const c_preverse_iterator val)195 static c_reverse_iterator _c_rb_tree_reverse_iterator_assign(c_preverse_iterator thiz,
196 								const c_preverse_iterator val)
197 {
198 	if(thiz != val)
199 		thiz->_i = val->_i;
200 	return *thiz;
201 }
202 
_c_rb_tree_reverse_iterator_ref(c_preverse_iterator thiz)203 static value_type _c_rb_tree_reverse_iterator_ref(c_preverse_iterator thiz)
204 {
205 	c_iterator iter = _A_get_iterator(thiz->_i);
206 	_A_iterator_decrement(&iter);
207 	return ((_base_ptr)(iter._i))->_A_value_field;
208 }
209 
_c_rb_tree_reverse_iterator_ref_assign(c_preverse_iterator thiz,const value_type val)210 static value_type _c_rb_tree_reverse_iterator_ref_assign(c_preverse_iterator thiz, const value_type val)
211 {
212 	c_iterator iter = _A_get_iterator(thiz->_i);
213 	_A_iterator_decrement(&iter);
214 	return ((_base_ptr)(iter._i))->_A_value_field = val;
215 }
216 
_c_rb_tree_reverse_iterator_inc(c_preverse_iterator thiz)217 static c_reverse_iterator _c_rb_tree_reverse_iterator_inc(c_preverse_iterator thiz)
218 {
219 	_A_reverse_iterator_increment(thiz);
220 	return *thiz;
221 }
222 
_c_rb_tree_reverse_iterator_dec(c_preverse_iterator thiz)223 static c_reverse_iterator _c_rb_tree_reverse_iterator_dec(c_preverse_iterator thiz)
224 {
225 	_A_reverse_iterator_decrement(thiz);
226 	return *thiz;
227 }
228 
_c_rb_tree_reverse_iterator_equal(c_preverse_iterator thiz,const c_preverse_iterator val)229 static c_bool _c_rb_tree_reverse_iterator_equal(c_preverse_iterator thiz, const c_preverse_iterator val)
230 {
231 	return (thiz->_i == val->_i &&
232 				thiz->_pft == val->_pft);
233 }
234 
235 static c_reverse_iterator_ft _c_rb_tree_reverse_iter_ft =
236 {
237     _c_rb_tree_reverse_iterator_assign,
238     _c_rb_tree_reverse_iterator_ref,
239     _c_rb_tree_reverse_iterator_ref_assign,
240     _c_rb_tree_reverse_iterator_inc,
241     NULL, /* _c_rb_tree_reverse_iterator_inc_n */
242     _c_rb_tree_reverse_iterator_dec,
243     NULL, /* _c_rb_tree_reverse_iterator_dec_n */
244     NULL, /* _c_rb_tree_reverse_iterator_diff */
245     NULL, /* _c_rb_tree_reverse_iterator_at */
246     NULL, /* _c_rb_tree_reverse_iterator_positive_n */
247     NULL, /* _c_rb_tree_reverse_iterator_negative_n */
248     _c_rb_tree_reverse_iterator_equal,
249     NULL, /* _c_rb_tree_reverse_iterator_less */
250 };
251 
_A_get_reverse_iterator(_base_ptr val)252 static c_reverse_iterator _A_get_reverse_iterator(_base_ptr val)
253 {
254 	c_reverse_iterator iter;
255 	iter._pft = &_c_rb_tree_reverse_iter_ft;
256 	iter._i = val;
257 	return iter;
258 }
259 
_c_rb_tree_rotate_left(_base_ptr val,_base_ptr * proot)260 static void _c_rb_tree_rotate_left(_base_ptr val, _base_ptr * proot)
261 {
262 	_base_ptr wal = val->_A_right;
263 	val->_A_right = wal->_A_left;
264 	if(wal->_A_left != NULL)
265 		wal->_A_left->_A_parent = val;
266 	wal->_A_parent = val->_A_parent;
267 
268 	if(val == *proot)
269 		*proot = wal;
270 	else if(val == val->_A_parent->_A_left)
271 		val->_A_parent->_A_left = wal;
272 	else
273 		val->_A_parent->_A_right = wal;
274 	wal->_A_left = val;
275 	val->_A_parent = wal;
276 }
277 
_c_rb_tree_rotate_right(_base_ptr val,_base_ptr * proot)278 static void _c_rb_tree_rotate_right(_base_ptr val, _base_ptr * proot)
279 {
280 	_base_ptr wal = val->_A_left;
281 	val->_A_left = wal->_A_right;
282 	if(wal->_A_right != NULL)
283 		wal->_A_right->_A_parent = val;
284 	wal->_A_parent = val->_A_parent;
285 
286 	if(val == *proot)
287 		*proot = wal;
288 	else if(val == val->_A_parent->_A_right)
289 		val->_A_parent->_A_right = wal;
290 	else
291 		val->_A_parent->_A_left = wal;
292 	wal->_A_right = val;
293 	val->_A_parent = wal;
294 }
295 
_c_rb_tree_rebalance(_base_ptr val,_base_ptr * proot)296 void _c_rb_tree_rebalance(_base_ptr val, _base_ptr * proot)
297 {
298 	val->_A_color = _S_c_rb_tree_red;
299 	while(val != *proot && val->_A_parent->_A_color == _S_c_rb_tree_red)
300 	{
301 		if(val->_A_parent == val->_A_parent->_A_parent->_A_left)
302 		{
303 			_base_ptr wal = val->_A_parent->_A_parent->_A_right;
304 			if(wal && wal->_A_color == _S_c_rb_tree_red)
305 			{
306 				val->_A_parent->_A_color = _S_c_rb_tree_black;
307 				wal->_A_color = _S_c_rb_tree_black;
308 				val->_A_parent->_A_parent->_A_color = _S_c_rb_tree_red;
309 				val = val->_A_parent->_A_parent;
310 			}
311 			else
312 			{
313 				if(val == val->_A_parent->_A_right)
314 				{
315 					val = val->_A_parent;
316 					_c_rb_tree_rotate_left(val, proot);
317 				}
318 				val->_A_parent->_A_color = _S_c_rb_tree_black;
319 				val->_A_parent->_A_parent->_A_color = _S_c_rb_tree_red;
320 				_c_rb_tree_rotate_right(val->_A_parent->_A_parent, proot);
321 			}
322 		}
323 		else
324 		{
325 			_base_ptr wal = val->_A_parent->_A_parent->_A_left;
326 			if(wal && wal->_A_color == _S_c_rb_tree_red)
327 			{
328 				val->_A_parent->_A_color = _S_c_rb_tree_black;
329 				wal->_A_color = _S_c_rb_tree_black;
330 				val->_A_parent->_A_parent->_A_color = _S_c_rb_tree_red;
331 				val = val->_A_parent->_A_parent;
332 			}
333 			else
334 			{
335 				if(val == val->_A_parent->_A_left)
336 				{
337 					val = val->_A_parent;
338 					_c_rb_tree_rotate_right(val, proot);
339 				}
340 				val->_A_parent->_A_color = _S_c_rb_tree_black;
341 				val->_A_parent->_A_parent->_A_color = _S_c_rb_tree_red;
342 				_c_rb_tree_rotate_left(val->_A_parent->_A_parent, proot);
343 			}
344 		}
345 	}
346 	(*proot)->_A_color = _S_c_rb_tree_black;
347 }
348 
_c_rb_tree_rebalance_for_erase(_base_ptr z,_base_ptr * root,_base_ptr * leftmost,_base_ptr * rightmost)349 static _base_ptr _c_rb_tree_rebalance_for_erase(_base_ptr z,
350 						     _base_ptr * root,
351 						     _base_ptr * leftmost,
352 						     _base_ptr * rightmost)
353 {
354 	_base_ptr y = z;
355 	_base_ptr x = NULL;
356 	_base_ptr x_parent = NULL;
357 
358 	if(y->_A_left == NULL)
359 		x = y->_A_right;
360 	else
361 		if(y->_A_right == NULL)
362 			x = y->_A_left;
363 		else
364 		{
365 			y = y->_A_right;
366 			while(y->_A_left != NULL)
367 				y = y->_A_left;
368 			x = y->_A_right;
369 		}
370 
371 	if(y != z)
372 	{
373 		_c_rb_tree_color_type tmp;
374 		z->_A_left->_A_parent = y;
375 		y->_A_left = z->_A_left;
376 		if(y != z->_A_right)
377 		{
378 			x_parent = y->_A_parent;
379 			if(x)
380 				x->_A_parent = y->_A_parent;
381 			y->_A_parent->_A_left = x;
382 			y->_A_right = z->_A_right;
383 			z->_A_right->_A_parent = y;
384 		}
385 		else
386 			x_parent = y;
387 		if(*root == z)
388 			*root = y;
389 		else if(z->_A_parent->_A_left == z)
390 			z->_A_parent->_A_left = y;
391 		else
392 			z->_A_parent->_A_right = y;
393 		y->_A_parent = z->_A_parent;
394 		C_SWAP(y->_A_color, z->_A_color, tmp);
395 		y = z;
396 	}
397 	else
398 	{
399 		x_parent = y->_A_parent;
400 		if(x)
401 			x->_A_parent = y->_A_parent;
402 		if(*root == z)
403 			*root = x;
404 		else
405 		{
406 			if(z->_A_parent->_A_left == z)
407 				z->_A_parent->_A_left = x;
408 			else
409 				z->_A_parent->_A_right = x;
410 		}
411 		if(*leftmost == z)
412 		{
413 			if(z->_A_right == NULL)
414 				*leftmost = z->_A_parent;
415 			else
416 				*leftmost = _S_minimum(x);
417 		}
418 		if(*rightmost == z)
419 		{
420 			if(z->_A_left == NULL)
421 				*rightmost = z->_A_parent;
422 			else
423 				*rightmost = _S_maximum(x);
424 		}
425 	}
426 	if(y->_A_color != _S_c_rb_tree_red)
427 	{
428 		while(x != *root &&
429 			(x == NULL ||
430 			 x->_A_color == _S_c_rb_tree_black))
431 			if(x == x_parent->_A_left)
432 			{
433 				_base_ptr w = x_parent->_A_right;
434 				if(w->_A_color == _S_c_rb_tree_red)
435 				{
436 					w->_A_color = _S_c_rb_tree_black;
437 					x_parent->_A_color = _S_c_rb_tree_red;
438 					_c_rb_tree_rotate_left(x_parent, root);
439 					w = x_parent->_A_right;
440 				}
441 				if((w->_A_left == NULL ||
442 				    w->_A_left->_A_color == _S_c_rb_tree_black) &&
443 				   (w->_A_right == NULL ||
444 				    w->_A_right->_A_color == _S_c_rb_tree_black))
445 				{
446 					w->_A_color = _S_c_rb_tree_red;
447 					x = x_parent;
448 					x_parent = x_parent->_A_parent;
449 				}
450 				else
451 				{
452 					if(w->_A_right == NULL ||
453 					   w->_A_right->_A_color == _S_c_rb_tree_black)
454 					{
455 						if(w->_A_left)
456 							w->_A_left->_A_color = _S_c_rb_tree_black;
457 						w->_A_color = _S_c_rb_tree_red;
458 						_c_rb_tree_rotate_right(w, root);
459 						w = x_parent->_A_right;
460 					}
461 					w->_A_color = x_parent->_A_color;
462 					x_parent->_A_color = _S_c_rb_tree_black;
463 					if(w->_A_right)
464 						w->_A_right->_A_color = _S_c_rb_tree_black;
465 					_c_rb_tree_rotate_left(x_parent, root);
466 					break;
467 				}
468 			}
469 			else
470 			{
471 				_base_ptr w = x_parent->_A_left;
472 				if(w->_A_color == _S_c_rb_tree_red)
473 				{
474 					w->_A_color = _S_c_rb_tree_black;
475 					x_parent->_A_color = _S_c_rb_tree_red;
476 					_c_rb_tree_rotate_right(x_parent, root);
477 					w = x_parent->_A_left;
478 				}
479 				if((w->_A_right == NULL ||
480 				    w->_A_right->_A_color == _S_c_rb_tree_black) &&
481 				   (w->_A_left == NULL ||
482 				    w->_A_left->_A_color == _S_c_rb_tree_black))
483 				{
484 					w->_A_color = _S_c_rb_tree_red;
485 					x = x_parent;
486 					x_parent = x_parent->_A_parent;
487 				}
488 				else
489 				{
490 					if(w->_A_left == NULL ||
491 						w->_A_left->_A_color == _S_c_rb_tree_black)
492 					{
493 						if(w->_A_right)
494 							w->_A_right->_A_color = _S_c_rb_tree_black;
495 						w->_A_color = _S_c_rb_tree_red;
496 						_c_rb_tree_rotate_left(w, root);
497 						w = x_parent->_A_left;
498 					}
499 					w->_A_color = x_parent->_A_color;
500 					x_parent->_A_color = _S_c_rb_tree_black;
501 					if(w->_A_left)
502 						w->_A_left->_A_color = _S_c_rb_tree_black;
503 					_c_rb_tree_rotate_right(x_parent, root);
504 					break;
505 				}
506 			}
507 		if(x)
508 			x->_A_color = _S_c_rb_tree_black;
509 	}
510 	return y;
511 }
512 
_A_get_node()513 static _base_ptr _A_get_node()
514 {
515 	_base_ptr tmp = __c_malloc(sizeof(_c_rb_tree_node));
516 	memset(tmp, 0, sizeof(_c_rb_tree_node));
517 	return tmp;
518 }
519 
_A_put_node(_base_ptr val)520 static void _A_put_node(_base_ptr val)
521 {
522 	__c_free(val);
523 }
524 
_A_create_node(const value_type val)525 static _base_ptr _A_create_node(const value_type val)
526 {
527 	_base_ptr tmp = _A_get_node();
528 	tmp->_A_value_field = val;
529 	return tmp;
530 }
531 
_A_clone_node(_base_ptr val)532 static _base_ptr _A_clone_node(_base_ptr val)
533 {
534 	_base_ptr tmp = _A_create_node(val->_A_value_field);
535 	tmp->_A_color = val->_A_color;
536 	tmp->_A_left = NULL;
537 	tmp->_A_right = NULL;
538 	return tmp;
539 }
540 
_A_destroy_node(_base_ptr val)541 static void _A_destroy_node(_base_ptr val)
542 {
543 	_A_put_node(val);
544 }
545 
_A_root(c_prb_tree thiz)546 static _base_ptr * _A_root(c_prb_tree thiz)
547 {
548 	return &thiz->_A_header->_A_parent;
549 }
550 
_A_leftmost(c_prb_tree thiz)551 static _base_ptr * _A_leftmost(c_prb_tree thiz)
552 {
553 	return &thiz->_A_header->_A_left;
554 }
555 
_A_rightmost(c_prb_tree thiz)556 static _base_ptr * _A_rightmost(c_prb_tree thiz)
557 {
558 	return &thiz->_A_header->_A_right;
559 }
560 
_S_left(_base_ptr val)561 static _base_ptr * _S_left(_base_ptr val)
562 {
563 	return &val->_A_left;
564 }
565 
_S_right(_base_ptr val)566 static _base_ptr * _S_right(_base_ptr val)
567 {
568 	return &val->_A_right;
569 }
570 
_S_parent(_base_ptr val)571 static _base_ptr * _S_parent(_base_ptr val)
572 {
573 	return &val->_A_parent;
574 }
575 
_S_value(_base_ptr val)576 static value_type * _S_value(_base_ptr val)
577 {
578 	return &val->_A_value_field;
579 }
580 
_S_key(c_prb_tree thiz,_base_ptr val)581 static key_type _S_key(c_prb_tree thiz, _base_ptr val)
582 {
583 	return thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, *_S_value(val));
584 }
585 
_S_color(_base_ptr val)586 static _color_type * _S_color(_base_ptr val)
587 {
588 	return &val->_A_color;
589 }
590 
_A_insert(c_prb_tree thiz,_base_ptr x,_base_ptr y,const value_type val)591 static c_iterator _A_insert(c_prb_tree thiz, _base_ptr x, _base_ptr y, const value_type val)
592 {
593 	_base_ptr _x = x;
594 	_base_ptr _y = y;
595 	_base_ptr _z = NULL;
596 	if(_y == thiz->_A_header ||
597 		_x != NULL ||
598 		thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, val),
599 			_S_key(thiz, _y)) < 0)
600 	{
601 		_z = _A_create_node(val);
602 		*_S_left(_y) = _z;
603 
604 		if(_y == thiz->_A_header)
605 		{
606 			*_A_root(thiz) = _z;
607 			*_A_rightmost(thiz) = _z;
608 		}
609 		else if(_y == *_A_leftmost(thiz))
610 			*_A_leftmost(thiz) = _z;
611 	}
612 	else
613 	{
614 		_z = _A_create_node(val);
615 		*_S_right(_y) = _z;
616 		if(_y == *_A_rightmost(thiz))
617 			*_A_rightmost(thiz) = _z;
618 	}
619 
620 	*_S_parent(_z) = _y;
621 	*_S_left(_z) = NULL;
622 	*_S_right(_z) = NULL;
623 	_c_rb_tree_rebalance(_z, &thiz->_A_header->_A_parent);
624 	++ thiz->_A_node_count;
625 	return _A_get_iterator(_z);
626 }
627 
_A_copy(_base_ptr x,_base_ptr p)628 static _base_ptr _A_copy(_base_ptr x, _base_ptr p)
629 {
630 	_base_ptr top = _A_clone_node(x);
631 	top->_A_parent = p;
632 
633 	if(x->_A_right)
634 		top->_A_right = _A_copy(*_S_right(x), top);
635 	p = top;
636 	x = *_S_left(x);
637 
638 	while(x != NULL)
639 	{
640 		_base_ptr y = _A_clone_node(x);
641 		p->_A_left = y;
642 		y->_A_parent = p;
643 		if(x->_A_right)
644 			y->_A_right = _A_copy(*_S_right(x), y);
645 		p = y;
646 		x = *_S_left(x);
647 	}
648 
649 	return top;
650 }
651 
_A_erase(_base_ptr x)652 static void _A_erase(_base_ptr x)
653 {
654 	while(x != NULL)
655 	{
656 		_base_ptr y;
657 		_A_erase(*_S_right(x));
658 		y = *_S_left(x);
659 		_A_destroy_node(x);
660 		x = y;
661 	}
662 }
663 
_A_empty_initialize(c_prb_tree thiz)664 static void _A_empty_initialize(c_prb_tree thiz)
665 {
666 	*_S_color(thiz->_A_header) = _S_c_rb_tree_red;
667 	*_A_root(thiz) = NULL;
668 	*_A_leftmost(thiz) = thiz->_A_header;
669 	*_A_rightmost(thiz) = thiz->_A_header;
670 }
671 
__c_rb_tree(c_prb_tree thiz,COMPARER pcmp)672 void __c_rb_tree(c_prb_tree thiz, COMPARER pcmp)
673 {
674 	thiz->_A_header = _A_get_node();
675 	thiz->_A_node_count = 0;
676 	thiz->_A_key_compare = pcmp;
677 	_A_empty_initialize(thiz);
678 }
679 
__c_eert_br(c_prb_tree thiz)680 void __c_eert_br(c_prb_tree thiz)
681 {
682 	c_rb_tree_clear(thiz);
683 	_A_put_node(thiz->_A_header);
684 }
685 
c_rb_tree_assign(c_prb_tree thiz,const c_prb_tree T)686 c_prb_tree c_rb_tree_assign(c_prb_tree thiz, const c_prb_tree T)
687 {
688 	if(thiz != T)
689 	{
690 		c_rb_tree_clear(thiz);
691 		thiz->_A_node_count = 0;
692 		thiz->_A_key_compare = T->_A_key_compare;
693 		if(*_A_root(T) == NULL)
694 		{
695 			*_A_root(thiz) = NULL;
696 			*_A_leftmost(thiz) = thiz->_A_header;
697 			*_A_rightmost(thiz) = thiz->_A_header;
698 		}
699 		else
700 		{
701 			*_A_root(thiz) = _A_copy(*_A_root(T), thiz->_A_header);
702 			*_A_leftmost(thiz) = _S_minimum(*_A_root(thiz));
703 			*_A_rightmost(thiz) = _S_maximum(*_A_root(thiz));
704 			thiz->_A_node_count = T->_A_node_count;
705 		}
706 	}
707 	return thiz;
708 }
709 
c_rb_tree_begin(c_prb_tree thiz)710 c_iterator c_rb_tree_begin(c_prb_tree thiz)
711 {
712 	return _A_get_iterator(*_A_leftmost(thiz));
713 }
714 
c_rb_tree_end(c_prb_tree thiz)715 c_iterator c_rb_tree_end(c_prb_tree thiz)
716 {
717 	return _A_get_iterator(thiz->_A_header);
718 }
719 
c_rb_tree_rbegin(c_prb_tree thiz)720 c_reverse_iterator c_rb_tree_rbegin(c_prb_tree thiz)
721 {
722 	return _A_get_reverse_iterator(thiz->_A_header);
723 }
724 
c_rb_tree_rend(c_prb_tree thiz)725 c_reverse_iterator c_rb_tree_rend(c_prb_tree thiz)
726 {
727 	return _A_get_reverse_iterator(*_A_leftmost(thiz));
728 }
729 
c_rb_tree_empty(c_prb_tree thiz)730 c_bool c_rb_tree_empty(c_prb_tree thiz)
731 {
732 	return thiz->_A_node_count == 0;
733 }
734 
c_rb_tree_size(c_prb_tree thiz)735 size_type c_rb_tree_size(c_prb_tree thiz)
736 {
737 	return thiz->_A_node_count;
738 }
739 
c_rb_tree_max_size(c_prb_tree thiz)740 size_type c_rb_tree_max_size(c_prb_tree thiz)
741 {
742 	return (size_type)(-1);
743 }
744 
c_rb_tree_swap(c_prb_tree thiz,c_prb_tree T)745 void c_rb_tree_swap(c_prb_tree thiz, c_prb_tree T)
746 {
747 	c_rb_tree tmp;
748 	C_SWAP(*thiz, *T, tmp);
749 }
750 
c_rb_tree_insert_unique(c_prb_tree thiz,const value_type val)751 c_iter_bool_pair c_rb_tree_insert_unique(c_prb_tree thiz, const value_type val)
752 {
753 	_base_ptr y = thiz->_A_header;
754 	_base_ptr x = *_A_root(thiz);
755 	c_bool comp = 1;
756 	c_iterator j;
757 
758 	while(x != NULL)
759 	{
760 		y = x;
761 		comp = thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, val),
762 									_S_key(thiz, x)) < 0;
763 		x = comp ? *_S_left(x) : *_S_right(x);
764 	}
765 	j = _A_get_iterator(y);
766 	if(comp)
767 	{
768 		c_iterator beg = c_rb_tree_begin(thiz);
769 		if(ITER_EQUAL(j, beg))
770 			return c_make_iter_bool_pair(_A_insert(thiz, x, y, val), true);
771 		else
772 			ITER_DEC(j);
773 	}
774 	if(thiz->_A_key_compare(_S_key(thiz, j._i),
775 				thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, val)) < 0)
776 		return c_make_iter_bool_pair(_A_insert(thiz, x, y, val), true);
777 	return c_make_iter_bool_pair(j, false);
778 }
779 
c_rb_tree_insert_equal(c_prb_tree thiz,const value_type val)780 c_iterator c_rb_tree_insert_equal(c_prb_tree thiz, const value_type val)
781 {
782 	_base_ptr y = thiz->_A_header;
783 	_base_ptr x = *_A_root(thiz);
784 	while(x != NULL)
785 	{
786 		y = x;
787 		x = thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, val),
788 					_S_key(thiz, x)) < 0 ?
789 			*_S_left(x) : *_S_right(x);
790 	}
791 	return _A_insert(thiz, x, y, val);
792 }
793 
c_rb_tree_insert_unique1(c_prb_tree thiz,c_iterator position,const value_type val)794 c_iterator c_rb_tree_insert_unique1(c_prb_tree thiz, c_iterator position, const value_type val)
795 {
796 	if(position._i == thiz->_A_header->_A_left)
797 	{
798 		if(c_rb_tree_size(thiz) > 0 &&
799 			thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, val),
800 						_S_key(thiz, position._i)) < 0)
801 			return _A_insert(thiz, position._i, position._i, val);
802 		else
803 			return c_rb_tree_insert_unique(thiz, val).first;
804 	}
805 	else if(position._i == thiz->_A_header)
806 	{
807 		if(thiz->_A_key_compare(_S_key(thiz, *_A_rightmost(thiz)),
808 					thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue,
809 								val)) < 0)
810 			return _A_insert(thiz, 0, *_A_rightmost(thiz), val);
811 		else
812 			return c_rb_tree_insert_unique(thiz, val).first;
813 	}
814 	else
815 	{
816 		c_iterator before = position;
817 		ITER_DEC(before);
818 		if(thiz->_A_key_compare(_S_key(thiz, before._i),
819 					thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue,
820 								val)) < 0 &&
821 			thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue,
822 									val),
823 						_S_key(thiz, position._i)) < 0)
824 			if(*_S_right(before._i) == NULL)
825 				return _A_insert(thiz, 0, before._i, val);
826 			else
827 				return _A_insert(thiz, position._i, position._i, val);
828 		else
829 			return c_rb_tree_insert_unique(thiz, val).first;
830 	}
831 }
832 
c_rb_tree_insert_equal1(c_prb_tree thiz,c_iterator position,const value_type val)833 c_iterator c_rb_tree_insert_equal1(c_prb_tree thiz, c_iterator position, const value_type val)
834 {
835 	if(position._i == thiz->_A_header->_A_left)
836 	{
837 		if(c_rb_tree_size(thiz) > 0 &&
838 			thiz->_A_key_compare(_S_key(thiz, position._i),
839 						thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue,
840 									val)) >= 0)
841 			return _A_insert(thiz, position._i, position._i, val);
842 		else
843 			return c_rb_tree_insert_equal(thiz, val);
844 	}
845 	else if(position._i == thiz->_A_header)
846 	{
847 		if(thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue, val),
848 					_S_key(thiz, *_A_rightmost(thiz)))  >= 0)
849 			return _A_insert(thiz, 0, *_A_rightmost(thiz), val);
850 		else
851 			return c_rb_tree_insert_equal(thiz, val);
852 	}
853 	else
854 	{
855 		c_iterator before = position;
856 		ITER_DEC(before);
857 		if(thiz->_A_key_compare(thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue,
858 						val),
859 					_S_key(thiz, before._i)) >= 0 &&
860 			thiz->_A_key_compare(_S_key(thiz, position._i),
861 						thiz->_A_keyofvalue.O(&thiz->_A_keyofvalue,
862 									val)) >= 0)
863 		{
864 			if(*_S_right(before._i) == NULL)
865 				return _A_insert(thiz, 0, before._i, val);
866 			else
867 				return _A_insert(thiz, position._i, position._i, val);
868 		}
869 		else
870 			return c_rb_tree_insert_equal(thiz, val);
871 
872 	}
873 }
874 
c_rb_tree_insert_unique2(c_prb_tree thiz,c_iterator first,c_iterator last)875 void c_rb_tree_insert_unique2(c_prb_tree thiz, c_iterator first, c_iterator last)
876 {
877 	for(; !ITER_EQUAL(first, last); ITER_INC(first))
878 		c_rb_tree_insert_unique(thiz, ITER_REF(first));
879 }
880 
c_rb_tree_insert_equal2(c_prb_tree thiz,c_iterator first,c_iterator last)881 void c_rb_tree_insert_equal2(c_prb_tree thiz, c_iterator first, c_iterator last)
882 {
883 	for(; !ITER_EQUAL(first, last); ITER_INC(first))
884 		c_rb_tree_insert_equal(thiz, ITER_REF(first));
885 }
886 
c_rb_tree_erase(c_prb_tree thiz,c_iterator position)887 void c_rb_tree_erase(c_prb_tree thiz, c_iterator position)
888 {
889 	_base_ptr y = _c_rb_tree_rebalance_for_erase(position._i,
890 							&thiz->_A_header->_A_parent,
891 							&thiz->_A_header->_A_left,
892 							&thiz->_A_header->_A_right);
893 	_A_destroy_node(y);
894 	-- thiz->_A_node_count;
895 }
896 
c_rb_tree_erase1(c_prb_tree thiz,key_type key)897 size_type c_rb_tree_erase1(c_prb_tree thiz, key_type key)
898 {
899 	c_iter_iter_pair p = c_rb_tree_equal_range(thiz, key);
900 	difference_type n = 0;
901 	c_distance1(p.first, p.second, &n);
902 	c_rb_tree_erase2(thiz, p.first, p.second);
903 	return n;
904 }
905 
c_rb_tree_erase2(c_prb_tree thiz,c_iterator first,c_iterator last)906 void c_rb_tree_erase2(c_prb_tree thiz, c_iterator first, c_iterator last)
907 {
908 	c_iterator begin, end;
909 	begin = c_rb_tree_begin(thiz);
910 	end = c_rb_tree_end(thiz);
911 	if(ITER_EQUAL(first, begin) &&
912 		ITER_EQUAL(last, end))
913 		c_rb_tree_clear(thiz);
914 	else
915 		while(!ITER_EQUAL(first, last))
916 		{
917 			c_rb_tree_erase(thiz, first);
918 			ITER_INC(first);
919 		}
920 }
921 
c_rb_tree_clear(c_prb_tree thiz)922 void c_rb_tree_clear(c_prb_tree thiz)
923 {
924 	if(thiz->_A_node_count != 0)
925 	{
926 		_A_erase(*_A_root(thiz));
927 		*_A_leftmost(thiz) = thiz->_A_header;
928 		*_A_root(thiz) = NULL;
929 		*_A_rightmost(thiz) = thiz->_A_header;
930 		thiz->_A_node_count = 0;
931 	}
932 }
933 
934 
c_rb_tree_find(c_prb_tree thiz,key_type key)935 c_iterator c_rb_tree_find(c_prb_tree thiz, key_type key)
936 {
937 	_base_ptr y = thiz->_A_header;
938 	_base_ptr x = *_A_root(thiz);
939 	c_iterator j;
940 	c_iterator end = c_rb_tree_end(thiz);
941 
942 	while(x != NULL)
943 		if(thiz->_A_key_compare(_S_key(thiz, x), key) >= 0)
944 		{
945 			y = x;
946 			x = *_S_left(x);
947 		}
948 		else
949 			x = *_S_right(x);
950 	j = _A_get_iterator(y);
951 	return (ITER_EQUAL(j, end) ||
952 			thiz->_A_key_compare(key, _S_key(thiz, j._i)) < 0) ?
953 			end : j;
954 }
955 
c_rb_tree_count(c_prb_tree thiz,key_type key)956 size_type c_rb_tree_count(c_prb_tree thiz, key_type key)
957 {
958 	c_iter_iter_pair p = c_rb_tree_equal_range(thiz, key);
959 	difference_type n = 0;
960 	c_distance1(p.first, p.second, &n);
961 	return abs(n);
962 }
963 
c_rb_tree_lower_bound(c_prb_tree thiz,key_type key)964 c_iterator c_rb_tree_lower_bound(c_prb_tree thiz, key_type key)
965 {
966 	_base_ptr y = thiz->_A_header;
967 	_base_ptr x = *_A_root(thiz);
968 
969 	while(x != NULL)
970 		if(thiz->_A_key_compare(_S_key(thiz, x), key) >= 0)
971 		{
972 			y = x;
973 			x = *_S_left(x);
974 		}
975 		else
976 			x = *_S_right(x);
977 
978 	return _A_get_iterator(y);
979 }
980 
c_rb_tree_upper_bound(c_prb_tree thiz,key_type key)981 c_iterator c_rb_tree_upper_bound(c_prb_tree thiz, key_type key)
982 {
983 	_base_ptr y = thiz->_A_header;
984 	_base_ptr x = *_A_root(thiz);
985 
986 	while(x != NULL)
987 		if(thiz->_A_key_compare(key, _S_key(thiz, x)) < 0)
988 		{
989 			y = x;
990 			x = *_S_left(x);
991 		}
992 		else
993 			x = *_S_right(x);
994 
995 	return _A_get_iterator(y);
996 }
997 
c_rb_tree_equal_range(c_prb_tree thiz,key_type key)998 c_iter_iter_pair c_rb_tree_equal_range(c_prb_tree thiz, key_type key)
999 {
1000 	return c_make_iter_iter_pair(c_rb_tree_lower_bound(thiz, key),
1001 					c_rb_tree_upper_bound(thiz, key));
1002 }
1003 
c_rb_tree_less(c_prb_tree thiz,const c_prb_tree T,COMPARER cmp)1004 c_bool c_rb_tree_less(c_prb_tree thiz, const c_prb_tree T, COMPARER cmp)
1005 {
1006 	return c_lexicographical_compare(c_rb_tree_begin(thiz),
1007 					 c_rb_tree_end(thiz),
1008 					 c_rb_tree_begin(T),
1009 					 c_rb_tree_end(T),
1010 					 cmp);
1011 }
1012 
c_rb_tree_equal(c_prb_tree thiz,const c_prb_tree T,COMPARER cmp)1013 c_bool c_rb_tree_equal(c_prb_tree thiz, const c_prb_tree T, COMPARER cmp)
1014 {
1015 	c_binary_predicate bpred = c_binary_negate(cmp);
1016 	return c_rb_tree_size(thiz) == c_rb_tree_size(T) &&
1017 		c_equal2(c_rb_tree_begin(thiz), c_rb_tree_end(thiz), c_rb_tree_begin(T), bpred);
1018 }
1019 
__black_count(_base_ptr node,_base_ptr root)1020 static int __black_count(_base_ptr node, _base_ptr root)
1021 {
1022 	if(node == NULL)
1023 		return 0;
1024 	else
1025 	{
1026 		int bc = node->_A_color == _S_c_rb_tree_black ? 1 : 0;
1027 		if(node == root)
1028 			return bc;
1029 		else
1030 			return bc + __black_count(node->_A_parent, root);
1031 	}
1032 }
1033 
__c_rb_tree_verify(c_prb_tree thiz)1034 c_bool __c_rb_tree_verify(c_prb_tree thiz)
1035 {
1036 	c_iterator begin = c_rb_tree_begin(thiz);
1037 	c_iterator end = c_rb_tree_end(thiz);
1038 	int len;
1039 	c_iterator iter;
1040 
1041 	if(thiz->_A_node_count == 0 || ITER_EQUAL(begin, end))
1042 		return thiz->_A_node_count == 0 && ITER_EQUAL(begin, end) &&
1043 			thiz->_A_header->_A_left == thiz->_A_header &&
1044 			thiz->_A_header->_A_right == thiz->_A_header;
1045 
1046 	len = __black_count(*_A_leftmost(thiz), *_A_root(thiz));
1047 	for(iter = c_rb_tree_begin(thiz); !ITER_EQUAL(iter, end); ITER_INC(iter))
1048 	{
1049 		_base_ptr x = iter._i;
1050 		_base_ptr L = *_S_left(x);
1051 		_base_ptr R = *_S_right(x);
1052 
1053 		if(x->_A_color == _S_c_rb_tree_red)
1054 			if((L && L->_A_color == _S_c_rb_tree_red) ||
1055 				(R && R->_A_color == _S_c_rb_tree_red))
1056 			return false;
1057 
1058 		if(L && thiz->_A_key_compare(_S_key(thiz, x), _S_key(thiz, L)) < 0)
1059 			return false;
1060 		if(R && thiz->_A_key_compare(_S_key(thiz, R), _S_key(thiz, x)) < 0)
1061 			return false;
1062 
1063 		if(!L && !R && __black_count(x, *_A_root(thiz)) != len)
1064 			return false;
1065 	}
1066 
1067 	if(*_A_leftmost(thiz) != _S_minimum(*_A_root(thiz)))
1068 		return false;
1069 	if(*_A_rightmost(thiz) != _S_maximum(*_A_root(thiz)))
1070 		return false;
1071 
1072 	return true;
1073 }
1074