Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also update documentation and fix some method coverage.
- Loading branch information
Showing
17 changed files
with
1,242 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; | ||
|
Oops, something went wrong.