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

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