Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ChunkyString::iterator ChunkyString::erase(ChunkyString::iterator i)
- {
- //erasing last character in a one character chunk
- if (i.chunk_->length_ == 1)
- {
- --size_;
- i = ChunkyString::iterator(chunks_.erase(i.chunk_),0);
- }
- //if the utilization for the chunk the iterator is in is less than 1/4
- //we use insert on the other chars
- else if (i.chunk_->length_ < (Chunk::CHUNKSIZE/4)+1 && i.chunk_ != chunks_.begin())
- {
- //std::cout<<"unutilized Chunkyboi is "<<*this<<std::endl;
- //std::cout<<i.index_<<" "<<i.chunk_->length_<<std::endl;
- char temp[i.chunk_->length_-1];
- size_t count = 0;
- for (size_t index = 0; index != i.chunk_->length_; ++index)
- {
- if (index != i.index_)
- {
- //std::cout<<"Copying char - "<< i.chunk_->chars_[index]<<std::endl;
- temp[count] = i.chunk_->chars_[index];
- ++count;
- }
- }
- std::list<Chunk>::iterator before_i = --chunks_.erase(i.chunk_);
- //std::cout<<"before_i length - "<<before_i->length_<<std::endl;
- //++i.chunk_; //reincrement because of preincrement
- ChunkyString::iterator j = ChunkyString::iterator(before_i, before_i->length_);
- size_t ind = 0;
- ChunkyString::iterator r; //the iterator to be returned
- //call insert on the end of the chunk before i
- while (ind != count)
- {
- //std::cout<<"current j - "<<*j<<std::endl;
- j = insert(j, temp[ind]);
- --size_;
- if (ind == i.index_)
- {
- //the iterator we plan to return
- r = j; //the char at j at this point would be the one we would want to return
- }
- ++ind;
- }
- if (ind == i.index_)
- {
- //the iterator we plan to return
- r = ++j; //the char at j at this point would be the one we would want to return
- }
- //if (size_==1) { std::cout<<"GOT AN EMPTY STRING"<<std::endl;}
- //else {std::cout<<"unutilized Chunkyboi is "<<*this<<std::endl;}
- --size_;
- //std::cout<<"r - "<<*r<<std::endl;
- i = r;
- }
- else
- {
- //iterate from erase location to end of chars_ in Chunk
- //std::cout<<"Chunkyboi is "<<*this<<i.chunk_->length_<<std::endl;
- for (size_t index = i.index_; index != i.chunk_->length_-1; ++index)
- {
- i.chunk_->chars_[index] = i.chunk_->chars_[index+1];
- //replace each character with the one after
- }
- //if (size_==1) { std::cout<<"GOT AN EMPTY STRING"<<std::endl;}
- //else {std::cout<<"Chunkyboi is "<<*this<<std::endl;}
- --i.chunk_->length_;
- --size_;
- if (i.index_ == i.chunk_->length_)
- {
- //std::cout<<"GOT SPECIAL CASE"<<std::endl;
- i = ChunkyString::iterator(++i.chunk_, 0);
- }
- }
- //what we do here is that we check if the size of a ChunkString is less than ChunkSize
- //and we are using more than 1 chunk to store it, we move everything into a chunk
- if (utilization() < 0.25 && size_< Chunk::CHUNKSIZE && chunks_.size() > 1)
- {
- std::cout<< *this <<" "<<size_<<" Chunks size - "<<chunks_.size()<< std::endl;
- /*
- for( auto chunk : chunks_)
- {
- for (size_t i= 0; i < chunk.length_; ++i)
- {
- std::cout<<chunk.chars_[i]<<std::endl;
- }
- }*/
- //ChunkyString copy = *this;
- Chunk newChunk = {0,{}};
- size_t index = 0;
- size_t i_index = 0;
- for(ChunkyString::iterator it = begin(); it != end(); ++it)
- {
- //std::cout<<*it<<std::endl;
- newChunk.chars_[index] = *it;
- ++newChunk.length_;
- if (i == it)
- {
- i_index = index;
- }
- ++index;
- }
- chunks_ = std::list<Chunk>();
- chunks_.push_back(newChunk);
- i = ChunkyString::iterator(chunks_.begin(),i_index);
- //std::cout<<*i<<std::endl;
- std::cout<< *this <<" "<<size_<<" Chunks size - "<<chunks_.size()<< std::endl;
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement