PeriDyno 1.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Set.inl
Go to the documentation of this file.
1#include "STLMacro.h"
2#include "Math/SimpleMath.h"
3#include <glm/glm.hpp>
4
5namespace dyno
6{
7 template <typename T>
8 DYN_FUNC Set<T>::Set()
9 : STLBuffer<T>()
10 {
11 }
12
13 template <typename T>
14 DYN_FUNC T* Set<T>::find(T val)
15 {
16 int ind = leftBound(val, m_startLoc, m_size);
17
18 return ind >= m_size || m_startLoc[ind] != val ? nullptr : m_startLoc + ind;
19 }
20
21
22 template <typename T>
23 DYN_FUNC T* Set<T>::insert(T val)
24 {
25 //return nullptr if the data buffer is full
26 if (m_size >= m_maxSize) return nullptr;
27
28 //return the index of the first element that is equal to or greater than val
29 int ind = leftBound(val, m_startLoc, m_size);
30
31 if (ind == m_size)
32 {
33 m_startLoc[ind] = val;
34 m_size++;
35
36 return m_startLoc + ind;
37 };
38
39 //return the original address if val is found
40 if (m_startLoc[ind] == val)
41 return m_startLoc + ind;
42 else
43 {
44 //if found, move all element backward
45 for (size_t j = m_size; j > ind; j--)
46 {
47 m_startLoc[j] = m_startLoc[j - 1];
48 }
49
50 //insert val into location ind.
51 m_startLoc[ind] = val;
52 m_size++;
53
54 return m_startLoc + ind;
55 }
56 }
57
58 template <typename T>
59 DYN_FUNC void Set<T>::clear()
60 {
61 m_size = 0;
62 }
63
64 template <typename T>
65 DYN_FUNC uint Set<T>::size()
66 {
67 return m_size;
68 }
69
70 template <typename T>
71 DYN_FUNC uint Set<T>::count(T val)
72 {
73 return find(val) ? 1 : 0;
74 }
75
76 template <typename T>
77 DYN_FUNC bool Set<T>::empty()
78 {
79 return m_startLoc == nullptr;
80 }
81}
82
DYN_FUNC STLBuffer()
Definition STLBuffer.h:19
DYN_FUNC T * find(T val)
Definition Set.inl:14
DYN_FUNC uint count(T val)
Definition Set.inl:71
DYN_FUNC uint size()
Definition Set.inl:65
uint m_size
Definition Set.h:47
DYN_FUNC Set()
Definition Set.inl:8
DYN_FUNC bool empty()
Definition Set.inl:77
DYN_FUNC T * insert(T val)
Definition Set.inl:23
DYN_FUNC void clear()
Definition Set.inl:59
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC int leftBound(T target, T *startLoc, uint maxSize)
Find the left bound of a target with a binary search algorithm.
Definition STLMacro.h:22
unsigned int uint
Definition VkProgram.h:14