Advertisement
meta1211

Untitled

Dec 9th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.63 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #define SegmentLen 8
  5. #define Segment unsigned char
  6.  
  7. class BitPointer
  8. {
  9.     Segment *byte;
  10.     int bitPosition;
  11. private:
  12.     void SetValue(bool value)
  13.     {
  14.         int mask = 1 << (bitPosition % SegmentLen);
  15.         if (value)
  16.         {
  17.             *byte = *byte | mask;
  18.         }
  19.         else
  20.         {
  21.             *byte = *byte & (~mask);
  22.         }
  23.     }
  24.  
  25.     bool GetValue()
  26.     {
  27.         int mask = 1 << (bitPosition % SegmentLen);
  28.         return *byte & mask;
  29.     }
  30. public:
  31.  
  32.     BitPointer &operator = (bool a)
  33.     {
  34.         SetValue(a);
  35.         return *this;
  36.     }
  37.    
  38.     BitPointer &operator = (BitPointer a)
  39.     {
  40.         SetValue(a);
  41.         return *this;
  42.     }
  43.  
  44.     operator bool()
  45.     {
  46.         return GetValue();
  47.     }
  48.  
  49.     BitPointer(Segment *Byte, int position)
  50.     {
  51.         byte = Byte;
  52.         bitPosition = position;
  53.     }
  54. };
  55.  
  56. class BitArray
  57. {
  58. private:
  59.     unsigned char *bytes;
  60.     int segmentsCount;
  61.     int bitsCount;
  62.     //bool OR(bool, bool);
  63.     //bool AND(bool, bool);
  64.  
  65.     void CopyObject(BitArray &source);
  66.     BitArray& ForEach(BitArray &a, BitArray &b, bool(*operation)(bool, bool));
  67.     int CompareEach(BitArray &a, BitArray &b);
  68.  
  69. public:
  70.     void Scan(int);
  71.     void Print();
  72.     std::string ToString();
  73.  
  74. #pragma region Logic operators
  75.     bool operator ==(BitArray&);
  76.     bool operator !=(BitArray&);
  77.     bool operator >(BitArray&);
  78.     bool operator <(BitArray&);
  79. #pragma endregion
  80.  
  81. #pragma region Countructors
  82.     BitArray(int Capacity = SegmentLen);
  83.     BitArray(const char*);
  84.     BitArray(const char*, int);
  85.     BitArray(BitArray&);
  86.     BitArray(BitArray*);
  87. #pragma endregion
  88.  
  89. #pragma region Transformation operators
  90.     BitArray& operator =(BitArray&);
  91.     BitArray& operator =(BitArray*);
  92.     BitArray& operator &(BitArray&);
  93.     BitArray& operator &=(BitArray&);
  94.     BitArray& operator |(BitArray&);
  95.     BitArray& operator |=(BitArray&);
  96.     BitArray& operator ^(BitArray&);
  97.     BitArray& operator ^=(BitArray&);
  98.     BitArray& operator ~();
  99.     BitArray& operator <<(int);
  100.     BitArray& operator <<=(int);
  101.     BitArray& operator >>(int);
  102.     BitArray& operator >>=(int);
  103.     //bool operator [](int);
  104.     BitPointer operator[](int index);
  105. #pragma endregion
  106.  
  107. #pragma region Seters and geters
  108.     bool GetValue(int);
  109.     int GetBitsCount();
  110.     int GetWeight();
  111.     void SetValue(int, bool);
  112.     void SetValue(int, bool, int);
  113.     void InvertValue(int);
  114.     void InvertValue(int, int);
  115. #pragma endregion
  116.  
  117.     friend std::ostream& operator<<(std::ostream& stream, BitArray& x) {
  118.         stream << x.ToString();
  119.         return stream;
  120.     }
  121.  
  122.     friend std::istream& operator>>(std::istream& stream, BitArray& x) {
  123.         std::string buffer;
  124.         stream >> buffer;
  125.         BitArray a(buffer.c_str());
  126.         x.CopyObject(a);
  127.         return stream;
  128.     }
  129.  
  130.     ~BitArray();
  131. };
  132.  
  133.  
  134. #include "BitArray.h"
  135.  
  136. BitArray test;
  137. #pragma region Seters and geters
  138.  
  139. bool BitArray::GetValue(int index)
  140. {
  141.     int mask = 1 << (index % SegmentLen);
  142.     return bytes[index / SegmentLen] & mask;
  143. }
  144.  
  145. int BitArray::GetBitsCount()
  146. {
  147.     return bitsCount;
  148. }
  149.  
  150. int BitArray::GetWeight()
  151. {
  152.     int sum = 0;
  153.     for (int i = 0; i < bitsCount; i++)
  154.     {
  155.         if (GetValue(i))
  156.         {
  157.             sum++;
  158.         }
  159.     }
  160.     return sum;
  161. }
  162.  
  163. void BitArray::SetValue(int index, bool value)
  164. {
  165.     int mask = 1 << (index % SegmentLen);
  166.     if (value)
  167.     {
  168.         bytes[index / SegmentLen] = bytes[index / SegmentLen] | mask;
  169.     }
  170.     else
  171.     {
  172.         bytes[index / SegmentLen] = bytes[index / SegmentLen] & (~mask);
  173.     }
  174. }
  175.  
  176. void BitArray::SetValue(int start, bool value, int len)
  177. {
  178.     for (int i = 0; i < len; i++)
  179.     {
  180.         SetValue(i + start, value);
  181.     }
  182. }
  183.  
  184. void BitArray::InvertValue(int index)
  185. {
  186.     SetValue(index, !GetValue(index));
  187. }
  188.  
  189. void BitArray::InvertValue(int start, int len)
  190. {
  191.     for (int i = 0; i < len; i++)
  192.     {
  193.         InvertValue(i + start);
  194.     }
  195. }
  196. #pragma endregion
  197.  
  198.  
  199. bool AND(bool x , bool y)
  200. {
  201.     return x && y;
  202. }
  203.  
  204. bool OR(bool x, bool y)
  205. {
  206.     return x || y;
  207. }
  208.  
  209. bool XOR(bool x, bool y)
  210. {
  211.     return (!x && y) || (x && !y);
  212. }
  213.  
  214. void BitArray::CopyObject(BitArray & source)
  215. {
  216.     bytes = new Segment[source.segmentsCount];
  217.     bitsCount = source.bitsCount;
  218.     segmentsCount = source.segmentsCount;
  219.     for (int i = 0; i < bitsCount; i++)
  220.     {
  221.         SetValue(i, source.GetValue(i));
  222.     }
  223. }
  224.  
  225. BitArray& BitArray::ForEach(BitArray & a, BitArray & b, bool(*operation)(bool, bool))
  226. {
  227.     int FirstLen = a.bitsCount;
  228.     int SecondLen = b.bitsCount;
  229.     int i = 0, j = 0;
  230.     BitArray *result = new BitArray(FirstLen > SecondLen ? FirstLen : SecondLen);
  231.     while (i < FirstLen && j < SecondLen)
  232.     {
  233.         result->SetValue(i, operation(a.GetValue(i), b.GetValue(i)));
  234.         i++;
  235.         j++;
  236.     }
  237.     while (i < FirstLen)
  238.     {
  239.         result->SetValue(i, a.GetValue(i));
  240.         i++;
  241.     }
  242.     while (j < SecondLen)
  243.     {
  244.         result->SetValue(j, b.GetValue(j));
  245.         j++;
  246.     }
  247.     return *result;
  248. }
  249.  
  250. int BitArray::CompareEach(BitArray & a, BitArray & b)
  251. {
  252.     BitArray &tempA = a;
  253.     BitArray &tempB = b;
  254.     if (a.bitsCount != b.bitsCount)
  255.     {
  256.         if (a.bitsCount > b.bitsCount)
  257.         {
  258.             BitArray g(b.ToString().c_str(), a.bitsCount);
  259.             tempB = g;
  260.         }
  261.         else
  262.         {
  263.             BitArray g(a.ToString().c_str(), b.bitsCount);
  264.             tempA = g;
  265.         }
  266.     }
  267.     int len = tempA.bitsCount;
  268.     for (int i = 0; i < len; i++)
  269.     {
  270.         if (tempA.GetValue(i) > tempB.GetValue(i))
  271.         {
  272.             return -1;
  273.         }
  274.         else if(tempA.GetValue(i) < tempB.GetValue(i))
  275.         {
  276.             return 1;
  277.         }
  278.     }
  279.     return 0;
  280. }
  281.  
  282. void BitArray::Scan(int count)
  283. {
  284.     std::string buffer;
  285.     std::cin >> buffer;
  286.     if (buffer.length() != count)
  287.     {
  288.         throw std::out_of_range("Wrong length!");
  289.     }
  290.     BitArray a(buffer.c_str(), count);
  291.     CopyObject(a);
  292. }
  293.  
  294. void BitArray::Print()
  295. {
  296.     std::cout << ToString() << '\n';
  297. }
  298.  
  299. std::string BitArray::ToString()
  300. {
  301.     std::string *str = new std::string();
  302.     for (int i = 0; i < bitsCount; i++)
  303.     {
  304.         str->push_back(GetValue(i) ? '1' : '0');
  305.     }
  306.     return *str;
  307. }
  308.  
  309. #pragma region Logic operators
  310. bool BitArray::operator==(BitArray & a)
  311. {
  312.     return  CompareEach(*this, a) == 0;
  313. }
  314.  
  315. bool BitArray::operator!=(BitArray & a)
  316. {
  317.     return  CompareEach(*this, a) != 0;
  318. }
  319.  
  320. bool BitArray::operator>(BitArray & a)
  321. {
  322.     return  CompareEach(*this, a) == -1;
  323. }
  324.  
  325. bool BitArray::operator<(BitArray & a)
  326. {
  327.     return  CompareEach(*this, a) == 1;
  328. }
  329. #pragma endregion
  330.  
  331. #pragma region Transformation operators
  332.  
  333. BitArray& BitArray::operator=(BitArray &source)
  334. {
  335.     CopyObject(source);
  336.     return *this;
  337. }
  338.  
  339. BitArray& BitArray::operator=(BitArray *source)
  340. {
  341.     CopyObject(*source);
  342.     return *this;
  343. }
  344.  
  345. BitArray& BitArray::operator&(BitArray &b)
  346. {
  347.     return *(new BitArray(ForEach(*this, b, AND)));
  348. }
  349.  
  350. BitArray& BitArray::operator&=(BitArray &b)
  351. {
  352.     *this = &(*this & b);
  353.     return *this;
  354. }
  355.  
  356. BitArray& BitArray::operator|(BitArray &b)
  357. {
  358.     return *(new BitArray(ForEach(*this, b, OR)));
  359. }
  360.  
  361. BitArray& BitArray::operator|=(BitArray &b)
  362. {
  363.     *this = &(*this | b);
  364.     return *this;
  365. }
  366.  
  367. BitArray& BitArray::operator^(BitArray &b)
  368. {
  369.     return *(new BitArray(ForEach(*this, b, XOR)));
  370. }
  371.  
  372. BitArray& BitArray::operator^=(BitArray &b)
  373. {
  374.     *this = &(*this | b);
  375.     return *this;
  376. }
  377.  
  378. BitArray& BitArray::operator~()
  379. {
  380.     BitArray *result = new BitArray(this);
  381.     for (int i = 0; i < bitsCount; i++)
  382.     {
  383.         result->SetValue(i, !result->GetValue(i));
  384.     }
  385.     return *result;
  386. }
  387.  
  388. BitArray& BitArray::operator<<(int shiftLen)
  389. {
  390.     BitArray *result = new BitArray(*this);
  391.     for (int k = 0; k < shiftLen; k++)
  392.     {
  393.         for (int i = 0; i < result->GetBitsCount() - k - 1; i++)
  394.         {
  395.             result->SetValue(i, result->GetValue(i + 1));
  396.         }
  397.         result->SetValue(bitsCount - k - 1, false);
  398.     }
  399.     return *result;
  400. }
  401.  
  402. BitArray& BitArray::operator<<=(int index)
  403. {
  404.     *this = &(*this << index);
  405.     return *this;
  406. }
  407.  
  408. BitArray& BitArray::operator>>(int shiftLen)
  409. {
  410.     BitArray *result = new BitArray(*this);
  411.     for (int k = 0; k < shiftLen; k++)
  412.     {
  413.         for (int i = result->GetBitsCount() - k - 1; i > 0; i--)
  414.         {
  415.             result->SetValue(i, result->GetValue(i - 1));
  416.         }
  417.         result->SetValue(k, false);
  418.     }
  419.     return *result;
  420. }
  421.  
  422. BitArray& BitArray::operator>>=(int index)
  423. {
  424.     *this = &(*this >> index);
  425.     return *this;
  426. }
  427.  
  428. BitPointer BitArray::operator[](int index)
  429. {
  430.     return BitPointer(&bytes[index / SegmentLen], index % SegmentLen);
  431. }
  432.  
  433. //bool BitArray::operator[](int index)
  434. //{
  435. //  return GetValue(index);
  436. //}
  437.  
  438.  
  439. #pragma endregion
  440.  
  441. #pragma region Countructors
  442. BitArray::BitArray(int Capacity)
  443. {
  444.     bitsCount = Capacity;
  445.     segmentsCount = Capacity % SegmentLen == 0 ? Capacity / SegmentLen : Capacity / SegmentLen + 1;
  446.     bytes = new Segment[segmentsCount];
  447.     for (int i = 0; i < segmentsCount; i++)
  448.     {
  449.         bytes[i] = 0;
  450.     }
  451. }
  452.  
  453. BitArray::BitArray(const char *vector)
  454. {
  455.     bitsCount = strlen(vector);
  456.     segmentsCount = bitsCount % SegmentLen == 0 ? bitsCount / SegmentLen : bitsCount / SegmentLen + 1;
  457.     bytes = new Segment[segmentsCount];
  458.     for (int i = 0; i < segmentsCount; i++)
  459.     {
  460.         bytes[i] = 0;
  461.     }
  462.     for (int curentBit = 0; curentBit < bitsCount; curentBit++)
  463.     {
  464.         SetValue(curentBit, vector[curentBit] == '1');
  465.     }
  466. }
  467.  
  468. BitArray::BitArray(const char *vector, int len)
  469. {
  470.     int vectorLen = strlen(vector);
  471.     bitsCount = len;
  472.     segmentsCount = len % SegmentLen == 0 ? len / SegmentLen : len / SegmentLen + 1;
  473.     bytes = new Segment[segmentsCount];
  474.     for (int i = 0; i < segmentsCount; i++)
  475.     {
  476.         bytes[i] = 0;
  477.     }
  478.     for (int curentBit = 0; curentBit < vectorLen; curentBit++)
  479.     {
  480.         SetValue(curentBit, vector[curentBit] == '1');
  481.     }
  482. }
  483.  
  484. BitArray::BitArray(BitArray & source)
  485. {
  486.     bytes = new Segment[source.segmentsCount];
  487.     bitsCount = source.bitsCount;
  488.     segmentsCount = source.segmentsCount;
  489.     for (int i = 0; i < bitsCount; i++)
  490.     {
  491.         SetValue(i, source.GetValue(i));
  492.     }
  493. }
  494.  
  495. BitArray::BitArray(BitArray *source)
  496. {
  497.     bytes = new Segment[source->segmentsCount];
  498.     bitsCount = source->bitsCount;
  499.     segmentsCount = source->segmentsCount;
  500.     for (int i = 0; i < bitsCount; i++)
  501.     {
  502.         SetValue(i, source->GetValue(i));
  503.     }
  504. }
  505.  
  506. BitArray::~BitArray()
  507. {
  508.     delete[]bytes;
  509. }
  510. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement