Permalink
Browse files

merged master and issue8 - #4

  • Loading branch information...
shacharz committed Sep 4, 2013
1 parent 68054ac commit c69f7f0a46f2098f86210a03c9cd22cb752b81ed
@@ -0,0 +1,50 @@
(function () {
peer5.core.apiValidators.FileSystemApiValidator = peer5.core.apiValidators.ApiValidatorBase.subClass({
//juset so we'll know what is the name of the class we are (nameSpace) in if we need
name:'peer5.core.apiValidators.FileSystemApiValidator',
/*
* ctor will be called when instantiating a class with for example here new peer5.client.MediaElementWrapper(x,y,z);
* to call super function we do this._super();
*
* */
ctor:function (browserName, browserVersion) {
this.browserName = browserName
this.browserVersion = browserVersion
this.browserVersionSupprot = {
chrome:26, //just so it'll work with current version of chrome and websockets
firefox:19,
msie:11,
opera:12,
safari:1000
}
},
validate:function () {
if(peer5.config.USE_FS == false){
return true;
}
if (window.webkitRequestFileSystem) {
//test func
peer5.core.data.FSio.requestQuota(100, function (succ) {
if (!succ) {
peer5.info('changing USE_FS to false')
peer5.config.USE_FS = false;
}
})
}else{
//no fileSystem
peer5.config.USE_FS = false;
}
return true;
}
})
})
();
@@ -0,0 +1,46 @@
(function () {
peer5.core.dataStructures.DoublyLinkedList = Object.subClass({
name:'peer5.core.dataStructures.DoublyLinkedList',
/** @Public Methods*/
ctor:function () {
this.length = 0;
this.tail = null;
this.head = null;
},
insert:function(val){
var e = {prev:this.tail,next:null,value:val};
if(this.tail)
this.tail.next = e;
this.tail = e;
if(this.length == 0)
this.head = e;
this.length++;
},
delete:function(elem){
if(elem.prev)
elem.prev.next = elem.next;
if(elem.next)
elem.next.prev = elem.prev;
if(this.tail == elem) //TODO: verify that this equality works iff they are pointing at the same object
this.tail = elem.prev;
if(this.head == elem)
this.head = elem.next;
if(!this.head)
debugger;
this.length--;
},
toString:function(){
var iter = this.head;
while(iter){
console.log(iter.value);
iter = iter.next;
}
}
/** @private functions*/
})
})();
View
@@ -0,0 +1,50 @@
(function () {
peer5.core.dataStructures.LRU = Object.subClass({
name:'peer5.core.dataStructures.LRU',
/** @Public Methods*/
ctor:function (maxElements,deleteCB) {
this.max = maxElements;
this.dict = {}; //<key,<value,pointer to list>>
this.list = new peer5.core.dataStructures.DoublyLinkedList(); //keyX <=> keyY <=> keyZ
this.deleteCB = deleteCB;
},
set:function(key,value){
peer5.debug("LRU.set with key: " + key);
if(this.dict[key]) //already have this key
this._delete(key); //deleting the old element matching this key - without cb since it causes a loop of set/delete
this.list.insert(key);
this.dict[key] = {value:value,p:this.list.tail}; //the most recent element
if(this.list.length > this.max){
//delete the lru element
var lruElem = this.list.head;
this._delete(lruElem.value,this.deleteCB); //value of the list elements are the keys in dictionary
}
},
get:function(key){
peer5.debug("LRU.get with key: " + key);
if(this.dict[key]){
var listElem = this.dict[key].p;
if(listElem == this.list.tail) //already most recent
return this.dict[key].value;
this.list.delete(listElem);
this.list.insert(key); //(most recent)
this.dict[key].p = this.list.tail;
return this.dict[key].value;
}else{
return false;
}
},
/** @private functions*/
_delete:function(key,cb){
peer5.debug("removing key " + key + " from lru");
var delElem = this.dict[key];
this.list.delete(delElem.p);
delete this.dict[key];
if(cb)
cb(key,delElem.value);
}
})
})();
@@ -0,0 +1,45 @@
(function () {
describe("DoublyLinkedList tests", function () {
it('some init stuff', function() {
// peer5.setLogLevel(5);
});
it('add a few elements and print them', function () {
var numElems = 10;
var list = new peer5.core.dataStructures.DoublyLinkedList();
for(var i=0;i<numElems;++i){
list.insert(i);
}
list.toString();
expect(list.length).toEqual(numElems);
expect(list.head.value).toEqual(0);
expect(list.tail.value).toEqual(numElems-1);
});
it('add a few elements and delete them', function () {
var numElems = 10;
var list = new peer5.core.dataStructures.DoublyLinkedList();
for(var i=0;i<numElems;++i){
list.insert(i);
}
list.delete(list.head);
expect(list.head.value).toEqual(1);
expect(list.length).toEqual(numElems-1);
list.delete(list.tail);
expect(list.tail.value).toEqual(numElems-2);
expect(list.length).toEqual(numElems-2);
var iter = list.head;
for(var i= 0;i<list.length/2;++i){
iter = iter.next;
}
list.delete(iter);
list.toString();
while(list.head){
list.delete(list.head);
}
expect(list.length).toEqual(0);
list.toString();
});
});
})();
@@ -0,0 +1,10 @@
server: http://localhost:9876
load:
- ../../../client/test/jasmine/src/jasmine.js
- ../../../client/test/jasmine/src/JasmineAdapter.js
- ../../../client/test/jasmine/src/NameSpacer.js
- ../../util/lang_ext.js
- ../../util/namespaces.js
- ../../dataStructures/DoublyLinkedList.js
- DoublyLinkedList.test.js
@@ -0,0 +1,24 @@
(function () {
describe("DoublyLinkedList tests", function () {
it('some init stuff', function() {
// peer5.setLogLevel(5);
});
it('add a few elements', function () {
var numElems = 10;
var cacheSize = 10;
var cache = new peer5.core.dataStructures.LRU(cacheSize,
function(key,value){console.log("deleting: " + key + " " + value)});
for(var i=0;i<numElems;++i){
cache.set(i,i);
}
cache.get(0);
for(var i=numElems;i<2*numElems;++i){
cache.set(i,i);
}
expect(cache.get(0)).toEqual(false);
});
});
})();
@@ -0,0 +1,11 @@
server: http://localhost:9876
load:
- ../../../client/test/jasmine/src/jasmine.js
- ../../../client/test/jasmine/src/JasmineAdapter.js
- ../../../client/test/jasmine/src/NameSpacer.js
- ../../util/lang_ext.js
- ../../util/namespaces.js
- ../../dataStructures/DoublyLinkedList.js
- ../../dataStructures/LRU.js
- LRU.test.js
View
@@ -5,10 +5,8 @@
"author": "",
"dependencies": {
"express": ">=3.0",
"socket.io": ">=0.8",
"underscore": "",
"uglify-js": "=2.2.5",
"librato-metrics": ">=0.0",
"ws": ">=0.4",
"ua-parser":"=0.3.1"
},

0 comments on commit c69f7f0

Please sign in to comment.