2 Description: history graph computation
4 Author: Marco Costalba (C) 2005-2007
6 Copyright: See COPYING file that comes with this distribution
12 #define IS_NODE(x) (x == NODE || x == NODE_R || x == NODE_L)
15 void Lanes::init(const CGitHash
& expectedSha
) {
20 add(BRANCH
, expectedSha
, activeLane
);
29 void Lanes::setBoundary(bool b
) {
30 // changes the state so must be called as first one
32 NODE
= b
? BOUNDARY_C
: MERGE_FORK
;
33 NODE_R
= b
? BOUNDARY_R
: MERGE_FORK_R
;
34 NODE_L
= b
? BOUNDARY_L
: MERGE_FORK_L
;
38 typeVec
[activeLane
] = BOUNDARY
;
41 bool Lanes::isFork(const CGitHash
& sha
, bool& isDiscontinuity
) {
43 int pos
= findNextSha(sha
, 0);
44 isDiscontinuity
= (activeLane
!= pos
);
45 if (pos
== -1) // new branch case
48 return (findNextSha(sha
, pos
+ 1) != -1);
53 pos = findNextSha(sha, pos + 1);
54 // if (isDiscontinuity)
55 // isDiscontinuity = (activeLane != pos);
61 void Lanes::setFork(const CGitHash
& sha
) {
63 int rangeStart
, rangeEnd
, idx
;
64 rangeStart
= rangeEnd
= idx
= findNextSha(sha
, 0);
69 idx
= findNextSha(sha
, idx
+ 1);
71 typeVec
[activeLane
] = NODE
;
73 int& startT
= typeVec
[rangeStart
];
74 int& endT
= typeVec
[rangeEnd
];
88 for (int i
= rangeStart
+ 1; i
< rangeEnd
; i
++) {
100 void Lanes::setMerge(const CGitHashList
& parents
) {
101 // setFork() must be called before setMerge()
104 return; // handle as a simple active line
106 int& t
= typeVec
[activeLane
];
107 bool wasFork
= (t
== NODE
);
108 bool wasFork_L
= (t
== NODE_L
);
109 bool wasFork_R
= (t
== NODE_R
);
110 bool startJoinWasACross
= false, endJoinWasACross
= false;
114 int rangeStart
= activeLane
, rangeEnd
= activeLane
;
115 CGitHashList::const_iterator
it(parents
.begin());
116 for (++it
; it
!= parents
.end(); ++it
) { // skip first parent
118 int idx
= findNextSha(*it
, 0);
121 if (idx
> rangeEnd
) {
124 endJoinWasACross
= typeVec
[idx
] == CROSS
;
127 if (idx
< rangeStart
) {
130 startJoinWasACross
= typeVec
[idx
] == CROSS
;
135 rangeEnd
= add(HEAD
, *it
, rangeEnd
+ 1);
137 int& startT
= typeVec
[rangeStart
];
138 int& endT
= typeVec
[rangeEnd
];
140 if (startT
== NODE
&& !wasFork
&& !wasFork_R
)
143 if (endT
== NODE
&& !wasFork
&& !wasFork_L
)
146 if (startT
== JOIN
&& !startJoinWasACross
)
149 if (endT
== JOIN
&& !endJoinWasACross
)
158 for (int i
= rangeStart
+ 1; i
< rangeEnd
; i
++) {
168 else if (t
== TAIL_R
|| t
== TAIL_L
)
173 void Lanes::setInitial() {
175 int& t
= typeVec
[activeLane
];
176 if (!IS_NODE(t
) && t
!= APPLIED
)
177 t
= (boundary
? BOUNDARY
: INITIAL
);
180 void Lanes::setApplied() {
182 // applied patches are not merges, nor forks
183 typeVec
[activeLane
] = APPLIED
; // TODO test with boundaries
186 void Lanes::changeActiveLane(const CGitHash
& sha
) {
188 int& t
= typeVec
[activeLane
];
189 if (t
== INITIAL
|| isBoundary(t
))
194 int idx
= findNextSha(sha
, 0); // find first sha
196 typeVec
[idx
] = ACTIVE
; // called before setBoundary()
198 idx
= add(BRANCH
, sha
, activeLane
); // new branch
203 void Lanes::afterMerge() {
206 return; // will be reset by changeActiveLane()
208 for (unsigned int i
= 0; i
< typeVec
.size(); i
++) {
212 if (isHead(t
) || isJoin(t
) || t
== CROSS
)
215 else if (t
== CROSS_EMPTY
)
223 void Lanes::afterFork() {
225 for (unsigned int i
= 0; i
< typeVec
.size(); i
++) {
232 else if (isTail(t
) || t
== CROSS_EMPTY
)
235 if (!boundary
&& IS_NODE(t
))
236 t
= ACTIVE
; // boundary will be reset by changeActiveLane()
238 while (typeVec
.back() == EMPTY
) {
240 nextShaVec
.pop_back();
244 bool Lanes::isBranch() {
246 return (typeVec
[activeLane
] == BRANCH
);
249 void Lanes::afterBranch() {
251 typeVec
[activeLane
] = ACTIVE
; // TODO test with boundaries
254 void Lanes::afterApplied() {
256 typeVec
[activeLane
] = ACTIVE
; // TODO test with boundaries
259 void Lanes::nextParent(const CGitHash
& sha
) {
262 nextShaVec
[activeLane
].Empty();
264 nextShaVec
[activeLane
] = sha
;
267 int Lanes::findNextSha(const CGitHash
& next
, int pos
) {
269 for (unsigned int i
= pos
; i
< nextShaVec
.size(); i
++)
270 if (nextShaVec
[i
] == next
)
275 int Lanes::findType(int type
, int pos
) {
277 for (unsigned int i
= pos
; i
< typeVec
.size(); i
++)
278 if (typeVec
[i
] == type
)
283 int Lanes::add(int type
, const CGitHash
& next
, int pos
) {
285 // first check empty lanes starting from pos
286 if (pos
< (int)typeVec
.size()) {
287 pos
= findType(EMPTY
, pos
);
290 nextShaVec
[pos
] = next
;
294 // if all lanes are occupied add a new lane
295 typeVec
.push_back(type
);
296 nextShaVec
.push_back(next
);
297 return typeVec
.size() - 1;