Bo Majewski | 3201191 | 2023-11-07 10:04:17 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/ash/fileapi/file_accumulator.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | namespace ash { |
| 10 | |
| 11 | FileAccumulator::FileAccumulator(size_t max_capacity) |
| 12 | : max_capacity_(max_capacity), sealed_(false) {} |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 13 | FileAccumulator::FileAccumulator(FileAccumulator&& accumulator) |
| 14 | : max_capacity_(accumulator.max_capacity_), |
| 15 | sealed_(accumulator.sealed_), |
| 16 | files_(std::move(accumulator.files_)) {} |
Bo Majewski | 3201191 | 2023-11-07 10:04:17 | [diff] [blame] | 17 | |
| 18 | FileAccumulator::~FileAccumulator() = default; |
| 19 | |
| 20 | bool FileAccumulator::Add(const RecentFile& file) { |
| 21 | if (sealed_) { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | files_.emplace_back(file); |
| 26 | std::push_heap(files_.begin(), files_.end(), RecentFileComparator()); |
| 27 | if (files_.size() > max_capacity_) { |
| 28 | std::pop_heap(files_.begin(), files_.end(), RecentFileComparator()); |
| 29 | files_.pop_back(); |
| 30 | } |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | const std::vector<RecentFile>& FileAccumulator::Get() { |
| 36 | if (!sealed_) { |
| 37 | sealed_ = true; |
| 38 | std::sort_heap(files_.begin(), files_.end(), RecentFileComparator()); |
| 39 | } |
| 40 | return files_; |
| 41 | } |
| 42 | |
| 43 | void FileAccumulator::Clear() { |
| 44 | files_.clear(); |
| 45 | sealed_ = false; |
| 46 | } |
| 47 | |
| 48 | } // namespace ash |