~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/sd/bv/ptrset.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "ptrset.h"
 
2
#include "ktransition.h"
 
3
#include "klocation.h"
 
4
#include "lstring.h"
 
5
 
 
6
#include <stdio.h>
 
7
 
 
8
template class PtrSet<KTransition>;
 
9
template class PtrSet<KLocation>;
 
10
 
 
11
template class PtrSetEl<KTransition>;
 
12
template class PtrSetEl<KLocation>;
 
13
 
 
14
template <class T> PtrSetEl<T>::PtrSetEl(T *newitem, const PtrSetEl<T> *f /* = NULL */)
 
15
{
 
16
        item = newitem;
 
17
        child[0] = child[1] = NULL;
 
18
        father = f;
 
19
}
 
20
 
 
21
template <class T> PtrSetEl<T>::~PtrSetEl() {
 
22
        if ( NULL != child[0] )
 
23
                delete child[0];
 
24
        if ( NULL != child[1] )
 
25
                delete child[1];
 
26
        if ( NULL != item )
 
27
                delete item;
 
28
}
 
29
 
 
30
template <class T> PtrSetEl<T>::PtrSetEl(const PtrSetEl<T> &copy, const PtrSetEl<T> *f /* = NULL */)
 
31
{
 
32
        item = new T(*copy.item);
 
33
        if ( NULL == copy.child[0] )
 
34
                child[0] = NULL;
 
35
        else
 
36
                child[0] = new PtrSetEl<T>(*copy.child[0], this);
 
37
        if ( NULL == copy.child[1] )
 
38
                child[1] = NULL;
 
39
        else
 
40
                child[1] = new PtrSetEl<T>(*copy.child[1], this);
 
41
        father = f;
 
42
}
 
43
 
 
44
template <class T> T *PtrSetEl<T>::search(T *key, mode notfound) {
 
45
        if ( *key == *item )
 
46
                return item;
 
47
        int i = *item < *key;
 
48
        T *result = NULL;
 
49
        if ( NULL != child[i] )
 
50
                result = child[i]->search(key, notfound);
 
51
        else if ( SInsert == notfound )
 
52
                child[i] = new PtrSetEl<T>(key, this);
 
53
        return NULL != result || i != notfound ? result : item;
 
54
}
 
55
 
 
56
template <class T> void PtrSetEl<T>::print(int depth) {
 
57
        if ( NULL != child[0] )
 
58
                child[0]->print(depth + 1);
 
59
        printf("%*c%s:\n", depth, ':', ((string *)item)->getstr());
 
60
        if ( NULL != child[1] )
 
61
                child[1]->print(depth + 1);
 
62
}
 
63
 
 
64
 
 
65
/*--------------------------------------------------------------------------*/
 
66
 
 
67
 
 
68
template <class T> PtrSet<T>::PtrSet()
 
69
:root(NULL) {
 
70
}
 
71
 
 
72
template <class T> PtrSet<T>::~PtrSet() {
 
73
        if ( NULL != root )
 
74
                delete root;
 
75
}
 
76
 
 
77
template <class T> PtrSet<T>::PtrSet(const PtrSet<T> &copy)
 
78
{
 
79
        if ( NULL == copy.root )
 
80
                root = (PtrSetEl<T> *) NULL;
 
81
        else
 
82
                root = new PtrSetEl<T>(*copy.root);
 
83
}
 
84
 
 
85
template <class T> T *PtrSet<T>::search(T *key, mode notfound) {
 
86
        if ( NULL != root )
 
87
                return root->search(key, notfound);
 
88
        if ( SInsert == notfound )
 
89
                root = new PtrSetEl<T>(key);
 
90
        return NULL;
 
91
}
 
92
 
 
93
template <class T> bool PtrSet<T>::first(mode dir /* = SNext */) {
 
94
        current = root;
 
95
        if ( NULL == current )
 
96
                return False;
 
97
        while ( NULL != current->child[dir] )
 
98
                current = current->child[dir];
 
99
        return True;
 
100
}
 
101
 
 
102
template <class T> bool PtrSet<T>::next(mode dir /* = SNext */) {
 
103
        if ( NULL == current )
 
104
                return False;
 
105
        register const PtrSetEl<T> *temp = current->child[1 - dir];
 
106
        if ( NULL != temp ) {
 
107
                do
 
108
                        current = temp;
 
109
                while ( NULL != (temp = temp->child[dir]) );
 
110
                return True;
 
111
        }
 
112
        do {
 
113
                temp = current;
 
114
                if ( NULL == (current = temp->father) )
 
115
                        return False;
 
116
        } while (current->child[dir] != temp);
 
117
        return True;
 
118
}
 
119
 
 
120
template <class T> void PtrSet<T>::print() {
 
121
        if ( NULL != root )
 
122
                root->print(0);
 
123
}