blob: 690987a0ceca4d2c5870ab73a2f244cb04ba852f [file] [log] [blame]
Colin Cross96c44122020-11-25 14:29:50 -08001// 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 (
Yu Liu26a716d2024-08-30 23:40:32 +000018 "bytes"
19 "encoding/gob"
20 "errors"
Colin Cross96c44122020-11-25 14:29:50 -080021 "fmt"
Colin Cross96c44122020-11-25 14:29:50 -080022)
23
Colin Crossc85750b2022-04-21 12:50:51 -070024// DepSet is designed to be conceptually compatible with Bazel's depsets:
Colin Cross96c44122020-11-25 14:29:50 -080025// https://docs.bazel.build/versions/master/skylark/depsets.html
26
27type DepSetOrder int
28
29const (
30 PREORDER DepSetOrder = iota
31 POSTORDER
32 TOPOLOGICAL
33)
34
35func (o DepSetOrder) String() string {
36 switch o {
37 case PREORDER:
38 return "PREORDER"
39 case POSTORDER:
40 return "POSTORDER"
41 case TOPOLOGICAL:
42 return "TOPOLOGICAL"
43 default:
44 panic(fmt.Errorf("Invalid DepSetOrder %d", o))
45 }
46}
47
Colin Crossc85750b2022-04-21 12:50:51 -070048type depSettableType comparable
49
50// A DepSet efficiently stores a slice of an arbitrary type from transitive dependencies without
51// copying. It is stored as a DAG of DepSet nodes, each of which has some direct contents and a list
52// of dependency DepSet nodes.
Colin Cross96c44122020-11-25 14:29:50 -080053//
Colin Crossc85750b2022-04-21 12:50:51 -070054// A DepSet has an order that will be used to walk the DAG when ToList() is called. The order
Colin Cross96c44122020-11-25 14:29:50 -080055// can be POSTORDER, PREORDER, or TOPOLOGICAL. POSTORDER and PREORDER orders return a postordered
56// or preordered left to right flattened list. TOPOLOGICAL returns a list that guarantees that
57// elements of children are listed after all of their parents (unless there are duplicate direct
Colin Crossc85750b2022-04-21 12:50:51 -070058// elements in the DepSet or any of its transitive dependencies, in which case the ordering of the
Colin Cross96c44122020-11-25 14:29:50 -080059// duplicated element is not guaranteed).
60//
Colin Crossc85750b2022-04-21 12:50:51 -070061// A DepSet is created by NewDepSet or NewDepSetBuilder.Build from the slice for direct contents
62// and the *DepSets of dependencies. A DepSet is immutable once created.
63type DepSet[T depSettableType] struct {
Colin Cross96c44122020-11-25 14:29:50 -080064 preorder bool
65 reverse bool
66 order DepSetOrder
Colin Crossc85750b2022-04-21 12:50:51 -070067 direct []T
68 transitive []*DepSet[T]
Colin Cross96c44122020-11-25 14:29:50 -080069}
70
Yu Liu26a716d2024-08-30 23:40:32 +000071func (d *DepSet[T]) GobEncode() ([]byte, error) {
72 w := new(bytes.Buffer)
73 encoder := gob.NewEncoder(w)
74 err := errors.Join(encoder.Encode(d.preorder), encoder.Encode(d.reverse),
75 encoder.Encode(d.order), encoder.Encode(d.direct), encoder.Encode(d.transitive))
76 if err != nil {
77 return nil, err
78 }
79
80 return w.Bytes(), nil
81}
82
83func (d *DepSet[T]) GobDecode(data []byte) error {
84 r := bytes.NewBuffer(data)
85 decoder := gob.NewDecoder(r)
86 err := errors.Join(decoder.Decode(&d.preorder), decoder.Decode(&d.reverse),
87 decoder.Decode(&d.order), decoder.Decode(&d.direct), decoder.Decode(&d.transitive))
88 if err != nil {
89 return err
90 }
91
92 return nil
93}
94
Colin Crossc85750b2022-04-21 12:50:51 -070095// NewDepSet returns an immutable DepSet with the given order, direct and transitive contents.
96func NewDepSet[T depSettableType](order DepSetOrder, direct []T, transitive []*DepSet[T]) *DepSet[T] {
97 var directCopy []T
98 var transitiveCopy []*DepSet[T]
99 for _, t := range transitive {
100 if t.order != order {
101 panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s",
102 order, t.order))
103 }
Colin Cross96c44122020-11-25 14:29:50 -0800104 }
105
Colin Crossc85750b2022-04-21 12:50:51 -0700106 if order == TOPOLOGICAL {
107 // TOPOLOGICAL is implemented as a postorder traversal followed by reversing the output.
108 // Pre-reverse the inputs here so their order is maintained in the output.
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700109 directCopy = ReverseSlice(direct)
110 transitiveCopy = ReverseSlice(transitive)
Colin Crossc85750b2022-04-21 12:50:51 -0700111 } else {
112 directCopy = append([]T(nil), direct...)
113 transitiveCopy = append([]*DepSet[T](nil), transitive...)
114 }
115
116 return &DepSet[T]{
Colin Cross96c44122020-11-25 14:29:50 -0800117 preorder: order == PREORDER,
118 reverse: order == TOPOLOGICAL,
119 order: order,
120 direct: directCopy,
Colin Crossc85750b2022-04-21 12:50:51 -0700121 transitive: transitiveCopy,
Colin Cross96c44122020-11-25 14:29:50 -0800122 }
123}
124
Colin Crossc85750b2022-04-21 12:50:51 -0700125// DepSetBuilder is used to create an immutable DepSet.
126type DepSetBuilder[T depSettableType] struct {
Colin Cross96c44122020-11-25 14:29:50 -0800127 order DepSetOrder
Colin Crossc85750b2022-04-21 12:50:51 -0700128 direct []T
129 transitive []*DepSet[T]
Colin Cross96c44122020-11-25 14:29:50 -0800130}
131
Colin Crossc85750b2022-04-21 12:50:51 -0700132// NewDepSetBuilder returns a DepSetBuilder to create an immutable DepSet with the given order and
133// type, represented by a slice of type that will be in the DepSet.
134func NewDepSetBuilder[T depSettableType](order DepSetOrder) *DepSetBuilder[T] {
135 return &DepSetBuilder[T]{
136 order: order,
Colin Cross96c44122020-11-25 14:29:50 -0800137 }
138}
139
Colin Crossc85750b2022-04-21 12:50:51 -0700140// DirectSlice adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct
141// contents are to the right of any existing direct contents.
142func (b *DepSetBuilder[T]) DirectSlice(direct []T) *DepSetBuilder[T] {
143 b.direct = append(b.direct, direct...)
Colin Cross96c44122020-11-25 14:29:50 -0800144 return b
145}
146
Colin Crossc85750b2022-04-21 12:50:51 -0700147// Direct adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct
148// contents are to the right of any existing direct contents.
149func (b *DepSetBuilder[T]) Direct(direct ...T) *DepSetBuilder[T] {
150 b.direct = append(b.direct, direct...)
Colin Cross96c44122020-11-25 14:29:50 -0800151 return b
152}
153
154// Transitive adds transitive contents to the DepSet being built by a DepSetBuilder. Newly added
Colin Crossc85750b2022-04-21 12:50:51 -0700155// transitive contents are to the right of any existing transitive contents.
156func (b *DepSetBuilder[T]) Transitive(transitive ...*DepSet[T]) *DepSetBuilder[T] {
157 for _, t := range transitive {
158 if t.order != b.order {
159 panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s",
160 b.order, t.order))
161 }
162 }
163 b.transitive = append(b.transitive, transitive...)
Colin Cross96c44122020-11-25 14:29:50 -0800164 return b
165}
166
Colin Crossc85750b2022-04-21 12:50:51 -0700167// Returns the DepSet being built by this DepSetBuilder. The DepSetBuilder retains its contents
Colin Cross96c44122020-11-25 14:29:50 -0800168// for creating more depSets.
Colin Crossc85750b2022-04-21 12:50:51 -0700169func (b *DepSetBuilder[T]) Build() *DepSet[T] {
170 return NewDepSet(b.order, b.direct, b.transitive)
Colin Cross96c44122020-11-25 14:29:50 -0800171}
172
173// walk calls the visit method in depth-first order on a DepSet, preordered if d.preorder is set,
174// otherwise postordered.
Colin Crossc85750b2022-04-21 12:50:51 -0700175func (d *DepSet[T]) walk(visit func([]T)) {
176 visited := make(map[*DepSet[T]]bool)
Colin Cross96c44122020-11-25 14:29:50 -0800177
Colin Crossc85750b2022-04-21 12:50:51 -0700178 var dfs func(d *DepSet[T])
179 dfs = func(d *DepSet[T]) {
Colin Cross96c44122020-11-25 14:29:50 -0800180 visited[d] = true
181 if d.preorder {
182 visit(d.direct)
183 }
184 for _, dep := range d.transitive {
185 if !visited[dep] {
186 dfs(dep)
187 }
188 }
189
190 if !d.preorder {
191 visit(d.direct)
192 }
193 }
194
195 dfs(d)
196}
197
Colin Crossc85750b2022-04-21 12:50:51 -0700198// ToList returns the DepSet flattened to a list. The order in the list is based on the order
199// of the DepSet. POSTORDER and PREORDER orders return a postordered or preordered left to right
Colin Cross96c44122020-11-25 14:29:50 -0800200// flattened list. TOPOLOGICAL returns a list that guarantees that elements of children are listed
201// after all of their parents (unless there are duplicate direct elements in the DepSet or any of
202// its transitive dependencies, in which case the ordering of the duplicated element is not
203// guaranteed).
Colin Crossc85750b2022-04-21 12:50:51 -0700204func (d *DepSet[T]) ToList() []T {
Colin Cross96c44122020-11-25 14:29:50 -0800205 if d == nil {
206 return nil
207 }
Colin Crossc85750b2022-04-21 12:50:51 -0700208 var list []T
209 d.walk(func(paths []T) {
210 list = append(list, paths...)
Colin Cross96c44122020-11-25 14:29:50 -0800211 })
Colin Cross48016d52023-06-27 09:45:26 -0700212 list = firstUniqueInPlace(list)
Colin Cross96c44122020-11-25 14:29:50 -0800213 if d.reverse {
Colin Crossb5e3f7d2023-07-06 15:37:53 -0700214 ReverseSliceInPlace(list)
Colin Cross96c44122020-11-25 14:29:50 -0800215 }
216 return list
217}