source: to-imperative/trunk/runtime/rf_table.ih @ 1498

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