-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSL.cpp
More file actions
212 lines (188 loc) · 6.3 KB
/
DSL.cpp
File metadata and controls
212 lines (188 loc) · 6.3 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "DSL.h"
/**
* Construct the tree.
* inf is the largest Comparable
* and is used to signal failed finds.
*/
template <class Comparable>
DSL<Comparable>::DSL( const Comparable & inf ) : INFINITY( inf )
{
bottom = new SkipNode<Comparable>( );
bottom->right = bottom->down = bottom;
tail = new SkipNode<Comparable>( INFINITY );
tail->right = tail;
header = new SkipNode<Comparable>( INFINITY, tail, bottom );
}
/**
* Copy constructor.
* Left as an exercise.
*/
template <class Comparable>
DSL<Comparable>::DSL( const DSL<Comparable> & rhs ) : INFINITY( rhs.INFINITY)
{
cout << "Copy constructor is unimplemented" << endl;
}
/**
* Destructor.
*/
template <class Comparable>
DSL<Comparable>::~DSL( )
{
makeEmpty( );
delete header;
delete tail;
delete bottom;
}
/**
* Insert item x into the DSL.
*/
template <class Comparable>
void DSL<Comparable>::insert( const Comparable & x )
{
SkipNode<Comparable> *current = header;
bottom->element = x;
while( current != bottom )
{
while( current->element < x )
current = current->right;
// If gap size is 3 or at bottom level and
// must insert, then promote middle element
if( current->down->right->right->element < current->element )
{
current->right = new SkipNode<Comparable>( current->element,
current->right, current->down->right->right );
current->element = current->down->right->element;
}
else
current = current->down;
}
// Raise height of DSL if necessary
if( header->right != tail )
header = new SkipNode<Comparable>( INFINITY, tail, header );
}
/**
* Remove item x from the DSL. Unimplemented.
*/
template <class Comparable>
void DSL<Comparable>::remove( const Comparable & x )
{
cout << "Sorry, remove unimplemented; " << x <<
" still present" << endl;
}
/**
* Find the smallest item in the tree.
* Return smallest item or INFINITY if empty.
*/
template <class Comparable>
const Comparable & DSL<Comparable>::findMin( ) const
{
if( isEmpty( ) )
return INFINITY;
SkipNode<Comparable> *current = header;
while( current->down != bottom )
current = current->down;
return elementAt( current );
}
/**
* Find the largest item in the tree.
* Return the largest item or INFINITY if empty.
*/
template <class Comparable>
const Comparable & DSL<Comparable>::findMax( ) const
{
if( isEmpty( ) )
return INFINITY;
SkipNode<Comparable> *current = header;
for( ; ; )
if( current->right->right != tail )
current = current->right;
else if( current->down != bottom )
current = current->down;
else
return elementAt( current );
}
/**
* Find item x in the tree.
* Return the matching item or INFINITY if not found.
*/
template <class Comparable>
const Comparable & DSL<Comparable>::find( const Comparable & x ) const
{
SkipNode<Comparable> *current = header;
bottom->element = x;
for( ; ; )
if( x < current->element )
current = current->down;
else if( current->element < x )
current = current->right;
else
return elementAt( current );
}
/**
* Make the tree logically empty.
*/
template <class Comparable>
void DSL<Comparable>::makeEmpty( )
{
reclaimMemory( header );
header->right = tail;
header->down = bottom;
}
/**
* Test if the tree is logically empty.
* Return true if empty, false otherwise.
*/
template <class Comparable>
bool DSL<Comparable>::isEmpty( ) const
{
return header->right == tail && header->down == bottom;
}
/**
* Internal method to get element field from node t.
* Return the element field or INFINITY if t is at the bottom.
*/
template <class Comparable>
const Comparable & DSL<Comparable>::
elementAt( SkipNode<Comparable> *t ) const
{
if( t == bottom )
return INFINITY;
else
return t->element;
}
/**
* Print the DSL.
*/
template <class Comparable>
void DSL<Comparable>::printList( ) const
{
SkipNode<Comparable> *current = header;
while( current->down != bottom )
current = current->down;
while( current->right != tail )
{
cout << current->element << endl;
current = current->right;
}
}
/**
* Deep copy. Left as an exercise
*/
template <class Comparable>
const DSL<Comparable> &
DSL<Comparable>::operator=( const DSL<Comparable> & rhs )
{
if( this != &rhs )
cout << "Sorry, operator= is unimplemented" << endl;
return *this;
}
/**
* reclaimMemory is left as an exercise.
* Hint: delete from top level to bottom level.
*/
template <class Comparable>
void DSL<Comparable>::reclaimMemory( SkipNode<Comparable> *t ) const
{
if( t != bottom )
cout << "reclaimMemory is unimplemented -- leaking!" << endl;
}