Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BigNumber.h"
- #include "BigNumberIterator.h"
- #include <iostream>
- BigNumber::BigNumber() {
- }
- BigNumber::BigNumber(std::string input) { //Remember to check if all literals are not digits!
- uint32_t input_len = input.size();
- if (input_len == 0) {
- throw "String used for class instance construction must not be empty.";
- }
- if (input_len == 1 && input[0] == 0) {
- this->size = 0;
- return;
- }
- uint32_t current_alloc_size = BigNumber::ALLOC_SIZE;
- uint32_t start, len, current_bit;
- start = this->is_negative = input[0] == '-';
- len = current_bit = 0;
- this->add_new_block_to_list(current_alloc_size);
- Block *current_block = &this->blocks.back(); //Points to created element on the list.
- int64_t *current_elements = current_block->get_bits_array();
- int64_t current_element = 0;
- uint32_t BITS_PER_ELEMENT = sizeof(uint64_t) * Architecture::PlatformChecks::bits_per_byte(); // should move to header
- while (start < input_len - 1 || input[input_len - 1] != '0') {
- if (current_bit == BITS_PER_ELEMENT) {
- current_bit = 0;
- ++current_element;
- ++len;
- if (current_element == current_alloc_size) {
- current_alloc_size = current_alloc_size << 1;
- this->add_new_block_to_list(current_alloc_size);
- Block *current_block = &this->blocks.back(); //Points to created element on the list.
- current_elements = current_block->get_bits_array();
- current_element = 0;
- }
- }
- current_elements[current_element] += ((input[input_len - 1] & 1) << current_bit);
- int tmp = ((input[input_len - 1] & 1) << current_bit); // for debug purposes
- for (int32_t i = start; i < input_len; ++i) {
- if (i < input_len - 1)
- input[i + 1] += 10 * (input[i] & 1);
- input[i] = (input[i] - '0' >> 1) + '0';
- }
- if (input[start] == '0')
- ++start;
- ++current_bit;
- }
- }
- BigNumber::~BigNumber() {
- for (uint32_t i = 0; i < this->blocks.size(); ++i) {
- this->blocks.pop_front();
- }
- }
- void BigNumber::add_new_block_to_list(uint32_t &alloc_size) {
- while (alloc_size > 4) {
- try {
- this->blocks.push_back(Block(alloc_size));
- }
- catch (std::string) { // Can I do this?
- alloc_size = alloc_size >> 1;
- }
- }
- throw ("Not enough memory.");
- }
- BigNumberIterator BigNumber::get_lowest_byte() {
- return BigNumberIterator(*this, blocks.begin(), 0);
- }
- BigNumberIterator BigNumber::get_highest_byte() {
- std::list<Block>::iterator lastBlock = --blocks.end();
- return BigNumberIterator(*this, lastBlock, (*lastBlock).get_size() - 1);
- }
- BigNumberIterator BigNumber::end() {
- //No idea how to implement it in more elegant way. To be redesigned.
- return BigNumberIterator(*this, blocks.end(), 0).end();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement