]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/cursor.tcc
42b0c52f3c595951e0c4a1b2e711e4613830208d
[lilypond.git] / flower / include / cursor.tcc
1 #ifndef CURSOR_CC
2 #define CURSOR_CC
3
4 #include "cursor.hh"
5 #include <assert.h>
6
7 template<class T>
8 void
9 Cursor<T>::backspace()
10 {
11   Cursor<T> c (*this);
12   if (c.ok())
13     c--;        
14   list_l_->remove (*this);
15 }
16
17 template<class T>
18 void
19 Cursor<T>::del()
20 {
21   Cursor<T> c (*this);
22   if (c.ok())
23     c++;
24   list_l_->remove (*this);    
25   *this = c;
26 }
27
28
29 template<class T>
30 Cursor<T> 
31 Cursor<T>::operator -=(int j)    
32 {
33   while (j--)
34     (*this)--;
35   return *this;
36 }
37 template<class T>
38 Cursor<T> 
39 Cursor<T>::operator +=(int j)    
40 {
41   while (j++)
42     (*this)++;
43   return *this;
44 }
45
46 template<class T>
47 Cursor<T> 
48 Cursor<T>::operator +(int i) const    
49 {
50   Cursor<T> r = *this;
51
52   if (i<0)
53     return r -(-i);
54
55   while (i--)
56     r++;
57
58   return r;
59 }
60
61 template<class T>
62 Cursor<T>
63 Cursor<T>::operator -(int i) const
64 {
65   Cursor<T> r = *this;
66   if (i<0)
67     return r +(-i);
68
69   while (i--)
70     r--;
71   
72   return r;
73 }
74 /*
75   warning:  can't use Cursor::operator == (Cursor),
76   since it uses Cursor::operator-(Cursor)
77  */
78 template<class T>
79 int
80 Cursor<T>::operator-(Cursor<T> rhs) const
81 {
82   assert (rhs.list_l_ == list_l_);
83   int dif = 0;
84
85   // search from *this on further up (positive difference)
86   Cursor<T> c (*this);
87   while (c.ok() && c.pointer_ != rhs.pointer_) 
88     {
89       c--;
90       dif++;
91     }
92   
93   if (c.ok())
94     goto gotcha;                // so, sue me.
95
96   // search in direction of bottom. (negative diff)
97   dif =0;
98   c=*this;    
99   while (c.ok() && c.pointer_ !=rhs.pointer_) 
100     {
101       dif --;
102       c++;
103     }
104   assert (c.ok());
105
106  gotcha:
107   assert ((*this - dif).pointer_ == c.pointer_);
108   return dif;
109 }
110
111 #endif