-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhashtree.ipp
105 lines (88 loc) · 2.82 KB
/
hashtree.ipp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************************
* MODULE : hashtree
* DESCRIPTION: A tree class that stores a node's children in a hashmap instead
* of a list. Can be used to implement a dictionary that maps
* strings to strings efficiently.
* COPYRIGHT : (C) 2002 Felix Breuer
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
#ifndef HASHTREE_C
#define HASHTREE_C
#include "hashtree.hpp"
template <class K, class V>
inline bool
hashtree_rep<K, V>::contains (K key) {
return children->contains (key);
}
template <class K, class V>
void
hashtree_rep<K, V>::add_child (K key, hashtree<K, V>& child) {
child.realize (); // make sure the child has a rep!
children (key)= child;
}
template <class K, class V>
void
hashtree_rep<K, V>::add_new_child (K key) {
hashtree<K, V> child;
add_child (key, child);
}
template <class K, class V>
void
hashtree_rep<K, V>::set_label (V val) {
label= val;
}
template <class K, class V>
V
hashtree_rep<K, V>::get_label () {
return label;
}
/******************************************************************************
* Method to ensure that hashtree is non null
******************************************************************************/
template <class K, class V>
inline void
hashtree<K, V>::realize () {
if (this->is_nil ()) {
*this= hashtree ();
}
}
/******************************************************************************
* Overloaded operators
******************************************************************************/
template <class K, class V>
inline hashtree_rep<K, V>*
hashtree<K, V>::operator->(void) {
// always make sure there is a rep!
realize ();
return this->rep;
}
template <class K, class V>
inline hashtree<K, V>
hashtree<K, V>::operator[] (K key) {
if (*this->contains (key)) return *this->children (key);
else TM_FAILED ("read-access to non-existent node requested");
}
template <class K, class V>
inline hashtree<K, V>
hashtree<K, V>::operator() (K key) {
realize ();
if (!(*this)->contains (key)) (*this)->add_new_child (key);
return (*this)->children (key);
}
/******************************************************************************
* Functions
******************************************************************************/
template <class K, class V>
inline bool
is_nil (hashtree<K, V> ht) {
return ht.is_nil ();
}
template <class K, class V>
inline int
N (hashtree<K, V> ht) {
return N (ht->children);
}
#endif // HASHTREE_C