@@ -153,15 +153,14 @@ func checkTreapNode(t *treapNode) {
153
153
}
154
154
}
155
155
156
- // treapIter is a unidirectional iterator type which may be used to iterate over a
156
+ // treapIter is a bidirectional iterator type which may be used to iterate over a
157
157
// an mTreap in-order forwards (increasing order) or backwards (decreasing order).
158
158
// Its purpose is to hide details about the treap from users when trying to iterate
159
159
// over it.
160
160
//
161
- // To create iterators over the treap, call iter or rev on an mTreap.
161
+ // To create iterators over the treap, call start or end on an mTreap.
162
162
type treapIter struct {
163
- t * treapNode
164
- inc bool // if true, iterate in increasing order, otherwise decreasing order.
163
+ t * treapNode
165
164
}
166
165
167
166
// span returns the span at the current position in the treap.
@@ -179,42 +178,41 @@ func (i *treapIter) valid() bool {
179
178
// next moves the iterator forward by one. Once the iterator
180
179
// ceases to be valid, calling next will panic.
181
180
func (i treapIter ) next () treapIter {
182
- if i .inc {
183
- i .t = i .t .succ ()
184
- } else {
185
- i .t = i .t .pred ()
186
- }
181
+ i .t = i .t .succ ()
187
182
return i
188
183
}
189
184
190
- // iter returns an iterator which may be used to iterate over the treap
191
- // in increasing order of span size ("forwards").
192
- func (root * mTreap ) iter () treapIter {
193
- i := treapIter {inc : true }
185
+ // prev moves the iterator backwards by one. Once the iterator
186
+ // ceases to be valid, calling prev will panic.
187
+ func (i treapIter ) prev () treapIter {
188
+ i .t = i .t .pred ()
189
+ return i
190
+ }
191
+
192
+ // start returns an iterator which points to the start of the treap (the
193
+ // left-most node in the treap).
194
+ func (root * mTreap ) start () treapIter {
194
195
t := root .treap
195
196
if t == nil {
196
- return i
197
+ return treapIter {}
197
198
}
198
199
for t .left != nil {
199
200
t = t .left
200
201
}
201
- i .t = t
202
- return i
202
+ return treapIter {t : t }
203
203
}
204
204
205
- // rev returns an iterator which may be used to iterate over the treap
206
- // in decreasing order of span size ("reverse").
207
- func (root * mTreap ) rev () treapIter {
208
- i := treapIter {inc : false }
205
+ // end returns an iterator which points to the end of the treap (the
206
+ // right-most node in the treap).
207
+ func (root * mTreap ) end () treapIter {
209
208
t := root .treap
210
209
if t == nil {
211
- return i
210
+ return treapIter {}
212
211
}
213
212
for t .right != nil {
214
213
t = t .right
215
214
}
216
- i .t = t
217
- return i
215
+ return treapIter {t : t }
218
216
}
219
217
220
218
// insert adds span to the large span treap.
@@ -342,13 +340,11 @@ func (root *mTreap) removeSpan(span *mspan) {
342
340
}
343
341
344
342
// erase removes the element referred to by the current position of the
345
- // iterator and returns i.next(). This operation consumes the given
346
- // iterator, so it should no longer be used and iteration should continue
347
- // from the returned iterator.
348
- func (root * mTreap ) erase (i treapIter ) treapIter {
349
- n := i .next ()
343
+ // iterator. This operation consumes the given iterator, so it should no
344
+ // longer be used. It is up to the caller to get the next or previous
345
+ // iterator before calling erase, if need be.
346
+ func (root * mTreap ) erase (i treapIter ) {
350
347
root .removeNode (i .t )
351
- return n
352
348
}
353
349
354
350
// rotateLeft rotates the tree rooted at node x.
0 commit comments