| ︙ | | | ︙ | |
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
|
**
** This file contains code used to trace paths of through the
** directed acyclic graph (DAG) of check-ins.
*/
#include "config.h"
#include "path.h"
#include <assert.h>
#if INTERFACE
/* Nodes for the paths through the DAG.
*/
struct PathNode {
int rid; /* ID for this node */
u8 fromIsParent; /* True if pFrom is the parent of rid */
u8 isPrim; /* True if primary side of common ancestor */
u8 isHidden; /* Abbreviate output in "fossil bisect ls" */
u8 nDelay; /* Delay this many steps before walking */
char *zBranch; /* Branch name for this node. Might be NULL */
PathNode *pFrom; /* Node we came from */
union {
PathNode *pPeer; /* List of nodes of the same generation */
PathNode *pTo; /* Next on path from beginning to end */
} u;
PathNode *pAll; /* List of all nodes */
};
#endif
/*
** Local variables for this module
*/
static struct {
PathNode *pCurrent; /* Current generation of nodes */
PathNode *pAll; /* All nodes */
Bag seen; /* Nodes seen before */
int nStep; /* Number of steps from first to last */
int nNotHidden; /* Number of steps not counting hidden nodes */
u8 brCost; /* Extra cost for moving to a different branch */
PathNode *pStart; /* Earliest node */
PathNode *pEnd; /* Most recent */
} path;
/*
** Return the first (last) element of the computed path.
*/
|
>
<
>
|
|
|
<
|
>
|
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
|
**
** This file contains code used to trace paths of through the
** directed acyclic graph (DAG) of check-ins.
*/
#include "config.h"
#include "path.h"
#include <assert.h>
#include <math.h>
#if INTERFACE
/* Nodes for the paths through the DAG.
*/
struct PathNode {
int rid; /* ID for this node */
u8 fromIsParent; /* True if pFrom is the parent of rid */
u8 isPrim; /* True if primary side of common ancestor */
u8 isHidden; /* Abbreviate output in "fossil bisect ls" */
char *zBranch; /* Branch name for this node. Might be NULL */
double mtime; /* Date/time of this check-in */
PathNode *pFrom; /* Node we came from */
union {
double rCost; /* Cost of getting to this node from pStart */
PathNode *pTo; /* Next on path from beginning to end */
} u;
PathNode *pAll; /* List of all nodes */
};
#endif
/*
** Local variables for this module
*/
static struct {
PQueue pending; /* Nodes pending review for inclusion in the graph */
PathNode *pAll; /* All nodes */
int nStep; /* Number of steps from first to last */
int nNotHidden; /* Number of steps not counting hidden nodes */
int brCost; /* Extra cost for moving to a different branch */
int revCost; /* Extra cost for changing directions */
PathNode *pStart; /* Earliest node */
PathNode *pEnd; /* Most recent */
} path;
/*
** Return the first (last) element of the computed path.
*/
|
| ︙ | | | ︙ | |
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
|
/*
** Return the number of non-hidden steps in the computed path.
*/
int path_length_not_hidden(void){ return path.nNotHidden; }
/*
** Create a new node
*/
static PathNode *path_new_node(int rid, PathNode *pFrom, int isParent){
PathNode *p;
p = fossil_malloc( sizeof(*p) );
memset(p, 0, sizeof(*p));
p->rid = rid;
p->fromIsParent = isParent;
p->pFrom = pFrom;
if( path.brCost ){
p->zBranch = branch_of_rid(rid);
if( pFrom && fossil_strcmp(p->zBranch, pFrom->zBranch)!=0 ){
p->nDelay = path.brCost;
}
}
p->u.pPeer = path.pCurrent;
path.pCurrent = p;
p->pAll = path.pAll;
path.pAll = p;
bag_insert(&path.seen, rid);
return p;
}
/*
** Reset memory used by the shortest path algorithm.
*/
void path_reset(void){
PathNode *p;
while( path.pAll ){
p = path.pAll;
path.pAll = p->pAll;
fossil_free(p->zBranch);
fossil_free(p);
}
bag_clear(&path.seen);
memset(&path, 0, sizeof(path));
}
/*
** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
*/
static void path_reverse_path(void){
|
|
>
>
>
>
>
>
|
|
|
|
>
>
|
>
|
|
>
|
<
|
|
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
|
/*
** Return the number of non-hidden steps in the computed path.
*/
int path_length_not_hidden(void){ return path.nNotHidden; }
/*
** Create a new node and insert it into the path.pending queue.
*/
static PathNode *path_new_node(int rid, PathNode *pFrom, int isParent){
PathNode *p;
p = fossil_malloc( sizeof(*p) );
memset(p, 0, sizeof(*p));
p->pAll = path.pAll;
path.pAll = p;
p->rid = rid;
p->fromIsParent = isParent;
p->pFrom = pFrom;
p->u.rCost = pFrom ? pFrom->u.rCost : 0.0;
if( path.brCost ){
p->zBranch = branch_of_rid(rid);
p->mtime = mtime_of_rid(rid, 0.0);
if( pFrom ){
p->u.rCost += fabs(pFrom->mtime - p->mtime);
if( fossil_strcmp(p->zBranch, pFrom->zBranch)!=0 ){
p->u.rCost += path.brCost;
}
}
}else{
/* When brCost==0, we try to minimize the number of nodes
** along the path. The cost is just the number of nodes back
** to the start. We do not need to know the branch name nor
** the mtime */
p->u.rCost += 1.0;
}
pqueuex_insert_ptr(&path.pending, (void*)p, p->u.rCost);
return p;
}
/*
** Reset memory used by the shortest path algorithm.
*/
void path_reset(void){
PathNode *p;
while( path.pAll ){
p = path.pAll;
path.pAll = p->pAll;
fossil_free(p->zBranch);
fossil_free(p);
}
pqueuex_clear(&path.pending);
memset(&path, 0, sizeof(path));
}
/*
** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
*/
static void path_reverse_path(void){
|
| ︙ | | | ︙ | |
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
int iTo, /* Path ends here */
int directOnly, /* No merge links if true */
int oneWayOnly, /* Parent->child only if true */
Bag *pHidden, /* Hidden nodes */
int branchCost /* Add extra codes to changing branches */
){
Stmt s;
PathNode *pPrev;
PathNode *p;
int nPriorPeer = 1;
path_reset();
path.brCost = branchCost;
path.pStart = path_new_node(iFrom, 0, 0);
if( iTo==iFrom ){
path.pEnd = path.pStart;
return path.pStart;
|
|
<
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
int iTo, /* Path ends here */
int directOnly, /* No merge links if true */
int oneWayOnly, /* Parent->child only if true */
Bag *pHidden, /* Hidden nodes */
int branchCost /* Add extra codes to changing branches */
){
Stmt s;
Bag seen;
PathNode *p;
path_reset();
path.brCost = branchCost;
path.pStart = path_new_node(iFrom, 0, 0);
if( iTo==iFrom ){
path.pEnd = path.pStart;
return path.pStart;
|
| ︙ | | | ︙ | |
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
|
}else{
db_prepare(&s,
"SELECT cid, 1 FROM plink WHERE pid=:pid "
"UNION ALL "
"SELECT pid, 0 FROM plink WHERE cid=:pid"
);
}
while( path.pCurrent ){
if( nPriorPeer ) path.nStep++;
nPriorPeer = 0;
pPrev = path.pCurrent;
path.pCurrent = 0;
while( pPrev ){
if( pPrev->nDelay>0 && (nPriorPeer>0 || pPrev->u.pPeer!=0) ){
PathNode *pThis = pPrev;
pPrev = pThis->u.pPeer;
pThis->u.pPeer = path.pCurrent;
path.pCurrent = pThis;
pThis->nDelay--;
continue;
}
nPriorPeer++;
db_bind_int(&s, ":pid", pPrev->rid);
while( db_step(&s)==SQLITE_ROW ){
int cid = db_column_int(&s, 0);
int isParent = db_column_int(&s, 1);
if( bag_find(&path.seen, cid) ) continue;
p = path_new_node(cid, pPrev, isParent);
if( pHidden && bag_find(pHidden,cid) ) p->isHidden = 1;
if( cid==iTo ){
db_finalize(&s);
path.pEnd = p;
path_reverse_path();
for(p=path.pStart->u.pTo; p; p=p->u.pTo ){
if( !p->isHidden ) path.nNotHidden++;
}
return path.pStart;
}
}
db_reset(&s);
pPrev = pPrev->u.pPeer;
}
}
db_finalize(&s);
path_reset();
return 0;
}
/*
|
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
|
|
<
<
|
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
|
}else{
db_prepare(&s,
"SELECT cid, 1 FROM plink WHERE pid=:pid "
"UNION ALL "
"SELECT pid, 0 FROM plink WHERE cid=:pid"
);
}
bag_init(&seen);
while( (p = pqueuex_extract_ptr(&path.pending))!=0 ){
if( p->rid==iTo ){
db_finalize(&s);
path.pEnd = p;
path_reverse_path();
for(p=path.pStart->u.pTo; p; p=p->u.pTo ){
if( !p->isHidden ) path.nNotHidden++;
}
return path.pStart;
}
if( bag_find(&seen, p->rid) ) continue;
bag_insert(&seen, p->rid);
db_bind_int(&s, ":pid", p->rid);
while( db_step(&s)==SQLITE_ROW ){
int cid = db_column_int(&s, 0);
int isParent = db_column_int(&s, 1);
PathNode *pNew;
if( bag_find(&seen, cid) ) continue;
pNew = path_new_node(cid, p, isParent);
if( pHidden && bag_find(pHidden,cid) ) pNew->isHidden = 1;
}
db_reset(&s);
}
db_finalize(&s);
path_reset();
return 0;
}
/*
|
| ︙ | | | ︙ | |
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
** Find the closest common ancestor of two nodes. "Closest" means the
** fewest number of arcs.
*/
int path_common_ancestor(int iMe, int iYou){
Stmt s;
PathNode *pPrev;
PathNode *p;
Bag me, you;
if( iMe==iYou ) return iMe;
if( iMe==0 || iYou==0 ) return 0;
path_reset();
path.pStart = path_new_node(iMe, 0, 0);
path.pStart->isPrim = 1;
path.pEnd = path_new_node(iYou, 0, 0);
db_prepare(&s, "SELECT pid FROM plink WHERE cid=:cid");
bag_init(&me);
bag_insert(&me, iMe);
bag_init(&you);
bag_insert(&you, iYou);
while( path.pCurrent ){
pPrev = path.pCurrent;
path.pCurrent = 0;
while( pPrev ){
db_bind_int(&s, ":cid", pPrev->rid);
while( db_step(&s)==SQLITE_ROW ){
int pid = db_column_int(&s, 0);
if( bag_find(pPrev->isPrim ? &you : &me, pid) ){
/* pid is the common ancestor */
PathNode *pNext;
for(p=path.pAll; p && p->rid!=pid; p=p->pAll){}
assert( p!=0 );
pNext = p;
while( pNext ){
pNext = p->pFrom;
p->pFrom = pPrev;
pPrev = p;
p = pNext;
}
if( pPrev==path.pStart ) path.pStart = path.pEnd;
path.pEnd = pPrev;
path_reverse_path();
db_finalize(&s);
return pid;
}else if( bag_find(&path.seen, pid) ){
/* pid is just an alternative path on one of the legs */
continue;
}
p = path_new_node(pid, pPrev, 0);
p->isPrim = pPrev->isPrim;
bag_insert(pPrev->isPrim ? &me : &you, pid);
}
db_reset(&s);
pPrev = pPrev->u.pPeer;
}
}
db_finalize(&s);
path_reset();
return 0;
}
/*
|
|
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
|
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
** Find the closest common ancestor of two nodes. "Closest" means the
** fewest number of arcs.
*/
int path_common_ancestor(int iMe, int iYou){
Stmt s;
PathNode *pThis;
PathNode *p;
Bag me, you;
if( iMe==iYou ) return iMe;
if( iMe==0 || iYou==0 ) return 0;
path_reset();
path.pStart = path_new_node(iMe, 0, 0);
path.pStart->isPrim = 1;
path.pEnd = path_new_node(iYou, 0, 0);
db_prepare(&s, "SELECT pid FROM plink WHERE cid=:cid");
bag_init(&me);
bag_insert(&me, iMe);
bag_init(&you);
bag_insert(&you, iYou);
while( (pThis = pqueuex_extract_ptr(&path.pending))!=0 ){
db_bind_int(&s, ":cid", pThis->rid);
while( db_step(&s)==SQLITE_ROW ){
int pid = db_column_int(&s, 0);
if( bag_find(pThis->isPrim ? &you : &me, pid) ){
/* pid is the common ancestor */
PathNode *pNext;
for(p=path.pAll; p && p->rid!=pid; p=p->pAll){}
assert( p!=0 );
pNext = p;
while( pNext ){
pNext = p->pFrom;
p->pFrom = pThis;
pThis = p;
p = pNext;
}
if( pThis==path.pStart ) path.pStart = path.pEnd;
path.pEnd = pThis;
path_reverse_path();
db_finalize(&s);
return pid;
}else if( bag_find(pThis->isPrim ? &me : &you, pid) ){
/* pid is just an alternative path to a node we've already visited */
continue;
}
p = path_new_node(pid, pThis, 0);
p->isPrim = pThis->isPrim;
bag_insert(pThis->isPrim ? &me : &you, pid);
}
db_reset(&s);
}
db_finalize(&s);
path_reset();
return 0;
}
/*
|
| ︙ | | | ︙ | |