Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "chunkvector.h"
- ChunkVector::ChunkVector() {
- elementSize = 0;
- chunkSize = 0;
- head = NULL;
- }
- ChunkVector::~ChunkVector() {
- }
- int ChunkVector::size() {
- return elementSize;
- }
- int ChunkVector::get(int index) {
- int count = 0;
- chunk* tavi = head;
- while(true) {
- if(count+tavi->index>index) {
- return tavi->arr[index-count];
- }
- count+=tavi->index;
- tavi=tavi->next;
- }
- }
- void ChunkVector::set(int index, int value) {
- int count = 0;
- chunk* tavi = head;
- while(true) {
- if(count+tavi->index>index) {
- tavi->arr[index-count]=value;
- return;
- }
- count+=tavi->index;
- tavi=tavi->next;
- }
- }
- void ChunkVector::add(int value) {
- if(head==NULL) {
- head = new chunk;
- head->index=0;
- head->next=NULL;
- chunkSize++;
- }
- chunk* tavi = head;
- while(tavi->next!=NULL) {
- tavi=tavi->next;
- }
- if(tavi->index==CHUNK_SIZE) {
- chunkSize++;
- chunk* axali = new chunk;
- axali->index=tavi->index/2;
- axali->next=NULL;
- tavi->index=tavi->index-axali->index;
- for(int i=0; i<axali->index; i++) {
- axali->arr[i]= tavi->arr[tavi->index-i];
- }
- tavi->next=axali;
- tavi = axali;
- }
- tavi->arr[tavi->index]=value;
- tavi->index++;
- elementSize++;
- }
- void ChunkVector::remove(int index) {
- int count = 0;
- chunk* tavi = head;
- chunk* prev = head;
- while(true) {
- if(count+tavi->index>index) {
- break;
- }
- count+=tavi->index;
- prev = tavi;
- tavi=tavi->next;
- }
- for(int i = index-count; i<tavi->index-1; i++) {
- tavi->arr[i]=tavi->arr[i+1];
- }
- elementSize--;
- tavi->index--;
- if(tavi->index==0) {
- if(tavi==head) {
- head=tavi->next;
- }else {
- prev->next=tavi->next;
- }
- chunkSize--;
- delete tavi;
- }
- }
- int ChunkVector::getChunkNumber() {
- return chunkSize;
- }
- void ChunkVector::insert(int index, int value) {
- if(index==size()) {
- add(value);
- return;
- }
- int count = 0;
- chunk* tavi = head;
- chunk* prev = head;
- while(true) {
- if(count+tavi->index>index) {
- break;
- }
- count+=tavi->index;
- prev = tavi;
- tavi=tavi->next;
- }
- if(tavi->index==CHUNK_SIZE) {
- chunk* axali = new chunk;
- chunkSize++;
- axali->next=tavi->next;
- axali->index=tavi->index/2;
- tavi->index=tavi->index-axali->index;
- for(int i=0; i<axali->index; i++) {
- axali->arr[i]=tavi->arr[tavi->index+i];
- }
- tavi->next=axali;
- }
- int i;
- for (i=tavi->index; i>index-count; i--) {
- tavi->arr[i] = tavi->arr[i-1];
- }
- tavi->arr[i] = value;
- elementSize++;
- tavi->index++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement