blob: 60ebcacc855f3ce9a48242afc3ea83122433750b [file] [log] [blame]
Colin Cross9e44e212020-07-14 15:02:16 -07001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import "fmt"
18
19// DepSet is designed to be conceptually compatible with Bazel's depsets:
20// https://docs.bazel.build/versions/master/skylark/depsets.html
21
22// A DepSet efficiently stores Paths from transitive dependencies without copying. It is stored
23// as a DAG of DepSet nodes, each of which has some direct contents and a list of dependency
24// DepSet nodes.
25//
26// A DepSet has an order that will be used to walk the DAG when ToList() is called. The order
27// can be POSTORDER, PREORDER, or TOPOLOGICAL. POSTORDER and PREORDER orders return a postordered
28// or preordered left to right flattened list. TOPOLOGICAL returns a list that guarantees that
29// elements of children are listed after all of their parents (unless there are duplicate direct
30// elements in the DepSet or any of its transitive dependencies, in which case the ordering of the
31// duplicated element is not guaranteed).
32//
33// A DepSet is created by NewDepSet or NewDepSetBuilder.Build from the Paths for direct contents
34// and the *DepSets of dependencies. A DepSet is immutable once created.
35type DepSet struct {
36 preorder bool
37 reverse bool
38 order DepSetOrder
39 direct Paths
40 transitive []*DepSet
41}
42
43// DepSetBuilder is used to create an immutable DepSet.
44type DepSetBuilder struct {
45 order DepSetOrder
46 direct Paths
47 transitive []*DepSet
48}
49
50type DepSetOrder int
51
52const (
53 PREORDER DepSetOrder = iota
54 POSTORDER
55 TOPOLOGICAL
56)
57
58func (o DepSetOrder) String() string {
59 switch o {
60 case PREORDER:
61 return "PREORDER"
62 case POSTORDER:
63 return "POSTORDER"
64 case TOPOLOGICAL:
65 return "TOPOLOGICAL"
66 default:
67 panic(fmt.Errorf("Invalid DepSetOrder %d", o))
68 }
69}
70
71// NewDepSet returns an immutable DepSet with the given order, direct and transitive contents.
72func NewDepSet(order DepSetOrder, direct Paths, transitive []*DepSet) *DepSet {
73 var directCopy Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -070074 transitiveCopy := make([]*DepSet, 0, len(transitive))
75
76 for _, dep := range transitive {
77 if dep != nil {
78 if dep.order != order {
79 panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s",
80 order, dep.order))
81 }
82 transitiveCopy = append(transitiveCopy, dep)
83 }
84 }
85
Colin Cross9e44e212020-07-14 15:02:16 -070086 if order == TOPOLOGICAL {
87 directCopy = ReversePaths(direct)
Colin Cross0de8a1e2020-09-18 14:15:30 -070088 reverseDepSetsInPlace(transitiveCopy)
Colin Cross9e44e212020-07-14 15:02:16 -070089 } else {
90 // Use copy instead of append(nil, ...) to make a slice that is exactly the size of the input
91 // slice. The DepSet is immutable, there is no need for additional capacity.
92 directCopy = make(Paths, len(direct))
93 copy(directCopy, direct)
Colin Cross9e44e212020-07-14 15:02:16 -070094 }
95
96 return &DepSet{
97 preorder: order == PREORDER,
98 reverse: order == TOPOLOGICAL,
99 order: order,
100 direct: directCopy,
101 transitive: transitiveCopy,
102 }
103}
104
105// NewDepSetBuilder returns a DepSetBuilder to create an immutable DepSet with the given order.
106func NewDepSetBuilder(order DepSetOrder) *DepSetBuilder {
107 return &DepSetBuilder{order: order}
108}
109
110// Direct adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct
111// contents are to the right of any existing direct contents.
112func (b *DepSetBuilder) Direct(direct ...Path) *DepSetBuilder {
113 b.direct = append(b.direct, direct...)
114 return b
115}
116
117// Transitive adds transitive contents to the DepSet being built by a DepSetBuilder. Newly added
118// transitive contents are to the right of any existing transitive contents.
119func (b *DepSetBuilder) Transitive(transitive ...*DepSet) *DepSetBuilder {
120 b.transitive = append(b.transitive, transitive...)
121 return b
122}
123
124// Returns the DepSet being built by this DepSetBuilder. The DepSetBuilder retains its contents
125// for creating more DepSets.
126func (b *DepSetBuilder) Build() *DepSet {
127 return NewDepSet(b.order, b.direct, b.transitive)
128}
129
130// walk calls the visit method in depth-first order on a DepSet, preordered if d.preorder is set,
131// otherwise postordered.
132func (d *DepSet) walk(visit func(Paths)) {
133 visited := make(map[*DepSet]bool)
134
135 var dfs func(d *DepSet)
136 dfs = func(d *DepSet) {
137 visited[d] = true
138 if d.preorder {
139 visit(d.direct)
140 }
141 for _, dep := range d.transitive {
142 if !visited[dep] {
143 dfs(dep)
144 }
145 }
146
147 if !d.preorder {
148 visit(d.direct)
149 }
150 }
151
152 dfs(d)
153}
154
155// ToList returns the DepSet flattened to a list. The order in the list is based on the order
156// of the DepSet. POSTORDER and PREORDER orders return a postordered or preordered left to right
157// flattened list. TOPOLOGICAL returns a list that guarantees that elements of children are listed
158// after all of their parents (unless there are duplicate direct elements in the DepSet or any of
159// its transitive dependencies, in which case the ordering of the duplicated element is not
160// guaranteed).
161func (d *DepSet) ToList() Paths {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700162 if d == nil {
163 return nil
164 }
Colin Cross9e44e212020-07-14 15:02:16 -0700165 var list Paths
166 d.walk(func(paths Paths) {
167 list = append(list, paths...)
168 })
169 list = FirstUniquePaths(list)
170 if d.reverse {
171 reversePathsInPlace(list)
172 }
173 return list
174}
175
176// ToSortedList returns the direct and transitive contents of a DepSet in lexically sorted order
177// with duplicates removed.
178func (d *DepSet) ToSortedList() Paths {
179 list := d.ToList()
180 return SortedUniquePaths(list)
181}
182
183func reversePathsInPlace(list Paths) {
184 for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
185 list[i], list[j] = list[j], list[i]
186 }
187}
188
Colin Cross0de8a1e2020-09-18 14:15:30 -0700189func reverseDepSetsInPlace(list []*DepSet) {
190 for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
191 list[i], list[j] = list[j], list[i]
Colin Cross9e44e212020-07-14 15:02:16 -0700192 }
Colin Cross0de8a1e2020-09-18 14:15:30 -0700193
Colin Cross9e44e212020-07-14 15:02:16 -0700194}