markyrocks

c++

May 18th, 2021
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. #include <Windows.h>
  2. #include<string>
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. struct b {
  7. private:
  8. byte control;
  9. size_t size;
  10. size_t index;
  11. unsigned long long realsize;
  12. int bit;
  13. byte* _byte;
  14. public:
  15.  
  16. b() : index(0), bit(0), size(1), realsize(1), control(0) {
  17. _byte = new byte[1]{};
  18. this->populate();
  19. }
  20. b(unsigned long long i) : index(0), bit(0), size(ceil(i/8.0) ? ceil(i / 8.0) : 1), realsize(i),control(0) {
  21. _byte = new byte[ size ];
  22. this->populate();
  23. }
  24. b(unsigned long long i, byte* bi) : index(0), bit((i % 8)), size(ceil(i / 8.0)), realsize(i),control(*bi) {
  25. _byte = new byte[size];
  26. this->populate();
  27. this->clearRemainder();
  28. }
  29. b(unsigned long long i, bool bo) : index(0), bit((i % 8)), size(i / 8.0 ? ceil(i / 8.0) : 1), realsize(i), control( bo==1 ? 255 : 0) {
  30. _byte = new byte[size];
  31. this->populate();
  32. this->clearRemainder();
  33. }
  34. ~b() {
  35. delete[] _byte;
  36. }
  37. private:
  38.  
  39. void realocate() {
  40. byte* t = (byte*)malloc(this->size+1); //get new memory
  41.  
  42. if (t) {
  43. ZeroMemory(t, size + 1);
  44. memcpy(t, this->_byte, size);
  45. delete[] this->_byte;
  46.  
  47. this->_byte = t; //watch this go down in flames.
  48. size++;
  49. }
  50. }
  51. void realocate(unsigned long long ull) {
  52. this->setIndexAndBit(ull);
  53. byte* t = (byte*)malloc(this->index + 1);
  54.  
  55. if (t) {
  56. ZeroMemory(t, index + 1);
  57. memcpy(t, this->_byte, size);
  58. delete[] this->_byte;
  59.  
  60. this->_byte = t;
  61. size = index + 1;
  62. }
  63. }
  64. void setIndexAndBit(unsigned long long i) {
  65. this->bit = i % 8;
  66. this->index = (i - bit) > 7 ? (i - bit) >> 3 : 0;
  67. }
  68. void populate() {
  69. for (size_t i = 0; i < size;i++) {
  70. _byte[i] = control;
  71. }
  72. }
  73. void clearRemainder() {
  74. if (!bit) { return; }
  75. for (int i=7;i> bit-1; i--) {
  76. this->_byte[size - 1] &= ~(1 << (7 - i));
  77. }
  78. bit = 0;
  79. }
  80. string ByteToString() {
  81. string s{};
  82. for (int i = 0; i < 8; i++) {
  83. if (checkbit(i)) { s.push_back('1'); }
  84. else { s.push_back('0'); }
  85. }
  86. return s;
  87. }
  88. bool checkbit(int bi) {
  89. if (this->index + 1 > this->size) { return false; }
  90. if (this->_byte[this->index] == (this->_byte[index] | (1 << (7 - bi)))) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. bool checkbit(byte bi, int i) {
  96. if (bi == (bi | (1 << (7 - i)))) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. void bitshiftright(unsigned long long ull) {
  102. unsigned long long s = (unsigned long long)this->size;
  103. if (realsize + ull > s * 8) {
  104. this->realocate(realsize + ull);
  105. }
  106. for (unsigned long long i = realsize - 1; ; i--) {
  107. this->SetBit(i+ull, test(i));
  108. if (i == 0) { break; }
  109. }
  110.  
  111. for (unsigned long long i = 0; i < ull; i++) { //cleans up the left
  112. this->SetBit(i, 0);
  113. this->SetBit(realsize + i, 0);
  114. }
  115. this->shrink();
  116. }
  117. void bitshiftleft(unsigned long long ull) {
  118. unsigned long long s = (unsigned long long)this->size;
  119. if (realsize + ull > s * 8) {
  120. this->realocate(realsize + ull);
  121. }
  122. for (unsigned long long i = 0; i < realsize; i++) {
  123. this->SetBit(i, test(i + ull));
  124. }
  125. for (unsigned long long i = realsize; i < ull + realsize; i++) { //cleans up the right
  126. this->SetBit(i, 0);
  127. }
  128. }
  129. void alignright() {
  130.  
  131. while (!this->_byte[this->size - 1]) {
  132. for (auto i = size - 1; i > 0; i--) {
  133. if (!this->_byte[i]) {
  134. this->_byte[i] = this->_byte[i - 1];
  135. this->_byte[i - 1] = 0;
  136. }
  137. }
  138. }
  139. unsigned long long s = (unsigned long long)this->size;
  140. this->realsize = s * 8;
  141. }
  142. bool compare(bool bo) {
  143. if (bo) {
  144. return _byte[index] == (_byte[index] | (1 << (7 - bit)));
  145. }
  146. else {
  147. return _byte[index] == (_byte[index] & ~(1 << (7 - bit)));
  148. }
  149. }
  150. b& copy(b& other) {
  151. while (other.realsize > this->realsize) { this->pushback(false); }
  152. memcpy(this->_byte, other._byte, size);
  153. return *this;
  154. }
  155. public:
  156.  
  157. void shrink() {
  158. unsigned long long ull = size - 1;
  159.  
  160. while (_byte[ull] == 0) {
  161. ull--;
  162. if (ull == 0) { break; }
  163. }
  164. this->resize(((ull+1) << 3));
  165. }
  166. void operator>>(unsigned long long ull) {
  167. this->bitshiftright(ull);
  168. }
  169. void operator<<(unsigned long long ull) {
  170. return this->bitshiftleft(ull);
  171. }
  172. bool operator==(bool bo) {
  173. return this->compare(bo);
  174. }
  175. void operator=(bool bo) {
  176. if (bo) {
  177. this->_byte[index] |= (1 << (7 - bit));
  178. }
  179. else {
  180. this->_byte[index] &= (~(1 << (7 - bit)));
  181. }
  182. }
  183. b& operator=(b& other) {
  184.  
  185. return this->copy(other);
  186. }
  187. b& operator-(b& other) {
  188.  
  189. }
  190.  
  191. b& subtract(b& other) { //untested not finished
  192.  
  193. if (this->realsize < other.realsize) {
  194. while (this->realsize < other.realsize) { this->bitshiftright(1); } //not efficient
  195. }
  196. else if (other.realsize < this->realsize) {
  197. while (other.realsize < this->realsize) { other.bitshiftright(1); }
  198. }
  199.  
  200. //this->bitshiftright(1);
  201. //other.bitshiftright(1);
  202. other.shrink();
  203. this->shrink();
  204.  
  205. for (unsigned long long i = 0; i < realsize; i++) {
  206.  
  207. if ( this->test(i) == 1 && other.test(i) == 0 ) {
  208. this->SetBit(i, 1);//unecessary
  209. }
  210. else if (this->test(i) == 1 && other.test(i) == 1) {
  211. this->SetBit(i, 0);
  212. }
  213. else if (this->test(i) == 0 && other.test(i) == 1) {
  214. for (unsigned long long j = i;; j--) {
  215. if (this->test(j) == 0) {
  216. this->SetBit(j, 1);
  217. }
  218. else if (this->test(j) == 1) {
  219. this->SetBit(j, 0);
  220. break;
  221. }
  222.  
  223. if (j == 0) { break; } //gets to here it would be negitive... not sure how to handle that
  224. }
  225.  
  226. }
  227.  
  228. }
  229. //this->bitshiftright(7);
  230. //this->shrink();
  231. return *this;
  232.  
  233. }
  234.  
  235. b& operator[](unsigned long long i) {
  236. this->setIndexAndBit(i) ;
  237. if (index + 1 > size) {
  238. this->realocate();
  239. this->realsize = i + 1;
  240. }
  241. return *this;
  242. }
  243. b& operator+(b& other) {
  244.  
  245. return this->add(other);
  246. }
  247. b& add(b& other) {
  248.  
  249. while (this->realsize < other.realsize) { this->pushback(false); }
  250.  
  251. while (other.realsize < this->realsize) { other.pushback(false); }
  252.  
  253. /*this->print();
  254. cout << "\n";
  255. other.print();*/
  256.  
  257.  
  258. this->pushback(false);
  259. other.pushback(false);
  260. this->bitshiftright(1);
  261. other.bitshiftright(1);
  262. this->alignright();
  263. other.alignright();
  264.  
  265. //this->print();
  266. //cout << "\n";
  267. //other.print();
  268. for (unsigned long long i = 0; i < realsize; i++) {
  269.  
  270. if ((this->test(i) == 0 && other.test(i) == 1) || (this->test(i) == 1 && other.test(i) == 0)) {
  271. this->SetBit(i, 1);
  272. }
  273. else if (this->test(i) == 1 && other.test(i) == 1) {
  274. for (unsigned long long j = i;; j--) {
  275. if (this->test(j) == 1) {
  276. this->SetBit(j, 0);
  277. }
  278. else if(this->test(j)==0){
  279. this->SetBit(j, 1);
  280. break;
  281. }
  282.  
  283. if (j == 0) { break; } //if it gets to here something went very wrong
  284. }
  285.  
  286. }
  287.  
  288. }
  289. return *this;
  290. }
  291. void resize(unsigned long long ull) {
  292. this->setIndexAndBit(ull-1);
  293. byte* t = (byte*)malloc(this->index + 1);
  294.  
  295. if (t) {
  296. ZeroMemory(t, index + 1);
  297. memcpy(t, this->_byte, index + 1);
  298. delete[] this->_byte;
  299. this->_byte = t;
  300. size = index + 1;
  301. }
  302. this->realsize = ull; //this could be clear remainder...
  303. int i = 0;
  304. while ((realsize + i) % 8 != 0) {
  305. this->SetBit(realsize + i, 0);
  306. i++;
  307. }
  308. }
  309. unsigned long long capacity() {
  310. unsigned long long s = (unsigned long long)this->size;
  311. return s << 3;
  312. }
  313. void SetBit(unsigned long long ull, bool bo) {
  314. unsigned long long s = (unsigned long long)this->size;
  315. if (ull > (s<<3) - 1) {
  316. this->realocate(ull);
  317. this->realsize = ull + 1;
  318. }
  319. this->setIndexAndBit(ull);
  320. if (bo) {
  321. this->_byte[index] |= (1 << (7 - bit));
  322. }
  323. else {
  324. this->_byte[index] &= (~(1 << (7 - bit)));
  325. }
  326. }
  327. bool isEmpty() {
  328. return !realsize;
  329. }
  330. void swap(unsigned long long b1,unsigned long long b2) {
  331. bool bb1 = this->test(b1);
  332. this->SetBit(b1,this->test(b2));
  333. this->SetBit(b2, bb1);
  334. }
  335. bool test(unsigned long long ull) {
  336. this->setIndexAndBit(ull);
  337. return this->checkbit(bit);
  338. }
  339. void pushback(bool bo) {
  340. unsigned long long s = (unsigned long long)this->size;
  341. if (realsize+1 > s << 3 ) {
  342. this->realocate();
  343. }
  344. realsize++;
  345. this->setIndexAndBit(realsize - 1);
  346. this->SetBit(realsize - 1, bo);
  347. }
  348. void pushback(byte* bp) { //this might be useless
  349. for (int i = 0; i < 8; i++) {
  350. this->pushback(checkbit(*bp, i));
  351. }
  352. }
  353. void clear() {
  354. this->control = 0b00000000;
  355. this->populate();
  356. }
  357. void popback() {
  358. this->SetBit(realsize - 1, 0);
  359. realsize--;
  360. }
  361. void insert(unsigned long long ull, bool bo) {
  362. unsigned long long s = (unsigned long long)this->size;
  363. if (ull > ( s << 3 ) -1 ) {
  364. this->SetBit(ull,bo);
  365. return;
  366. }
  367. else if (realsize + 1 > s << 3 ) {
  368. this->realocate();
  369. }
  370. for (unsigned long long i = realsize; i > ull; i--) {
  371. this->swap(i, i - 1);
  372. }
  373. this->SetBit(ull, bo);
  374. realsize++;
  375. }
  376. void flip(unsigned long long ull) {
  377. this->setIndexAndBit(ull);
  378. if (this->checkbit(bit)) {
  379. this->SetBit(ull, 0);
  380. }
  381. else { this->SetBit(ull, 1); }
  382. }
  383. void print() {
  384. for (size_t i = 0; i < size; i++) {
  385. index = i;
  386. cout << this->ByteToString() << "\n";
  387. }
  388.  
  389. }
  390. unsigned long long count() {
  391. unsigned long long c{0};
  392.  
  393. for (unsigned long long i = 0; i < realsize; i++) {
  394. if (this->test(i)) {
  395. c++;
  396. }
  397. }
  398. return c;
  399. }
  400. unsigned long long Size() {
  401. return this->realsize;
  402. }
  403. };
  404.  
  405.  
  406. int main() {
  407. b a(16, true);
  408. b ab(8, true);
  409. b c{};
  410. ab.SetBit(21, true);
  411. //cout << ab.Size();
  412. //cout << "\n";
  413. //ab.print();
  414. //cout << "\n";
  415. //a.print();
  416. c = a + ab;
  417.  
  418. c.print();
  419. //cout << "\n";
  420. //c >> 5;
  421. //c.print();
  422. }
  423.  
Advertisement
Add Comment
Please, Sign In to add comment