Advertisement
Guest User

erase implementation

a guest
Nov 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. ChunkyString::iterator ChunkyString::erase(ChunkyString::iterator i)
  2. {
  3. //erasing last character in a one character chunk
  4. if (i.chunk_->length_ == 1)
  5. {
  6. --size_;
  7. i = ChunkyString::iterator(chunks_.erase(i.chunk_),0);
  8. }
  9. //if the utilization for the chunk the iterator is in is less than 1/4
  10. //we use insert on the other chars
  11. else if (i.chunk_->length_ < (Chunk::CHUNKSIZE/4)+1 && i.chunk_ != chunks_.begin())
  12. {
  13. //std::cout<<"unutilized Chunkyboi is "<<*this<<std::endl;
  14. //std::cout<<i.index_<<" "<<i.chunk_->length_<<std::endl;
  15. char temp[i.chunk_->length_-1];
  16. size_t count = 0;
  17. for (size_t index = 0; index != i.chunk_->length_; ++index)
  18. {
  19. if (index != i.index_)
  20. {
  21. //std::cout<<"Copying char - "<< i.chunk_->chars_[index]<<std::endl;
  22. temp[count] = i.chunk_->chars_[index];
  23. ++count;
  24. }
  25. }
  26. std::list<Chunk>::iterator before_i = --chunks_.erase(i.chunk_);
  27. //std::cout<<"before_i length - "<<before_i->length_<<std::endl;
  28. //++i.chunk_; //reincrement because of preincrement
  29. ChunkyString::iterator j = ChunkyString::iterator(before_i, before_i->length_);
  30. size_t ind = 0;
  31. ChunkyString::iterator r; //the iterator to be returned
  32. //call insert on the end of the chunk before i
  33. while (ind != count)
  34. {
  35. //std::cout<<"current j - "<<*j<<std::endl;
  36. j = insert(j, temp[ind]);
  37. --size_;
  38. if (ind == i.index_)
  39. {
  40. //the iterator we plan to return
  41. r = j; //the char at j at this point would be the one we would want to return
  42. }
  43. ++ind;
  44. }
  45. if (ind == i.index_)
  46. {
  47. //the iterator we plan to return
  48. r = ++j; //the char at j at this point would be the one we would want to return
  49. }
  50. //if (size_==1) { std::cout<<"GOT AN EMPTY STRING"<<std::endl;}
  51. //else {std::cout<<"unutilized Chunkyboi is "<<*this<<std::endl;}
  52. --size_;
  53. //std::cout<<"r - "<<*r<<std::endl;
  54. i = r;
  55. }
  56. else
  57. {
  58. //iterate from erase location to end of chars_ in Chunk
  59. //std::cout<<"Chunkyboi is "<<*this<<i.chunk_->length_<<std::endl;
  60. for (size_t index = i.index_; index != i.chunk_->length_-1; ++index)
  61. {
  62. i.chunk_->chars_[index] = i.chunk_->chars_[index+1];
  63. //replace each character with the one after
  64. }
  65. //if (size_==1) { std::cout<<"GOT AN EMPTY STRING"<<std::endl;}
  66. //else {std::cout<<"Chunkyboi is "<<*this<<std::endl;}
  67. --i.chunk_->length_;
  68. --size_;
  69. if (i.index_ == i.chunk_->length_)
  70. {
  71. //std::cout<<"GOT SPECIAL CASE"<<std::endl;
  72. i = ChunkyString::iterator(++i.chunk_, 0);
  73. }
  74. }
  75. //what we do here is that we check if the size of a ChunkString is less than ChunkSize
  76. //and we are using more than 1 chunk to store it, we move everything into a chunk
  77. if (utilization() < 0.25 && size_< Chunk::CHUNKSIZE && chunks_.size() > 1)
  78. {
  79. std::cout<< *this <<" "<<size_<<" Chunks size - "<<chunks_.size()<< std::endl;
  80. /*
  81. for( auto chunk : chunks_)
  82. {
  83. for (size_t i= 0; i < chunk.length_; ++i)
  84. {
  85. std::cout<<chunk.chars_[i]<<std::endl;
  86. }
  87. }*/
  88. //ChunkyString copy = *this;
  89. Chunk newChunk = {0,{}};
  90. size_t index = 0;
  91. size_t i_index = 0;
  92. for(ChunkyString::iterator it = begin(); it != end(); ++it)
  93. {
  94. //std::cout<<*it<<std::endl;
  95. newChunk.chars_[index] = *it;
  96. ++newChunk.length_;
  97. if (i == it)
  98. {
  99. i_index = index;
  100. }
  101. ++index;
  102. }
  103. chunks_ = std::list<Chunk>();
  104. chunks_.push_back(newChunk);
  105. i = ChunkyString::iterator(chunks_.begin(),i_index);
  106. //std::cout<<*i<<std::endl;
  107. std::cout<< *this <<" "<<size_<<" Chunks size - "<<chunks_.size()<< std::endl;
  108. }
  109. return i;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement