Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Created April 21, 2018 16:51
Show Gist options
  • Select an option

  • Save Spuffynism/f559e5e70f557d23b70b260a9c3036c3 to your computer and use it in GitHub Desktop.

Select an option

Save Spuffynism/f559e5e70f557d23b70b260a9c3036c3 to your computer and use it in GitHub Desktop.
Simple vector implementation in js to help with amortized analysis
function Vector() {
this.capacity = 1;
this.elements = new Array(this.capacity);
this.size = 0;
this.add = (element) => {
if (this.size > this.capacity)
console.err('this.size > this.capacity');
if (this.size === this.capacity) {
this.capacity *= 2;
let t = new Array(this.capacity);
for(let i = 0; i < this.size; i++) {
t[i] = this.elements[i];
}
this.elements = t;
}
this.elements[this.size++] = element;
};
this.get = (i) => {
return this.elements[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment