#ifndef __rf_table_ih__ #define __rf_table_ih__ #include #include "rf_table.hh" #include "rf_object.ih" #include "rf_expr.ih" #include "rf_sysdefs.hh" namespace rftype { inline Table::Table() : Object() { if (!count) { TableNode::nil = new TableNode; TableNode::nil->left = TableNode::nil; TableNode::nil->right = TableNode::nil; TableNode::nil->p = TableNode::nil; TableNode::nil->color = TableNode::BLACK; } root = TableNode::nil; size = 0; count ++; } inline Table::Table(Table const& _tab) : Object() { size = _tab.size; count++; if (_tab.root == TableNode::nil) root = TableNode::nil; else { TableNode* node = new TableNode; root = node; node->p = TableNode::nil; copy_tree(*(_tab.root), *node); } } inline unsigned Table::get_type () const { return reg.get_type(); } inline uint32_t Table::hash () const { return 0; } inline bool Table::operator== (Object const& _table) const { try { Table const& t = static_cast (_table); return table_equal(*root, *t.root); } catch (std::bad_cast&) { return false; } } inline Table::~Table () { if (root != TableNode::nil) delete_tree(root); if (!--count) delete(TableNode::nil); } inline Expr Table::domain () const { Expr res = Expr(); if (root != TableNode::nil) list_node(*root, res); return res; } inline Expr Table::values () const { Expr res = Expr(); if (root != TableNode::nil) list_values(*root, res); return res; } inline Expr Table::entries () const { Expr res = Expr(); if (root != TableNode::nil) list_entries(*root, res); return res; } inline size_t Table::get_size () const { return size; } inline void Table::bind (Expr const& _key, Expr const& _val) { size++; TableNode* new_node; if ((new_node = search_node(_key)) == TableNode::nil) { new_node = new TableNode; new_node->key = _key; new_node->left = TableNode::nil; new_node->right = TableNode::nil; new_node->p = TableNode::nil; new_node->val = _val; insert_node_RB(new_node); } else new_node->val = _val; } inline void Table::unbind (Expr& _key) { size--; TableNode* new_node; if ((new_node = search_node(_key)) != TableNode::nil) { new_node = delete_node_RB(new_node); delete(new_node); } } inline bool Table::lookup (Expr const& _key, Expr& _val) const { TableNode* new_node; if ((new_node = search_node(_key)) == TableNode::nil) return false; else { _val = new_node->val; return true; } } inline bool Table::in_table (Expr const& _key) const { TableNode* new_node; if ((new_node = search_node(_key)) == TableNode::nil) return false; else return true; } // ReplaceTable: this - TargetTable inline void Table::replace_table (Table const& _source_table) { size = _source_table.size; TableNode* x; if (root != TableNode::nil) { delete_tree(root); root = TableNode::nil; } if (_source_table.root != TableNode::nil) { x = new TableNode; x->p = TableNode::nil; root = x; copy_tree(*_source_table.root, *root); } } inline void Table::clear () { size = 0; if (root != TableNode::nil) { delete_tree(root); root = TableNode::nil; } } inline pxx::WString Table::to_string () const { size_t max_len = hex_ptr_len + 15; wchar_t* str = static_cast(alloca(max_len * sizeof(wchar_t))); int len = swprintf(str, max_len, L"", this); if (-1 == len) FATAL(" is more then %u wide characters", this, max_len); return pxx::WString(str, len); } } #endif // __rf_table_ih__