1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#ifndef DISTRIBUTEDDATAMGR_PASTEBOARD_DEDUPLICATE_MEMORY_H
17bc03f14fSopenharmony_ci#define DISTRIBUTEDDATAMGR_PASTEBOARD_DEDUPLICATE_MEMORY_H
18bc03f14fSopenharmony_ci
19bc03f14fSopenharmony_ci#include <cstdint>
20bc03f14fSopenharmony_ci#include <list>
21bc03f14fSopenharmony_ci
22bc03f14fSopenharmony_cinamespace OHOS {
23bc03f14fSopenharmony_cinamespace MiscServices {
24bc03f14fSopenharmony_ci
25bc03f14fSopenharmony_citemplate <typename T>
26bc03f14fSopenharmony_ciclass DeduplicateMemory {
27bc03f14fSopenharmony_cipublic:
28bc03f14fSopenharmony_ci    explicit DeduplicateMemory(int64_t expirationMilliSeconds);
29bc03f14fSopenharmony_ci    ~DeduplicateMemory();
30bc03f14fSopenharmony_ci    bool IsDuplicate(const T &data);
31bc03f14fSopenharmony_ci
32bc03f14fSopenharmony_ciprivate:
33bc03f14fSopenharmony_ci    int64_t GetTimestamp();
34bc03f14fSopenharmony_ci    void ClearExpiration();
35bc03f14fSopenharmony_ci    bool FindExist(const T &data);
36bc03f14fSopenharmony_ci
37bc03f14fSopenharmony_ci    struct MemoryIdentity {
38bc03f14fSopenharmony_ci        int64_t timestamp;
39bc03f14fSopenharmony_ci        const T data;
40bc03f14fSopenharmony_ci    };
41bc03f14fSopenharmony_ci
42bc03f14fSopenharmony_ci    std::list<MemoryIdentity> memory_;
43bc03f14fSopenharmony_ci    int64_t expirationMS_;
44bc03f14fSopenharmony_ci};
45bc03f14fSopenharmony_ci
46bc03f14fSopenharmony_citemplate <typename T>
47bc03f14fSopenharmony_ciDeduplicateMemory<T>::DeduplicateMemory(int64_t expirationMilliSeconds) : expirationMS_(expirationMilliSeconds)
48bc03f14fSopenharmony_ci{
49bc03f14fSopenharmony_ci}
50bc03f14fSopenharmony_ci
51bc03f14fSopenharmony_citemplate <typename T>
52bc03f14fSopenharmony_ciDeduplicateMemory<T>::~DeduplicateMemory()
53bc03f14fSopenharmony_ci{
54bc03f14fSopenharmony_ci    memory_.clear();
55bc03f14fSopenharmony_ci}
56bc03f14fSopenharmony_ci
57bc03f14fSopenharmony_citemplate <typename T>
58bc03f14fSopenharmony_cibool DeduplicateMemory<T>::IsDuplicate(const T &data)
59bc03f14fSopenharmony_ci{
60bc03f14fSopenharmony_ci    ClearExpiration();
61bc03f14fSopenharmony_ci    if (FindExist(data)) {
62bc03f14fSopenharmony_ci        return true;
63bc03f14fSopenharmony_ci    }
64bc03f14fSopenharmony_ci    int64_t timestamp = GetTimestamp();
65bc03f14fSopenharmony_ci    memory_.push_back(MemoryIdentity({.timestamp = timestamp, .data = data}));
66bc03f14fSopenharmony_ci    return false;
67bc03f14fSopenharmony_ci}
68bc03f14fSopenharmony_ci
69bc03f14fSopenharmony_citemplate <typename T>
70bc03f14fSopenharmony_cibool DeduplicateMemory<T>::FindExist(const T &data)
71bc03f14fSopenharmony_ci{
72bc03f14fSopenharmony_ci    for (const MemoryIdentity &item : memory_) {
73bc03f14fSopenharmony_ci        if (item.data == data) {
74bc03f14fSopenharmony_ci            return true;
75bc03f14fSopenharmony_ci        }
76bc03f14fSopenharmony_ci    }
77bc03f14fSopenharmony_ci    return false;
78bc03f14fSopenharmony_ci}
79bc03f14fSopenharmony_ci
80bc03f14fSopenharmony_citemplate <typename T>
81bc03f14fSopenharmony_ciint64_t DeduplicateMemory<T>::GetTimestamp()
82bc03f14fSopenharmony_ci{
83bc03f14fSopenharmony_ci    return std::chrono::duration_cast<std::chrono::milliseconds>(
84bc03f14fSopenharmony_ci        std::chrono::steady_clock::now().time_since_epoch()).count();
85bc03f14fSopenharmony_ci}
86bc03f14fSopenharmony_ci
87bc03f14fSopenharmony_citemplate <typename T>
88bc03f14fSopenharmony_civoid DeduplicateMemory<T>::ClearExpiration()
89bc03f14fSopenharmony_ci{
90bc03f14fSopenharmony_ci    int64_t timestamp = GetTimestamp();
91bc03f14fSopenharmony_ci    if (timestamp < expirationMS_) {
92bc03f14fSopenharmony_ci        return;
93bc03f14fSopenharmony_ci    }
94bc03f14fSopenharmony_ci    int64_t expirationTimestamp = timestamp - expirationMS_;
95bc03f14fSopenharmony_ci    memory_.remove_if([expirationTimestamp](const MemoryIdentity &identity) {
96bc03f14fSopenharmony_ci        return expirationTimestamp > identity.timestamp;
97bc03f14fSopenharmony_ci    });
98bc03f14fSopenharmony_ci}
99bc03f14fSopenharmony_ci} // namespace MiscServices
100bc03f14fSopenharmony_ci} // namespace OHOS
101bc03f14fSopenharmony_ci
102bc03f14fSopenharmony_ci#endif // DISTRIBUTEDDATAMGR_PASTEBOARD_DEDUPLICATE_MEMORY_H
103