##// END OF EJS Templates
stabletailgraph: fix terminology in doc
pacien -
r51352:e1496403 default
parent child Browse files
Show More
@@ -1,110 +1,111 b''
1 1 # stabletailsort.py - stable ordering of revisions
2 2 #
3 3 # Copyright 2021-2023 Pacien TRAN-GIRARD <pacien.trangirard@pacien.net>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 """
9 9 Stable-tail sort computation.
10 10
11 11 The "stable-tail sort", or STS, is a reverse topological ordering of the
12 12 ancestors of a node, which tends to share large suffixes with the stable-tail
13 13 sort of ancestors and other nodes, giving it its name.
14 14
15 15 Its properties should make it suitable for making chunks of ancestors with high
16 16 reuse and incrementality for example.
17 17
18 18 This module and implementation are experimental. Most functions are not yet
19 19 optimised to operate on large production graphs.
20 20 """
21 21
22 22 import itertools
23 23 from ..node import nullrev
24 24 from .. import ancestor
25 25
26 26
27 27 def _sorted_parents(cl, p1, p2):
28 28 """
29 29 Chooses and returns the pair (px, pt) from (p1, p2).
30 30
31 31 Where
32 32 "px" denotes the parent starting the "exclusive" part, and
33 33 "pt" denotes the parent starting the "Tail" part.
34 34
35 35 "px" is chosen as the parent with the lowest rank with the goal of
36 36 minimising the size of the exclusive part and maximise the size of the
37 tail part, hopefully reducing the overall complexity of the stable sort.
37 tail part, hopefully reducing the overall complexity of the stable-tail
38 sort.
38 39
39 40 In case of equal ranks, the stable node ID is used as a tie-breaker.
40 41 """
41 42 r1, r2 = cl.fast_rank(p1), cl.fast_rank(p2)
42 43 if r1 < r2:
43 44 return (p1, p2)
44 45 elif r1 > r2:
45 46 return (p2, p1)
46 47 elif cl.node(p1) < cl.node(p2):
47 48 return (p1, p2)
48 49 else:
49 50 return (p2, p1)
50 51
51 52
52 53 def _nonoedipal_parent_revs(cl, rev):
53 54 """
54 55 Returns the non-Ε“dipal parent pair of the given revision.
55 56
56 57 An Ε“dipal merge is a merge with parents p1, p2 with either
57 58 p1 in ancestors(p2) or p2 in ancestors(p1).
58 59 In the first case, p1 is the Ε“dipal parent.
59 60 In the second case, p2 is the Ε“dipal parent.
60 61
61 62 Ε’dipal edges start empty exclusive parts. They do not bring new ancestors.
62 63 As such, they can be skipped when computing any topological sort or any
63 64 iteration over the ancestors of a node.
64 65
65 66 The Ε“dipal edges are eliminated here using the rank information.
66 67 """
67 68 p1, p2 = cl.parentrevs(rev)
68 69 if p1 == nullrev or cl.fast_rank(p2) == cl.fast_rank(rev) - 1:
69 70 return p2, nullrev
70 71 elif p2 == nullrev or cl.fast_rank(p1) == cl.fast_rank(rev) - 1:
71 72 return p1, nullrev
72 73 else:
73 74 return p1, p2
74 75
75 76
76 77 def _stable_tail_sort(cl, head_rev):
77 78 """
78 79 Naive topological iterator of the ancestors given by the stable-tail sort.
79 80
80 81 The stable-tail sort of a node "h" is defined as the sequence:
81 82 sts(h) := [h] + excl(h) + sts(pt(h))
82 83 where excl(h) := u for u in sts(px(h)) if u not in ancestors(pt(h))
83 84
84 85 This implementation uses a call-stack whose size is
85 86 O(number of open merges).
86 87
87 88 As such, this implementation exists mainly as a defining reference.
88 89 """
89 90 cursor_rev = head_rev
90 91 while cursor_rev != nullrev:
91 92 yield cursor_rev
92 93
93 94 p1, p2 = _nonoedipal_parent_revs(cl, cursor_rev)
94 95 if p1 == nullrev:
95 96 cursor_rev = p2
96 97 elif p2 == nullrev:
97 98 cursor_rev = p1
98 99 else:
99 100 px, pt = _sorted_parents(cl, p1, p2)
100 101
101 102 tail_ancestors = ancestor.lazyancestors(
102 103 cl.parentrevs, (pt,), inclusive=True
103 104 )
104 105 exclusive_ancestors = (
105 106 a for a in _stable_tail_sort(cl, px) if a not in tail_ancestors
106 107 )
107 108
108 109 excl_part_size = cl.fast_rank(cursor_rev) - cl.fast_rank(pt) - 1
109 110 yield from itertools.islice(exclusive_ancestors, excl_part_size)
110 111 cursor_rev = pt
General Comments 0
You need to be logged in to leave comments. Login now