vcpkg/toolsrc/include/SortedVector.h

51 lines
1.3 KiB
C
Raw Normal View History

#pragma once
#include <algorithm>
#include <vector>
// Add more forwarding functions to the m_data std::vector as needed.
namespace vcpkg
{
template<class T>
class SortedVector
{
public:
using size_type = typename std::vector<T>::size_type;
using iterator = typename std::vector<T>::const_iterator;
2017-04-04 07:36:07 +08:00
SortedVector() : m_data() {}
2017-04-04 06:03:21 +08:00
explicit SortedVector(std::vector<T> v) : m_data(std::move(v))
{
if (!std::is_sorted(m_data.begin(), m_data.end()))
{
std::sort(m_data.begin(), m_data.end());
}
}
2017-04-04 07:36:07 +08:00
template<class Compare>
2017-04-04 06:03:21 +08:00
SortedVector(std::vector<T> v, Compare comp) : m_data(std::move(v))
{
if (!std::is_sorted(m_data.cbegin(), m_data.cend(), comp))
{
std::sort(m_data.begin(), m_data.end(), comp);
}
}
iterator begin() const { return this->m_data.cbegin(); }
iterator end() const { return this->m_data.cend(); }
iterator cbegin() const { return this->m_data.cbegin(); }
2017-04-18 04:59:58 +08:00
iterator cend() const { return this->m_data.cend(); }
2017-04-18 04:59:58 +08:00
bool empty() const { return this->m_data.empty(); }
size_type size() const { return this->m_data.size(); }
private:
std::vector<T> m_data;
};
}