2
/******************************************************************************
4
* DESCRIPTION: A tree class that stores a node's children in a hashmap instead
5
* of a list. Can be used to implement a dictionary that maps
6
* strings to strings efficiently.
7
* COPYRIGHT : (C) 2002 Felix Breuer
8
*******************************************************************************
9
* This software falls under the GNU general public license version 3 or later.
10
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
11
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
12
******************************************************************************/
16
#include "hashtree.hpp"
18
/******************************************************************************
19
* Methods normally provided by
20
* CONCRETE_TEMPLATE_2_CODE(hashtree,class,K,class,V);
21
******************************************************************************/
23
template<class K,class V> inline
24
hashtree<K,V>::hashtree(const hashtree<K,V>& x): rep(x.rep) {
25
if (this->rep!=NULL) INC_COUNT (this->rep);
28
template<class K,class V> inline
29
hashtree<K,V>::~hashtree() {
30
if (this->rep!=NULL) DEC_COUNT (this->rep);
33
template<class K,class V> inline hashtree<K,V>&
34
hashtree<K,V>::operator= (hashtree<K,V> x) {
35
if (this->rep!=NULL) DEC_COUNT (this->rep);
37
if (x.rep!=NULL) INC_COUNT (x.rep);
41
/******************************************************************************
42
* Methods of hashtree_rep<K,V>
43
******************************************************************************/
45
template<class K, class V> inline bool
46
hashtree_rep<K,V>::contains (K key) {
47
return children->contains(key);
50
template<class K, class V> void
51
hashtree_rep<K,V>::add_child (K key, hashtree<K,V>& child) {
52
child.realize(); // make sure the child has a rep!
53
children (key) = child;
56
template<class K, class V> void
57
hashtree_rep<K,V>::add_new_child (K key) {
59
add_child (key, child);
62
template<class K, class V> void
63
hashtree_rep<K,V>::set_label (V val) {
67
template<class K, class V> V
68
hashtree_rep<K,V>::get_label () {
72
/******************************************************************************
73
* Method to ensure that hashtree is non null
74
******************************************************************************/
76
template<class K, class V> inline void
77
hashtree<K,V>::realize() {
79
rep = tm_new<hashtree_rep<K,V> > ();
84
/******************************************************************************
85
* Overloaded operators
86
******************************************************************************/
88
template<class K, class V> inline hashtree_rep<K,V>*
89
hashtree<K,V>::operator-> (void) {
90
// always make sure there is a rep!
95
template<class K, class V> inline hashtree<K,V>
96
hashtree<K,V>::operator[] (K key) {
97
if (*this->contains (key)) return *this->children (key);
98
else FAILED ("read-access to non-existent node requested");
101
template<class K, class V> inline hashtree<K,V>
102
hashtree<K,V>::operator() (K key) {
104
if (!(*this)->contains (key)) (*this)->add_new_child (key);
105
return (*this)->children (key);
108
/******************************************************************************
110
******************************************************************************/
112
template<class K, class V> inline bool
113
is_nil (hashtree<K,V> ht) {
114
return ht.rep == NULL;
117
template<class K, class V> inline int
118
N (hashtree<K,V> ht) {
119
return N(ht->children);