summaryrefslogtreecommitdiff
path: root/flower/cursor.cc
blob: 75f607dcaaefe86cfe8cbdaf5bc9b47035a38a03 (about) (plain)
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
#ifndef CURSOR_CC
#define CURSOR_CC

#include "cursor.hh"
#include <assert.h>

template<class T>
Cursor<T> 
Cursor<T>::operator ++( int )    
{
    Cursor<T> r = *this;
    assert( pointer_ );
    pointer_ = pointer_->next();
    return r;
}
template<class T>
Cursor<T> 
Cursor<T>::operator -=( int j )    
{
    while (j--)
	(*this)--;
    return *this;
}
template<class T>
Cursor<T> 
Cursor<T>::operator +=( int j )    
{
    while (j++)
	(*this)++;
    return *this;
}

template<class T>
Cursor<T>
Cursor<T>::operator --( int )
{
    Cursor<T> r = *this;
    assert( pointer_ );
    pointer_ = pointer_->previous();
    return r;
}

template<class T>
Cursor<T> 
Cursor<T>::operator +( int i ) const    
{
    Cursor<T> r = *this;

    if (i<0)
	return r -(-i);

    while (i--)
	r++;

    return r;
}

template<class T>
Cursor<T>
Cursor<T>::operator -( int i ) const
{
    Cursor<T> r = *this;
    if (i<0)
	return r +(-i);

    while (i--)
	r--;
    
    return r;
}
/*
  warning:  can't use Cursor::operator == (Cursor),
  since it uses Cursor::operator-(Cursor)
 */
template<class T>
int
Cursor<T>::operator-(Cursor<T> rhs) const
{
    assert(rhs.list == list);
    int dif = 0;

    // search from *this on further up (positive difference)
    Cursor<T> c(*this);
    while (c.ok() && c.pointer_ != rhs.pointer_) {
	c--;
	dif++;
    }
    
    if (c.ok())
	goto gotcha;		// so, sue me.

    // search in direction of bottom. (negative diff)
    dif =0;
    c=*this;    
    while (c.ok() && c.pointer_ !=rhs.pointer_) {
	dif --;
	c++;
    }
    assert(c.ok());

gotcha:
    assert((*this - dif).pointer_ == c.pointer_);
    return dif;
}

#endif