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_REFERENCE_SET_H_
6 #define COMPONENTS_ZUCCHINI_REFERENCE_SET_H_
7 
8 #include <stddef.h>
9 
10 #include <vector>
11 
12 #include "components/zucchini/image_utils.h"
13 
14 namespace zucchini {
15 
16 class TargetPool;
17 
18 // Container of distinct references of one type, along with traits, only used
19 // during patch generation.
20 class ReferenceSet {
21  public:
22   using const_iterator = std::vector<Reference>::const_iterator;
23 
24   // |traits| specifies the reference represented. |target_pool| specifies
25   // common targets shared by all reference represented, and mediates target
26   // translation between offsets and indexes.
27   ReferenceSet(const ReferenceTypeTraits& traits,
28                const TargetPool& target_pool);
29   ReferenceSet(const ReferenceSet&) = delete;
30   ReferenceSet(ReferenceSet&&);
31   ~ReferenceSet();
32 
33   // Either one of the initializers below should be called exactly once. These
34   // insert all references from |ref_reader/refs| into this class. The targets
35   // of these references must be in |target_pool_|.
36   void InitReferences(ReferenceReader&& ref_reader);
37   void InitReferences(const std::vector<Reference>& refs);
38 
references()39   const std::vector<Reference>& references() const { return references_; }
traits()40   const ReferenceTypeTraits& traits() const { return traits_; }
target_pool()41   const TargetPool& target_pool() const { return target_pool_; }
type_tag()42   TypeTag type_tag() const { return traits_.type_tag; }
pool_tag()43   PoolTag pool_tag() const { return traits_.pool_tag; }
width()44   offset_t width() const { return traits_.width; }
45 
46   // Looks up the Reference by an |offset| that it spans. |offset| is assumed to
47   // be valid, i.e., |offset| must be spanned by some Reference in
48   // |references_|.
49   Reference at(offset_t offset) const;
50 
size()51   size_t size() const { return references_.size(); }
begin()52   const_iterator begin() const { return references_.begin(); }
end()53   const_iterator end() const { return references_.end(); }
54 
55  private:
56   ReferenceTypeTraits traits_;
57   const TargetPool& target_pool_;
58   // List of distinct Reference instances sorted by location.
59   std::vector<Reference> references_;
60 };
61 
62 }  // namespace zucchini
63 
64 #endif  // COMPONENTS_ZUCCHINI_REFERENCE_SET_H_
65