aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/SortedVector.h
blob: aa4a40844d6f02b0b7c7c3b16cecbfec791934ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once

#include <vector>
#include <algorithm>

// 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;

        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());
            }
        }
        template <class Compare>
        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();
        }

        bool empty() const
        {
            return this->m_data.empty();
        }

        size_type size() const
        {
            return this->m_data.size();
        }

    private:
        std::vector<T> m_data;
    };
}