Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate shims #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,718 changes: 11 additions & 1,707 deletions README.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

var hasOwnProperty = Object.prototype.hasOwnProperty;

module.exports = copy;
function copy(target, source) {
for (var name in source) {
if (hasOwnProperty.call(source, name)) {
target[name] = source[name];
}
}
}
13 changes: 0 additions & 13 deletions demo/array-demo.js

This file was deleted.

14 changes: 7 additions & 7 deletions deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var GenericOrder = require("./generic-order");
var ObservableRange = require("./observable-range");
var ObservableObject = require("./observable-object");
var Iterator = require("./iterator");
var addEach = require("./operators/add-each");
var equalsOperator = require("./operators/equals");
var copyProperties = require("./copy");
var equalsOperator = require("pop-equals");

// by Petka Antonov
// https://github.com/petkaantonov/deque/blob/master/js/deque.js
Expand All @@ -28,10 +28,10 @@ function Deque(values, capacity) {
this.addEach(values);
}

addEach(Deque.prototype, GenericCollection.prototype);
addEach(Deque.prototype, GenericOrder.prototype);
addEach(Deque.prototype, ObservableRange.prototype);
addEach(Deque.prototype, ObservableObject.prototype);
copyProperties(Deque.prototype, GenericCollection.prototype);
copyProperties(Deque.prototype, GenericOrder.prototype);
copyProperties(Deque.prototype, ObservableRange.prototype);
copyProperties(Deque.prototype, ObservableObject.prototype);

Deque.prototype.maxCapacity = (1 << 30) | 0;
Deque.prototype.minCapacity = 16;
Expand Down Expand Up @@ -398,7 +398,7 @@ Deque.prototype.has = function (value, equals) {
var mask = this.capacity - 1;
for (var index = 0; index < this.length; index++) {
var offset = (this.front + index) & mask;
if (this[offset] === value) {
if (equals(value, this[offset])) {
return true;
}
}
Expand Down
20 changes: 5 additions & 15 deletions dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var GenericCollection = require("./generic-collection");
var GenericMap = require("./generic-map");
var ObservableObject = require("./observable-object");
var Iterator = require("./iterator");
var addEach = require("./operators/add-each");
var copy = require("./copy");

// Burgled from https://github.com/domenic/dict

Expand All @@ -23,31 +23,24 @@ function Dict(values, getDefault) {
Dict.Dict = Dict; // hack so require("dict").Dict will work in MontageJS.

function mangle(key) {
return "~" + key;
return "$" + key;
}

function unmangle(mangled) {
return mangled.slice(1);
}

addEach(Dict.prototype, GenericCollection.prototype);
addEach(Dict.prototype, GenericMap.prototype);
addEach(Dict.prototype, ObservableObject.prototype);
copy(Dict.prototype, GenericCollection.prototype);
copy(Dict.prototype, GenericMap.prototype);
copy(Dict.prototype, ObservableObject.prototype);

Dict.prototype.isDict = true;

Dict.prototype.constructClone = function (values) {
return new this.constructor(values, this.mangle, this.getDefault);
};

Dict.prototype.assertString = function (key) {
if (typeof key !== "string") {
throw new TypeError("key must be a string but Got " + key);
}
}

Dict.prototype.get = function (key, defaultValue) {
this.assertString(key);
var mangled = mangle(key);
if (mangled in this.store) {
return this.store[mangled];
Expand All @@ -59,7 +52,6 @@ Dict.prototype.get = function (key, defaultValue) {
};

Dict.prototype.set = function (key, value) {
this.assertString(key);
var mangled = mangle(key);
var from;
if (mangled in this.store) { // update
Expand All @@ -86,13 +78,11 @@ Dict.prototype.set = function (key, value) {
};

Dict.prototype.has = function (key) {
this.assertString(key);
var mangled = mangle(key);
return mangled in this.store;
};

Dict.prototype["delete"] = function (key) {
this.assertString(key);
var mangled = mangle(key);
var from;
if (mangled in this.store) {
Expand Down
12 changes: 6 additions & 6 deletions fast-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var Set = require("./fast-set");
var GenericCollection = require("./generic-collection");
var GenericMap = require("./generic-map");
var ObservableObject = require("./observable-object");
var equalsOperator = require("./operators/equals");
var hashOperator = require("./operators/hash");
var addEach = require("./operators/add-each");
var equalsOperator = require("pop-equals");
var hashOperator = require("pop-hash");
var copy = require("./copy");

module.exports = FastMap;

Expand Down Expand Up @@ -35,9 +35,9 @@ function FastMap(values, equals, hash, getDefault) {

FastMap.FastMap = FastMap; // hack so require("fast-map").FastMap will work in MontageJS

addEach(FastMap.prototype, GenericCollection.prototype);
addEach(FastMap.prototype, GenericMap.prototype);
addEach(FastMap.prototype, ObservableObject.prototype);
copy(FastMap.prototype, GenericCollection.prototype);
copy(FastMap.prototype, GenericMap.prototype);
copy(FastMap.prototype, ObservableObject.prototype);

FastMap.prototype.constructClone = function (values) {
return new this.constructor(
Expand Down
26 changes: 18 additions & 8 deletions fast-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ var GenericCollection = require("./generic-collection");
var GenericSet = require("./generic-set");
var TreeLog = require("./tree-log");
var ObservableObject = require("./observable-object");
var noop = require("./operators/noop");
var hashOperator = require("./operators/hash");
var equalsOperator = require("./operators/equals");
var addEach = require("./operators/add-each");
var hashOperator = require("pop-hash");
var equalsOperator = require("pop-equals");
var iterate = require("pop-iterate");
var arrayify = require("pop-arrayify");
var copy = require("./copy");

var object_has = Object.prototype.hasOwnProperty;

Expand All @@ -32,9 +33,9 @@ function FastSet(values, equals, hash, getDefault) {

FastSet.FastSet = FastSet; // hack so require("fast-set").FastSet will work in MontageJS

addEach(FastSet.prototype, GenericCollection.prototype);
addEach(FastSet.prototype, GenericSet.prototype);
addEach(FastSet.prototype, ObservableObject.prototype);
copy(FastSet.prototype, GenericCollection.prototype);
copy(FastSet.prototype, GenericSet.prototype);
copy(FastSet.prototype, ObservableObject.prototype);

FastSet.prototype.Buckets = Dict;
FastSet.prototype.Bucket = List;
Expand Down Expand Up @@ -115,8 +116,12 @@ FastSet.prototype.one = function () {
}
};

FastSet.prototype.toArray = function () {
return flatten(this.buckets.map(arrayify));
};

FastSet.prototype.iterate = function () {
return this.buckets.values().flatten().iterate();
return iterate(this.toArray());
};

FastSet.prototype.log = function (charmap, logNode, callback, thisp) {
Expand Down Expand Up @@ -190,3 +195,8 @@ FastSet.prototype.logNode = function (node, write) {
}
};

function flatten(arrays) {
return Array.prototype.concat.apply([], arrays);
}

function noop() {}
14 changes: 8 additions & 6 deletions generic-collection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use strict";

var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var cloneOperator = require("./operators/clone");
var equalsOperator = require("pop-equals");
var compareOperator = require("pop-compare");
var cloneOperator = require("pop-clone");
var unzipOperator = require("pop-zip/pop-unzip");

module.exports = GenericCollection;
function GenericCollection() {
Expand Down Expand Up @@ -88,7 +89,7 @@ GenericCollection.prototype.group = function (callback, thisp, equals) {
};

GenericCollection.prototype.toArray = function () {
return this.map(Function.identity);
return this.map(identity);
};

// this depends on stringable keys, which apply to Array and Iterator
Expand Down Expand Up @@ -204,7 +205,7 @@ GenericCollection.prototype.flatten = function () {
GenericCollection.prototype.zip = function () {
var table = Array.prototype.slice.call(arguments);
table.unshift(this);
return Array.unzip(table);
return unzipOperator(table);
}

GenericCollection.prototype.join = function (delimiter) {
Expand All @@ -220,7 +221,7 @@ GenericCollection.prototype.sorted = function (compare, by, order) {
by = compare.by;
compare = compare.compare || this.contentCompare || compareOperator;
} else {
by = by || Function.identity;
by = by || identity;
}
if (order === undefined)
order = 1;
Expand Down Expand Up @@ -262,3 +263,4 @@ GenericCollection.prototype.only = function () {
}
};

function identity(value) { return value; }
13 changes: 7 additions & 6 deletions generic-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
var ObservableMap = require("./observable-map");
var ObservableObject = require("./observable-object");
var Iterator = require("./iterator");
var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var addEach = require("./operators/add-each");
var equalsOperator = require("pop-equals");
var compareOperator = require("pop-compare");
var copy = require("./copy");

module.exports = GenericMap;
function GenericMap() {
throw new Error("Can't construct. GenericMap is a mixin.");
}

addEach(GenericMap.prototype, ObservableMap.prototype);
addEach(GenericMap.prototype, ObservableObject.prototype);
copy(GenericMap.prototype, ObservableMap.prototype);
copy(GenericMap.prototype, ObservableObject.prototype);

// all of these methods depend on the constructor providing a `store` set

Expand Down Expand Up @@ -152,7 +152,7 @@ GenericMap.prototype.keys = function () {
};

GenericMap.prototype.values = function () {
return this.map(Function.identity);
return this.map(identity);
};

GenericMap.prototype.entries = function () {
Expand Down Expand Up @@ -212,3 +212,4 @@ GenericMapIterator.prototype.next = function () {
}
};

function identity(value) { return value; }
4 changes: 2 additions & 2 deletions generic-order.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var equalsOperator = require("pop-equals");
var compareOperator = require("pop-compare");

module.exports = GenericOrder;
function GenericOrder() {
Expand Down
4 changes: 3 additions & 1 deletion generic-set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

var has = require("pop-has");

module.exports = GenericSet;
function GenericSet() {
throw new Error("Can't construct. GenericSet is a mixin.");
Expand All @@ -14,7 +16,7 @@ GenericSet.prototype.union = function (that) {

GenericSet.prototype.intersection = function (that) {
return this.constructClone(this.filter(function (value) {
return that.has(value);
return has(that, value);
}));
};

Expand Down
44 changes: 31 additions & 13 deletions heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ var GenericCollection = require("./generic-collection");
var ObservableObject = require("./observable-object");
var ObservableRange = require("./observable-range");
var ObservableMap = require("./observable-map");
var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var addEach = require("./operators/add-each");
var equalsOperator = require("pop-equals");
var compareOperator = require("pop-compare");
var copy = require("./copy");

// Max Heap by default. Comparison can be reversed to produce a Min Heap.

Expand All @@ -28,10 +28,10 @@ function Heap(values, equals, compare) {

Heap.Heap = Heap; // hack so require("heap").Heap will work in MontageJS

addEach(Heap.prototype, GenericCollection.prototype);
addEach(Heap.prototype, ObservableObject.prototype);
addEach(Heap.prototype, ObservableRange.prototype);
addEach(Heap.prototype, ObservableMap.prototype);
copy(Heap.prototype, GenericCollection.prototype);
copy(Heap.prototype, ObservableObject.prototype);
copy(Heap.prototype, ObservableRange.prototype);
copy(Heap.prototype, ObservableMap.prototype);

Heap.prototype.constructClone = function (values) {
return new this.constructor(
Expand Down Expand Up @@ -59,7 +59,11 @@ Heap.prototype.pop = function () {
// If there are any values remaining, put the last value on the top and
// let it sink back down.
if (this.content.length > 0) {
this.content.set(0, top);
if (this.content.set) {
this.content.set(0, top);
} else {
this.content[0] = top;
}
this.sink(0);
}
this.length--;
Expand Down Expand Up @@ -91,7 +95,11 @@ Heap.prototype.delete = function (value) {
this.length = this.content.length;
if (index === this.content.length)
return true;
this.content.set(index, top);
if (this.content.set) {
this.content.set(index, top);
} else {
this.content[index] = top;
}
var comparison = this.contentCompare(top, value);
if (comparison > 0) {
this.float(index);
Expand Down Expand Up @@ -126,8 +134,13 @@ Heap.prototype.float = function (index) {
var parent = this.content[parentIndex];
// If the parent is less than it
if (this.contentCompare(parent, value) < 0) {
this.content.set(parentIndex, value);
this.content.set(index, parent);
if (this.content.set) {
this.content.set(parentIndex, value);
this.content.set(index, parent);
} else {
this.content[parentIndex] = value;
this.content[index] = parent;
}
} else {
// Stop propagating if the parent is greater than the value.
break;
Expand Down Expand Up @@ -180,8 +193,13 @@ Heap.prototype.sink = function (index) {
// if there is a child that is less than the value, float the child and
// sink the value.
if (needsSwap) {
this.content.set(index, this.content[swapIndex]);
this.content.set(swapIndex, value);
if (this.content.set) {
this.content.set(index, this.content[swapIndex]);
this.content.set(swapIndex, value);
} else {
this.content[index] = this.content[swapIndex];
this.content[swapIndex] = value;
}
index = swapIndex;
// and continue sinking
} else {
Expand Down
Loading