diff options
-rw-r--r-- | .gitmodules | 9 | ||||
m--------- | js/crossfilter | 0 | ||||
m--------- | js/d3 | 0 | ||||
m--------- | js/dc | 0 | ||||
-rw-r--r-- | js/util/memoize.js | 45 |
5 files changed, 54 insertions, 0 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c10d45c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "js/crossfilter"] + path = js/crossfilter + url = https://github.com/square/crossfilter.git +[submodule "js/d3"] + path = js/d3 + url = https://github.com/mbostock/d3.git +[submodule "js/dc"] + path = js/dc + url = https://github.com/NickQiZhu/dc.js.git diff --git a/js/crossfilter b/js/crossfilter new file mode 160000 +Subproject ae994e8f8014ad4304e0575973bb23c1fb1ac0b diff --git a/js/d3 b/js/d3 new file mode 160000 +Subproject 91d35b4205d4ef150c61c415b7379404bced726 diff --git a/js/dc b/js/dc new file mode 160000 +Subproject 517c4887dac9dffa08494d31c7a54321861d53e diff --git a/js/util/memoize.js b/js/util/memoize.js new file mode 100644 index 0000000..796c060 --- /dev/null +++ b/js/util/memoize.js @@ -0,0 +1,45 @@ +/* + * Helper function for memoization + * + * The passed labeller function must generate unique and exact names for entries that are naturally equal. + * IDs are persistent. + * + * names, idByName, and entries are accessible fields from the memoizer. + * Behaviour on external modification of these fields is undefined. + * + * An entry object passed to the memoizer has its field "id" set to the memoized id by default. + * The name of the ID field can be modified by calling the getter/setter idField() from the memoizer. + * Behaviour on changing the ID field used while the memoizer has already performed memoization is undefined. + */ +function memoize(labeller) { + var idField = "id"; + var names = []; + var idByName = {}; + var entries = []; + var _memoizer = function(entry) { + var name = labeller(entry); + if (name in idByName) { + /* We already have a suitable entry. */ + var id = idByName[name]; + return entries[id]; + } + /* New entry. */ + /* Set the entry's ID. */ + entry[idField] = entries.length; + /* Index the new entry. */ + idByName[name] = entry[idField]; + names[entry[idField]] = name; + entries.push(entry); + + return entry; + } + _memoizer.names = function() { return names; }; + _memoizer.idByName = function() { return idByName; }; + _memoizer.entries = function() { return entries; }; + _memoizer.idField = function(x) { + if (!arguments.length) return idField; + idField = x; + return _memoizer; + }; + return _memoizer; +} |