2017-04-01 18:30:52 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
2017-04-28 08:56:06 +08:00
|
|
|
#include <vector>
|
2017-04-01 18:30:52 +08:00
|
|
|
|
|
|
|
// Add more forwarding functions to the m_data std::vector as needed.
|
|
|
|
namespace vcpkg
|
|
|
|
{
|
2017-04-28 08:56:06 +08:00
|
|
|
template<class T>
|
2017-04-01 18:30:52 +08:00
|
|
|
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))
|
2017-04-01 18:30:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-28 08:56:06 +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))
|
2017-04-01 18:30:52 +08:00
|
|
|
{
|
|
|
|
if (!std::is_sorted(m_data.cbegin(), m_data.cend(), comp))
|
|
|
|
{
|
|
|
|
std::sort(m_data.begin(), m_data.end(), comp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:56:06 +08:00
|
|
|
iterator begin() const { return this->m_data.cbegin(); }
|
2017-04-01 18:30:52 +08:00
|
|
|
|
2017-04-28 08:56:06 +08:00
|
|
|
iterator end() const { return this->m_data.cend(); }
|
2017-04-01 18:30:52 +08:00
|
|
|
|
2017-04-28 08:56:06 +08:00
|
|
|
iterator cbegin() const { return this->m_data.cbegin(); }
|
2017-04-18 04:59:58 +08:00
|
|
|
|
2017-04-28 08:56:06 +08:00
|
|
|
iterator cend() const { return this->m_data.cend(); }
|
2017-04-18 04:59:58 +08:00
|
|
|
|
2017-04-28 08:56:06 +08:00
|
|
|
bool empty() const { return this->m_data.empty(); }
|
2017-04-01 18:30:52 +08:00
|
|
|
|
2017-04-28 08:56:06 +08:00
|
|
|
size_type size() const { return this->m_data.size(); }
|
2017-04-01 18:30:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> m_data;
|
|
|
|
};
|
|
|
|
}
|