Lolly 1.4.27
Loading...
Searching...
No Matches
hashtree.hpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : hashtree
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 ******************************************************************************/
13
14#ifndef HASHTREE_H
15#define HASHTREE_H
16
17#include "hashmap.hpp"
18
19template <class K, class V> class hashtree;
20template <class K, class V> int N (hashtree<K, V> tree);
21template <class K, class V> bool is_nil (hashtree<K, V> tree);
22
23template <class K, class V> class hashtree_rep : concrete_struct {
25
26public:
28
29 inline hashtree_rep () : children (hashtree<K, V> (false)), label () {}
30 inline hashtree_rep (V val) : children (hashtree<K, V> ()), label (val) {}
31
32 inline bool contains (K key);
33 void add_child (K key, hashtree<K, V>& child);
34 void add_new_child (K key);
35 void set_label (V val);
37
38 friend class hashtree<K, V>;
39 friend int N<K, V> (hashtree<K, V> tree);
40};
41
42/******************************************************************************
43 * The class hashtree is a tree-class with the special property that
44 * a node stores its children not in a list or in an array but in a hashmap
45 * so as to make child-lookup efficient (for broad trees). Each edge of
46 * the tree has a label and these labels are used as keys for the hashmap.
47 * The nodes of the may have a value associated with them.
48 *
49 * This data structure is suitable for storing dictionaries which map
50 * strings of keys to some value in the following fashion:
51 * Consider this englisch->german dictionary:
52 *
53 * he -> er
54 * head -> Kopf
55 * heaven -> Himmel
56 * hell -> H?lle
57 * hello -> hallo
58 *
59 * which is represented by the following tree.
60 *
61 * []--h--[]--e--[er]--a--[]--d--[Kopf]
62 * | |
63 * | v
64 * l |
65 * | +--e--[]--n--[Himmel]
66 * |
67 * +--l--[H?lle]--o--[hallo]
68 *
69 * However, using hashmaps to store the children of a node is problematic
70 * in TeXmacs. A Hashmap contains a *copy* of a default value.
71 * If now a hashtree (node) contains a hasmap which in turn
72 * contains *at least one* hashtree, the instantiation of a single
73 * hashtree leads to an infinite recursion. I chose to solve this problem
74 * by allowing hashtree which do not have a hashtree_rep (the equivalent
75 * of NULL pointers so to speak). These NULL nodes are created by passing
76 * a boolean value to the hashtree constructor. One cannot accidentally
77 * obtain a NULL element by e.g. accessing a child (see below).
78 *
79 * In general, I tried to imitate the TeXmacs-way of memory management
80 * as closely as possibly, however the workaround is not that pretty.
81 * As more elegant way might be to modify the hashmap class so that
82 * a hashmap contains only a pointer to a function that returns
83 * a default value instead of a instance of a value-element.
84 * But I didn't want to modify core TeXmacs code.
85 ******************************************************************************/
86
87template <class K, class V> class hashtree {
88 // CONCRETE_TEMPLATE_2(hashtree,K,V);
90
91 // this constructor always returns a NULL element
92 inline hashtree (bool) : rep (NULL) {}
93
94 // ensures that this hashtree has a rep
95 void realize ();
96
97public:
98 inline hashtree (const hashtree<K, V>&);
99 inline ~hashtree ();
101
102 // default constructor returns a non-NULL node, which does not have a value
103 inline hashtree () : rep (tm_new<hashtree_rep<K, V>> ()) {}
104
105 // returns a non-NULL node, that has value
106 inline hashtree (V val) : rep (tm_new<hashtree_rep<K, V>> (val)) {}
107
108 // returns this node's value
109 inline hashtree_rep<K, V>* operator->(void);
110
111 // returns this node's child with the label "key". If the node doesn't
112 // have such a child, an error is raised.
113 inline hashtree<K, V> operator() (K key); // read only access
114
115 // returns this node's child with the label "key". If the node doesn't
116 // have such a child, a non-NULL node is created, added to this node's
117 // children with the appropriate label and returned.
118 // Thus, this method may change the in-memory representation of a tree
119 // but it does not change the dictionary the tree represents.
120 inline hashtree<K, V> operator[] (K key); // rw access
121
122 friend class hashtree_rep<K, V>;
123 friend bool is_nil<K, V> (hashtree<K, V> ht);
124 friend int N<K, V> (hashtree<K, V> ht);
125};
126
127#include "hashtree.ipp"
128
129#endif // HASHTREE_H
bool contains(K key)
Definition hashtree.ipp:47
hashmap< K, hashtree< K, V > > children
Definition hashtree.hpp:24
void add_child(K key, hashtree< K, V > &child)
Definition hashtree.ipp:53
hashtree_rep(V val)
Definition hashtree.hpp:30
void set_label(V val)
Definition hashtree.ipp:67
void add_new_child(K key)
Definition hashtree.ipp:60
void realize()
Definition hashtree.ipp:83
hashtree< K, V > & operator=(hashtree< K, V > x)
Definition hashtree.ipp:34
hashtree(V val)
Definition hashtree.hpp:106
hashtree_rep< K, V > * operator->(void)
Definition hashtree.ipp:96
hashtree< K, V > operator[](K key)
Definition hashtree.ipp:104
hashtree< K, V > operator()(K key)
Definition hashtree.ipp:111
hashtree_rep< K, V > * rep
Definition hashtree.hpp:89
hashtree(bool)
Definition hashtree.hpp:92
The list class represents a linked list.
Definition list.hpp:48
C * tm_new()
bool is_nil(hashtree< K, V > tree)
Definition hashtree.ipp:123
int N(hashtree< K, V > tree)
Definition hashtree.ipp:129
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23