xref: /aosp_15_r20/external/zucchini/targets_affinity.h (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
1*a03ca8b9SKrzysztof Kosiński // Copyright 2017 The Chromium Authors. All rights reserved.
2*a03ca8b9SKrzysztof Kosiński // Use of this source code is governed by a BSD-style license that can be
3*a03ca8b9SKrzysztof Kosiński // found in the LICENSE file.
4*a03ca8b9SKrzysztof Kosiński 
5*a03ca8b9SKrzysztof Kosiński #ifndef COMPONENTS_ZUCCHINI_TARGETS_AFFINITY_H_
6*a03ca8b9SKrzysztof Kosiński #define COMPONENTS_ZUCCHINI_TARGETS_AFFINITY_H_
7*a03ca8b9SKrzysztof Kosiński 
8*a03ca8b9SKrzysztof Kosiński #include <stddef.h>
9*a03ca8b9SKrzysztof Kosiński #include <stdint.h>
10*a03ca8b9SKrzysztof Kosiński 
11*a03ca8b9SKrzysztof Kosiński #include <deque>
12*a03ca8b9SKrzysztof Kosiński #include <vector>
13*a03ca8b9SKrzysztof Kosiński 
14*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/image_utils.h"
15*a03ca8b9SKrzysztof Kosiński 
16*a03ca8b9SKrzysztof Kosiński namespace zucchini {
17*a03ca8b9SKrzysztof Kosiński 
18*a03ca8b9SKrzysztof Kosiński class EquivalenceMap;
19*a03ca8b9SKrzysztof Kosiński 
20*a03ca8b9SKrzysztof Kosiński // Computes and stores affinity between old and new targets for a single target
21*a03ca8b9SKrzysztof Kosiński // pool. This is only used during patch generation.
22*a03ca8b9SKrzysztof Kosiński class TargetsAffinity {
23*a03ca8b9SKrzysztof Kosiński  public:
24*a03ca8b9SKrzysztof Kosiński   TargetsAffinity();
25*a03ca8b9SKrzysztof Kosiński   TargetsAffinity(const TargetsAffinity&) = delete;
26*a03ca8b9SKrzysztof Kosiński   const TargetsAffinity& operator=(const TargetsAffinity&) = delete;
27*a03ca8b9SKrzysztof Kosiński   ~TargetsAffinity();
28*a03ca8b9SKrzysztof Kosiński 
29*a03ca8b9SKrzysztof Kosiński   // Infers affinity between |old_targets| and |new_targets| using similarities
30*a03ca8b9SKrzysztof Kosiński   // described by |equivalence_map|, and updates internal state for retrieval of
31*a03ca8b9SKrzysztof Kosiński   // affinity scores. Both |old_targets| and |new_targets| are targets in the
32*a03ca8b9SKrzysztof Kosiński   // same pool and are sorted in ascending order.
33*a03ca8b9SKrzysztof Kosiński   void InferFromSimilarities(const EquivalenceMap& equivalence_map,
34*a03ca8b9SKrzysztof Kosiński                              const std::deque<offset_t>& old_targets,
35*a03ca8b9SKrzysztof Kosiński                              const std::deque<offset_t>& new_targets);
36*a03ca8b9SKrzysztof Kosiński 
37*a03ca8b9SKrzysztof Kosiński   // Assigns labels to targets based on associations previously inferred, using
38*a03ca8b9SKrzysztof Kosiński   // |min_affinity| to reject associations with weak |affinity|. Label 0 is
39*a03ca8b9SKrzysztof Kosiński   // assigned to unassociated targets. Labels for old targets are written to
40*a03ca8b9SKrzysztof Kosiński   // |old_labels| and labels for new targets are written to |new_labels|.
41*a03ca8b9SKrzysztof Kosiński   // Returns the upper bound on assigned labels (>= 1 since 0 is used).
42*a03ca8b9SKrzysztof Kosiński   uint32_t AssignLabels(double min_affinity,
43*a03ca8b9SKrzysztof Kosiński                         std::vector<uint32_t>* old_labels,
44*a03ca8b9SKrzysztof Kosiński                         std::vector<uint32_t>* new_labels);
45*a03ca8b9SKrzysztof Kosiński 
46*a03ca8b9SKrzysztof Kosiński   // Returns the affinity score between targets identified by |old_key| and
47*a03ca8b9SKrzysztof Kosiński   // |new_keys|. Affinity > 0 means an association is likely, < 0 means
48*a03ca8b9SKrzysztof Kosiński   // incompatible association, and 0 means neither targets have been associated.
49*a03ca8b9SKrzysztof Kosiński   double AffinityBetween(key_t old_key, key_t new_key) const;
50*a03ca8b9SKrzysztof Kosiński 
51*a03ca8b9SKrzysztof Kosiński  private:
52*a03ca8b9SKrzysztof Kosiński   struct Association {
53*a03ca8b9SKrzysztof Kosiński     key_t other = 0;
54*a03ca8b9SKrzysztof Kosiński     double affinity = 0.0;
55*a03ca8b9SKrzysztof Kosiński   };
56*a03ca8b9SKrzysztof Kosiński 
57*a03ca8b9SKrzysztof Kosiński   // Forward and backward associations between old and new targets. For each
58*a03ca8b9SKrzysztof Kosiński   // Association element, if |affinity == 0.0| then no association is defined
59*a03ca8b9SKrzysztof Kosiński   // (and |other| is meaningless|. Otherwise |affinity > 0.0|, and the
60*a03ca8b9SKrzysztof Kosiński   // association between |old_labels[old_key]| and |new_labels[new_key]| is
61*a03ca8b9SKrzysztof Kosiński   // represented by:
62*a03ca8b9SKrzysztof Kosiński   //   forward_association_[old_key].other == new_key;
63*a03ca8b9SKrzysztof Kosiński   //   backward_association_[new_key].other == old_key;
64*a03ca8b9SKrzysztof Kosiński   //   forward_association_[old_key].affinity ==
65*a03ca8b9SKrzysztof Kosiński   //       backward_association_[new_key].affinity;
66*a03ca8b9SKrzysztof Kosiński   // The two lists contain the same information, but having both enables quick
67*a03ca8b9SKrzysztof Kosiński   // lookup, given |old_key| or |new_key|.
68*a03ca8b9SKrzysztof Kosiński   std::vector<Association> forward_association_;
69*a03ca8b9SKrzysztof Kosiński   std::vector<Association> backward_association_;
70*a03ca8b9SKrzysztof Kosiński };
71*a03ca8b9SKrzysztof Kosiński 
72*a03ca8b9SKrzysztof Kosiński }  // namespace zucchini
73*a03ca8b9SKrzysztof Kosiński 
74*a03ca8b9SKrzysztof Kosiński #endif  // COMPONENTS_ZUCCHINI_TARGETS_AFFINITY_H_
75