aboutsummaryrefslogtreecommitdiffstats
path: root/src/particles/qquickparticleflatset_p.h
blob: 42114959ef19a0dc64492bf9117392396d36b187 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************************
**
** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#ifndef QQUICKPARTICLEFLATSET_P_H
#define QQUICKPARTICLEFLATSET_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtGlobal>

#include <vector>
#include <algorithm>
#include <iterator>

QT_BEGIN_NAMESPACE

// Minimal API, just for the consumption of Qt Quick Particles.
// For extra safety, it's in a private namespace

namespace QtQuickParticlesPrivate {

template <typename T>
class QFlatSet
{
public:
    using iterator = typename std::vector<T>::iterator;
    using const_iterator = typename std::vector<T>::const_iterator;
    using value_type = typename std::vector<T>::value_type;
    using size_type = int;

    iterator find(const T &t)
    {
        return std::find(begin(), end(), t);
    }

    const_iterator find(const T &t) const
    {
        return std::find(begin(), end(), t);
    }

    bool contains(const T &t) const
    {
        return find(t) != end();
    }

    void clear()
    {
        m_data.clear();
    }

    void reserve(int capacity)
    {
        m_data.reserve(capacity);
    }

    iterator insert(const T &t)
    {
        auto i = find(t);
        if (i != end())
            return i;
        T copy = t;
        m_data.push_back(std::move(copy));
        return std::prev(m_data.end());
    }

    iterator insert(T &&t)
    {
        auto i = find(t);
        if (i != end())
            return i;
        m_data.push_back(std::move(t));
        return std::prev(m_data.end());
    }

    size_type remove(const T &t)
    {
        auto i = std::find(m_data.begin(), m_data.end(), t);
        if (i != m_data.end()) {
            m_data.erase(i);
            return 1;
        }
        return 0;
    }

    iterator operator<<(const T &t)
    {
        return insert(t);
    }

    iterator operator<<(T &&t)
    {
        return insert(std::move(t));
    }

    iterator begin() { return m_data.begin(); }
    const_iterator begin() const { return m_data.begin(); }
    const_iterator cbegin() const { return m_data.cbegin(); }

    iterator end() { return m_data.end(); }
    const_iterator end() const { return m_data.end(); }
    const_iterator cend() const { return m_data.cend(); }

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

} // namespace QtQuickParticlesPrivate

QT_END_NAMESPACE

#endif // QQUICKPARTICLEFLATSET_P_H