Skip to content

Instantly share code, notes, and snippets.

@PAEz
Created September 25, 2012 04:59
Show Gist options
  • Save PAEz/3780081 to your computer and use it in GitHub Desktop.
Save PAEz/3780081 to your computer and use it in GitHub Desktop.
HashArray - Incomplete
function HashArray() {
this.array = [];
this.hash = {};
this.__defineGetter__("length", function() {
return this.array.length;
});
this.set = function(key, value) {
if (typeof(key) === 'string') {
if (this.hash[key]) {
this.hash[key].value = value;
} else return false;
} else if (typeof(key) === 'number') {
if (key < this.array.length && key > -1) {
this.array[key].value = value;
} else return false;
} else return false;
}
this.add = function(key, value) {
if (typeof(key) == 'number') key = String(key);
if (!this.hash[key]) {
var obj = {
key: key,
value: value,
index: this.array.length
};
this.array.push(obj);
this.hash[key] = obj;
} else return false;
}
this.get = function(key) {
if (typeof(key) === 'string' && this.hash[key]) {
return this.hash[key].value;
} else if (typeof(key) === 'number' && key < this.array.length && key > -1) {
return this.array[key].value;
} else return false;
}
this.remove = function(key) {
if (typeof(key) === 'string' && this.hash[key]) {;
var index = this.hash[key].index;
delete this.hash[key];
// delete left a null in the array so had to use splice?
//delete this.array[index];
this.array.splice(index, 1);
this.reindex(index);
} else if (typeof(key) === 'number' && key < this.array.length && key > -1) {
delete this.hash[this.array[key].key];
// delete left a null in the array so had to use splice?
//delete this.array[index];
this.array.splice(index, 1);
this.reindex(key);
} else return false;
}
// If one argument
// and its a number it will delete from array[argument[0]] till the end of the array
// if its a string then assume its a JSON'd list
// otherwise it can be an array of keys to delete, such as.... ['keyOne','keyTwo','keyTen']
// If its two arguments
// it expects both arguments to be numbers, being the starting Index and the amount to delete
this.removeRange = function(start, amount) {
if (arguments.length == 1) {
if (typeof(start) === 'number') {
for (var i = start, end = this.array.length; i < end; i++) {
delete this.hash[this.array[i].key];
}
this.array.splice(start);
this.reindex(start);
} else if (typeof(start) === 'string') {
this.removeRange(JSON.parse(list));
} else { // assume the first argument is a list
for (var i = 0, end = start.length; i < end; i++) {
if (typeof start[i] =='number'){
var index = start[i];
} else {
var index = this.hash[start[i]].index;
}
if (index > 0 && index < this.array.length){
delete this.hash[this.array[index].key];
delete this.array[index];
}
}
this.reindexAndRemoveNulls(0);
}
} else { // assume theres two arguments and their both numbers
for (var i = start, end = start + amount < this.array.length ? start + amount : this.array.length; i < end; i++) {
delete this.hash[this.array[i].key];
}
this.array.splice(start, amount);
this.reindex(start);
}
}
// Lists look like....
// [['key','value'], ['keyTwo','valueTwo'], ['keyEtc','valueEtc']]
this.setList = function(list) {
if (typeof(list) === 'string') {
this.setList(JSON.parse(list));
} else { //assume its an array
for (var i = 0, end = list.length; i < end; i++) {
if (!this.hash[list[i][0]]) {
var obj = {
key: list[i][0],
value: list[i][1],
index: this.array.length
};
this.array.push(obj);
this.hash[key] = obj;
} else {
this.hash[list[i][0]].value = list[i][1];
}
}
}
}
this.addList = function(list) {
if (typeof(list) === 'string') {
this.addList(JSON.parse(list));
} else { //assume its an array
for (var i = 0, end = list.length; i < end; i++) {
if (!this.hash[list[i][0]]) {
var obj = {
key: list[i][0],
value: list[i][1],
index: this.array.length
};
this.array.push(obj);
this.hash[key] = obj;
} else {
this.hash[list[i][0]].value = list[i][1];
}
}
}
}
this.removeList = function(list) {
this.removeRange(list);
}
this.getList = function(asJSON, start, amount) {
var list = [];
if (arguments.length == 0) var asJSON = false;
if (arguments.length == 1 || arguments.length == 2) {
if (arguments.length == 1) var start = 0;
for (var i = start, end = this.array.length; i < end; i++) {
list.push([this.array[i].key, this.array[i].value]);
}
} else { //assume theres 3 arguments
for (var i = start, end = amount + start > this.array.length ? this.array.length : start + amount; i < end; i++) {
list.push([this.array[i].key, this.array[i].value]);
}
}
return asJSON ? JSON.stringify(list) : list;
//return list;
}
this.clear = function() {
this.array = [];
this.hash = {};
}
this.fromJSON = function(string) {
this.clear();
this.setList(string);
}
this.toJSON = function() {
return this.getList(true);
}
this.reindex = function(index, onlyOne) {
if (onlyOne) {
this.array[index].index = index;
} else {
for (var i = index, end = this.array.length; i < end; i++) {
this.array[i].index = i;
}
}
}
this.reindexAndRemoveNulls = function(index) {
var count=0;
for (var i = index; i < this.array.length; i++) {
if (array[i]==null){
this.array.splice(i, 1);
} else {
this.array[i].index = count;
count++;
}
}
}
this.swap = function (from, to) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment