blob: a3e722a8d3a8f2ef919356481de0b5c67175d6bf [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#ifndef CHROME_BROWSER_ASH_FILEAPI_FILE_ACCUMULATOR_H_
6#define CHROME_BROWSER_ASH_FILEAPI_FILE_ACCUMULATOR_H_
7
8#include <vector>
9
10#include "chrome/browser/ash/fileapi/recent_file.h"
11
12namespace ash {
13
14// Accumulator of files located via search operation. The accumulator has a
15// limited capacity. Files are sorted based on the RecentFileComparator class.
16// If one adds n > max_capacity files, (n - max_capacity) files are discarded
17// based on the order given by the comparator class.
18//
19// Typical use consists of adding a number of files, then Get'ing the content.
20// Once the content is Get'ed, the accumulator is sealed, meaning no new files
21// may be added to the accumulator. To unseal the accumulator, call the Clear
22// method on it, which also removes all stored files.
23//
24// FilesAccumulator<RecentFilesComparator> acc(100);
25// acc.Add(recent_file_1);
26// ..
27// acc.Add(recent_file_n);
28// std::vector<RecentFile> content = acc.Get();
29class FileAccumulator {
30 public:
31 // Creates an accumulator with the given capacity. The capacity
32 // limits the maximum number of files that can be added via the Add method.
33 explicit FileAccumulator(size_t max_capacity);
Bo Majewski65e328462024-02-12 06:15:0434 FileAccumulator(FileAccumulator&& accumulator);
Bo Majewski32011912023-11-07 10:04:1735
36 ~FileAccumulator();
37
38 // Adds a single file to the accumulator. The return value indicates if the
39 // file has been added or not. A file may not be added if the accumulator is
40 // sealed.
41 bool Add(const RecentFile& file);
42
43 // Returns the content of this accumulator. The first time this method is
44 // called it "seals" this accumulator, re-orders the files from a heap to a
45 // simple vector. This method can be called multiple times.
46 const std::vector<RecentFile>& Get();
47
48 // Clears the accumulator and unseals it.
49 void Clear();
50
51 // Returns the maximum number of recent files that are can be stored in this
52 // cache.
53 size_t max_capacity() const { return max_capacity_; }
54
55 private:
56 // The maximum number of recent files kept in this cache.
57 const size_t max_capacity_;
58 // Whether or not the accumulator is in the sealed state.
59 bool sealed_;
60 // The content of the cache, kept sorted by the modified time.
61 std::vector<RecentFile> files_;
62};
63
64} // namespace ash
65
shaochenguang77a7a02b2023-11-22 19:33:5666#endif // CHROME_BROWSER_ASH_FILEAPI_FILE_ACCUMULATOR_H_