Skip to content

Commit e6bfa8b

Browse files
committed
Added function to arrays to get first element
1 parent aa7a8dc commit e6bfa8b

File tree

9 files changed

+161
-27
lines changed

9 files changed

+161
-27
lines changed

engine/render/renderer/platform/vulkan/VertexBuffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ VertexBuffer::VertexBuffer(unsigned long bufferSize) : size {bufferSize}
3232

3333
VertexBuffer::VertexBuffer(void* data, unsigned long bufferSize) : VertexBuffer(bufferSize)
3434
{
35-
Copy(data, bufferSize, 0);
35+
Copy(data, bufferSize);
3636
}
3737

3838
void VertexBuffer::Copy(const void* data, unsigned long dSize, unsigned long offset)

engine/utils/collections/BitSet.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,8 @@ unsigned long CalculateLeftMostBit(const unsigned char& byte)
7878

7979
unsigned long GetBitPosIndex(const unsigned long& bit)
8080
{
81-
switch (bit)
82-
{
83-
case 1:
84-
return GET_BIT_POS(1);
85-
case 2:
86-
return GET_BIT_POS(2);
87-
case 4:
88-
return GET_BIT_POS(4);
89-
case 8:
90-
return GET_BIT_POS(8);
91-
case 16:
92-
return GET_BIT_POS(16);
93-
case 32:
94-
return GET_BIT_POS(32);
95-
case 64:
96-
return GET_BIT_POS(64);
97-
case 128:
98-
return GET_BIT_POS(128);
99-
default:
100-
return 0;
101-
}
81+
return ((bit == 1) * 1) + ((bit == 2) * 2) + ((bit == 4) * 3) + ((bit == 8) * 4) +
82+
((bit == 16) * 5) + ((bit == 32) * 6) + ((bit == 64) * 7) + ((bit == 128) * 8);
10283
}
10384

10485
void SetBitsToOne(unsigned char* bitfield, const unsigned long& bits)

engine/utils/collections/BitSet.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,21 @@ class BitSet
266266
return leftMostBit;
267267
}
268268

269+
inline unsigned long FirstSetBit() const
270+
{
271+
using namespace BitUtils;
272+
273+
unsigned long bit = 0;
274+
for (unsigned int i = 0; i < size; i++)
275+
{
276+
auto index = GetBitPosIndex((bitfield[i] ^ (bitfield[i] & (bitfield[i] - 1))));
277+
bit = (index > 0) * (i * BYTE_SIZE_IN_BITS) + index;
278+
if (bit > 0) break;
279+
}
280+
281+
return bit;
282+
}
283+
269284
/**
270285
* Sets a specified bit to 0.
271286
* @param bit the bit to unset, ranging from 1 - bytes * 8.
@@ -396,6 +411,21 @@ class SBitSet
396411
return leftMostBit;
397412
}
398413

414+
inline unsigned long FirstSetBit() const
415+
{
416+
using namespace BitUtils;
417+
418+
unsigned long bit = 0;
419+
for (unsigned int i = 0; i < S; i++)
420+
{
421+
auto index = GetBitPosIndex((bitfield[i] ^ (bitfield[i] & (bitfield[i] - 1))));
422+
bit = (index > 0) * (i * BYTE_SIZE_IN_BITS) + index;
423+
if (bit > 0) break;
424+
}
425+
426+
return bit;
427+
}
428+
399429
/**
400430
* Resets the BitSet to 0.
401431
*/

engine/utils/collections/HeapArray.h

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class MHArray
399399
}
400400

401401
/**
402-
* Returns the element in the MSArray
402+
* Returns the last element in the MSArray
403403
* @return the last element in the array
404404
*/
405405
inline const T& Back() const
@@ -408,14 +408,32 @@ class MHArray
408408
}
409409

410410
/**
411-
* Returns the element in the MSArray
411+
* Returns the last element in the MSArray
412412
* @return the last element in the array
413413
*/
414414
inline T& Back()
415415
{
416416
return data[bitField.LeftMostBit() - 1];
417417
}
418418

419+
/**
420+
* Returns the first element in the MSArray
421+
* @return the first element in the array
422+
*/
423+
inline const T& Front() const
424+
{
425+
return data[bitField.FirstSetBit() - 1];
426+
}
427+
428+
/**
429+
* Returns the first element in the MSArray
430+
* @return the first element in the array
431+
*/
432+
inline T& Front()
433+
{
434+
return data[bitField.FirstSetBit() - 1];
435+
}
436+
419437
private:
420438

421439
// Private Constructors
@@ -449,6 +467,27 @@ class MHArray
449467

450468
// Functions
451469

470+
friend class MIter<MHArray<T>, T>;
471+
friend class CMIter<MHArray<T>, T>;
472+
473+
/**
474+
* Returns the index of the first element of the array. Used primarily for iterators
475+
* @return the index of the first element of the array
476+
*/
477+
inline size_t GetFirstElementIdx()
478+
{
479+
return bitField.FirstSetBit() - 1;
480+
}
481+
482+
/**
483+
* Returns the index of the first element of the array. Used primarily for const iterators
484+
* @return the index of the first element of the array
485+
*/
486+
inline const size_t GetFirstElementIdx() const
487+
{
488+
return bitField.FirstSetBit() - 1;
489+
}
490+
452491
/**
453492
* @brief Sets an element in the array to a corresponding value
454493
* @param index the index to insert the element into

engine/utils/collections/Iterators.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class MIter
243243
*/
244244
inline constexpr MIter(A* arrPtr) : ptr {arrPtr}
245245
{
246+
index = arrPtr->GetFirstElementIdx();
246247
ptr = arrPtr->Data() && arrPtr->Count() > 0 ? arrPtr : nullptr;
247248

248249
while (ptr && !ptr->IsActive(index)) index++;
@@ -359,6 +360,7 @@ class CMIter
359360
*/
360361
inline constexpr CMIter(const A* arrPtr) : ptr {arrPtr}
361362
{
363+
index = arrPtr->GetFirstElementIdx();
362364
ptr = arrPtr->Data() && arrPtr->Count() > 0 ? arrPtr : nullptr;
363365

364366
while (ptr && !ptr->IsActive(index)) index++;

engine/utils/collections/StackArray.h

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class MSArray
460460
}
461461

462462
/**
463-
* Returns the element in the MSArray
463+
* Returns the last element in the MSArray
464464
* @return the last element in the array
465465
*/
466466
inline const T& Back() const
@@ -469,16 +469,55 @@ class MSArray
469469
}
470470

471471
/**
472-
* Returns the element in the MSArray
472+
* Returns the last element in the MSArray
473473
* @return the last element in the array
474474
*/
475475
inline T& Back()
476476
{
477477
return data[bitField.LeftMostBit() - 1];
478478
}
479479

480+
/**
481+
* Returns the first element in the MSArray
482+
* @return the first element in the array
483+
*/
484+
inline const T& Front() const
485+
{
486+
return data[bitField.FirstSetBit() - 1];
487+
}
488+
489+
/**
490+
* Returns the first element in the MSArray
491+
* @return the first element in the array
492+
*/
493+
inline T& Front()
494+
{
495+
return data[bitField.FirstSetBit() - 1];
496+
}
497+
480498
private:
481499

500+
friend class MIter<MSArray<T, S>, T>;
501+
friend class CMIter<MSArray<T, S>, T>;
502+
503+
/**
504+
* Returns the index of the first element of the array. Used primarily for iterators
505+
* @return the index of the first element of the array
506+
*/
507+
inline size_t GetFirstElementIdx()
508+
{
509+
return bitField.FirstSetBit() - 1;
510+
}
511+
512+
/**
513+
* Returns the index of the first element of the array. Used primarily for const iterators
514+
* @return the index of the first element of the array
515+
*/
516+
inline const size_t GetFirstElementIdx() const
517+
{
518+
return bitField.FirstSetBit() - 1;
519+
}
520+
482521
/**
483522
* A private constructor for the MSArray. Simply initialises the count value of the array.
484523
* @param arrSize the amount to set the count to.

tests/utils/test_BitMaskField.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,29 @@ UTEST(test_BitSet, MoveBitField)
259259
ASSERT_EQ(0, bitMaskA.LeftMostBit());
260260
}
261261

262+
UTEST(test_BitSet, TestGetFirstSetBit)
263+
{
264+
Siege::BitUtils::BitSet bitMask(2);
265+
auto firstSet = bitMask.FirstSetBit();
266+
267+
ASSERT_EQ(0, firstSet);
268+
269+
bitMask.SetBit(5);
270+
bitMask.SetBit(7); // 0000 1010
271+
272+
ASSERT_EQ(5, bitMask.FirstSetBit());
273+
ASSERT_EQ(7, bitMask.LeftMostBit());
274+
275+
bitMask.UnsetBit(5);
276+
ASSERT_EQ(7, bitMask.FirstSetBit());
277+
278+
bitMask.SetBit(12);
279+
ASSERT_EQ(7, bitMask.FirstSetBit());
280+
281+
bitMask.SetBit(1);
282+
ASSERT_EQ(1, bitMask.FirstSetBit());
283+
}
284+
262285
// --------------------------------------- SBitSet ------------------------------------------------
263286

264287
UTEST(test_SBitSet, CreateBitSet)
@@ -433,4 +456,4 @@ UTEST(test_SBitSet, CopyBitField)
433456
ASSERT_EQ(10, bitMaskB[0]);
434457
ASSERT_EQ(32, bitMaskB[1]);
435458
ASSERT_EQ(1, bitMaskB[2]);
436-
}
459+
}

tests/utils/test_MHArray.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,13 @@ UTEST(test_MHArray, GetItemAtBackOfMHArray)
631631

632632
ASSERT_EQ(4, arrayB.Back());
633633
}
634+
635+
UTEST(test_MHArray, GetItemAtFrontOfMHArray)
636+
{
637+
MHArray<uint32_t> arrayA {0, 1, 2, 3, 4};
638+
const MHArray<uint32_t> arrayB {10, 1, 2, 3, 4};
639+
640+
ASSERT_EQ(0, arrayA.Front());
641+
642+
ASSERT_EQ(10, arrayB.Front());
643+
}

tests/utils/test_MSArray.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,14 @@ UTEST(test_MSArray, GetItemAtBackOfMSArray)
673673
ASSERT_EQ(4, arrayA.Back());
674674

675675
ASSERT_EQ(4, arrayB.Back());
676+
}
677+
678+
UTEST(test_MSArray, GetItemAtFrontOfMSArray)
679+
{
680+
MSArray<uint32_t, 5> arrayA {0, 1, 2, 3, 4};
681+
const MSArray<uint32_t, 5> arrayB {10, 1, 2, 3, 4};
682+
683+
ASSERT_EQ(0, arrayA.Front());
684+
685+
ASSERT_EQ(10, arrayB.Front());
676686
}

0 commit comments

Comments
 (0)