2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
12 #include <QStringList>
15 Index::operator QString() const {
16 QString retv
= QString::number(num_moves
);
18 for(int i
= 0; i
< (int)nested
.size(); i
++)
19 retv
+= QString("_%1.%2").arg( nested
[i
].variation
).arg( nested
[i
].num_moves
);
24 Index
Index::fromString(const QString
& s
) {
25 QStringList l
= s
.split("_");
29 Index
retv(l
[0].toInt());
30 for(int i
=1;i
<l
.size();i
++) {
31 QStringList v
= l
[i
].split(".");
34 retv
.nested
.push_back(Ref(v
[0].toInt(), v
[1].toInt()));
39 int Index::totalNumMoves() const {
42 for(int i
= 0; i
< (int)nested
.size(); i
++)
43 retv
+= nested
[i
].num_moves
+1;
48 bool Index::atVariationStart() const {
49 return nested
.size() && (nested
[nested
.size()-1].num_moves
== 0);
52 Index
Index::flipVariation(const Index
& vstart
, int v_id
) const {
53 int s
= vstart
.nested
.size();
55 if( (int)nested
.size() < s
56 || vstart
.num_moves
!= num_moves
)
59 for(int i
=0;i
<s
-1;i
++)
60 if(vstart
.nested
[i
] != nested
[i
])
63 if(vstart
.nested
[s
-1].variation
!= nested
[s
-1].variation
64 || vstart
.nested
[s
-1].num_moves
> nested
[s
-1].num_moves
)
67 if(nested
[s
-1].num_moves
> vstart
.nested
[s
-1].num_moves
) {
68 Index
retv(num_moves
);
70 retv
.nested
.push_back(vstart
.nested
[i
]);
71 retv
.nested
.push_back(Ref(v_id
, nested
[s
-1].num_moves
- vstart
.nested
[s
-1].num_moves
- 1));
72 for(int i
=s
;i
<(int)nested
.size();i
++)
73 retv
.nested
.push_back(nested
[i
]);
76 else if(nested
[s
-1].num_moves
== vstart
.nested
[s
-1].num_moves
77 && (int)nested
.size() > s
&& nested
[s
].variation
== v_id
) {
78 Index
retv(num_moves
);
79 for(int i
=0;i
<s
-1;i
++)
80 retv
.nested
.push_back(vstart
.nested
[i
]);
81 retv
.nested
.push_back(Ref(nested
[s
-1].variation
, nested
[s
-1].num_moves
+ nested
[s
].num_moves
+ 1));
82 for(int i
=s
+1;i
<(int)nested
.size();i
++)
83 retv
.nested
.push_back(nested
[i
]);
89 else if(num_moves
> vstart
.num_moves
) {
90 Index
retv(vstart
.num_moves
);
91 retv
.nested
.push_back(Ref(v_id
,num_moves
- vstart
.num_moves
- 1));
92 for(int i
=0;i
<(int)nested
.size();i
++)
93 retv
.nested
.push_back(nested
[i
]);
96 else if(num_moves
== vstart
.num_moves
97 && nested
.size() > 0 && nested
[0].variation
== v_id
) {
98 Index
retv(num_moves
+ nested
[0].num_moves
+ 1);
99 for(int i
=1;i
<(int)nested
.size();i
++)
100 retv
.nested
.push_back(nested
[i
]);
107 Index
Index::next(int variation_id
, int num
) const {
109 if(variation_id
!= -1)
110 retv
.nested
.push_back( Ref(variation_id
, num
-1) );
111 else if(retv
.nested
.size() == 0)
112 retv
.num_moves
+= num
;
114 retv
.nested
.last().num_moves
+= num
;
119 Index
Index::prev(int _num
) const {
124 if(retv
.nested
.size() == 0) {
125 if(retv
.num_moves
< num
) {
126 ERROR("Cannot rewind index " << *this <<
127 " by " << _num
<< "!");
130 retv
.num_moves
-= num
;
134 if(retv
.nested
.last().num_moves
>= num
) {
135 retv
.nested
.last().num_moves
-= num
;
139 num
-= retv
.nested
.last().num_moves
+1;
140 retv
.nested
.pop_back();
148 Index
Index::min(const Index
& ix
) const {
149 if(ix
.num_moves
!= num_moves
)
150 return Index( std::min(ix
.num_moves
, num_moves
) );
152 Index
retv(num_moves
);
153 for(int i
= 0; (i
< (int)nested
.size()) && (i
< (int)ix
.nested
.size()); i
++) {
154 if(nested
[i
].variation
!= ix
.nested
[i
].variation
)
156 retv
.nested
.push_back(Ref(nested
[i
].variation
,
157 std::min(ix
.nested
[i
].num_moves
, nested
[i
].num_moves
) ));
158 if(ix
.nested
[i
].num_moves
!= nested
[i
].num_moves
)
165 std::pair
<int, int> Index::stepsTo(const Index
& ix
) const {
167 int down
= 0, up
= 0;
168 bool branch
= ix
.num_moves
!= num_moves
;
169 if(num_moves
>ix
.num_moves
)
170 down
+= num_moves
-ix
.num_moves
;
171 if(num_moves
<ix
.num_moves
)
172 up
+= ix
.num_moves
-num_moves
;
174 for(i
= 0; (i
< (int)nested
.size()) && (i
< (int)ix
.nested
.size()); i
++) {
175 if(nested
[i
].variation
!= ix
.nested
[i
].variation
)
178 down
+= nested
[i
].num_moves
+1;
179 up
+= ix
.nested
[i
].num_moves
+1;
182 if(ix
.nested
[i
].num_moves
!= nested
[i
].num_moves
)
184 if(nested
[i
].num_moves
>ix
.nested
[i
].num_moves
)
185 down
+= nested
[i
].num_moves
-ix
.nested
[i
].num_moves
;
186 if(nested
[i
].num_moves
<ix
.nested
[i
].num_moves
)
187 up
+= ix
.nested
[i
].num_moves
-nested
[i
].num_moves
;
189 for(; i
<(int)nested
.size();i
++)
190 down
+= nested
[i
].num_moves
+1;
191 for(; i
<(int)ix
.nested
.size();i
++)
192 up
+= ix
.nested
[i
].num_moves
+1;
193 return std::pair
<int,int>(down
, up
);
196 int Index::lastIndex() {
197 return nested
.size() ? nested
[nested
.size()-1].num_moves
: num_moves
;