source: to-imperative/trunk/c++/runtime/rf_table.ih @ 2752

Last change on this file since 2752 was 2752, checked in by orlov, 13 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1#ifndef __rf_table_ih__
2
3#define __rf_table_ih__
4
5#include <new>
6#include "rf_table.hh"
7#include "rf_object.ih"
8#include "rf_expr.ih"
9#include "rf_sysdefs.hh"
10
11namespace rftype
12{
13
14inline Table::Table()
15  : Object()
16{
17  if (!count) {
18    TableNode::nil = new TableNode;
19    TableNode::nil->left = TableNode::nil;
20    TableNode::nil->right = TableNode::nil;
21    TableNode::nil->p = TableNode::nil;
22    TableNode::nil->color = TableNode::BLACK;
23  }
24  root = TableNode::nil;
25  size = 0;
26  count ++;
27}
28
29inline Table::Table(Table const& _tab)
30  : Object()
31{
32  size = _tab.size;
33  count++;
34  if (_tab.root == TableNode::nil)
35    root = TableNode::nil;
36  else {
37    TableNode* node = new TableNode;
38    root = node;
39    node->p = TableNode::nil;
40    copy_tree(*(_tab.root), *node);
41  }
42}       
43
44
45inline unsigned Table::get_type () const
46{
47  return reg.get_type();
48}
49
50inline uint32_t Table::hash () const
51{
52  return 0;
53}
54
55inline bool Table::operator== (Object const& _table) const
56{
57  try {
58    Table const&  t = static_cast <Table const&>(_table); 
59    return table_equal(*root, *t.root);
60 }
61  catch (std::bad_cast&)
62  {
63    return false;
64  }
65}
66
67inline Table::~Table ()
68
69   if (root != TableNode::nil)
70     delete_tree(root);
71   if (!--count)
72     delete(TableNode::nil);
73}
74
75inline Expr Table::domain () const
76
77  Expr res = Expr();
78  if (root != TableNode::nil)
79    list_node(*root, res);   
80  return res;
81}
82
83inline Expr Table::values () const
84
85  Expr res = Expr();
86  if (root != TableNode::nil)
87    list_values(*root, res);   
88  return res;
89}
90
91inline Expr Table::entries () const
92
93  Expr res = Expr();
94  if (root != TableNode::nil)
95    list_entries(*root, res);   
96  return res;
97}
98
99inline size_t Table::get_size () const
100{
101  return size;
102}
103
104//  zaglushka  - hash-function
105inline int expr_hash (Expr const& _key)
106{
107     return _key.get_len();
108}
109
110inline void Table::bind (Expr const& _key, Expr const& _val)
111{
112  size++;
113  TableNode* new_node;
114  if ((new_node = search_node(_key)) == TableNode::nil) {
115    new_node = new TableNode;
116    new_node->key = _key;
117    new_node->hash_key = expr_hash(_key);
118    new_node->left = TableNode::nil;
119    new_node->right = TableNode::nil;
120    new_node->p = TableNode::nil;
121    new_node->val = _val;
122    insert_node_RB(new_node);
123  }
124  else
125    new_node->val = _val;
126}
127
128
129inline void Table::unbind (Expr& _key)
130{
131  size--;
132  TableNode* new_node;
133  if ((new_node = search_node(_key)) != TableNode::nil) {
134    new_node = delete_node_RB(new_node);
135    delete(new_node);
136  }
137}
138
139
140inline bool Table::lookup (Expr const& _key, Expr& _val) const
141{   
142  TableNode* new_node;
143  if ((new_node = search_node(_key)) == TableNode::nil)
144    return false;   
145  else {
146    _val = new_node->val;
147    return true;
148  }
149}
150
151
152inline bool Table::in_table (Expr const& _key) const
153{
154  TableNode* new_node;
155  if ((new_node = search_node(_key)) == TableNode::nil)
156    return false;
157  else
158    return true;
159}
160 
161 
162//  ReplaceTable: this - TargetTable
163inline void Table::replace_table (Table const& _source_table)   
164{
165  size = _source_table.size;
166  TableNode* x;
167  if (root != TableNode::nil) {
168    delete_tree(root);
169    root = TableNode::nil;   
170  }
171  if (_source_table.root != TableNode::nil) {   
172    x = new TableNode;
173    x->p = TableNode::nil;
174    root = x;     
175    copy_tree(*_source_table.root, *root);
176  }
177}
178
179inline void Table::clear ()
180{
181  size = 0;
182  if (root != TableNode::nil) {
183    delete_tree(root);
184    root = TableNode::nil;   
185  }
186}
187
188inline pxx::WString Table::to_string () const
189{
190  size_t max_len = hex_ptr_len + 15;
191  wchar_t* str = static_cast<wchar_t*>(alloca(max_len * sizeof(wchar_t)));
192  int len = swprintf(str, max_len, L"<Table|%p>", this);
193  if (-1 == len)
194    FATAL("<Table|%p> is more then %u wide characters", this, max_len);
195  return pxx::WString(str, len);
196}
197 
198}
199
200#endif     // __rf_table_ih__
Note: See TracBrowser for help on using the repository browser.