RavEngine
Loading...
Searching...
No Matches
CmBlockArray.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef CM_BLOCK_ARRAY_H
30#define CM_BLOCK_ARRAY_H
31
32#include "foundation/PxAssert.h"
33#include "foundation/PxMath.h"
34#include "foundation/PxMemory.h"
35#include "foundation/PxAllocator.h"
36#include "foundation/PxUserAllocated.h"
37#include "foundation/PxIntrinsics.h"
38#include "foundation/PxArray.h"
39
40namespace physx
41{
42namespace Cm
43{
44
45template <typename T, PxU32 SlabSize = 4096>
47{
48 PxArray<T*> mBlocks;
49 PxU32 mSize;
50 PxU32 mCapacity;
51
52public:
53
54 BlockArray() : mSize(0), mCapacity(0)
55 {
56 }
57
59 {
60 for (PxU32 a = 0; a < mBlocks.size(); ++a)
61 {
62 for (PxU32 i = 0; i < SlabSize; ++i)
63 {
64 mBlocks[a][i].~T();
65 }
66 PX_FREE(mBlocks[a]);
67 }
68 mBlocks.resize(0);
69 }
70
71 void reserve(PxU32 capacity)
72 {
73 if (capacity > mCapacity)
74 {
75 PxU32 nbSlabsRequired = (capacity + SlabSize - 1) / SlabSize;
76
77 PxU32 nbSlabsToAllocate = nbSlabsRequired - mBlocks.size();
78
79 mCapacity += nbSlabsToAllocate * SlabSize;
80
81 for (PxU32 a = 0; a < nbSlabsToAllocate; ++a)
82 {
83 T* ts = reinterpret_cast<T*>(PX_ALLOC(sizeof(T) * SlabSize, "BlockArray"));
84 for(PxU32 i = 0; i < SlabSize; ++i)
85 PX_PLACEMENT_NEW(ts+i, T)();
86 mBlocks.pushBack(ts);
87 }
88 }
89 }
90
91 void resize(PxU32 size)
92 {
93 reserve(size);
94 for (PxU32 a = mSize; a < size; ++a)
95 {
96 mBlocks[a / SlabSize][a&(SlabSize - 1)].~T();
97 mBlocks[a / SlabSize][a&(SlabSize-1)] = T();
98 }
99 mSize = size;
100 }
101
102 void forceSize_Unsafe(PxU32 size)
103 {
104 PX_ASSERT(size <= mCapacity);
105 mSize = size;
106 }
107
108 void remove(PxU32 idx)
109 {
110 PX_ASSERT(idx < mSize);
111 for (PxU32 a = idx; a < mSize; ++a)
112 {
113 mBlocks[a / SlabSize][a&(SlabSize-1)] = mBlocks[(a + 1) / SlabSize][(a + 1) &(SlabSize-1)];
114 }
115
116 mSize--;
117 mBlocks[mSize / SlabSize][mSize&(SlabSize - 1)].~T();
118 }
119
120 void replaceWithLast(PxU32 idx)
121 {
122 PX_ASSERT(idx < mSize);
123 --mSize;
124 mBlocks[idx / SlabSize][idx%SlabSize] = mBlocks[mSize / SlabSize][mSize%SlabSize];
125 }
126
127 T& operator [] (const PxU32 idx)
128 {
129 PX_ASSERT(idx < mSize);
130
131 return mBlocks[idx / SlabSize][idx%SlabSize];
132 }
133
134 const T& operator [] (const PxU32 idx) const
135 {
136 PX_ASSERT(idx < mSize);
137
138 return mBlocks[idx / SlabSize][idx%SlabSize];
139 }
140
141 void pushBack(const T& item)
142 {
143 reserve(mSize + 1);
144 mBlocks[mSize / SlabSize][mSize%SlabSize] = item;
145 mSize++;
146 }
147
148 PxU32 capacity() const { return mCapacity; }
149
150 PxU32 size() const { return mSize; }
151};
152
153}
154}
155
156#endif
157
Definition CmBlockArray.h:47
Definition PxArray.h:53
PX_NOINLINE void resize(const uint32_t size, const T &a=T())
Definition PxArray.h:637
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39