[go: up one dir, main page]

File: lanes.cpp

package info (click to toggle)
qgit 2.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,152 kB
  • ctags: 1,477
  • sloc: cpp: 11,857; makefile: 51; sh: 39
file content (297 lines) | stat: -rw-r--r-- 5,685 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
	Description: history graph computation

	Author: Marco Costalba (C) 2005-2007

	Copyright: See COPYING file that comes with this distribution

*/
#include <QStringList>
#include "common.h"
#include "lanes.h"

#define IS_NODE(x) (x == NODE || x == NODE_R || x == NODE_L)

using namespace QGit;

void Lanes::init(const QString& expectedSha) {

	clear();
	activeLane = 0;
	setBoundary(false);
	add(BRANCH, expectedSha, activeLane);
}

void Lanes::clear() {

	typeVec.clear();
	nextShaVec.clear();
}

void Lanes::setBoundary(bool b) {
// changes the state so must be called as first one

	NODE   = b ? BOUNDARY_C : MERGE_FORK;
	NODE_R = b ? BOUNDARY_R : MERGE_FORK_R;
	NODE_L = b ? BOUNDARY_L : MERGE_FORK_L;
	boundary = b;

	if (boundary)
		typeVec[activeLane] = BOUNDARY;
}

bool Lanes::isFork(const QString& sha, bool& isDiscontinuity) {

	int pos = findNextSha(sha, 0);
	isDiscontinuity = (activeLane != pos);
	if (pos == -1) // new branch case
		return false;

	return (findNextSha(sha, pos + 1) != -1);
/*
	int cnt = 0;
	while (pos != -1) {
		cnt++;
		pos = findNextSha(sha, pos + 1);
//		if (isDiscontinuity)
//			isDiscontinuity = (activeLane != pos);
	}
	return (cnt > 1);
*/
}

void Lanes::setFork(const QString& sha) {

	int rangeStart, rangeEnd, idx;
	rangeStart = rangeEnd = idx = findNextSha(sha, 0);

	while (idx != -1) {
		rangeEnd = idx;
		typeVec[idx] = TAIL;
		idx = findNextSha(sha, idx + 1);
	}
	typeVec[activeLane] = NODE;

	int& startT = typeVec[rangeStart];
	int& endT = typeVec[rangeEnd];

	if (startT == NODE)
		startT = NODE_L;

	if (endT == NODE)
		endT = NODE_R;

	if (startT == TAIL)
		startT = TAIL_L;

	if (endT == TAIL)
		endT = TAIL_R;

	for (int i = rangeStart + 1; i < rangeEnd; i++) {

		int& t = typeVec[i];

		if (t == NOT_ACTIVE)
			t = CROSS;

		else if (t == EMPTY)
			t = CROSS_EMPTY;
	}
}

void Lanes::setMerge(const QStringList& parents) {
// setFork() must be called before setMerge()

	if (boundary)
		return; // handle as a simple active line

	int& t = typeVec[activeLane];
	bool wasFork   = (t == NODE);
	bool wasFork_L = (t == NODE_L);
	bool wasFork_R = (t == NODE_R);
	bool startJoinWasACross = false, endJoinWasACross = false;

	t = NODE;

	int rangeStart = activeLane, rangeEnd = activeLane;
	QStringList::const_iterator it(parents.constBegin());
	for (++it; it != parents.constEnd(); ++it) { // skip first parent

		int idx = findNextSha(*it, 0);
		if (idx != -1) {

			if (idx > rangeEnd) {

				rangeEnd = idx;
				endJoinWasACross = typeVec[idx] == CROSS;
			}

			if (idx < rangeStart) {

				rangeStart = idx;
				startJoinWasACross = typeVec[idx] == CROSS;
			}

			typeVec[idx] = JOIN;
		} else
			rangeEnd = add(HEAD, *it, rangeEnd + 1);
	}
	int& startT = typeVec[rangeStart];
	int& endT = typeVec[rangeEnd];

	if (startT == NODE && !wasFork && !wasFork_R)
		startT = NODE_L;

	if (endT == NODE && !wasFork && !wasFork_L)
		endT = NODE_R;

	if (startT == JOIN && !startJoinWasACross)
		startT = JOIN_L;

	if (endT == JOIN && !endJoinWasACross)
		endT = JOIN_R;

	if (startT == HEAD)
		startT = HEAD_L;

	if (endT == HEAD)
		endT = HEAD_R;

	for (int i = rangeStart + 1; i < rangeEnd; i++) {

		int& t = typeVec[i];

		if (t == NOT_ACTIVE)
			t = CROSS;

		else if (t == EMPTY)
			t = CROSS_EMPTY;

		else if (t == TAIL_R || t == TAIL_L)
			t = TAIL;
	}
}

void Lanes::setInitial() {

	int& t = typeVec[activeLane];
	if (!IS_NODE(t) && t != APPLIED)
		t = (boundary ? BOUNDARY : INITIAL);
}

void Lanes::setApplied() {

	// applied patches are not merges, nor forks
	typeVec[activeLane] = APPLIED; // TODO test with boundaries
}

void Lanes::changeActiveLane(const QString& sha) {

	int& t = typeVec[activeLane];
	if (t == INITIAL || isBoundary(t))
		t = EMPTY;
	else
		t = NOT_ACTIVE;

	int idx = findNextSha(sha, 0); // find first sha
	if (idx != -1)
		typeVec[idx] = ACTIVE; // called before setBoundary()
	else
		idx = add(BRANCH, sha, activeLane); // new branch

	activeLane = idx;
}

void Lanes::afterMerge() {

	if (boundary)
		return; // will be reset by changeActiveLane()

	for (int i = 0; i < typeVec.count(); i++) {

		int& t = typeVec[i];

		if (isHead(t) || isJoin(t) || t == CROSS)
			t = NOT_ACTIVE;

		else if (t == CROSS_EMPTY)
			t = EMPTY;

		else if (IS_NODE(t))
			t = ACTIVE;
	}
}

void Lanes::afterFork() {

	for (int i = 0; i < typeVec.count(); i++) {

		int& t = typeVec[i];

		if (t == CROSS)
			t = NOT_ACTIVE;

		else if (isTail(t) || t == CROSS_EMPTY)
			t = EMPTY;

		if (!boundary && IS_NODE(t))
			t = ACTIVE; // boundary will be reset by changeActiveLane()
	}
	while (typeVec.last() == EMPTY) {
		typeVec.pop_back();
		nextShaVec.pop_back();
	}
}

bool Lanes::isBranch() {

	return (typeVec[activeLane] == BRANCH);
}

void Lanes::afterBranch() {

	typeVec[activeLane] = ACTIVE; // TODO test with boundaries
}

void Lanes::afterApplied() {

	typeVec[activeLane] = ACTIVE; // TODO test with boundaries
}

void Lanes::nextParent(const QString& sha) {

	nextShaVec[activeLane] = (boundary ? "" : sha);
}

int Lanes::findNextSha(const QString& next, int pos) {

	for (int i = pos; i < nextShaVec.count(); i++)
		if (nextShaVec[i] == next)
			return i;
	return -1;
}

int Lanes::findType(int type, int pos) {

	for (int i = pos; i < typeVec.count(); i++)
		if (typeVec[i] == type)
			return i;
	return -1;
}

int Lanes::add(int type, const QString& next, int pos) {

	// first check empty lanes starting from pos
	if (pos < (int)typeVec.count()) {
		pos = findType(EMPTY, pos);
		if (pos != -1) {
			typeVec[pos] = type;
			nextShaVec[pos] = next;
			return pos;
		}
	}
	// if all lanes are occupied add a new lane
	typeVec.append(type);
	nextShaVec.append(next);
	return typeVec.count() - 1;
}