PDF4QT/Pdf4QtLib/sources/pdfflatarray.h

191 lines
4.9 KiB
C
Raw Normal View History

2021-04-30 20:12:10 +02:00
// Copyright (C) 2019-2021 Jakub Melka
2019-02-09 18:40:56 +01:00
//
2021-08-10 19:22:56 +02:00
// This file is part of PDF4QT.
2019-02-09 18:40:56 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is free software: you can redistribute it and/or modify
2019-02-09 18:40:56 +01:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
2021-04-30 20:12:10 +02:00
// with the written consent of the copyright owner, any later version.
2019-02-09 18:40:56 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is distributed in the hope that it will be useful,
2019-02-09 18:40:56 +01:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
2021-08-10 19:22:56 +02:00
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
2019-02-09 18:40:56 +01:00
#ifndef PDFFLATARRAY_H
#define PDFFLATARRAY_H
#include <QtGlobal>
#include <array>
#include <vector>
#include <algorithm>
namespace pdf
{
/// This represents a fast array, consisting of "fast" block of fixed size \p FlatSize,
/// and "slow" block of variable size. Usually, this array is used when vast majority
/// of usage size is below FlatSize, only minority is above FlatSize. Typical example
/// of use of this class:
///
/// We have colors in PDF, which can have usually 1, 3 or 4 color components. But in some
/// rare cases, we have much more components, for example for DeviceN color spaces.
/// For this reason, we will set FlatSize to 4 (so Gray, RGB and CMYK colors will not
/// use slow "variable" part).
template<typename T, size_t FlatSize>
class PDFFlatArray
{
public:
2020-11-19 19:54:33 +01:00
PDFFlatArray() :
2019-02-09 18:40:56 +01:00
m_flatBlock(),
2019-02-24 17:48:37 +01:00
m_flatBlockItemCount(0),
2019-02-09 18:40:56 +01:00
m_variableBlock()
{
}
2019-02-17 18:01:22 +01:00
template<typename... Arguments, typename std::enable_if<sizeof...(Arguments) <= FlatSize, int>::type = 0>
2020-11-19 19:54:33 +01:00
inline PDFFlatArray(Arguments... arguments) :
2019-02-17 18:01:22 +01:00
m_flatBlock({ arguments... }),
2019-02-24 17:48:37 +01:00
m_flatBlockItemCount(sizeof...(Arguments)),
2019-02-09 18:40:56 +01:00
m_variableBlock()
{
}
2019-03-17 16:12:36 +01:00
using value_type = T;
2019-02-09 18:40:56 +01:00
/// Returns the size of the array
size_t size() const { return getFlatBlockSize() + m_variableBlock.size(); }
/// Returns true, if array is empty
bool empty() const { return size() == 0; }
template<size_t index>
const T& get() const
{
if constexpr (index < FlatSize)
{
return m_flatBlock[size];
}
else
{
return m_variableBlock[size - FlatSize];
}
}
template<size_t index>
T& get()
{
if constexpr (index < FlatSize)
{
return m_flatBlock[size];
}
else
{
return m_variableBlock[size - FlatSize];
}
}
const T& operator[] (size_t index) const
{
Q_ASSERT(index < size());
if (index < FlatSize)
{
return m_flatBlock[index];
}
else
{
return m_variableBlock[index - FlatSize];
}
}
2019-02-16 18:26:16 +01:00
T& operator[] (size_t index)
{
Q_ASSERT(index < size());
if (index < FlatSize)
{
return m_flatBlock[index];
}
else
{
return m_variableBlock[index - FlatSize];
}
}
2019-02-10 18:32:15 +01:00
void clear()
{
2019-02-24 17:48:37 +01:00
m_flatBlockItemCount = 0;
2019-02-10 18:32:15 +01:00
m_variableBlock.clear();
}
void push_back(T object)
{
2019-02-24 17:48:37 +01:00
if (m_flatBlockItemCount < m_flatBlock.size())
2019-02-10 18:32:15 +01:00
{
2019-02-24 17:48:37 +01:00
m_flatBlock[m_flatBlockItemCount++] = std::move(object);
2019-02-10 18:32:15 +01:00
}
else
{
m_variableBlock.emplace_back(std::move(object));
}
}
2019-03-03 16:14:38 +01:00
void resize(std::size_t size)
{
if (size <= FlatSize)
{
m_flatBlockItemCount = size;
m_variableBlock.clear();
}
else
{
m_flatBlockItemCount = FlatSize;
m_variableBlock.resize(size - FlatSize);
}
}
/// Returns the last element of the array
inline const T& back() const { return m_variableBlock.empty() ? m_flatBlock[m_flatBlockItemCount - 1] : m_variableBlock.back(); }
/// Erases the last element from the array
inline void pop_back() { resize(size() - 1); }
2019-08-31 14:37:18 +02:00
bool operator==(const PDFFlatArray& other) const
{
const size_t size = this->size();
if (size != other.size())
{
return false;
}
for (size_t i = 0; i < size; ++i)
{
if ((*this)[i] != other[i])
{
return false;
}
}
return true;
}
2019-02-09 18:40:56 +01:00
private:
2019-02-24 17:48:37 +01:00
size_t getFlatBlockSize() const { return m_flatBlockItemCount; }
2019-02-09 18:40:56 +01:00
std::array<T, FlatSize> m_flatBlock;
2019-02-24 17:48:37 +01:00
size_t m_flatBlockItemCount; ///< Number of items in the flat block
2019-02-09 18:40:56 +01:00
std::vector<T> m_variableBlock;
};
} // namespace pdf
#endif // PDFFLATARRAY_H