BrowseRefs: Context menu enhancements
[TortoiseGit.git] / src / TortoiseProc / lanes.cpp
bloba1d5425fe59a112f6cba5db879e48655b5ae693f
1 /*
2 Description: history graph computation
4 Author: Marco Costalba (C) 2005-2007
6 Copyright: See COPYING file that comes with this distribution
8 */
9 #include "stdafx.h"
10 #include "lanes.h"
12 #define IS_NODE(x) (x == NODE || x == NODE_R || x == NODE_L)
15 void Lanes::init(const QString& expectedSha) {
17 clear();
18 activeLane = 0;
19 setBoundary(false);
20 add(BRANCH, expectedSha, activeLane);
23 void Lanes::clear() {
25 typeVec.clear();
26 nextShaVec.clear();
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;
35 boundary = b;
37 if (boundary)
38 typeVec[activeLane] = BOUNDARY;
41 bool Lanes::isFork(const QString& sha, bool& isDiscontinuity) {
43 int pos = findNextSha(sha, 0);
44 isDiscontinuity = (activeLane != pos);
45 if (pos == -1) // new branch case
46 return false;
48 return (findNextSha(sha, pos + 1) != -1);
50 int cnt = 0;
51 while (pos != -1) {
52 cnt++;
53 pos = findNextSha(sha, pos + 1);
54 // if (isDiscontinuity)
55 // isDiscontinuity = (activeLane != pos);
57 return (cnt > 1);
61 void Lanes::setFork(const QString& sha) {
63 int rangeStart, rangeEnd, idx;
64 rangeStart = rangeEnd = idx = findNextSha(sha, 0);
66 while (idx != -1) {
67 rangeEnd = idx;
68 typeVec[idx] = TAIL;
69 idx = findNextSha(sha, idx + 1);
71 typeVec[activeLane] = NODE;
73 int& startT = typeVec[rangeStart];
74 int& endT = typeVec[rangeEnd];
76 if (startT == NODE)
77 startT = NODE_L;
79 if (endT == NODE)
80 endT = NODE_R;
82 if (startT == TAIL)
83 startT = TAIL_L;
85 if (endT == TAIL)
86 endT = TAIL_R;
88 for (int i = rangeStart + 1; i < rangeEnd; i++) {
90 int& t = typeVec[i];
92 if (t == NOT_ACTIVE)
93 t = CROSS;
95 else if (t == EMPTY)
96 t = CROSS_EMPTY;
100 void Lanes::setMerge(const QStringList& parents) {
101 // setFork() must be called before setMerge()
103 if (boundary)
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 joinWasACross = false;
112 t = NODE;
114 int rangeStart = activeLane, rangeEnd = activeLane;
116 QStringList::const_iterator it=parents.begin();
118 for (++it; it != parents.end(); ++it) { // skip first parent
120 int idx = findNextSha(*it, 0);
121 if (idx != -1) {
123 if (typeVec[idx] == CROSS)
124 joinWasACross = true;
126 typeVec[idx] = JOIN;
128 if (idx > rangeEnd)
129 rangeEnd = idx;
131 if (idx < rangeStart)
132 rangeStart = idx;
133 } else
134 rangeEnd = add(HEAD, *it, rangeEnd + 1);
136 int& startT = typeVec[rangeStart];
137 int& endT = typeVec[rangeEnd];
139 if (startT == NODE && !wasFork && !wasFork_R)
140 startT = NODE_L;
142 if (endT == NODE && !wasFork && !wasFork_L)
143 endT = NODE_R;
145 if (startT == JOIN && !joinWasACross)
146 startT = JOIN_L;
148 if (endT == JOIN && !joinWasACross)
149 endT = JOIN_R;
151 if (startT == HEAD)
152 startT = HEAD_L;
154 if (endT == HEAD)
155 endT = HEAD_R;
157 for (int i = rangeStart + 1; i < rangeEnd; i++) {
159 int& t = typeVec[i];
161 if (t == NOT_ACTIVE)
162 t = CROSS;
164 else if (t == EMPTY)
165 t = CROSS_EMPTY;
167 else if (t == TAIL_R || t == TAIL_L)
168 t = TAIL;
172 void Lanes::setInitial() {
174 int& t = typeVec[activeLane];
175 if (!IS_NODE(t) && t != APPLIED)
176 t = (boundary ? BOUNDARY : INITIAL);
179 void Lanes::setApplied() {
181 // applied patches are not merges, nor forks
182 typeVec[activeLane] = APPLIED; // TODO test with boundaries
185 void Lanes::changeActiveLane(const QString& sha) {
187 int& t = typeVec[activeLane];
188 if (t == INITIAL || isBoundary(t))
189 t = EMPTY;
190 else
191 t = NOT_ACTIVE;
193 int idx = findNextSha(sha, 0); // find first sha
194 if (idx != -1)
195 typeVec[idx] = ACTIVE; // called before setBoundary()
196 else
197 idx = add(BRANCH, sha, activeLane); // new branch
199 activeLane = idx;
202 void Lanes::afterMerge() {
204 if (boundary)
205 return; // will be reset by changeActiveLane()
207 for (unsigned int i = 0; i < typeVec.size(); i++) {
209 int& t = typeVec[i];
211 if (isHead(t) || isJoin(t) || t == CROSS)
212 t = NOT_ACTIVE;
214 else if (t == CROSS_EMPTY)
215 t = EMPTY;
217 else if (IS_NODE(t))
218 t = ACTIVE;
222 void Lanes::afterFork() {
224 for (unsigned int i = 0; i < typeVec.size(); i++) {
226 int& t = typeVec[i];
228 if (t == CROSS)
229 t = NOT_ACTIVE;
231 else if (isTail(t) || t == CROSS_EMPTY)
232 t = EMPTY;
234 if (!boundary && IS_NODE(t))
235 t = ACTIVE; // boundary will be reset by changeActiveLane()
237 while (typeVec.back() == EMPTY) {
238 typeVec.pop_back();
239 nextShaVec.pop_back();
243 bool Lanes::isBranch() {
245 return (typeVec[activeLane] == BRANCH);
248 void Lanes::afterBranch() {
250 typeVec[activeLane] = ACTIVE; // TODO test with boundaries
253 void Lanes::afterApplied() {
255 typeVec[activeLane] = ACTIVE; // TODO test with boundaries
258 void Lanes::nextParent(const QString& sha) {
260 nextShaVec[activeLane] = (boundary ? QString(_T("")) : sha);
263 int Lanes::findNextSha(const QString& next, int pos) {
265 for (unsigned int i = pos; i < nextShaVec.size(); i++)
266 if (nextShaVec[i] == next)
267 return i;
268 return -1;
271 int Lanes::findType(int type, int pos) {
273 for (unsigned int i = pos; i < typeVec.size(); i++)
274 if (typeVec[i] == type)
275 return i;
276 return -1;
279 int Lanes::add(int type, const QString& next, int pos) {
281 // first check empty lanes starting from pos
282 if (pos < (int)typeVec.size()) {
283 pos = findType(EMPTY, pos);
284 if (pos != -1) {
285 typeVec[pos] = type;
286 nextShaVec[pos] = next;
287 return pos;
290 // if all lanes are occupied add a new lane
291 typeVec.push_back(type);
292 nextShaVec.push_back(next);
293 return typeVec.size() - 1;