PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
BruteSet.h
Go to the documentation of this file.
1
10
11#ifndef PBAT_COMMON_BRUTESET_H
12#define PBAT_COMMON_BRUTESET_H
13
14#include <cassert>
15#include <utility>
16
17namespace pbat::common {
18
27template <class T, auto kCapacity = 32>
29{
30 public:
34 BruteSet() : size(0) {}
39 T* begin() { return set; }
44 T* end() { return set + size; }
49 T const* begin() const { return set; }
54 T const* end() const { return set + size; }
62 bool Insert(T const& value)
63 {
64 assert(size < kCapacity);
65 if (not Contains(value))
66 {
67 set[size++] = value;
68 return true;
69 }
70 return false;
71 }
72
77 bool Contains(T const& value) const
78 {
79 for (int i = 0; i < size; ++i)
80 if (set[i] == value)
81 return true;
82 return false;
83 }
84
89 bool Remove(T const& value)
90 {
91 for (int i = 0; i < size; ++i)
92 {
93 if (set[i] == value)
94 {
95 set[i] = set[--size];
96 return true;
97 }
98 }
99 return false;
100 }
101
105 int Size() const { return size; }
110 bool IsFull() const { return size == kCapacity; }
115 bool IsEmpty() const { return size == 0; }
120 void Clear() { size = 0; }
121
122 private:
123 T set[kCapacity];
124 int size;
125};
126
127} // namespace pbat::common
128
129#endif // PBAT_COMMON_BRUTESET_H
void Clear()
Clear the set.
Definition BruteSet.h:120
bool Insert(T const &value)
Insert an element into the set.
Definition BruteSet.h:62
T const * begin() const
Const begin iterator.
Definition BruteSet.h:49
T const * end() const
Const end iterator.
Definition BruteSet.h:54
bool IsEmpty() const
Check if the set is empty.
Definition BruteSet.h:115
bool IsFull() const
Check if the set is full.
Definition BruteSet.h:110
BruteSet()
Construct a new BruteSet object.
Definition BruteSet.h:34
T * end()
End iterator.
Definition BruteSet.h:44
bool Remove(T const &value)
Remove an element from the set.
Definition BruteSet.h:89
T * begin()
Begin iterator.
Definition BruteSet.h:39
int Size() const
Get the size of the set.
Definition BruteSet.h:105
bool Contains(T const &value) const
Check if the set contains an element.
Definition BruteSet.h:77
Common functionality.
Definition ArgSort.h:20