1 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
2 | # Copyright 2010-2015 Chris Johns (chrisj@rtems.org) |
---|
3 | # All rights reserved. |
---|
4 | # |
---|
5 | # This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
6 | # |
---|
7 | # Redistribution and use in source and binary forms, with or without |
---|
8 | # modification, are permitted provided that the following conditions are met: |
---|
9 | # |
---|
10 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
11 | # this list of conditions and the following disclaimer. |
---|
12 | # |
---|
13 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
14 | # this list of conditions and the following disclaimer in the documentation |
---|
15 | # and/or other materials provided with the distribution. |
---|
16 | # |
---|
17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
21 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
27 | # POSSIBILITY OF SUCH DAMAGE. |
---|
28 | # |
---|
29 | |
---|
30 | # |
---|
31 | # RTEMS Red/Black Tree Support |
---|
32 | # |
---|
33 | |
---|
34 | import gdb |
---|
35 | |
---|
36 | rbt_left = 0 |
---|
37 | rbt_right = 1 |
---|
38 | |
---|
39 | def opp_dir(dir_): |
---|
40 | if dir_ == rbt_left: |
---|
41 | return rbt_right |
---|
42 | return rbt_left |
---|
43 | |
---|
44 | |
---|
45 | class node: |
---|
46 | """Manage the RBTree_Node_struct.""" |
---|
47 | |
---|
48 | def __init__(self, node_ptr): |
---|
49 | self.node_ptr = node_ptr |
---|
50 | self.node_val = None |
---|
51 | if node_ptr != 0: |
---|
52 | print '}}}}' |
---|
53 | self.node_val = node_ptr.dereference() |
---|
54 | |
---|
55 | def __str__(self): |
---|
56 | return self.to_string() |
---|
57 | |
---|
58 | def null(self): |
---|
59 | if not self.node_val: |
---|
60 | return True |
---|
61 | return False |
---|
62 | |
---|
63 | def pointer(self): |
---|
64 | return self.node_ptr |
---|
65 | |
---|
66 | def parent(self): |
---|
67 | if not self.null(): |
---|
68 | return self.node_val['parent'] |
---|
69 | return None |
---|
70 | |
---|
71 | def child(self): |
---|
72 | if not self.null(): |
---|
73 | return self.node_val['child'] |
---|
74 | return None |
---|
75 | |
---|
76 | def left(self): |
---|
77 | if not self.null(): |
---|
78 | return self.node_val['child'][rbt_left] |
---|
79 | return None |
---|
80 | |
---|
81 | def right(self): |
---|
82 | if not self.null(): |
---|
83 | return self.node_val['child'][rbt_right] |
---|
84 | return None |
---|
85 | |
---|
86 | def next(self, dir_): |
---|
87 | next_ = None |
---|
88 | if not self.null(): |
---|
89 | current = self.child(color, dir_) |
---|
90 | if current is not None: |
---|
91 | while current is not None: |
---|
92 | cn = node(current) |
---|
93 | current = cn.child(opp_dir(dir_)) |
---|
94 | next_ = current |
---|
95 | else: |
---|
96 | # RBTree_Node *parent = node->parent; |
---|
97 | # if ( parent->parent && node == parent->child[ opp_dir ] ) { |
---|
98 | # |
---|
99 | # node->parent |
---|
100 | if self.parent(): |
---|
101 | # pn = *(node->parent) or pn = *parent |
---|
102 | pn = node(self.parent()) |
---|
103 | if pn.parent() and (self.pointer() == pn.child(opp_dir(dir_))): |
---|
104 | next_ = self.parent() |
---|
105 | else: |
---|
106 | nn = self |
---|
107 | while pn.parent(): |
---|
108 | if nn.pointer() != pn.child(dir_): |
---|
109 | break |
---|
110 | nn = pn |
---|
111 | pn = node(pn.parent()) |
---|
112 | if pn.parent(): |
---|
113 | next_ = pn.pointer() |
---|
114 | return next_ |
---|
115 | |
---|
116 | def predecessor(self): |
---|
117 | return self.next(rbt_left) |
---|
118 | |
---|
119 | def successor(self): |
---|
120 | return self.next(rbt_right) |
---|
121 | |
---|
122 | def color(self): |
---|
123 | if not self.null(): |
---|
124 | return self.node_val['color'] |
---|
125 | return None |
---|
126 | |
---|
127 | def cast(self, typename): |
---|
128 | if not self.null(): |
---|
129 | nodetype = gdb.lookup_type(typename) |
---|
130 | return self.node_val.cast(nodetype) |
---|
131 | return None |
---|
132 | |
---|
133 | def off_tree(self): |
---|
134 | if parent.null(): |
---|
135 | return True |
---|
136 | return False |
---|
137 | |
---|
138 | def to_string(self): |
---|
139 | if self.null(): |
---|
140 | return 'NULL' |
---|
141 | print ']]] here' |
---|
142 | return 'Parent:%s Child:%s Color:%s' % (self.node_val['parent'], |
---|
143 | self.node_val['child'], |
---|
144 | self.node_val['color']) |
---|
145 | |
---|
146 | class control: |
---|
147 | """Manage the RBTree_Control.""" |
---|
148 | |
---|
149 | def __init__(self, ctrl): |
---|
150 | self.ctrl = ctrl |
---|
151 | |
---|
152 | def root(self): |
---|
153 | """Return the root node of the RBT.""" |
---|
154 | return node(self.ctrl['root']) |
---|
155 | |
---|
156 | def first(self, dir_): |
---|
157 | """ Return the min or max nodes of this RBT.""" |
---|
158 | return node(self.ctrl['first'][dir_]) |
---|
159 | |
---|
160 | def empty(self): |
---|
161 | if self.root().null(): |
---|
162 | return True |
---|
163 | return False |
---|