Advertisement
meta1211

BitsArray

Oct 22nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.32 KB | None | 0 0
  1. BitsArray.h
  2. #pragma once
  3. #include <iostream>
  4. #define SegmentLen 8
  5. #define Segment unsigned char
  6. class BitArray
  7. {
  8. private:
  9.     unsigned char *bytes;
  10.     int segmentsCount;
  11.     int bitsCount;
  12.  
  13.     //bool OR(bool, bool);
  14.     //bool AND(bool, bool);
  15.  
  16.     void CopyObject(BitArray &source);
  17.     BitArray & ForEach(BitArray &a, BitArray &b, bool(*operation)(bool, bool));
  18. public:
  19.     void Scan(int);
  20.     void Print();
  21.  
  22.     BitArray(int Capacity = SegmentLen);
  23.     BitArray(const char*);
  24.     BitArray(BitArray&);
  25.  
  26.     BitArray& operator =(BitArray&);
  27.     BitArray& operator &(BitArray&);
  28.     BitArray& operator &=(BitArray&);
  29.     BitArray& operator |(BitArray&);
  30.     BitArray& operator |=(BitArray&);
  31.     BitArray& operator ^(BitArray&);
  32.     BitArray& operator ^=(BitArray&);
  33.     BitArray& operator ~();
  34.     BitArray& operator <<(int);
  35.     BitArray& operator >>(int);
  36.     bool operator [](int);
  37.  
  38.     bool GetValue(int);
  39.     int GetBitsCount();
  40.     int GetWeight();
  41.     void SetValue(int, bool);
  42.     void SetValue(int, bool, int);
  43.     void InvertValue(int);
  44.     void InvertValue(int, int);
  45.  
  46.     ~BitArray();
  47. };
  48.  
  49. BitsArray.cpp
  50. #include "BitArray.h"
  51.  
  52. bool BitArray::GetValue(int index)
  53. {
  54.     int mask = 1 << (index % SegmentLen);
  55.     return bytes[index / SegmentLen] & mask;
  56. }
  57.  
  58. int BitArray::GetBitsCount()
  59. {
  60.     return bitsCount;
  61. }
  62.  
  63. int BitArray::GetWeight()
  64. {
  65.     int sum = 0;
  66.     for (int i = 0; i < bitsCount; i++)
  67.     {
  68.         if (GetValue(i))
  69.         {
  70.             sum++;
  71.         }
  72.     }
  73.     return sum;
  74. }
  75.  
  76. void BitArray::SetValue(int index, bool value)
  77. {
  78.     int mask = 1 << (index % SegmentLen);
  79.     if (value)
  80.     {
  81.         bytes[index / SegmentLen] = bytes[index / SegmentLen] | mask;
  82.     }
  83.     else
  84.     {
  85.         bytes[index / SegmentLen] = bytes[index / SegmentLen] & (~mask);
  86.     }
  87. }
  88.  
  89. void BitArray::SetValue(int start, bool value, int len)
  90. {
  91.     for (int i = 0; i < len; i++)
  92.     {
  93.         SetValue(i + start, value);
  94.     }
  95. }
  96.  
  97. void BitArray::InvertValue(int index)
  98. {
  99.     SetValue(index, GetValue(index) ? false : true);
  100. }
  101.  
  102. void BitArray::InvertValue(int start, int len)
  103. {
  104.     for (int i = 0; i < len; i++)
  105.     {
  106.         InvertValue(i + start);
  107.     }
  108. }
  109.  
  110. bool AND(bool x , bool y)
  111. {
  112.     return x && y;
  113. }
  114.  
  115. bool OR(bool x, bool y)
  116. {
  117.     return x || y;
  118. }
  119.  
  120. bool XOR(bool x, bool y)
  121. {
  122.     return !(x || y);
  123. }
  124.  
  125. void BitArray::CopyObject(BitArray & source)
  126. {
  127.     delete bytes;
  128.     bytes = new Segment[source.segmentsCount];
  129.     bitsCount = source.bitsCount;
  130.     segmentsCount = source.segmentsCount;
  131.     for (int i = 0; i < bitsCount; i++)
  132.     {
  133.         SetValue(i, source.GetValue(i));
  134.     }
  135. }
  136.  
  137. BitArray &BitArray::ForEach(BitArray & a, BitArray & b, bool(*operation)(bool, bool))
  138. {
  139.     int FirstLen = a.bitsCount;
  140.     int SecondLen = b.bitsCount;
  141.     int i = 0, j = 0;
  142.     BitArray *result = new BitArray(FirstLen > SecondLen ? FirstLen : SecondLen);
  143.     while (i < FirstLen && j < SecondLen)
  144.     {
  145.         result->SetValue(i, operation(a.GetValue(i), b.GetValue(i)));
  146.         i++;
  147.         j++;
  148.     }
  149.     while (i < FirstLen)
  150.     {
  151.         result->SetValue(i, a.GetValue(i));
  152.         i++;
  153.     }
  154.     while (j < SecondLen)
  155.     {
  156.         result->SetValue(j, b.GetValue(j));
  157.         j++;
  158.     }
  159.     return *result;
  160. }
  161.  
  162. void BitArray::Scan(int count)
  163. {
  164.     //TO DO with +=
  165. }
  166.  
  167. void BitArray::Print()
  168. {
  169.     for (int i = 0; i < bitsCount; i++)
  170.     {
  171.         std::cout << GetValue(i) << ' ';
  172.     }
  173.     std::cout << '\n';
  174. }
  175.  
  176. BitArray::BitArray(int Capacity)
  177. {
  178.     bitsCount = Capacity;
  179.     segmentsCount = Capacity % SegmentLen == 0 ? Capacity / SegmentLen : Capacity / SegmentLen + 1;
  180.     bytes = new Segment[segmentsCount];
  181.     for (int i = 0; i < segmentsCount; i++)
  182.     {
  183.         bytes[i] = 0;
  184.     }
  185. }
  186.  
  187. BitArray::BitArray(const char *vector)
  188. {
  189.     bitsCount = strlen(vector);
  190.     segmentsCount = bitsCount % SegmentLen == 0 ? bitsCount / SegmentLen : bitsCount / SegmentLen + 1;
  191.     bytes = new unsigned char[segmentsCount];
  192.     for (int i = 0; i < segmentsCount; i++)
  193.     {
  194.         bytes[i] = 0;
  195.     }
  196.     for (int curentBit = 0; curentBit < bitsCount; curentBit++)
  197.     {
  198.         SetValue(curentBit, vector[curentBit] == '1');
  199.     }
  200. }
  201.  
  202. BitArray::BitArray(BitArray & source)
  203. {
  204.     CopyObject(source);
  205. }
  206.  
  207. BitArray& BitArray::operator=(BitArray &source)
  208. {
  209.     CopyObject(source);
  210.     return *this;
  211. }
  212.  
  213. BitArray & BitArray::operator&(BitArray &b)
  214. {
  215.     return ForEach(*this, b, AND);
  216. }
  217.  
  218. BitArray & BitArray::operator&=(BitArray &b)
  219. {
  220.     *this = *this & b;
  221.     return *this;
  222. }
  223.  
  224. BitArray & BitArray::operator|(BitArray &b)
  225. {
  226.     return ForEach(*this, b, OR);
  227. }
  228.  
  229. BitArray & BitArray::operator|=(BitArray &b)
  230. {
  231.     *this = *this | b;
  232.     return *this;
  233. }
  234.  
  235. BitArray & BitArray::operator^(BitArray &b)
  236. {
  237.     return ForEach(*this, b, XOR);
  238. }
  239.  
  240. BitArray & BitArray::operator^=(BitArray &b)
  241. {
  242.     *this = *this | b;
  243.     return *this;
  244. }
  245.  
  246. BitArray & BitArray::operator~()
  247. {
  248.     BitArray *result = new BitArray(*this);
  249.     for (int i = 0; i < bitsCount; i++)
  250.     {
  251.         result->SetValue(i, result->GetValue(i) ? false : true);
  252.     }
  253.     return *result;
  254. }
  255.  
  256. BitArray & BitArray::operator<<(int shiftLen)
  257. {
  258.     for (int k = 0; k < shiftLen; k++)
  259.     {
  260.         for (int i = 0; i < bitsCount - k - 1; i++)
  261.         {
  262.             SetValue(i, GetValue(i + 1));
  263.         }
  264.         SetValue(bitsCount - k - 1, false);
  265.     }
  266.     return *this;
  267. }
  268.  
  269. BitArray & BitArray::operator>>(int shiftLen)
  270. {
  271.     for (int k = 0; k < shiftLen; k++)
  272.     {
  273.         for (int i = bitsCount - k - 1; i > 0; i--)
  274.         {
  275.             SetValue(i, GetValue(i - 1));
  276.         }
  277.         SetValue(k, false);
  278.     }
  279.     return *this;
  280. }
  281.  
  282. bool BitArray::operator[](int index)
  283. {
  284.     return GetValue(index);
  285. }
  286.  
  287. BitArray::~BitArray()
  288. {
  289.     delete[]bytes;
  290. }
  291.  
  292. Main.cpp
  293. #include <iostream>
  294. #include "BitArray.h"
  295.  
  296. int main()
  297. {
  298.     BitArray StringCounstructorTest("10111000");
  299.     std::cout << "a:\n";
  300.     StringCounstructorTest.Print();
  301.     BitArray newArray("111010001");
  302.     std::cout << "b:\n";
  303.     newArray.Print();
  304.     std::cout << "a & b:\n";
  305.     (StringCounstructorTest&newArray).Print();
  306.     std::cout << "a | b:\n";
  307.     (StringCounstructorTest|newArray).Print();
  308.     std::cout << "b << 2:\n";
  309.     newArray << 2;
  310.     newArray.Print();
  311.     std::cout << "a >> 2:\n";
  312.     StringCounstructorTest >> 2;
  313.     StringCounstructorTest.Print();
  314.     std::cout << "Set false from 1 for 3 elements in a:\n";
  315.     StringCounstructorTest.SetValue(1, false, 3);
  316.     StringCounstructorTest.Print();
  317.     std::cout << "Set true from 1 for 3 elements in b:\n";
  318.     newArray.SetValue(1, true, 3);
  319.     newArray.Print();
  320.     std::cout << "Inversion from 2 for 4 elements in b:\n";
  321.     newArray.InvertValue(2, 4);
  322.     newArray.Print();
  323.     std::cout << "a: length = " << StringCounstructorTest.GetBitsCount() << " weight = " << StringCounstructorTest.GetWeight() << " :\n";
  324.     return 0;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement