Skip to content

Commit

Permalink
Add SortedArray, +Set, +Map
Browse files Browse the repository at this point in the history
Also update documentation and fix some method coverage.
  • Loading branch information
kriskowal committed Oct 30, 2012
1 parent 5351fe3 commit 592ec42
Show file tree
Hide file tree
Showing 17 changed files with 1,242 additions and 289 deletions.
1,005 changes: 725 additions & 280 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion array.js
Expand Up @@ -90,7 +90,7 @@ Array.prototype.swap = function (index, length, plus) {
};

Array.prototype.toArray = Reducible.toArray;
Array.prototype.toObject = Reducible.toArray;
Array.prototype.toObject = Reducible.toObject;
Array.prototype.addEach = Reducible.addEach;
Array.prototype.equals = Reducible.equals;
Array.prototype.compare = Reducible.compare;
Expand Down
53 changes: 53 additions & 0 deletions bench/sorted.js
@@ -0,0 +1,53 @@

var SortedSet = require("../sorted-set");
var SortedArraySet = require("../sorted-array-set");

var Fuzz = require("../spec/fuzz");
var iterations = 1000;
var size = 10000;

[10, 100, 100, 101, 105, 112, 125, 150, 200, 300, 500, 1000, 10000, 20000, 30000].forEach(function (size) {

var numbers = [];
for (var i = 0; i < size; i++) {
numbers.push(i);
}
var random = Fuzz.makeRandom();
numbers.sort(function () {
return random() < .5;
});

function bench(Sorted) {
var set = Sorted();
for (var i = 0; i < size; i++) {
set.add(numbers[i]);
}
for (var i = 0; i < size; i++) {
set.delete(numbers[i]);
}
}

function hrtime() {
var hrtime = process.hrtime();
return hrtime[0] * 1000 + hrtime[1] / Math.pow(2, 32);
}

function time(callback) {
var start = hrtime();
callback();
var stop = hrtime();
return stop - start;
};

var sortedSetSpeed = time(function () {
bench(SortedSet);
});
var sortedArraySetSpeed = time(function () {
bench(SortedArraySet);
});

console.log(size + ":", (sortedSetSpeed < sortedArraySetSpeed ? "SortedSet WINS" : "SortedArraySet WINS"));
console.log(' SortedSet:', sortedSetSpeed);
console.log('SortedArraySet:', sortedArraySetSpeed);

});
10 changes: 10 additions & 0 deletions demo/sorted-array.js
@@ -0,0 +1,10 @@
var SortedArray = require("../sorted-array");

var array = SortedArray([]);
array.addEach([5, 2, 4, 3, 1]);
console.log(array.toArray());

var array = SortedArray([1, 1, 1, 1, 1]);
console.log(array.indexOf(1));
console.log(array.lastIndexOf(1));

4 changes: 2 additions & 2 deletions fast-map.js
Expand Up @@ -19,10 +19,10 @@ function FastMap(values, equals, hash, content) {
this.content = content;
this.contentSet = new Set(
undefined,
function (a, b) {
function keysEqual(a, b) {
return equals(a.key, b.key);
},
function (item) {
function keyHash(item) {
return hash(item.key);
}
);
Expand Down
1 change: 1 addition & 0 deletions list.js
Expand Up @@ -270,6 +270,7 @@ List.prototype.addEach = Reducible.addEach;
List.prototype.forEach = Reducible.forEach;
List.prototype.map = Reducible.map;
List.prototype.toArray = Reducible.toArray;
List.prototype.toObject = Reducible.toObject;
List.prototype.filter = Reducible.filter;
List.prototype.every = Reducible.every;
List.prototype.some = Reducible.some;
Expand Down
4 changes: 2 additions & 2 deletions lru-map.js
Expand Up @@ -20,10 +20,10 @@ function LruMap(values, maxLength, equals, hash, content) {
this.contentSet = new LruSet(
undefined,
maxLength,
function setContentEquals(a, b) {
function keysEqual(a, b) {
return equals(a.key, b.key);
},
function setContentHash(item) {
function keyHash(item) {
return hash(item.key);
}
);
Expand Down
4 changes: 2 additions & 2 deletions map.js
Expand Up @@ -19,10 +19,10 @@ function Map(values, equals, hash, content) {
this.content = content;
this.contentSet = new Set(
undefined,
function (a, b) {
function keysEqual(a, b) {
return equals(a.key, b.key);
},
function (item) {
function keyHash(item) {
return hash(item.key);
}
);
Expand Down
3 changes: 3 additions & 0 deletions minify
Expand Up @@ -21,6 +21,9 @@
observable.js \
reducible.js \
set.js \
sorted-array-map.js \
sorted-array-set.js \
sorted-array.js \
sorted-map.js \
sorted-set.js \
tree-log.js \
Expand Down
21 changes: 21 additions & 0 deletions object.js
Expand Up @@ -217,6 +217,27 @@ Object.set = function (object, key, value) {
}
};

Object.addEach = function (target, source) {
if (typeof source.forEach === "function") {
// copy map-alikes
if (typeof source.keys === "function") {
source.forEach(function (value, key) {
target[key] = value;
});
// iterate key value pairs of other iterables
} else {
source.forEach(function (pair) {
target[pair[0]] = pair[1];
});
}
} else {
// copy other objects as map-alikes
Object.keys(source).forEach(function (key) {
target[key] = source[key];
});
}
};

/**
Iterates over the owned properties of an object.
Expand Down
70 changes: 70 additions & 0 deletions sorted-array-map.js
@@ -0,0 +1,70 @@
"use strict";

require("./object");
var SortedArraySet = require("./sorted-array-set");
var Reducible = require("./reducible");
var AbstractMap = require("./abstract-map");

module.exports = SortedArrayMap;

function SortedArrayMap(values, equals, compare, content) {
if (!(this instanceof SortedArrayMap)) {
return new SortedArrayMap(values, equals, compare, content);
}
equals = equals || Object.equals;
compare = compare || Object.compare;
content = content || Function.noop;
this.contentEquals = equals;
this.contentCompare = compare;
this.content = content;
this.contentSet = new SortedArraySet(
null,
function keysEqual(a, b) {
return equals(a.key, b.key);
},
function compareKeys(a, b) {
return compare(a.key, b.key);
}
);
this.length = 0;
this.addEach(values);
}

SortedArrayMap.prototype.constructClone = function (values) {
return new this.constructor(
values,
this.contentEquals,
this.contentCompare,
this.content
);
};

SortedArrayMap.prototype.addEach = AbstractMap.addEach;
SortedArrayMap.prototype.has = AbstractMap.has;
SortedArrayMap.prototype.get = AbstractMap.get;
SortedArrayMap.prototype.set = AbstractMap.set;
SortedArrayMap.prototype['delete'] = AbstractMap['delete'];
SortedArrayMap.prototype.clear = AbstractMap.clear;
SortedArrayMap.prototype.reduce = AbstractMap.reduce;
SortedArrayMap.prototype.keys = AbstractMap.keys;
SortedArrayMap.prototype.values = AbstractMap.values;
SortedArrayMap.prototype.items = AbstractMap.items;
SortedArrayMap.prototype.Item = AbstractMap.Item;

SortedArrayMap.prototype.forEach = Reducible.forEach;
SortedArrayMap.prototype.map = Reducible.map;
SortedArrayMap.prototype.toArray = Reducible.toArray;
SortedArrayMap.prototype.toObject = Reducible.toObject;
SortedArrayMap.prototype.filter = Reducible.filter;
SortedArrayMap.prototype.every = Reducible.every;
SortedArrayMap.prototype.some = Reducible.some;
SortedArrayMap.prototype.all = Reducible.all;
SortedArrayMap.prototype.any = Reducible.any;
SortedArrayMap.prototype.min = Reducible.min;
SortedArrayMap.prototype.max = Reducible.max;
SortedArrayMap.prototype.sum = Reducible.sum;
SortedArrayMap.prototype.average = Reducible.average;
SortedArrayMap.prototype.flatten = Reducible.flatten;
SortedArrayMap.prototype.zip = Reducible.zip;
SortedArrayMap.prototype.clone = Reducible.clone;

24 changes: 24 additions & 0 deletions sorted-array-set.js
@@ -0,0 +1,24 @@
"use strict";

module.exports = SortedArraySet;

var SortedArray = require("./sorted-array");

function SortedArraySet(values, equals, compare, content) {
if (!(this instanceof SortedArraySet)) {
return new SortedArraySet(values, equals, compare, content);
}
SortedArray.call(this, values, equals, compare, content);
}

SortedArraySet.prototype = Object.create(SortedArray.prototype);

SortedArraySet.prototype.add = function (value) {
if (!this.has(value)) {
SortedArray.prototype.add.call(this, value);
return true;
} else {
return false;
}
};

0 comments on commit 592ec42

Please sign in to comment.