gwnavgeneration/common/bitarray_1024.h Source File

bitarray_1024.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
10 
13 
14 
15 namespace Kaim
16 {
17 
18 
19 class BitArray_1024
20 {
21  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
22 public:
23  // nullptr as default value to avoid compilation error in KyArray instantiation
24  // but it should already be used with a valid Heap
25  BitArray_1024(MemoryHeap* heap = nullptr) : m_wordsCount(0), m_rootWord(0), m_words(nullptr), m_allocated(false), m_heap(heap)
26  {}
27 
28  ~BitArray_1024();
29 
30  KyResult Init(void* words, KyInt32 wordsCount);
31 
32  KyResult Init(KyInt32 wordsCount);
33 
34  void ClearBits();
35 
36  void SetBit(KyInt32 bit_idx)
37  {
38  KY_DEBUG_ASSERTN(bit_idx >= 0, ("bit_idx must be greater or equal to 0"));
39 
40  KyUInt32 word_idx = bit_idx >> 5; // x / 32
41  KyUInt32 bit_idx_in_word = bit_idx & 0x1F; // (1 << 5) - 1
42 
43  KY_DEBUG_ASSERTN(word_idx < (KyUInt32)m_wordsCount, ("word_idx out of bounds"));
44 
45  m_rootWord |= (1 << word_idx);
46  m_words[word_idx] |= (1 << bit_idx_in_word);
47  }
48 
49  KyInt32 GetBit(KyInt32 bit_idx) const
50  {
51  KyUInt32 word_idx = bit_idx >> 5; // x / 32
52  KyUInt32 bit_idx_in_word = bit_idx & 0x1F; // (1 << 5) - 1
53  return m_words[word_idx] & (1 << bit_idx_in_word);
54  }
55 
56  KyInt32 GetFirstBitIdx() const;
57 
58  KyInt32 GetLastBitIdx() const;
59 
60 private:
61  KyInt32 m_wordsCount;
62  KyUInt32 m_rootWord;
63  KyUInt32* m_words;
64  bool m_allocated;
65  MemoryHeap* m_heap;
66 };
67 
68 
69 }
70 
71 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::int32_t KyInt32
int32_t
Definition: types.h:24