summaryrefslogtreecommitdiffstats
path: root/otautil/rangeset.cpp
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2017-11-12 09:35:54 +0100
committerandroid-build-team Robot <android-build-team-robot@google.com>2017-11-12 09:35:54 +0100
commitb2a5691d57886f207ca049853158ecb1d1f2980f (patch)
treedb51eae55903915fdee4cba91e684a74d0451f9f /otautil/rangeset.cpp
parentSnap for 4439972 from deca0cb59c0fbac2a9ecca2fb7db7d324a43b81c to pi-release (diff)
parentMerge "applypatch: Change the patch parameter to const Value& in Apply{BSDiff,Image}Patch." am: de07371b03 am: e2296b7a25 (diff)
downloadandroid_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.tar
android_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.tar.gz
android_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.tar.bz2
android_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.tar.lz
android_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.tar.xz
android_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.tar.zst
android_bootable_recovery-b2a5691d57886f207ca049853158ecb1d1f2980f.zip
Diffstat (limited to '')
-rw-r--r--otautil/rangeset.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/otautil/rangeset.cpp b/otautil/rangeset.cpp
index 532cba4a8..96955b9d0 100644
--- a/otautil/rangeset.cpp
+++ b/otautil/rangeset.cpp
@@ -103,6 +103,46 @@ void RangeSet::Clear() {
blocks_ = 0;
}
+std::vector<RangeSet> RangeSet::Split(size_t groups) const {
+ if (ranges_.empty() || groups == 0) return {};
+
+ if (blocks_ < groups) {
+ groups = blocks_;
+ }
+
+ // Evenly distribute blocks, with the first few groups possibly containing one more.
+ size_t mean = blocks_ / groups;
+ std::vector<size_t> blocks_per_group(groups, mean);
+ std::fill_n(blocks_per_group.begin(), blocks_ % groups, mean + 1);
+
+ std::vector<RangeSet> result;
+
+ // Forward iterate Ranges and fill up each group with the desired number of blocks.
+ auto it = ranges_.cbegin();
+ Range range = *it;
+ for (const auto& blocks : blocks_per_group) {
+ RangeSet buffer;
+ size_t needed = blocks;
+ while (needed > 0) {
+ size_t range_blocks = range.second - range.first;
+ if (range_blocks > needed) {
+ // Split the current range and don't advance the iterator.
+ buffer.PushBack({ range.first, range.first + needed });
+ range.first = range.first + needed;
+ break;
+ }
+ buffer.PushBack(range);
+ it++;
+ if (it != ranges_.cend()) {
+ range = *it;
+ }
+ needed -= range_blocks;
+ }
+ result.push_back(std::move(buffer));
+ }
+ return result;
+}
+
std::string RangeSet::ToString() const {
if (ranges_.empty()) {
return "";