TrinityCore
Loading...
Searching...
No Matches
FlatSet.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#ifndef TRINITYCORE_FLAT_SET_H
18#define TRINITYCORE_FLAT_SET_H
19
20#include <algorithm>
21#include <functional>
22#include <vector>
23
25{
26template <class Key, class Compare = std::less<Key>, class KeyContainer = std::vector<Key>>
28{
29public:
30 using iterator = typename KeyContainer::iterator;
31 using const_iterator = typename KeyContainer::const_iterator;
32
33 bool empty() const { return _storage.empty(); }
34 auto size() const { return _storage.size(); }
35
36 auto begin() { return _storage.begin(); }
37 auto begin() const { return _storage.begin(); }
38
39 auto end() { return _storage.end(); }
40 auto end() const { return _storage.end(); }
41
42 bool contains(Key const& value) const
43 {
44 return std::binary_search(this->begin(), this->end(), value, Compare());
45 }
46
47 auto find(Key const& value) const
48 {
49 auto compare = Compare();
50 auto end = this->end();
51 auto itr = std::lower_bound(this->begin(), end, value, compare);
52 if (itr != end && compare(value, *itr))
53 itr = end;
54
55 return itr;
56 }
57
58 auto find(Key const& value)
59 {
60 auto compare = Compare();
61 auto end = this->end();
62 auto itr = std::lower_bound(this->begin(), end, value, compare);
63 if (itr != end && compare(value, *itr))
64 itr = end;
65
66 return itr;
67 }
68
69 template <class... Args>
70 std::pair<iterator, bool> emplace(Args&&... args)
71 {
72 Key newElement(std::forward<Args>(args)...);
73 auto compare = Compare();
74 auto end = this->end();
75 auto itr = std::lower_bound(this->begin(), end, newElement, compare);
76 if (itr != end && !compare(newElement, *itr))
77 return { itr, false };
78
79 return { _storage.emplace(itr, std::move(newElement)), true };
80 }
81
82 std::pair<iterator, bool> insert(Key const& key) { return this->emplace(key); }
83
84 std::size_t erase(Key const& key)
85 {
86 auto itr = this->find(key);
87 if (itr == this->end())
88 return 0;
89
90 this->erase(itr);
91 return 1;
92 }
93 auto erase(const_iterator itr) { return _storage.erase(itr); }
94
95 void clear() { _storage.clear(); }
96
97 void shrink_to_fit() { _storage.shrink_to_fit(); }
98
99 friend std::strong_ordering operator<=>(FlatSet const& left, FlatSet const& right) = default;
100 friend bool operator==(FlatSet const& left, FlatSet const& right) = default;
101
102private:
103 KeyContainer _storage;
104};
105}
106
107#endif // TRINITYCORE_FLAT_SET_H
auto erase(const_iterator itr)
Definition FlatSet.h:93
friend std::strong_ordering operator<=>(FlatSet const &left, FlatSet const &right)=default
bool contains(Key const &value) const
Definition FlatSet.h:42
auto find(Key const &value) const
Definition FlatSet.h:47
auto find(Key const &value)
Definition FlatSet.h:58
std::pair< iterator, bool > emplace(Args &&... args)
Definition FlatSet.h:70
typename KeyContainer::iterator iterator
Definition FlatSet.h:30
std::pair< iterator, bool > insert(Key const &key)
Definition FlatSet.h:82
friend bool operator==(FlatSet const &left, FlatSet const &right)=default
std::size_t erase(Key const &key)
Definition FlatSet.h:84
typename KeyContainer::const_iterator const_iterator
Definition FlatSet.h:31