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

Last change on this file since 4007 was 4007, checked in by orlov, 12 years ago
  • No hash-code for Expr-s in table.
  • Domain, Values and Entries return table contents in sorted order.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 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
104inline void Table::bind (Expr const& _key, Expr const& _val)
105{
106  size++;
107  TableNode* new_node;
108  if ((new_node = search_node(_key)) == TableNode::nil) {
109    new_node = new TableNode;
110    new_node->key = _key;
111    new_node->left = TableNode::nil;
112    new_node->right = TableNode::nil;
113    new_node->p = TableNode::nil;
114    new_node->val = _val;
115    insert_node_RB(new_node);
116  }
117  else
118    new_node->val = _val;
119}
120
121
122inline void Table::unbind (Expr& _key)
123{
124  size--;
125  TableNode* new_node;
126  if ((new_node = search_node(_key)) != TableNode::nil) {
127    new_node = delete_node_RB(new_node);
128    delete(new_node);
129  }
130}
131
132
133inline bool Table::lookup (Expr const& _key, Expr& _val) const
134{   
135  TableNode* new_node;
136  if ((new_node = search_node(_key)) == TableNode::nil)
137    return false;   
138  else {
139    _val = new_node->val;
140    return true;
141  }
142}
143
144
145inline bool Table::in_table (Expr const& _key) const
146{
147  TableNode* new_node;
148  if ((new_node = search_node(_key)) == TableNode::nil)
149    return false;
150  else
151    return true;
152}
153 
154 
155//  ReplaceTable: this - TargetTable
156inline void Table::replace_table (Table const& _source_table)   
157{
158  size = _source_table.size;
159  TableNode* x;
160  if (root != TableNode::nil) {
161    delete_tree(root);
162    root = TableNode::nil;   
163  }
164  if (_source_table.root != TableNode::nil) {   
165    x = new TableNode;
166    x->p = TableNode::nil;
167    root = x;     
168    copy_tree(*_source_table.root, *root);
169  }
170}
171
172inline void Table::clear ()
173{
174  size = 0;
175  if (root != TableNode::nil) {
176    delete_tree(root);
177    root = TableNode::nil;   
178  }
179}
180
181inline pxx::WString Table::to_string () const
182{
183  size_t max_len = hex_ptr_len + 15;
184  wchar_t* str = static_cast<wchar_t*>(alloca(max_len * sizeof(wchar_t)));
185  int len = swprintf(str, max_len, L"<Table|%p>", this);
186  if (-1 == len)
187    FATAL("<Table|%p> is more then %u wide characters", this, max_len);
188  return pxx::WString(str, len);
189}
190 
191}
192
193#endif     // __rf_table_ih__
Note: See TracBrowser for help on using the repository browser.