~registry/texmacs/trunk

« back to all changes in this revision

Viewing changes to src/src/Kernel/Containers/hashtree.cpp

  • Committer: mgubi
  • Date: 2009-06-04 15:13:41 UTC
  • Revision ID: svn-v4:64cb5145-927a-446d-8aed-2fb7b4773692:trunk:2717
Support for X11 TeXmacs.app on Mac

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_C
 
15
#define HASHTREE_C
 
16
#include "hashtree.hpp"
 
17
 
 
18
/******************************************************************************
 
19
* Methods normally provided by
 
20
* CONCRETE_TEMPLATE_2_CODE(hashtree,class,K,class,V);
 
21
******************************************************************************/
 
22
 
 
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);
 
26
}
 
27
 
 
28
template<class K,class V> inline 
 
29
hashtree<K,V>::~hashtree() {
 
30
  if (this->rep!=NULL) DEC_COUNT (this->rep);
 
31
}
 
32
 
 
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); 
 
36
  this->rep = x.rep;
 
37
  if (x.rep!=NULL) INC_COUNT (x.rep);
 
38
  return *this;
 
39
}
 
40
 
 
41
/******************************************************************************
 
42
* Methods of hashtree_rep<K,V>
 
43
******************************************************************************/
 
44
 
 
45
template<class K, class V> inline bool
 
46
hashtree_rep<K,V>::contains (K key) {
 
47
  return children->contains(key);
 
48
}
 
49
 
 
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;
 
54
}
 
55
 
 
56
template<class K, class V> void 
 
57
hashtree_rep<K,V>::add_new_child (K key) {
 
58
  hashtree<K,V> child;
 
59
  add_child (key, child);
 
60
}
 
61
 
 
62
template<class K, class V> void 
 
63
hashtree_rep<K,V>::set_label (V val) {
 
64
  label = val;
 
65
}
 
66
 
 
67
template<class K, class V> V 
 
68
hashtree_rep<K,V>::get_label () {
 
69
  return label;
 
70
}
 
71
 
 
72
/******************************************************************************
 
73
* Method to ensure that hashtree is non null
 
74
******************************************************************************/
 
75
 
 
76
template<class K, class V> inline void
 
77
hashtree<K,V>::realize() {
 
78
  if (rep == NULL) {
 
79
    rep = tm_new<hashtree_rep<K,V> > ();
 
80
    INC_COUNT(rep);
 
81
  }
 
82
}
 
83
  
 
84
/******************************************************************************
 
85
* Overloaded operators
 
86
******************************************************************************/
 
87
  
 
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!
 
91
  realize ();
 
92
  return rep;
 
93
}
 
94
 
 
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");
 
99
}
 
100
  
 
101
template<class K, class V> inline hashtree<K,V> 
 
102
hashtree<K,V>::operator() (K key) {
 
103
  realize ();
 
104
  if (!(*this)->contains (key)) (*this)->add_new_child (key);
 
105
  return (*this)->children (key);
 
106
}
 
107
 
 
108
/******************************************************************************
 
109
* Functions
 
110
******************************************************************************/
 
111
 
 
112
template<class K, class V> inline bool
 
113
is_nil (hashtree<K,V> ht) {
 
114
  return ht.rep == NULL;
 
115
}
 
116
 
 
117
template<class K, class V> inline int
 
118
N (hashtree<K,V> ht) {
 
119
  return N(ht->children);
 
120
}
 
121
 
 
122
#endif // HASHTREE_C