Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include "chunkvector.h"
  2.  
  3. ChunkVector::ChunkVector() {
  4. elementSize = 0;
  5. chunkSize = 0;
  6. head = NULL;
  7.  
  8.  
  9. }
  10. ChunkVector::~ChunkVector() {
  11. }
  12. int ChunkVector::size() {
  13. return elementSize;
  14. }
  15. int ChunkVector::get(int index) {
  16. int count = 0;
  17. chunk* tavi = head;
  18. while(true) {
  19. if(count+tavi->index>index) {
  20. return tavi->arr[index-count];
  21. }
  22. count+=tavi->index;
  23. tavi=tavi->next;
  24.  
  25. }
  26.  
  27. }
  28. void ChunkVector::set(int index, int value) {
  29. int count = 0;
  30. chunk* tavi = head;
  31. while(true) {
  32. if(count+tavi->index>index) {
  33. tavi->arr[index-count]=value;
  34. return;
  35. }
  36. count+=tavi->index;
  37. tavi=tavi->next;
  38.  
  39. }
  40.  
  41.  
  42. }
  43. void ChunkVector::add(int value) {
  44. if(head==NULL) {
  45. head = new chunk;
  46. head->index=0;
  47. head->next=NULL;
  48. chunkSize++;
  49.  
  50. }
  51. chunk* tavi = head;
  52. while(tavi->next!=NULL) {
  53. tavi=tavi->next;
  54.  
  55. }
  56. if(tavi->index==CHUNK_SIZE) {
  57. chunkSize++;
  58. chunk* axali = new chunk;
  59. axali->index=tavi->index/2;
  60. axali->next=NULL;
  61. tavi->index=tavi->index-axali->index;
  62. for(int i=0; i<axali->index; i++) {
  63. axali->arr[i]= tavi->arr[tavi->index-i];
  64.  
  65. }
  66. tavi->next=axali;
  67. tavi = axali;
  68.  
  69. }
  70. tavi->arr[tavi->index]=value;
  71. tavi->index++;
  72. elementSize++;
  73.  
  74.  
  75.  
  76.  
  77. }
  78.  
  79. void ChunkVector::remove(int index) {
  80. int count = 0;
  81. chunk* tavi = head;
  82. chunk* prev = head;
  83. while(true) {
  84. if(count+tavi->index>index) {
  85. break;
  86.  
  87. }
  88. count+=tavi->index;
  89. prev = tavi;
  90. tavi=tavi->next;
  91.  
  92. }
  93. for(int i = index-count; i<tavi->index-1; i++) {
  94. tavi->arr[i]=tavi->arr[i+1];
  95.  
  96. }
  97. elementSize--;
  98. tavi->index--;
  99. if(tavi->index==0) {
  100. if(tavi==head) {
  101. head=tavi->next;
  102.  
  103. }else {
  104. prev->next=tavi->next;
  105.  
  106. }
  107. chunkSize--;
  108. delete tavi;
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. }
  119.  
  120. int ChunkVector::getChunkNumber() {
  121. return chunkSize;
  122. }
  123.  
  124. void ChunkVector::insert(int index, int value) {
  125. if(index==size()) {
  126. add(value);
  127. return;
  128.  
  129. }
  130. int count = 0;
  131. chunk* tavi = head;
  132. chunk* prev = head;
  133. while(true) {
  134. if(count+tavi->index>index) {
  135. break;
  136.  
  137. }
  138. count+=tavi->index;
  139. prev = tavi;
  140. tavi=tavi->next;
  141.  
  142. }
  143. if(tavi->index==CHUNK_SIZE) {
  144. chunk* axali = new chunk;
  145. chunkSize++;
  146. axali->next=tavi->next;
  147. axali->index=tavi->index/2;
  148. tavi->index=tavi->index-axali->index;
  149. for(int i=0; i<axali->index; i++) {
  150. axali->arr[i]=tavi->arr[tavi->index+i];
  151.  
  152.  
  153. }
  154. tavi->next=axali;
  155. }
  156.  
  157. int i;
  158. for (i=tavi->index; i>index-count; i--) {
  159. tavi->arr[i] = tavi->arr[i-1];
  160. }
  161. tavi->arr[i] = value;
  162. elementSize++;
  163. tavi->index++;
  164.  
  165.  
  166.  
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement