summaryrefslogtreecommitdiff
path: root/src/Bullet3Common/b3ResizablePool.h
blob: cafe3ff39640f299fbdad62811bbf0252912d1a6 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#ifndef B3_RESIZABLE_POOL_H
#define B3_RESIZABLE_POOL_H

#include "Bullet3Common/b3AlignedObjectArray.h"

enum
{
	B3_POOL_HANDLE_TERMINAL_FREE = -1,
	B3_POOL_HANDLE_TERMINAL_USED = -2
};

template <typename U>
struct b3PoolBodyHandle : public U
{
	B3_DECLARE_ALIGNED_ALLOCATOR();

	int m_nextFreeHandle;
	void setNextFree(int next)
	{
		m_nextFreeHandle = next;
	}
	int getNextFree() const
	{
		return m_nextFreeHandle;
	}
};

template <typename T>
class b3ResizablePool
{
protected:
	b3AlignedObjectArray<T> m_bodyHandles;
	int m_numUsedHandles;   // number of active handles
	int m_firstFreeHandle;  // free handles list

	T* getHandleInternal(int handle)
	{
		return &m_bodyHandles[handle];
	}
	const T* getHandleInternal(int handle) const
	{
		return &m_bodyHandles[handle];
	}

public:
	b3ResizablePool()
	{
		initHandles();
	}

	virtual ~b3ResizablePool()
	{
		exitHandles();
	}
	///handle management

	int getNumHandles() const
	{
		return m_bodyHandles.size();
	}

	void getUsedHandles(b3AlignedObjectArray<int>& usedHandles) const
	{
		for (int i = 0; i < m_bodyHandles.size(); i++)
		{
			if (m_bodyHandles[i].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
			{
				usedHandles.push_back(i);
			}
		}
	}

	T* getHandle(int handle)
	{
		b3Assert(handle >= 0);
		b3Assert(handle < m_bodyHandles.size());
		if ((handle < 0) || (handle >= m_bodyHandles.size()))
		{
			return 0;
		}

		if (m_bodyHandles[handle].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
		{
			return &m_bodyHandles[handle];
		}
		return 0;
	}
	const T* getHandle(int handle) const
	{
		b3Assert(handle >= 0);
		b3Assert(handle < m_bodyHandles.size());
		if ((handle < 0) || (handle >= m_bodyHandles.size()))
		{
			return 0;
		}

		if (m_bodyHandles[handle].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
		{
			return &m_bodyHandles[handle];
		}
		return 0;
	}

	void increaseHandleCapacity(int extraCapacity)
	{
		int curCapacity = m_bodyHandles.size();
		//b3Assert(curCapacity == m_numUsedHandles);
		int newCapacity = curCapacity + extraCapacity;
		m_bodyHandles.resize(newCapacity);

		{
			for (int i = curCapacity; i < newCapacity; i++)
				m_bodyHandles[i].setNextFree(i + 1);

			m_bodyHandles[newCapacity - 1].setNextFree(-1);
		}
		m_firstFreeHandle = curCapacity;
	}
	void initHandles()
	{
		m_numUsedHandles = 0;
		m_firstFreeHandle = -1;

		increaseHandleCapacity(1);
	}

	void exitHandles()
	{
		m_bodyHandles.resize(0);
		m_firstFreeHandle = -1;
		m_numUsedHandles = 0;
	}

	int allocHandle()
	{
		b3Assert(m_firstFreeHandle >= 0);

		int handle = m_firstFreeHandle;
		m_firstFreeHandle = getHandleInternal(handle)->getNextFree();
		m_numUsedHandles++;

		if (m_firstFreeHandle < 0)
		{
			//int curCapacity = m_bodyHandles.size();
			int additionalCapacity = m_bodyHandles.size();
			increaseHandleCapacity(additionalCapacity);

			getHandleInternal(handle)->setNextFree(m_firstFreeHandle);
		}
		getHandleInternal(handle)->setNextFree(B3_POOL_HANDLE_TERMINAL_USED);
		getHandleInternal(handle)->clear();
		return handle;
	}

	void freeHandle(int handle)
	{
		b3Assert(handle >= 0);

		if (m_bodyHandles[handle].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
		{
			getHandleInternal(handle)->clear();
			getHandleInternal(handle)->setNextFree(m_firstFreeHandle);
			m_firstFreeHandle = handle;
			m_numUsedHandles--;
		}
	}
};
///end handle management

#endif  //B3_RESIZABLE_POOL_H