blob: e91d02ca6c37563b926a0692883fb8ebf935f7cf [file] [log] [blame]
Tao Bao8f237572017-03-26 13:36:49 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stddef.h>
20
21#include <string>
Tao Baobf5b77d2017-03-30 16:57:29 -070022#include <utility>
Tao Bao8f237572017-03-26 13:36:49 -070023#include <vector>
24
Tao Baobf5b77d2017-03-30 16:57:29 -070025using Range = std::pair<size_t, size_t>;
26
27class RangeSet {
28 public:
29 RangeSet() : blocks_(0) {}
30
Tao Bao45685822017-10-13 14:54:12 -070031 explicit RangeSet(std::vector<Range>&& pairs);
Tao Baobf5b77d2017-03-30 16:57:29 -070032
Tao Bao67983152017-11-04 00:08:08 -070033 // Parses the given string into a RangeSet. Returns the parsed RangeSet, or an empty RangeSet on
34 // errors.
Tao Bao45685822017-10-13 14:54:12 -070035 static RangeSet Parse(const std::string& range_text);
Tao Baobf5b77d2017-03-30 16:57:29 -070036
Tao Bao67983152017-11-04 00:08:08 -070037 // Appends the given Range to the current RangeSet.
38 bool PushBack(Range range);
39
40 // Clears all the ranges from the RangeSet.
41 void Clear();
42
Tao Bao45685822017-10-13 14:54:12 -070043 std::string ToString() const;
Tianjie Xub9e7fc72017-07-26 16:41:24 -070044
Tao Bao67983152017-11-04 00:08:08 -070045 // Gets the block number for the i-th (starting from 0) block in the RangeSet.
Tao Bao45685822017-10-13 14:54:12 -070046 size_t GetBlockNumber(size_t idx) const;
Tao Bao8f237572017-03-26 13:36:49 -070047
Tao Bao67983152017-11-04 00:08:08 -070048 // Returns whether the current RangeSet overlaps with other. RangeSet has half-closed half-open
49 // bounds. For example, "3,5" contains blocks 3 and 4. So "3,5" and "5,7" are not overlapped.
Tao Bao45685822017-10-13 14:54:12 -070050 bool Overlaps(const RangeSet& other) const;
Tao Bao8f237572017-03-26 13:36:49 -070051
Tao Bao160514b2017-11-04 00:08:08 -070052 // Returns a vector of RangeSets that contain the same set of blocks represented by the current
53 // RangeSet. The RangeSets in the vector contain similar number of blocks, with a maximum delta
54 // of 1-block between any two of them. For example, 14 blocks would be split into 4 + 4 + 3 + 3,
55 // as opposed to 4 + 4 + 4 + 2. If the total number of blocks (T) is less than groups, it
56 // returns a vector of T 1-block RangeSets. Otherwise the number of the returned RangeSets must
57 // equal to groups. The current RangeSet remains intact after the split.
58 std::vector<RangeSet> Split(size_t groups) const;
59
Tao Bao67983152017-11-04 00:08:08 -070060 // Returns the number of Range's in this RangeSet.
Tao Baobf5b77d2017-03-30 16:57:29 -070061 size_t size() const {
62 return ranges_.size();
Tao Bao8f237572017-03-26 13:36:49 -070063 }
Tao Baobf5b77d2017-03-30 16:57:29 -070064
Tao Bao67983152017-11-04 00:08:08 -070065 // Returns the total number of blocks in this RangeSet.
Tao Baobf5b77d2017-03-30 16:57:29 -070066 size_t blocks() const {
67 return blocks_;
68 }
69
Tao Baobf5b77d2017-03-30 16:57:29 -070070 std::vector<Range>::const_iterator cbegin() const {
71 return ranges_.cbegin();
72 }
73
74 std::vector<Range>::const_iterator cend() const {
75 return ranges_.cend();
76 }
77
Tao Bao67983152017-11-04 00:08:08 -070078 std::vector<Range>::iterator begin() {
79 return ranges_.begin();
80 }
81
82 std::vector<Range>::iterator end() {
83 return ranges_.end();
84 }
85
Tao Baobf5b77d2017-03-30 16:57:29 -070086 std::vector<Range>::const_iterator begin() const {
Tao Bao67983152017-11-04 00:08:08 -070087 return ranges_.begin();
Tao Baobf5b77d2017-03-30 16:57:29 -070088 }
89
90 std::vector<Range>::const_iterator end() const {
Tao Bao67983152017-11-04 00:08:08 -070091 return ranges_.end();
Tao Baobf5b77d2017-03-30 16:57:29 -070092 }
93
94 // Reverse const iterators for MoveRange().
95 std::vector<Range>::const_reverse_iterator crbegin() const {
96 return ranges_.crbegin();
97 }
98
99 std::vector<Range>::const_reverse_iterator crend() const {
100 return ranges_.crend();
101 }
102
Tao Bao67983152017-11-04 00:08:08 -0700103 // Returns whether the RangeSet is valid (i.e. non-empty).
104 explicit operator bool() const {
105 return !ranges_.empty();
106 }
107
Tao Baobf5b77d2017-03-30 16:57:29 -0700108 const Range& operator[](size_t i) const {
109 return ranges_[i];
110 }
111
112 bool operator==(const RangeSet& other) const {
113 // The orders of Range's matter. "4,1,5,8,10" != "4,8,10,1,5".
114 return (ranges_ == other.ranges_);
115 }
116
117 bool operator!=(const RangeSet& other) const {
118 return ranges_ != other.ranges_;
119 }
120
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700121 protected:
Tao Baobf5b77d2017-03-30 16:57:29 -0700122 // Actual limit for each value and the total number are both INT_MAX.
123 std::vector<Range> ranges_;
124 size_t blocks_;
Tao Bao8f237572017-03-26 13:36:49 -0700125};
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700126
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700127// The class is a sorted version of a RangeSet; and it's useful in imgdiff to split the input
128// files when we're handling large zip files. Specifically, we can treat the input file as a
129// continuous RangeSet (i.e. RangeSet("0-99") for a 100 blocks file); and break it down into
130// several smaller chunks based on the zip entries.
131
132// For example, [source: 0-99] can be split into
133// [split_src1: 10-29]; [split_src2: 40-49, 60-69]; [split_src3: 70-89]
134// Here "10-29" simply means block 10th to block 29th with respect to the original input file.
135// Also, note that the split sources should be mutual exclusive, but they don't need to cover
136// every block in the original source.
137class SortedRangeSet : public RangeSet {
138 public:
Tao Bao67983152017-11-04 00:08:08 -0700139 // The block size when working with offset and file length.
140 static constexpr size_t kBlockSize = 4096;
141
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700142 SortedRangeSet() {}
143
144 // Ranges in the the set should be mutually exclusive; and they're sorted by the start block.
Tao Bao45685822017-10-13 14:54:12 -0700145 explicit SortedRangeSet(std::vector<Range>&& pairs);
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700146
Tao Bao45685822017-10-13 14:54:12 -0700147 void Insert(const Range& to_insert);
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700148
149 // Insert the input SortedRangeSet; keep the ranges sorted and merge the overlap ranges.
Tao Bao45685822017-10-13 14:54:12 -0700150 void Insert(const SortedRangeSet& rs);
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700151
152 // Compute the block range the file occupies, and insert that range.
Tao Bao45685822017-10-13 14:54:12 -0700153 void Insert(size_t start, size_t len);
154
Tao Bao45685822017-10-13 14:54:12 -0700155 using RangeSet::Overlaps;
156
157 bool Overlaps(size_t start, size_t len) const;
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700158
159 // Given an offset of the file, checks if the corresponding block (by considering the file as
160 // 0-based continuous block ranges) is covered by the SortedRangeSet. If so, returns the offset
161 // within this SortedRangeSet.
162 //
163 // For example, the 4106-th byte of a file is from block 1, assuming a block size of 4096-byte.
164 // The mapped offset within a SortedRangeSet("1-9 15-19") is 10.
165 //
166 // An offset of 65546 falls into the 16-th block in a file. Block 16 is contained as the 10-th
167 // item in SortedRangeSet("1-9 15-19"). So its data can be found at offset 40970 (i.e. 4096 * 10
168 // + 10) in a range represented by this SortedRangeSet.
Tao Bao45685822017-10-13 14:54:12 -0700169 size_t GetOffsetInRangeSet(size_t old_offset) const;
170};