blob: 07940f0cab4797cead7edb098599b69e94f20dcb [file] [log] [blame]
Samuel Huang06f1ae92018-03-13 18:19:34 +00001// 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
14namespace zucchini {
15
16class TargetPool;
17
Etienne Pierre-doray8f9a9e72018-08-13 18:49:00 +000018// Container of distinct references of one type, along with traits, only used
19// during patch generation.
Samuel Huang06f1ae92018-03-13 18:19:34 +000020class ReferenceSet {
21 public:
Etienne Pierre-doray8f9a9e72018-08-13 18:49:00 +000022 using const_iterator = std::vector<Reference>::const_iterator;
Samuel Huang06f1ae92018-03-13 18:19:34 +000023
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
Etienne Pierre-doray8f9a9e72018-08-13 18:49:00 +000039 const std::vector<Reference>& references() const { return references_; }
Samuel Huang06f1ae92018-03-13 18:19:34 +000040 const ReferenceTypeTraits& traits() const { return traits_; }
41 const TargetPool& target_pool() const { return target_pool_; }
42 TypeTag type_tag() const { return traits_.type_tag; }
43 PoolTag pool_tag() const { return traits_.pool_tag; }
44 offset_t width() const { return traits_.width; }
45
Etienne Pierre-doray8f9a9e72018-08-13 18:49:00 +000046 // 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;
Samuel Huang06f1ae92018-03-13 18:19:34 +000050
51 size_t size() const { return references_.size(); }
52 const_iterator begin() const { return references_.begin(); }
53 const_iterator end() const { return references_.end(); }
54
55 private:
56 ReferenceTypeTraits traits_;
57 const TargetPool& target_pool_;
Etienne Pierre-doray8f9a9e72018-08-13 18:49:00 +000058 // List of distinct Reference instances sorted by location.
59 std::vector<Reference> references_;
Samuel Huang06f1ae92018-03-13 18:19:34 +000060};
61
62} // namespace zucchini
63
64#endif // COMPONENTS_ZUCCHINI_REFERENCE_SET_H_