Added BitArray class

This commit is contained in:
Dmitriy Shishkov 2021-12-11 14:00:25 +03:00
parent 1127ffa916
commit 105887d468
6 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,37 @@
#include "pch.h"
#include "BitArray.hpp"
bool BitArray::get(unsigned index)
{
return (v[index / 8] >> (8 - 1 - index % 8)) & 1;
}
void BitArray::set(unsigned index, bool val)
{
if (val)
v[index / 8] |= 1 << (8 - 1 - index % 8);
else
v[index / 8] &= ~(1 << (8 - 1 - index % 8));
}
unsigned BitArray::set(unsigned index, int32_t val, unsigned size)
{
unsigned shift = index % 8, written = 0;
size = std::min(size, this->size - index);
index /= 8;
while (written < size) {
int input_shift = size - (8 - shift) - written;
int right_rem = input_shift < 0 ? -input_shift : 0;
v[index] = ((v[index] >> (8 - shift)) << (8 - shift)) |
(unsigned char)(((1 << (8 - shift)) - 1) & ((input_shift >= 0) ? (val >> input_shift) : (val << -input_shift))) |
(v[index] & ((1 << right_rem) - 1));
written += 8 - shift;
index++;
shift = 0;
}
return written;
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <vector>
using namespace std;
constexpr unsigned ceil_div(unsigned a, unsigned b) {
return a / b + (a % b > 0);
}
class BitArray
{
public:
BitArray(unsigned size_ = 0) : size{ size_ }, v(ceil_div(size_, 8)) {};
unsigned size;
vector<unsigned char> v;
bool get(unsigned index);
void set(unsigned index, bool val);
unsigned set(unsigned index, int32_t val, unsigned size);
};

View File

@ -151,6 +151,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="BitArray.hpp" />
<ClInclude Include="Encode.hpp" />
<ClInclude Include="framework.h" />
<ClInclude Include="Method.hpp" />
@ -159,6 +160,7 @@
<ClInclude Include="Tables.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="BitArray.cpp" />
<ClCompile Include="Encode.cpp" />
<ClCompile Include="Method.cpp" />
<ClCompile Include="pch.cpp">

View File

@ -33,6 +33,9 @@
<ClInclude Include="Encode.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="BitArray.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
@ -50,5 +53,8 @@
<ClCompile Include="Encode.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="BitArray.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

40
tests/BitArray_test.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "pch.h"
#define protected public
#define private public
#include "../QRCodeLibrary/BitArray.hpp"
class BitArrayTests: public ::testing::Test {
public:
BitArrayTests() : ::testing::Test(), a1(15) {};
void SetUp() override {
a1.v[0] = static_cast<unsigned char>(0b10101010);
a1.v[1] = static_cast<unsigned char>(0b11111111);
}
protected:
BitArray a1;
};
TEST_F(BitArrayTests, GetsBitByIndex) {
EXPECT_EQ(a1.get(10), 1);
EXPECT_EQ(a1.get(0), 1);
EXPECT_EQ(a1.get(1), 0);
}
TEST_F(BitArrayTests, SetsIndividualBit) {
a1.set(0, 0);
a1.set(1, 1);
EXPECT_EQ(a1.get(0), 0);
EXPECT_EQ(a1.get(1), 1);
}
TEST_F(BitArrayTests, SetsMultipleBits) {
a1.set(9, 0b00110, 5);
EXPECT_EQ(a1.v[1], 0b10011011);
a1.set(2, 0b111, 3);
EXPECT_EQ(a1.v[0], 0b10111010);
}

View File

@ -36,6 +36,7 @@
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="BitArray_test.cpp" />
<ClCompile Include="Encode_test.cpp" />
<ClCompile Include="Method_test.cpp" />
<ClCompile Include="QRCode_test.cpp" />