TrinityCore
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 <functional>
21#include <vector>
22
24{
25template <class Key, class Compare = std::less<Key>, class KeyContainer = std::vector<Key>>
27{
28public:
29 using iterator = typename KeyContainer::iterator;
30 using const_iterator = typename KeyContainer::const_iterator;
31
32 bool empty() const { return _storage.empty(); }
33 auto size() const { return _storage.size(); }
34
35 auto begin() { return _storage.begin(); }
36 auto begin() const { return _storage.begin(); }
37
38 auto end() { return _storage.end(); }
39 auto end() const { return _storage.end(); }
40
41 auto find(Key const& value) const
42 {
43 auto end = this->end();
44 auto itr = std::lower_bound(this->begin(), end, value, Compare());
45 if (itr != end && Compare()(value, *itr))
46 itr = end;
47
48 return itr;
49 }
50
51 auto find(Key const& value)
52 {
53 auto end = this->end();
54 auto itr = std::lower_bound(this->begin(), end, value, Compare());
55 if (itr != end && Compare()(value, *itr))
56 itr = end;
57
58 return itr;
59 }
60
61 template <class... Args>
62 std::pair<iterator, bool> emplace(Args&&... args)
63 {
64 Key newElement(std::forward<Args>(args)...);
65 auto end = this->end();
66 auto itr = std::lower_bound(this->begin(), end, newElement, Compare());
67 if (itr != end && !Compare()(newElement, *itr))
68 return { itr, false };
69
70 return { _storage.emplace(itr, std::move(newElement)), true };
71 }
72
73 std::pair<iterator, bool> insert(Key const& key) { return emplace(key); }
74
75 std::size_t erase(Key const& key)
76 {
77 auto itr = this->find(key);
78 if (itr == this->end())
79 return 0;
80
81 this->erase(itr);
82 return 1;
83 }
84 auto erase(const_iterator itr) { return _storage.erase(itr); }
85
86 void clear() { _storage.clear(); }
87
88 void shrink_to_fit() { _storage.shrink_to_fit(); }
89
90 friend bool operator==(FlatSet const& left, FlatSet const& right)
91 {
92 return left._storage == right._storage;
93 }
94
95 friend bool operator!=(FlatSet const& left, FlatSet const& right)
96 {
97 return !(left == right);
98 }
99
100private:
101 KeyContainer _storage;
102};
103}
104
105#endif // TRINITYCORE_FLAT_SET_H
auto erase(const_iterator itr)
Definition: FlatSet.h:84
auto find(Key const &value) const
Definition: FlatSet.h:41
auto find(Key const &value)
Definition: FlatSet.h:51
std::pair< iterator, bool > emplace(Args &&... args)
Definition: FlatSet.h:62
friend bool operator==(FlatSet const &left, FlatSet const &right)
Definition: FlatSet.h:90
typename KeyContainer::iterator iterator
Definition: FlatSet.h:29
std::pair< iterator, bool > insert(Key const &key)
Definition: FlatSet.h:73
std::size_t erase(Key const &key)
Definition: FlatSet.h:75
typename KeyContainer::const_iterator const_iterator
Definition: FlatSet.h:30
friend bool operator!=(FlatSet const &left, FlatSet const &right)
Definition: FlatSet.h:95