Skip to content

Commit c8f7dc6

Browse files
committed
add for hashtree
1 parent bd8a506 commit c8f7dc6

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

Kernel/Containers/hashtree.hpp

+9-21
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
#define HASHTREE_H
1616

1717
#include "hashmap.hpp"
18+
#include "sharedptr.hpp"
1819

1920
template <class K, class V> class hashtree;
2021
template <class K, class V> int N (hashtree<K, V> tree);
2122
template <class K, class V> bool is_nil (hashtree<K, V> tree);
2223

23-
template <class K, class V> class hashtree_rep : concrete_struct {
24+
template <class K, class V> class hashtree_rep {
2425
hashmap<K, hashtree<K, V>> children;
2526

2627
public:
@@ -75,38 +76,26 @@ template <class K, class V> class hashtree_rep : concrete_struct {
7576
* of NULL pointers so to speak). These NULL nodes are created by passing
7677
* a boolean value to the hashtree constructor. One cannot accidentally
7778
* 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.
8579
******************************************************************************/
8680

87-
template <class K, class V> class hashtree {
88-
// CONCRETE_TEMPLATE_2(hashtree,K,V);
89-
hashtree_rep<K, V>* rep;
81+
template <class K, class V>
82+
class hashtree : public counted_ptr<hashtree_rep<K, V>, true> {
9083

9184
// this constructor always returns a NULL element
92-
inline hashtree (bool) : rep (NULL) {}
85+
inline hashtree (bool) : base () {}
9386

9487
// ensures that this hashtree has a rep
9588
void realize ();
9689

9790
public:
98-
inline hashtree (const hashtree<K, V>&);
99-
inline ~hashtree ();
100-
inline hashtree<K, V>& operator= (hashtree<K, V> x);
101-
10291
// default constructor returns a non-NULL node, which does not have a value
103-
inline hashtree () : rep (tm_new<hashtree_rep<K, V>> ()) {}
92+
inline hashtree () : base (make ()) {}
10493

10594
// returns a non-NULL node, that has value
106-
inline hashtree (V val) : rep (tm_new<hashtree_rep<K, V>> (val)) {}
95+
inline hashtree (V val) : base (make (val)) {}
10796

10897
// returns this node's value
109-
inline hashtree_rep<K, V>* operator->(void);
98+
inline hashtree_rep<K, V>* operator->();
11099

111100
// returns this node's child with the label "key". If the node doesn't
112101
// have such a child, an error is raised.
@@ -120,8 +109,7 @@ template <class K, class V> class hashtree {
120109
inline hashtree<K, V> operator[] (K key); // rw access
121110

122111
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);
112+
friend int N<K, V> (hashtree<K, V> ht);
125113
};
126114

127115
#include "hashtree.ipp"

Kernel/Containers/hashtree.ipp

+3-31
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,6 @@
1515
#define HASHTREE_C
1616
#include "hashtree.hpp"
1717

18-
/******************************************************************************
19-
* Methods normally provided by
20-
* CONCRETE_TEMPLATE_2_CODE(hashtree,class,K,class,V);
21-
******************************************************************************/
22-
23-
template <class K, class V>
24-
inline 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 hashtree<K, V>::~hashtree () {
29-
if (this->rep != NULL) DEC_COUNT (this->rep);
30-
}
31-
32-
template <class K, class V>
33-
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-
4518
template <class K, class V>
4619
inline bool
4720
hashtree_rep<K, V>::contains (K key) {
@@ -81,9 +54,8 @@ hashtree_rep<K, V>::get_label () {
8154
template <class K, class V>
8255
inline void
8356
hashtree<K, V>::realize () {
84-
if (rep == NULL) {
85-
rep= tm_new<hashtree_rep<K, V>> ();
86-
INC_COUNT (rep);
57+
if (this->is_nil ()) {
58+
*this= hashtree ();
8759
}
8860
}
8961

@@ -121,7 +93,7 @@ hashtree<K, V>::operator() (K key) {
12193
template <class K, class V>
12294
inline bool
12395
is_nil (hashtree<K, V> ht) {
124-
return ht.rep == NULL;
96+
return ht.is_nil ();
12597
}
12698

12799
template <class K, class V>

0 commit comments

Comments
 (0)