blob: 29e5cf4322524d8350111424eef250ab0a245485 [file] [log] [blame]
Bo Majewski32011912023-11-07 10:04:171// 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
9namespace ash {
10
11FileAccumulator::FileAccumulator(size_t max_capacity)
12 : max_capacity_(max_capacity), sealed_(false) {}
Bo Majewski65e328462024-02-12 06:15:0413FileAccumulator::FileAccumulator(FileAccumulator&& accumulator)
14 : max_capacity_(accumulator.max_capacity_),
15 sealed_(accumulator.sealed_),
16 files_(std::move(accumulator.files_)) {}
Bo Majewski32011912023-11-07 10:04:1717
18FileAccumulator::~FileAccumulator() = default;
19
20bool 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
35const 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
43void FileAccumulator::Clear() {
44 files_.clear();
45 sealed_ = false;
46}
47
48} // namespace ash