-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathPhysics_ObjectPairHash.cpp
143 lines (116 loc) · 3.75 KB
/
Physics_ObjectPairHash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "StdAfx.h"
#include "Physics_ObjectPairHash.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
/***********************************
* CLASS CPhysicsObjectPairHash
***********************************/
CPhysicsObjectPairHash::CPhysicsObjectPairHash() {
for (int i = 0; i < 256; i++)
m_pHashList[i] = NULL;
}
void CPhysicsObjectPairHash::AddObjectPair(void *pObject0, void *pObject1) {
int entry = GetEntry(pObject0, pObject1);
// This particular entry may have more than one hash, so find the last one.
pair_hash_list *last = NULL;
for (pair_hash_list *hash = m_pHashList[entry]; hash; hash = hash->next)
last = hash;
// Setup our hash
pair_hash_list *hash = new pair_hash_list;
hash->object0 = pObject0;
hash->object1 = pObject1;
//hash->previous = last;
hash->next = NULL;
// Now link ourselves up to the list.
if (last)
last->next = hash;
else
m_pHashList[entry] = hash;
}
void CPhysicsObjectPairHash::RemoveObjectPair(void *pObject0, void *pObject1) {
int entry = GetEntry(pObject0, pObject1);
pair_hash_list *next = NULL;
pair_hash_list *last = NULL;
for (pair_hash_list *hash = m_pHashList[entry]; hash; hash = next) {
if ((hash->object0 == pObject0 || hash->object0 == pObject1) && (hash->object1 == pObject0 || hash->object1 == pObject1)) {
if (last)
last->next = hash->next;
else
m_pHashList[entry] = hash->next;
//if (hash->next)
// hash->next->previous = hash->previous;
next = hash->next; // Fix for access violation
delete hash;
} else {
next = hash->next;
last = hash;
}
}
}
void CPhysicsObjectPairHash::RemoveAllPairsForObject(void *pObject0) {
// Loop through all entries
for (int i = 0; i < 256; i++) {
pair_hash_list *next = NULL;
pair_hash_list *last = NULL;
for (pair_hash_list *hash = m_pHashList[i]; hash; hash = next) {
if (hash->object0 == pObject0 || hash->object1 == pObject0) {
if (last)
last->next = hash->next;
else
m_pHashList[i] = hash->next;
//if (hash->next)
// hash->next->previous = hash->previous;
next = hash->next; // Fix for access violation
delete hash;
} else {
next = hash->next;
last = hash;
}
}
}
}
bool CPhysicsObjectPairHash::IsObjectPairInHash(void *pObject0, void *pObject1) {
int entry = GetEntry(pObject0, pObject1);
for (pair_hash_list *hash = m_pHashList[entry]; hash; hash = hash->next) {
if ((hash->object0 == pObject0 || hash->object0 == pObject1) && (hash->object1 == pObject0 || hash->object1 == pObject1))
return true;
}
return false;
}
bool CPhysicsObjectPairHash::IsObjectInHash(void *pObject0) {
for (int i = 0; i < 256; i++) {
for (pair_hash_list *hash = m_pHashList[i]; hash; hash = hash->next) {
if (hash->object0 == pObject0 || hash->object1 == pObject0)
return true;
}
}
return false;
}
int CPhysicsObjectPairHash::GetPairCountForObject(void *pObject0) {
int c = 0;
for (int i = 0; i < 256; i++) {
for (pair_hash_list *hash = m_pHashList[i]; hash; hash = hash->next) {
if (hash->object0 == pObject0 || hash->object1 == pObject0)
c++;
}
}
return c;
}
int CPhysicsObjectPairHash::GetPairListForObject(void *pObject0, int nMaxCount, void **ppObjectList) {
int c = 0;
for (int i = 0; i < 256; i++) {
if (c >= nMaxCount)
break;
for (pair_hash_list *hash = m_pHashList[i]; hash; hash = hash->next) {
if (c >= nMaxCount)
break;
// Get the opposite object in the pair
ppObjectList[c++] = hash->object0 == pObject0 ? hash->object1 : hash->object0;
}
}
return c;
}
// Purpose: Generate a number in [0,255] given 2 pointers. Must be the same for the same 2 pointers.
int CPhysicsObjectPairHash::GetEntry(void *pObject0, void *pObject1) {
return (((intptr_t)pObject0 ^ (intptr_t)pObject1) >> (sizeof(void*))) & 0xFF;
}