Last active
February 24, 2022 04:02
-
-
Save rileyJones/5f0826764c343de8773ce16fc82f1edd to your computer and use it in GitHub Desktop.
WormArray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WormArray() { | |
public: | |
int* head; //Values from head inclusive to tail exclusive | |
int* tail; | |
int* min; | |
int* max; | |
int shift() { | |
if(head == tail) return 0; | |
return *(head++); | |
} | |
int unshift(int val) { | |
if(head != min) { | |
head--; | |
*head = val; | |
return 1; | |
} | |
return 0; | |
} | |
int push(int val) { | |
if(tail != max) { | |
*tail = val; | |
tail++; | |
return 1; | |
} | |
return 0; | |
} | |
int pop() { | |
if(head == tail) return 0; | |
return *(--tail); | |
} | |
WormArray(int length) { | |
min = malloc(length * sizeof(int)); | |
max = min + length; | |
head = (max + min) / 2; | |
tail = head; | |
} | |
WormArray(int length, float bias) { | |
min = malloc(length * sizeof(int)); | |
max = min + length; | |
head = (int*)((1-bias)max + (bias)min) ; | |
tail = head; | |
} | |
~WormArray() { | |
free(min); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment