PeriDyno 1.0.0
Loading...
Searching...
No Matches
MultiMap.inl
Go to the documentation of this file.
1#include "Math/SimpleMath.h"
2#include <glm/glm.hpp>
3
4#include "STLMacro.h"
5
6namespace dyno
7{
8 template <typename MKey, typename T>
10 {
11 }
12
13 template <typename MKey, typename T>
15 {
16 int ind = leftBound(Pair<MKey, T>(key, T()), m_startLoc, m_size);
17
18 return ind >= m_size || m_startLoc[ind] != Pair<MKey, T>(key, T()) ? nullptr : m_startLoc + ind;
19 }
20
21 template <typename MKey, typename T>
23 {
24 //return nullptr if the data buffer is full
25 if (m_size >= m_maxSize) return nullptr;
26
27 //return the index of the last element that is equal to or smaller than val
28 int ind = rightBound(pair, m_startLoc, m_size);
29
30 int ind_plus = ind + 1;
31
32 //move all element backward
33 for (size_t j = m_size; j > ind_plus; j--)
34 {
35 m_startLoc[j] = m_startLoc[j - 1];
36 }
37
38 //insert val to ind-th location.
39 m_startLoc[ind_plus] = pair;
40 m_size++;
41
42 return m_startLoc + ind_plus;
43 }
44
45 template <typename MKey, typename T>
46 DYN_FUNC T& MultiMap<MKey, T>::operator[](MKey key)
47 {
48 Pair<MKey, T>* ret = find(key);
49
50 if (ret == nullptr)
51 ret = insert(Pair<MKey, T>(key, T()));
52
53 return ret->second;
54 }
55
56
57 template <typename MKey, typename T>
58 DYN_FUNC const T& MultiMap<MKey, T>::operator[](MKey key) const
59 {
60 Pair<MKey, T>* ret = find(key);
61
62 if (ret == nullptr)
63 ret = insert(Pair<MKey, T>(key, T()));
64
65 return ret->second;
66 }
67
68
69 template <typename MKey, typename T>
71 {
72 m_size = 0;
73 }
74
75 template <typename MKey, typename T>
77 {
78 return m_size;
79 }
80
81 template <typename MKey, typename T>
82 DYN_FUNC uint MultiMap<MKey, T>::count(MKey key)
83 {
84 int ind = leftBound(Pair<MKey, T>(key, T(0)), m_startLoc, m_size);
85 if (ind >= m_size) return 0;
86
87 size_t num = 0;
88 while (m_startLoc[ind] == val && ind < m_size)
89 {
90 num++;
91 ind++;
92 }
93
94 return num;
95 }
96
97 template <typename MKey, typename T>
99 {
100 return m_startLoc == nullptr;
101 }
102}
103
DYN_FUNC void clear()
Definition MultiMap.inl:70
DYN_FUNC uint count(MKey key)
Definition MultiMap.inl:82
Pair< MKey, T > * m_startLoc
Definition MultiMap.h:56
DYN_FUNC MultiMap()
Definition MultiMap.inl:9
DYN_FUNC T & operator[](MKey key)
Definition MultiMap.inl:46
DYN_FUNC iterator find(MKey key)
Definition MultiMap.inl:14
DYN_FUNC uint size()
Definition MultiMap.inl:76
DYN_FUNC bool empty()
Definition MultiMap.inl:98
DYN_FUNC iterator insert(Pair< MKey, T > pair)
Definition MultiMap.inl:22
T second
Definition Pair.h:41
#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
DYN_FUNC int rightBound(T target, T *startLoc, uint maxSize)
Find the right bound of a target with a binary search algorithm.
Definition STLMacro.h:54