summaryrefslogtreecommitdiff
path: root/flower/cursor.cc
blob: 2a9f885689e57bdcfb3601bfd61603ba44abfc60 (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
#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;
}

template<class T>
int
Cursor<T>::operator-(Cursor<T> c) const
{
    assert(c.list == list);
    int dif = 0;
    Cursor<T> upward(c);
    while (upward.ok() && upward.pointer_ != pointer_) {
	upward++;
	dif++;
    }
    
    if (upward.ok())
	return dif;
    dif =0;
    while (c.ok()&& c.pointer_ != pointer_) {
	dif --;
	c--;
    }
    assert(c.ok());
    return dif;
}

#endif