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

Last change on this file since 2040 was 2040, checked in by orlov, 15 years ago
  • New RTS option -break -- for breaking in functions compiled with -dbg.
  • Table support for Values and Entries new library functions.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 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 () const
74
75  Expr res = Expr();
76  if (root != TableNode::nil)
77    list_node(*root, res);   
78  return res;
79}
80
81inline Expr Table::values () const
82
83  Expr res = Expr();
84  if (root != TableNode::nil)
85    list_values(*root, res);   
86  return res;
87}
88
89inline Expr Table::entries () const
90
91  Expr res = Expr();
92  if (root != TableNode::nil)
93    list_entries(*root, res);   
94  return res;
95}
96
97//  zaglushka  - hash-function
98inline int expr_hash (Expr const& _key)
99{
100     return _key.get_len();
101}
102
103inline void Table::bind (Expr const& _key, Expr const& _val)
104{
105  TableNode* new_node;
106  if ((new_node = search_node(_key)) == TableNode::nil) {
107    new_node = new TableNode;
108    new_node->key = _key;
109    new_node->hash_key = expr_hash(_key);
110    new_node->left = TableNode::nil;
111    new_node->right = TableNode::nil;
112    new_node->p = TableNode::nil;
113    new_node->val = _val;
114    insert_node_RB(new_node);
115  }
116  else
117    new_node->val = _val;
118}
119
120
121inline void Table::unbind (Expr& _key)
122{
123  TableNode* new_node;
124  if ((new_node = search_node(_key)) != TableNode::nil) {
125    new_node = delete_node_RB(new_node);
126    delete(new_node);
127  }
128}
129
130
131inline bool Table::lookup (Expr const& _key, Expr& _val)
132{   
133  TableNode* new_node;
134  if ((new_node = search_node(_key)) == TableNode::nil)
135    return false;   
136  else {
137    _val = new_node->val;
138    return true;
139  }
140}
141
142
143inline bool Table::in_table (Expr const& _key)
144{
145  TableNode* new_node;
146  if ((new_node = search_node(_key)) == TableNode::nil)
147    return false;
148  else
149    return true;
150}
151 
152 
153//  ReplaceTable: this - TargetTable
154inline void Table::replace_table (Table const& _source_table)   
155{   
156  TableNode* x;
157  if (root != TableNode::nil) {
158    delete_tree(root);
159    root = TableNode::nil;   
160  }
161  if (_source_table.root != TableNode::nil) {   
162    x = new TableNode;
163    x->p = TableNode::nil;
164    root = x;     
165    copy_tree(*_source_table.root, *root);
166  }
167}
168
169inline pxx::WString Table::to_string () const
170{
171  size_t max_len = hex_ptr_len + 15;
172  wchar_t* str = static_cast<wchar_t*>(alloca(max_len * sizeof(wchar_t)));
173  int len = swprintf(str, max_len, L"<Table|%p>", this);
174  if (-1 == len)
175    FATAL("<Table|%p> is more then %u wide characters", this, max_len);
176  return pxx::WString(str, len);
177}
178 
179}
180
181#endif     // __rf_table_ih__
Note: See TracBrowser for help on using the repository browser.