Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include "BigNumber.h"
  2. #include "BigNumberIterator.h"
  3.  
  4. #include <iostream>
  5.  
  6. BigNumber::BigNumber() {
  7. }
  8.  
  9. BigNumber::BigNumber(std::string input) { //Remember to check if all literals are not digits!
  10.     uint32_t input_len = input.size();
  11.  
  12.     if (input_len == 0) {
  13.         throw "String used for class instance construction must not be empty.";
  14.     }
  15.  
  16.     if (input_len == 1 && input[0] == 0) {
  17.         this->size = 0;
  18.         return;
  19.     }
  20.  
  21.     uint32_t current_alloc_size = BigNumber::ALLOC_SIZE;
  22.     uint32_t start, len, current_bit;
  23.  
  24.     start = this->is_negative = input[0] == '-';
  25.     len = current_bit = 0;
  26.  
  27.     this->add_new_block_to_list(current_alloc_size);
  28.     Block *current_block = &this->blocks.back(); //Points to created element on the list.
  29.  
  30.     int64_t *current_elements = current_block->get_bits_array();
  31.     int64_t current_element = 0;
  32.     uint32_t BITS_PER_ELEMENT = sizeof(uint64_t) * Architecture::PlatformChecks::bits_per_byte(); // should move to header
  33.     while (start < input_len - 1 || input[input_len - 1] != '0') {
  34.         if (current_bit == BITS_PER_ELEMENT) {
  35.             current_bit = 0;
  36.             ++current_element;
  37.             ++len;
  38.             if (current_element == current_alloc_size) {
  39.                 current_alloc_size = current_alloc_size << 1;
  40.                
  41.                 this->add_new_block_to_list(current_alloc_size);
  42.                 Block *current_block = &this->blocks.back(); //Points to created element on the list.
  43.  
  44.                 current_elements = current_block->get_bits_array();
  45.                 current_element = 0;
  46.             }
  47.         }
  48.  
  49.         current_elements[current_element] += ((input[input_len - 1] & 1) << current_bit);
  50.         int tmp = ((input[input_len - 1] & 1) << current_bit); // for debug purposes
  51.         for (int32_t i = start; i < input_len; ++i) {
  52.             if (i < input_len - 1)
  53.                 input[i + 1] += 10 * (input[i] & 1);
  54.             input[i] = (input[i] - '0' >> 1) + '0';
  55.         }
  56.         if (input[start] == '0')
  57.             ++start;
  58.  
  59.         ++current_bit;
  60.     }
  61. }
  62.  
  63. BigNumber::~BigNumber() {
  64.     for (uint32_t i = 0; i < this->blocks.size(); ++i) {
  65.         this->blocks.pop_front();
  66.     }
  67. }
  68.  
  69. void BigNumber::add_new_block_to_list(uint32_t &alloc_size) {
  70.     while (alloc_size > 4) {
  71.         try {
  72.             this->blocks.push_back(Block(alloc_size));
  73.         }
  74.         catch (std::string) { // Can I do this?
  75.             alloc_size = alloc_size >> 1;
  76.         }
  77.     }
  78.     throw ("Not enough memory.");
  79. }
  80.  
  81. BigNumberIterator BigNumber::get_lowest_byte() {  
  82.     return BigNumberIterator(*this, blocks.begin(), 0);
  83. }
  84.  
  85. BigNumberIterator BigNumber::get_highest_byte() {
  86.     std::list<Block>::iterator lastBlock = --blocks.end();
  87.    
  88.     return BigNumberIterator(*this, lastBlock, (*lastBlock).get_size() - 1);
  89. }
  90.  
  91. BigNumberIterator BigNumber::end() {
  92.     //No idea how to implement it in more elegant way. To be redesigned.
  93.     return BigNumberIterator(*this, blocks.end(), 0).end();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement