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

Last change on this file since 1059 was 1059, checked in by sveta, 18 years ago
  • Function names are changed.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 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  static bool flag = false;
18  if (!flag) {
19    flag = true;
20    TableNode::nil = new TableNode;
21    TableNode::nil->color = TableNode::BLACK;
22  }
23  root = TableNode::nil;
24}
25
26inline unsigned Table::get_type () const
27{
28  return reg.get_type();
29}
30
31inline uint32_t Table::hash () const
32{
33  return 0;
34}
35
36inline bool Table::operator== (Object const& _table) const
37{
38  try {
39    Table const&  t = static_cast <Table const&>(_table); 
40    return table_equal(*root, *t.root);
41 }
42  catch (std::bad_cast&)
43  {
44    return false;
45  }
46}
47
48//
49// Destructor: deleting of this-RBtree
50inline Table::~Table ()
51
52   if (root != TableNode::nil)
53     delete_tree(root);
54}
55
56inline Expr Table::domain ()
57
58  Expr res = Expr();
59  if (root != TableNode::nil)
60    list_node(*root, res);   
61  return res;
62}
63
64//  zaglushka  - hash-function
65inline int expr_hash (Expr const& _key)
66{
67  return _key.get_len();
68}
69
70inline void Table::bind (Expr const& _key, Expr const& _val)
71{
72  TableNode* new_node;
73  if ((new_node = search_node(_key)) == TableNode::nil) {
74    new_node = new TableNode;
75    new_node->key = _key;
76    new_node->hash_key = expr_hash(_key);
77    new_node->left = TableNode::nil;
78    new_node->right = TableNode::nil;
79    new_node->p = TableNode::nil;
80    new_node->val = _val;
81    insert_node_RB(new_node);
82  }
83  else
84    new_node->val = _val;
85}
86
87inline void Table::unbind (Expr& _key)
88{
89  TableNode* new_node;
90  if ((new_node = search_node(_key)) != TableNode::nil) {
91    new_node = delete_node_RB(new_node);
92    delete(new_node);
93  }
94}
95
96
97inline bool Table::lookup (Expr const& _key, Expr& _val)
98{   
99  TableNode* new_node;
100  if ((new_node = search_node(_key)) == TableNode::nil)
101    return false;   
102  else {
103    _val = new_node->val;
104    return true;
105  }
106}
107
108
109inline bool Table::in_table (Expr const& _key)
110{
111  TableNode* new_node;
112  if ((new_node = search_node(_key)) == TableNode::nil)
113    return false;
114  else
115    return true;
116}
117
118 
119//
120// Table_copy : this - sourceTable, newTable - result
121inline void Table::table_copy (Table& _new_table)
122
123  TableNode* node;
124  if (root != TableNode::nil) {
125    node = new TableNode;
126    node->p = TableNode::nil;       
127    _new_table.root = node;   // p - root 
128    copy_tree(*root, *node);  // p - root
129  }
130}
131
132 
133//
134//  ReplaceTable: this - TargetTable
135inline void Table::replace_table (Table const& _source_table)   
136{   
137  TableNode* x;
138  if (root != TableNode::nil) {
139    delete_tree(root);
140    root = TableNode::nil;   
141  }
142  if (_source_table.root != TableNode::nil) {   
143    x = new TableNode;
144    x->p = TableNode::nil;
145    root = x;     
146    copy_tree(*_source_table.root, *root);
147  }
148}
149
150 
151}
152
153#endif     // __rf_table_ih__
Note: See TracBrowser for help on using the repository browser.