Updating Set API, adding Map, adding Cache.

This commit is contained in:
Richard Davey 2016-12-09 09:32:24 +00:00
parent e9c4f28db3
commit 4662100514
5 changed files with 341 additions and 40 deletions

39
v3/src/cache/BaseCache.js vendored Normal file
View file

@ -0,0 +1,39 @@
var CacheEntry = require('./CacheEntry');
var BaseCache = function ()
{
this.entries = new Map();
};
BaseCache.prototype.constructor = BaseCache;
BaseCache.prototype = {
add: function (key, data)
{
this.files.set(key, data);
},
has: function (key)
{
return this.files.has(key);
},
get: function (key)
{
return this.files.get(key);
},
remove: function (key)
{
this.files.delete(key);
},
destroy: function ()
{
this.files.clear();
}
};
module.exports = BaseCache;

12
v3/src/cache/CacheEntry.js vendored Normal file
View file

@ -0,0 +1,12 @@
var CacheEntry = function (key, url, data)
{
this.key = key;
this.url = url;
this.data = data;
};
CacheEntry.prototype.constructor = CacheEntry;
module.exports = CacheEntry;

View file

@ -1,9 +1,14 @@
var CONST = require('../../loader/const');
var BaseLoader = require('../../loader/BaseLoader');
var NumberArray = require('../../utils/array/NumberArray');
var ImageFile = require('../../loader/filetypes/ImageFile');
var JSONFile = require('../../loader/filetypes/JSONFile');
var XMLFile = require('../../loader/filetypes/XMLFile');
var BinaryFile = require('../../loader/filetypes/BinaryFile');
var GLSLFile = require('../../loader/filetypes/GLSLFile');
var TextFile = require('../../loader/filetypes/TextFile');
var AtlasJSONFile = require('../../loader/filetypes/AtlasJSONFile');
var NumberArray = require('../../utils/array/NumberArray');
var Loader = function (state)
{
@ -39,6 +44,42 @@ Loader.prototype.json = function (key, url)
return this;
};
Loader.prototype.xml = function (key, url)
{
var file = new XMLFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.binary = function (key, url)
{
var file = new BinaryFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.text = function (key, url)
{
var file = new TextFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.glsl = function (key, url)
{
var file = new GLSLFile(key, url, this.path);
this.addFile(file);
return this;
};
Loader.prototype.atlas = function (key, textureURL, atlasURL)
{
var file = new AtlasJSONFile(key, textureURL, atlasURL, this.path);

188
v3/src/structs/Map.js Normal file
View file

@ -0,0 +1,188 @@
// The keys of a Map can be arbitrary values.
/*
var map = new Map([
[ 1, 'one' ],
[ 2, 'two' ],
[ 3, 'three' ]
]);
*/
var Map = function (elements)
{
this.entries = {};
if (Array.isArray(elements))
{
for (var i = 0; i < elements.length; i++)
{
this.add(elements[i][0], elements[i][1]);
}
}
};
Map.prototype.constructor = Map;
Map.prototype = {
set: function (key, value)
{
if (!this.entries.hasOwnProperty(key))
{
this.entries[key] = value;
}
return this;
},
delete: function (value)
{
var index = this.entries.indexOf(value);
if (index > -1)
{
this.entries.splice(index, 1);
}
},
keys: function ()
{
},
values: function ()
{
},
dump: function ()
{
console.group('Map');
for (var i = 0; i < this.entries.length; i++)
{
var entry = this.entries[i];
console.log(entry);
}
console.groupEnd();
},
get: function (property, value)
{
for (var i = 0; i < this.entries.length; i++)
{
var entry = this.entries[i];
if (entry[property] === value)
{
return entry;
}
}
},
// For when you know this Map will be modified during the iteration
each: function (callback)
{
var temp = this.entries.slice();
for (var i = 0; i < temp.length; i++)
{
if (callback(temp[i]) === false)
{
break;
}
}
},
// For when you absolutely know this Map won't be modified during the iteration
iterate: function (callback)
{
for (var i = 0; i < this.entries.length; i++)
{
if (callback(this.entries[i]) === false)
{
break;
}
}
},
clear: function ()
{
this.entries.length = 0;
},
contains: function (value)
{
return (this.entries.indexOf(value) > -1);
},
union: function (set)
{
var newMap = new Map();
set.values.forEach(function (value)
{
newMap.add(value);
});
this.entries.forEach(function (value)
{
newMap.add(value);
});
return newMap;
},
intersect: function (set)
{
var newMap = new Map();
this.entries.forEach(function (value)
{
if (set.contains(value))
{
newMap.add(value);
}
});
return newMap;
},
difference: function (set)
{
var newMap = new Map();
this.entries.forEach(function (value)
{
if (!set.contains(value))
{
newMap.add(value);
}
});
return newMap;
}
};
Object.defineProperties(Map.prototype, {
size: {
enumerable: true,
get: function ()
{
return this.entries.length;
},
set: function (value)
{
return this.entries.length = value;
}
}
});
module.exports = Map;

View file

@ -1,48 +1,37 @@
var Set = function ()
// A Set is a collection of unique elements.
var Set = function (elements)
{
this.values = [];
this.entries = [];
if (Array.isArray(elements))
{
for (var i = 0; i < elements.length; i++)
{
this.add(elements[i]);
}
}
};
Set.prototype.constructor = Set;
Set.prototype = {
add: function (value)
set: function (value)
{
if (this.values.indexOf(value) === -1)
if (this.entries.indexOf(value) === -1)
{
this.values.push(value);
}
},
delete: function (value)
{
var index = this.values.indexOf(value);
if (index > -1)
{
this.values.splice(index, 1);
}
},
dump: function ()
{
console.group('Set');
for (var i = 0; i < this.values.length; i++)
{
var entry = this.values[i];
console.log(entry);
this.entries.push(value);
}
console.groupEnd();
return this;
},
get: function (property, value)
{
for (var i = 0; i < this.values.length; i++)
for (var i = 0; i < this.entries.length; i++)
{
var entry = this.values[i];
var entry = this.entries[i];
if (entry[property] === value)
{
@ -51,10 +40,36 @@ Set.prototype = {
}
},
delete: function (value)
{
var index = this.entries.indexOf(value);
if (index > -1)
{
this.entries.splice(index, 1);
}
return this;
},
dump: function ()
{
console.group('Set');
for (var i = 0; i < this.entries.length; i++)
{
var entry = this.entries[i];
console.log(entry);
}
console.groupEnd();
},
// For when you know this Set will be modified during the iteration
each: function (callback)
{
var temp = this.values.slice();
var temp = this.entries.slice();
for (var i = 0; i < temp.length; i++)
{
@ -63,28 +78,34 @@ Set.prototype = {
break;
}
}
return this;
},
// For when you absolutely know this Set won't be modified during the iteration
iterate: function (callback)
{
for (var i = 0; i < this.values.length; i++)
for (var i = 0; i < this.entries.length; i++)
{
if (callback(this.values[i]) === false)
if (callback(this.entries[i]) === false)
{
break;
}
}
return this;
},
clear: function ()
{
this.values.length = 0;
this.entries.length = 0;
return this;
},
contains: function (value)
{
return (this.values.indexOf(value) > -1);
return (this.entries.indexOf(value) > -1);
},
union: function (set)
@ -96,7 +117,7 @@ Set.prototype = {
newSet.add(value);
});
this.values.forEach(function (value)
this.entries.forEach(function (value)
{
newSet.add(value);
});
@ -108,7 +129,7 @@ Set.prototype = {
{
var newSet = new Set();
this.values.forEach(function (value)
this.entries.forEach(function (value)
{
if (set.contains(value))
{
@ -123,7 +144,7 @@ Set.prototype = {
{
var newSet = new Set();
this.values.forEach(function (value)
this.entries.forEach(function (value)
{
if (!set.contains(value))
{
@ -144,12 +165,12 @@ Object.defineProperties(Set.prototype, {
get: function ()
{
return this.values.length;
return this.entries.length;
},
set: function (value)
{
return this.values.length = value;
return this.entries.length = value;
}
}