Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BitsArray.h
- #pragma once
- #include <iostream>
- #define SegmentLen 8
- #define Segment unsigned char
- class BitArray
- {
- private:
- unsigned char *bytes;
- int segmentsCount;
- int bitsCount;
- //bool OR(bool, bool);
- //bool AND(bool, bool);
- void CopyObject(BitArray &source);
- BitArray & ForEach(BitArray &a, BitArray &b, bool(*operation)(bool, bool));
- public:
- void Scan(int);
- void Print();
- BitArray(int Capacity = SegmentLen);
- BitArray(const char*);
- BitArray(BitArray&);
- BitArray& operator =(BitArray&);
- BitArray& operator &(BitArray&);
- BitArray& operator &=(BitArray&);
- BitArray& operator |(BitArray&);
- BitArray& operator |=(BitArray&);
- BitArray& operator ^(BitArray&);
- BitArray& operator ^=(BitArray&);
- BitArray& operator ~();
- BitArray& operator <<(int);
- BitArray& operator >>(int);
- bool operator [](int);
- bool GetValue(int);
- int GetBitsCount();
- int GetWeight();
- void SetValue(int, bool);
- void SetValue(int, bool, int);
- void InvertValue(int);
- void InvertValue(int, int);
- ~BitArray();
- };
- BitsArray.cpp
- #include "BitArray.h"
- bool BitArray::GetValue(int index)
- {
- int mask = 1 << (index % SegmentLen);
- return bytes[index / SegmentLen] & mask;
- }
- int BitArray::GetBitsCount()
- {
- return bitsCount;
- }
- int BitArray::GetWeight()
- {
- int sum = 0;
- for (int i = 0; i < bitsCount; i++)
- {
- if (GetValue(i))
- {
- sum++;
- }
- }
- return sum;
- }
- void BitArray::SetValue(int index, bool value)
- {
- int mask = 1 << (index % SegmentLen);
- if (value)
- {
- bytes[index / SegmentLen] = bytes[index / SegmentLen] | mask;
- }
- else
- {
- bytes[index / SegmentLen] = bytes[index / SegmentLen] & (~mask);
- }
- }
- void BitArray::SetValue(int start, bool value, int len)
- {
- for (int i = 0; i < len; i++)
- {
- SetValue(i + start, value);
- }
- }
- void BitArray::InvertValue(int index)
- {
- SetValue(index, GetValue(index) ? false : true);
- }
- void BitArray::InvertValue(int start, int len)
- {
- for (int i = 0; i < len; i++)
- {
- InvertValue(i + start);
- }
- }
- bool AND(bool x , bool y)
- {
- return x && y;
- }
- bool OR(bool x, bool y)
- {
- return x || y;
- }
- bool XOR(bool x, bool y)
- {
- return !(x || y);
- }
- void BitArray::CopyObject(BitArray & source)
- {
- delete bytes;
- bytes = new Segment[source.segmentsCount];
- bitsCount = source.bitsCount;
- segmentsCount = source.segmentsCount;
- for (int i = 0; i < bitsCount; i++)
- {
- SetValue(i, source.GetValue(i));
- }
- }
- BitArray &BitArray::ForEach(BitArray & a, BitArray & b, bool(*operation)(bool, bool))
- {
- int FirstLen = a.bitsCount;
- int SecondLen = b.bitsCount;
- int i = 0, j = 0;
- BitArray *result = new BitArray(FirstLen > SecondLen ? FirstLen : SecondLen);
- while (i < FirstLen && j < SecondLen)
- {
- result->SetValue(i, operation(a.GetValue(i), b.GetValue(i)));
- i++;
- j++;
- }
- while (i < FirstLen)
- {
- result->SetValue(i, a.GetValue(i));
- i++;
- }
- while (j < SecondLen)
- {
- result->SetValue(j, b.GetValue(j));
- j++;
- }
- return *result;
- }
- void BitArray::Scan(int count)
- {
- //TO DO with +=
- }
- void BitArray::Print()
- {
- for (int i = 0; i < bitsCount; i++)
- {
- std::cout << GetValue(i) << ' ';
- }
- std::cout << '\n';
- }
- BitArray::BitArray(int Capacity)
- {
- bitsCount = Capacity;
- segmentsCount = Capacity % SegmentLen == 0 ? Capacity / SegmentLen : Capacity / SegmentLen + 1;
- bytes = new Segment[segmentsCount];
- for (int i = 0; i < segmentsCount; i++)
- {
- bytes[i] = 0;
- }
- }
- BitArray::BitArray(const char *vector)
- {
- bitsCount = strlen(vector);
- segmentsCount = bitsCount % SegmentLen == 0 ? bitsCount / SegmentLen : bitsCount / SegmentLen + 1;
- bytes = new unsigned char[segmentsCount];
- for (int i = 0; i < segmentsCount; i++)
- {
- bytes[i] = 0;
- }
- for (int curentBit = 0; curentBit < bitsCount; curentBit++)
- {
- SetValue(curentBit, vector[curentBit] == '1');
- }
- }
- BitArray::BitArray(BitArray & source)
- {
- CopyObject(source);
- }
- BitArray& BitArray::operator=(BitArray &source)
- {
- CopyObject(source);
- return *this;
- }
- BitArray & BitArray::operator&(BitArray &b)
- {
- return ForEach(*this, b, AND);
- }
- BitArray & BitArray::operator&=(BitArray &b)
- {
- *this = *this & b;
- return *this;
- }
- BitArray & BitArray::operator|(BitArray &b)
- {
- return ForEach(*this, b, OR);
- }
- BitArray & BitArray::operator|=(BitArray &b)
- {
- *this = *this | b;
- return *this;
- }
- BitArray & BitArray::operator^(BitArray &b)
- {
- return ForEach(*this, b, XOR);
- }
- BitArray & BitArray::operator^=(BitArray &b)
- {
- *this = *this | b;
- return *this;
- }
- BitArray & BitArray::operator~()
- {
- BitArray *result = new BitArray(*this);
- for (int i = 0; i < bitsCount; i++)
- {
- result->SetValue(i, result->GetValue(i) ? false : true);
- }
- return *result;
- }
- BitArray & BitArray::operator<<(int shiftLen)
- {
- for (int k = 0; k < shiftLen; k++)
- {
- for (int i = 0; i < bitsCount - k - 1; i++)
- {
- SetValue(i, GetValue(i + 1));
- }
- SetValue(bitsCount - k - 1, false);
- }
- return *this;
- }
- BitArray & BitArray::operator>>(int shiftLen)
- {
- for (int k = 0; k < shiftLen; k++)
- {
- for (int i = bitsCount - k - 1; i > 0; i--)
- {
- SetValue(i, GetValue(i - 1));
- }
- SetValue(k, false);
- }
- return *this;
- }
- bool BitArray::operator[](int index)
- {
- return GetValue(index);
- }
- BitArray::~BitArray()
- {
- delete[]bytes;
- }
- Main.cpp
- #include <iostream>
- #include "BitArray.h"
- int main()
- {
- BitArray StringCounstructorTest("10111000");
- std::cout << "a:\n";
- StringCounstructorTest.Print();
- BitArray newArray("111010001");
- std::cout << "b:\n";
- newArray.Print();
- std::cout << "a & b:\n";
- (StringCounstructorTest&newArray).Print();
- std::cout << "a | b:\n";
- (StringCounstructorTest|newArray).Print();
- std::cout << "b << 2:\n";
- newArray << 2;
- newArray.Print();
- std::cout << "a >> 2:\n";
- StringCounstructorTest >> 2;
- StringCounstructorTest.Print();
- std::cout << "Set false from 1 for 3 elements in a:\n";
- StringCounstructorTest.SetValue(1, false, 3);
- StringCounstructorTest.Print();
- std::cout << "Set true from 1 for 3 elements in b:\n";
- newArray.SetValue(1, true, 3);
- newArray.Print();
- std::cout << "Inversion from 2 for 4 elements in b:\n";
- newArray.InvertValue(2, 4);
- newArray.Print();
- std::cout << "a: length = " << StringCounstructorTest.GetBitsCount() << " weight = " << StringCounstructorTest.GetWeight() << " :\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement