aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/ImmutableSortedVector.h
blob: 681f9fd4d67f3f3b119af21a3ae4e339ed6c7f3a (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
#pragma once

#include <vector>
#include <algorithm>

// Add more forwarding functions to the delegate std::vector as needed.
namespace vcpkg
{
    template <class T>
    class ImmutableSortedVector
    {
    public:
        static ImmutableSortedVector<T> create(std::vector<T> vector)
        {
            ImmutableSortedVector out;
            out.delegate = std::move(vector);
            if (!std::is_sorted(out.delegate.cbegin(), out.delegate.cend()))
            {
                std::sort(out.delegate.begin(), out.delegate.end());
            }

            return out;
        }

        typename std::vector<T>::const_iterator begin() const
        {
            return this->delegate.cbegin();
        }

        typename std::vector<T>::const_iterator end() const
        {
            return this->delegate.cend();
        }

        typename std::vector<T>::const_iterator cbegin() const
        {
            return this->delegate.cbegin();
        }

        typename std::vector<T>::const_iterator cend() const
        {
            return this->delegate.cend();
        }

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