Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++
1.0 KB
Raw Blame
/*
 *  SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef KISROTATINGCACHE_H
#define KISROTATINGCACHE_H

#include <optional>
#include <utility>

#include <boost/container/static_vector.hpp>


/**
 */
template <typename Key, typename Value, size_t CacheSize>
class KisRotatingCache
{
public:
    using CacheItem = std::pair<Key, Value>;
public:
    void push(Key key, Value value) {
        if (m_storage.size() == m_storage.max_size()) {
            m_storage.erase(m_storage.begin());
        }
        m_storage.emplace_back(std::move(key), std::move(value));
    }

    inline std::optional<Value> find(const Key &key) const {
        for (auto it = m_storage.begin(); it != m_storage.end(); ++it) {
            if (it->first == key) {
                return it->second;
            }
        }
        return std::nullopt;
    }

    void clear() {
        m_storage.clear();
    }

private:
    boost::container::static_vector<CacheItem, CacheSize> m_storage;
};

#endif // KISROTATINGCACHE_H