Skip to content

Commit 5ba993d

Browse files
authored
Merge pull request #8 from bugparty/codex/add-bv_bounds_check-option-and-update-checks
Add optional bounds checking toggle
2 parents 02e8a3c + ab4a138 commit 5ba993d

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.21)
22
project(bitvector)
33

44
set(CMAKE_CXX_STANDARD 17)
5+
option(BV_BOUNDS_CHECK "Enable bounds checking in bitvector" OFF)
6+
if(NOT BV_BOUNDS_CHECK)
7+
add_compile_definitions(BITVECTOR_DISABLE_BOUNDS_CHECK)
8+
endif()
59
if(MSVC)
610
# Set compiler flags for all configurations
711
#add_compile_options("/O2" "/Zi" "/DEBUG")

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# bitvector
2+
3+
This repository provides a small bit vector implementation along with tests and benchmarks.
4+
5+
## Running the benchmarks without bounds checking
6+
7+
Bounds checking is enabled by default. To benchmark without checks, configure and build with:
8+
9+
```bash
10+
cmake -S . -B build -DBV_BOUNDS_CHECK=OFF -DCMAKE_BUILD_TYPE=Release
11+
cmake --build build --config Release
12+
./build/bitvector_benchmark
13+
```

bitvector.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ namespace bowen
208208

209209
reference operator[](size_t pos)
210210
{
211+
#ifndef BITVECTOR_DISABLE_BOUNDS_CHECK
211212
if (pos >= m_size){
212213
std::stringstream ss;
213214
ss << "bitvector index out of range" << "pos: "<< pos << " size: " << m_size << std::endl;
214215
throw std::out_of_range(ss.str());
215216
}
217+
#endif
216218

217219
size_t word_index = pos >> WORD_SHIFT;
218220
BitType mask = static_cast<BitType>(1) << (pos & (WORD_BITS - 1));
@@ -221,21 +223,25 @@ namespace bowen
221223

222224
bool operator[](size_t pos) const
223225
{
226+
#ifndef BITVECTOR_DISABLE_BOUNDS_CHECK
224227
if (pos >= m_size){
225228
std::stringstream ss;
226229
ss << "bitvector index out of range" << "pos: "<< pos << " size: " << m_size << std::endl;
227230
throw std::out_of_range(ss.str());
228231
}
232+
#endif
229233
size_t word_index = pos >> WORD_SHIFT;
230234
BitType mask = static_cast<BitType>(1) << (pos & (WORD_BITS - 1));
231235
return (m_data[word_index] & mask) != 0;
232236
}
233237
inline void set_bit(size_t pos, bool value){
238+
#ifndef BITVECTOR_DISABLE_BOUNDS_CHECK
234239
if (pos >= m_size){
235240
std::stringstream ss;
236241
ss << "bitvector index out of range" << "pos: "<< pos << " size: " << m_size << std::endl;
237242
throw std::out_of_range(ss.str());
238243
}
244+
#endif
239245
BitType mask = 1UL << (pos % WORD_BITS);
240246
BitType * ptr = &m_data[pos / WORD_BITS];
241247
if (value)
@@ -316,12 +322,14 @@ namespace bowen
316322
}
317323
void incrementUntilZero(size_t& pos){
318324
// Ensure the position is within bounds
325+
#ifndef BITVECTOR_DISABLE_BOUNDS_CHECK
319326
if (pos >= m_size){
320327
std::stringstream ss;
321328
ss << "bitvector index out of range" << "pos: "<< pos << " size: " << m_size << std::endl;
322329
throw std::out_of_range(ss.str());
323330
return;
324331
}
332+
#endif
325333
while (pos < m_size&& pos%WORD_BITS!=0 && (*this)[pos] != 0) // Check if bit at pos is 1
326334
{
327335
++pos; // Increment pos to the next bit

0 commit comments

Comments
 (0)