Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 18 | "bytes" |
| 19 | "encoding/gob" |
| 20 | "errors" |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 21 | "fmt" |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 24 | // DepSet is designed to be conceptually compatible with Bazel's depsets: |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 25 | // https://docs.bazel.build/versions/master/skylark/depsets.html |
| 26 | |
| 27 | type DepSetOrder int |
| 28 | |
| 29 | const ( |
| 30 | PREORDER DepSetOrder = iota |
| 31 | POSTORDER |
| 32 | TOPOLOGICAL |
| 33 | ) |
| 34 | |
| 35 | func (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 Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 48 | type 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 Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 53 | // |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 54 | // A DepSet has an order that will be used to walk the DAG when ToList() is called. The order |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 55 | // 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 Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 58 | // elements in the DepSet or any of its transitive dependencies, in which case the ordering of the |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 59 | // duplicated element is not guaranteed). |
| 60 | // |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 61 | // 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. |
| 63 | type DepSet[T depSettableType] struct { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 64 | preorder bool |
| 65 | reverse bool |
| 66 | order DepSetOrder |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 67 | direct []T |
| 68 | transitive []*DepSet[T] |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 71 | func (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 | |
| 83 | func (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 Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 95 | // NewDepSet returns an immutable DepSet with the given order, direct and transitive contents. |
| 96 | func 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 Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 106 | 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 Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 109 | directCopy = ReverseSlice(direct) |
| 110 | transitiveCopy = ReverseSlice(transitive) |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 111 | } else { |
| 112 | directCopy = append([]T(nil), direct...) |
| 113 | transitiveCopy = append([]*DepSet[T](nil), transitive...) |
| 114 | } |
| 115 | |
| 116 | return &DepSet[T]{ |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 117 | preorder: order == PREORDER, |
| 118 | reverse: order == TOPOLOGICAL, |
| 119 | order: order, |
| 120 | direct: directCopy, |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 121 | transitive: transitiveCopy, |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 125 | // DepSetBuilder is used to create an immutable DepSet. |
| 126 | type DepSetBuilder[T depSettableType] struct { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 127 | order DepSetOrder |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 128 | direct []T |
| 129 | transitive []*DepSet[T] |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 132 | // 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. |
| 134 | func NewDepSetBuilder[T depSettableType](order DepSetOrder) *DepSetBuilder[T] { |
| 135 | return &DepSetBuilder[T]{ |
| 136 | order: order, |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 140 | // 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. |
| 142 | func (b *DepSetBuilder[T]) DirectSlice(direct []T) *DepSetBuilder[T] { |
| 143 | b.direct = append(b.direct, direct...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 144 | return b |
| 145 | } |
| 146 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 147 | // 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. |
| 149 | func (b *DepSetBuilder[T]) Direct(direct ...T) *DepSetBuilder[T] { |
| 150 | b.direct = append(b.direct, direct...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 151 | return b |
| 152 | } |
| 153 | |
| 154 | // Transitive adds transitive contents to the DepSet being built by a DepSetBuilder. Newly added |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 155 | // transitive contents are to the right of any existing transitive contents. |
| 156 | func (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 Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 164 | return b |
| 165 | } |
| 166 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 167 | // Returns the DepSet being built by this DepSetBuilder. The DepSetBuilder retains its contents |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 168 | // for creating more depSets. |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 169 | func (b *DepSetBuilder[T]) Build() *DepSet[T] { |
| 170 | return NewDepSet(b.order, b.direct, b.transitive) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // walk calls the visit method in depth-first order on a DepSet, preordered if d.preorder is set, |
| 174 | // otherwise postordered. |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 175 | func (d *DepSet[T]) walk(visit func([]T)) { |
| 176 | visited := make(map[*DepSet[T]]bool) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 177 | |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 178 | var dfs func(d *DepSet[T]) |
| 179 | dfs = func(d *DepSet[T]) { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 180 | 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 Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 198 | // 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 Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 200 | // 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 Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 204 | func (d *DepSet[T]) ToList() []T { |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 205 | if d == nil { |
| 206 | return nil |
| 207 | } |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 208 | var list []T |
| 209 | d.walk(func(paths []T) { |
| 210 | list = append(list, paths...) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 211 | }) |
Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 212 | list = firstUniqueInPlace(list) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 213 | if d.reverse { |
Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 214 | ReverseSliceInPlace(list) |
Colin Cross | 96c4412 | 2020-11-25 14:29:50 -0800 | [diff] [blame] | 215 | } |
| 216 | return list |
| 217 | } |