Skip to content

Instantly share code, notes, and snippets.

@rileyJones
Last active February 24, 2022 04:02
Show Gist options
  • Save rileyJones/5f0826764c343de8773ce16fc82f1edd to your computer and use it in GitHub Desktop.
Save rileyJones/5f0826764c343de8773ce16fc82f1edd to your computer and use it in GitHub Desktop.
WormArray
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