1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_ZUCCHINI_TARGET_POOL_H_
6 #define COMPONENTS_ZUCCHINI_TARGET_POOL_H_
7 
8 #include <stddef.h>
9 
10 #include <deque>
11 #include <vector>
12 
13 #include "components/zucchini/image_utils.h"
14 #include "components/zucchini/patch_reader.h"
15 
16 namespace zucchini {
17 
18 class OffsetMapper;
19 class TargetSource;
20 
21 // Ordered container of distinct targets that have the same semantics, along
22 // with a list of associated reference types, only used during patch generation.
23 class TargetPool {
24  public:
25   using const_iterator = std::deque<offset_t>::const_iterator;
26 
27   TargetPool();
28   // Initializes the object with given sorted and unique |targets|.
29   explicit TargetPool(std::deque<offset_t>&& targets);
30   TargetPool(TargetPool&&);
31   TargetPool(const TargetPool&);
32   ~TargetPool();
33 
34   // Insert new targets from various sources. These invalidate all previous key
35   // lookups.
36   // - From a list of targets, useful for adding extra targets in Zucchini-gen:
37   void InsertTargets(const std::vector<offset_t>& targets);
38   // - From TargetSource, useful for adding extra targets in Zucchini-apply:
39   void InsertTargets(TargetSource* targets);
40   // - From list of References, useful for listing targets in Zucchini-gen:
41   void InsertTargets(const std::vector<Reference>& references);
42   // - From ReferenceReader, useful for listing targets in Zucchini-apply:
43   void InsertTargets(ReferenceReader&& references);
44 
45   // Adds |type| as a reference type associated with the pool of targets.
AddType(TypeTag type)46   void AddType(TypeTag type) { types_.push_back(type); }
47 
48   // Returns a canonical key associated with a valid target at |offset|.
49   key_t KeyForOffset(offset_t offset) const;
50 
51   // Returns a canonical key associated with the target nearest to |offset|.
52   key_t KeyForNearestOffset(offset_t offset) const;
53 
54   // Returns the target for a |key|, which is assumed to be valid and held by
55   // this class.
OffsetForKey(key_t key)56   offset_t OffsetForKey(key_t key) const { return targets_[key]; }
57 
58   // Returns whether a particular key is valid.
KeyIsValid(key_t key)59   bool KeyIsValid(key_t key) const { return key < targets_.size(); }
60 
61   // Uses |offset_mapper| to transform "old" |targets_| to "new" |targets_|,
62   // resulting in sorted and unique targets.
63   void FilterAndProject(const OffsetMapper& offset_mapper);
64 
65   // Accessors for testing.
targets()66   const std::deque<offset_t>& targets() const { return targets_; }
types()67   const std::vector<TypeTag>& types() const { return types_; }
68 
69   // Returns the number of targets.
size()70   size_t size() const { return targets_.size(); }
begin()71   const_iterator begin() const { return targets_.cbegin(); }
end()72   const_iterator end() const { return targets_.cend(); }
73 
74  private:
75   std::vector<TypeTag> types_;     // Enumerates type_tag for this pool.
76   std::deque<offset_t> targets_;   // Targets for pool in ascending order.
77 };
78 
79 }  // namespace zucchini
80 
81 #endif  // COMPONENTS_ZUCCHINI_TARGET_POOL_H_
82