2 Description: history graph computation
4 Author: Marco Costalba (C) 2005-2007
6 Copyright: See COPYING file that comes with this distribution
13 #define IS_NODE(x) (x == NODE || x == NODE_R || x == NODE_L)
17 void Lanes::init(const QString
& expectedSha
) {
22 add(BRANCH
, expectedSha
, activeLane
);
31 void Lanes::setBoundary(bool b
) {
32 // changes the state so must be called as first one
34 NODE
= b
? BOUNDARY_C
: MERGE_FORK
;
35 NODE_R
= b
? BOUNDARY_R
: MERGE_FORK_R
;
36 NODE_L
= b
? BOUNDARY_L
: MERGE_FORK_L
;
40 typeVec
[activeLane
] = BOUNDARY
;
43 bool Lanes::isFork(const QString
& sha
, bool& isDiscontinuity
) {
45 int pos
= findNextSha(sha
, 0);
46 isDiscontinuity
= (activeLane
!= pos
);
47 if (pos
== -1) // new branch case
50 return (findNextSha(sha
, pos
+ 1) != -1);
55 pos = findNextSha(sha, pos + 1);
56 // if (isDiscontinuity)
57 // isDiscontinuity = (activeLane != pos);
63 void Lanes::setFork(const QString
& sha
) {
65 int rangeStart
, rangeEnd
, idx
;
66 rangeStart
= rangeEnd
= idx
= findNextSha(sha
, 0);
71 idx
= findNextSha(sha
, idx
+ 1);
73 typeVec
[activeLane
] = NODE
;
75 int& startT
= typeVec
[rangeStart
];
76 int& endT
= typeVec
[rangeEnd
];
90 for (int i
= rangeStart
+ 1; i
< rangeEnd
; i
++) {
102 void Lanes::setMerge(const QStringList
& parents
) {
103 // setFork() must be called before setMerge()
106 return; // handle as a simple active line
108 int& t
= typeVec
[activeLane
];
109 bool wasFork
= (t
== NODE
);
110 bool wasFork_L
= (t
== NODE_L
);
111 bool wasFork_R
= (t
== NODE_R
);
112 bool startJoinWasACross
= false, endJoinWasACross
= false;
116 int rangeStart
= activeLane
, rangeEnd
= activeLane
;
117 QStringList::const_iterator
it(parents
.constBegin());
118 for (++it
; it
!= parents
.constEnd(); ++it
) { // skip first parent
120 int idx
= findNextSha(*it
, 0);
123 if (idx
> rangeEnd
) {
126 endJoinWasACross
= typeVec
[idx
] == CROSS
;
129 if (idx
< rangeStart
) {
132 startJoinWasACross
= typeVec
[idx
] == CROSS
;
137 rangeEnd
= add(HEAD
, *it
, rangeEnd
+ 1);
139 int& startT
= typeVec
[rangeStart
];
140 int& endT
= typeVec
[rangeEnd
];
142 if (startT
== NODE
&& !wasFork
&& !wasFork_R
)
145 if (endT
== NODE
&& !wasFork
&& !wasFork_L
)
148 if (startT
== JOIN
&& !startJoinWasACross
)
151 if (endT
== JOIN
&& !endJoinWasACross
)
160 for (int i
= rangeStart
+ 1; i
< rangeEnd
; i
++) {
170 else if (t
== TAIL_R
|| t
== TAIL_L
)
175 void Lanes::setInitial() {
177 int& t
= typeVec
[activeLane
];
178 if (!IS_NODE(t
) && t
!= APPLIED
)
179 t
= (boundary
? BOUNDARY
: INITIAL
);
182 void Lanes::setApplied() {
184 // applied patches are not merges, nor forks
185 typeVec
[activeLane
] = APPLIED
; // TODO test with boundaries
188 void Lanes::changeActiveLane(const QString
& sha
) {
190 int& t
= typeVec
[activeLane
];
191 if (t
== INITIAL
|| isBoundary(t
))
196 int idx
= findNextSha(sha
, 0); // find first sha
198 typeVec
[idx
] = ACTIVE
; // called before setBoundary()
200 idx
= add(BRANCH
, sha
, activeLane
); // new branch
205 void Lanes::afterMerge() {
208 return; // will be reset by changeActiveLane()
210 for (int i
= 0; i
< typeVec
.count(); i
++) {
214 if (isHead(t
) || isJoin(t
) || t
== CROSS
)
217 else if (t
== CROSS_EMPTY
)
225 void Lanes::afterFork() {
227 for (int i
= 0; i
< typeVec
.count(); i
++) {
234 else if (isTail(t
) || t
== CROSS_EMPTY
)
237 if (!boundary
&& IS_NODE(t
))
238 t
= ACTIVE
; // boundary will be reset by changeActiveLane()
240 while (typeVec
.last() == EMPTY
) {
242 nextShaVec
.pop_back();
246 bool Lanes::isBranch() {
248 return (typeVec
[activeLane
] == BRANCH
);
251 void Lanes::afterBranch() {
253 typeVec
[activeLane
] = ACTIVE
; // TODO test with boundaries
256 void Lanes::afterApplied() {
258 typeVec
[activeLane
] = ACTIVE
; // TODO test with boundaries
261 void Lanes::nextParent(const QString
& sha
) {
263 nextShaVec
[activeLane
] = (boundary
? "" : sha
);
266 int Lanes::findNextSha(const QString
& next
, int pos
) {
268 for (int i
= pos
; i
< nextShaVec
.count(); i
++)
269 if (nextShaVec
[i
] == next
)
274 int Lanes::findType(int type
, int pos
) {
276 for (int i
= pos
; i
< typeVec
.count(); i
++)
277 if (typeVec
[i
] == type
)
282 int Lanes::add(int type
, const QString
& next
, int pos
) {
284 // first check empty lanes starting from pos
285 if (pos
< (int)typeVec
.count()) {
286 pos
= findType(EMPTY
, pos
);
289 nextShaVec
[pos
] = next
;
293 // if all lanes are occupied add a new lane
294 typeVec
.append(type
);
295 nextShaVec
.append(next
);
296 return typeVec
.count() - 1;