Skip to content

Instantly share code, notes, and snippets.

@shacharz
Created March 27, 2014 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shacharz/9807577 to your computer and use it in GitHub Desktop.
Save shacharz/9807577 to your computer and use it in GitHub Desktop.
(function (exports) {
function Dictionary(size, default_val) {
this.myDict = {}; //<keys : <val,index>>
this.keys = [];
if (size) {
if (!default_val) {
default_val = 1;
}
for (var i=0; i < size; i++) {
this.keys.push(i);
var tuple = [default_val, this.keys.length - 1];
this.myDict[i] = tuple
}
}
}
Dictionary.prototype = {
has:function (key) {
return (key in this.myDict);
},
set:function (key, value) {
if(this.has(key)){
peer5.warn("Trying to set a key to Dictionary.js that already exists: " + key);
peer5.log("The old value was connected: " + this.myDict[key][0].connected);
var index = this.myDict[key][1];
var tuple = [value,index];
this.myDict[key] = tuple;
}
else{
this.keys.push(key);
var tuple = [value, this.keys.length - 1];
this.myDict[key] = tuple;
}
},
get:function (key) {
if (this.myDict[key]) {
return this.myDict[key][0];
} else
return undefined;
},
getKeys:function(){
return this.keys;
},
randomSample:function (sampleSize, sampler) {
var sKeys = sampler(this.keys, sampleSize);
var rv = [];
for (var i = 0; i < sKeys.length; ++i) {
if(this.myDict[sKeys[i]] && this.myDict[sKeys[i]][0])
rv.push(this.myDict[sKeys[i]][0]);
else{
peer5.warn('Error in randomSampling, the key: ' + sKeys[i] + ' is undefined');
}
}
return rv;
},
keys:function () {
return this.keys;
},
length:function () {
return this.keys.length;
},
//return array of values
filterValues:function (predicate) {
var rv = [];
for (var i = 0; i < this.keys.length; ++i) {
if (predicate(this.myDict[this.keys[i]][0])) {
rv.push(this.myDict[this.keys[i]][0]);
}
}
return rv;
},
values:function () {
var rv = [];
for (var i = 0; i < this.keys.length; ++i) {
rv.push(this.myDict[this.keys[i]][0]);
}
return rv;
},
getFirstVals:function (numOfVals) {
var rv = [];
for (var key in this.myDict) {
if (this.myDict.hasOwnProperty(key)) {
rv.push(this.myDict[key][0]);
}
if (rv.length == numOfVals) {
break;
}
}
return rv;
},
getFirstKeys:function (numOfVals,startingVal) {
var rv = [];
for (var key in this.myDict) {
if (rv.length >= numOfVals) {
break;
}
if(startingVal && parseInt(key) <= startingVal)
continue;
if (this.myDict.hasOwnProperty(parseInt(key))) {
rv.push(parseInt(key));
}
}
return rv;
},
slice:function (keys) {
var rv = [];
for (var i = 0; i < keys.length; ++i) {
if (this.myDict[keys[i]]) {
rv.push(this.myDict[keys[i]][0]);
}
}
return rv;
},
// concat: function (){}
remove:function (key) {
var elem = this.myDict[key];
if(!elem){
console.log(elem);
}
delete this.myDict[key];
var index = elem[1];
this.keys[index] = this.keys[this.keys.length - 1];
if(this.keys[index] in this.myDict){ //for case when removing the last key
this.myDict[this.keys[index]][1] = index;
}
this.keys.length--;
}
}
exports.Dictionary = Dictionary;
})(typeof exports === 'undefined' ? this['Dictionary'] = {} : exports);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment