File indexing completed on 2024-06-23 05:06:55

0001 /*
0002     SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <algorithm>
0010 
0011 namespace Akonadi
0012 {
0013 namespace Internal
0014 {
0015 /**
0016  * Pool of implicitly shared values, use for optimizing memory use
0017  * when having a large amount of copies from a small set of different values.
0018  */
0019 template<typename T, template<typename> class Container>
0020 class SharedValuePool
0021 {
0022 public:
0023     /** Returns the shared value equal to @p value .*/
0024     T sharedValue(const T &value)
0025     {
0026         // for small pool sizes this is actually faster than using lower_bound and a sorted vector
0027         typename Container<T>::const_iterator it = std::find(m_pool.constBegin(), m_pool.constEnd(), value);
0028         if (it != m_pool.constEnd()) {
0029             return *it;
0030         }
0031         m_pool.push_back(value);
0032         return value;
0033     }
0034 
0035 private:
0036     Container<T> m_pool;
0037 };
0038 
0039 }
0040 }