Skip to content

Commit

Permalink
update Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
dankogai committed Aug 10, 2020
1 parent 69ce1ea commit b362bff
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ JS=combinatorics.js
MJS=combinatorics.mjs
DTS=combinatorics.d.ts
COMMONJS_DIR=commonjs
COMMONJS=$(COMMONJS_DIR)/$(JS)
UMD_DIR=umd
UMD=$(UMD_DIR)/$(JS)

all: $(PJ) $(JS) $(COMMONJS_DIR)/$(JS) $(UMD_DIR)/$(JS)
all: $(PJ) $(JS) $(COMMONJS) $(UMD)

$(JS): $(PJ) $(TS)
tsc -d --target es6 $(TS)

$(COMMONJS_DIR)/$(JS): $(PJ) $(TS)
$(COMMONJS): $(PJ) $(TS)
tsc --module commonjs --outDir $(COMMONJS_DIR) --target es6 $(TS)

$(UMD_DIR)/$(JS): $(PJ) $(TS)
$(UMD): $(PJ) $(TS)
tsc --module umd --outDir $(UMD_DIR) --target es6 $(TS)

test: $(PJ) $(JS)
test: all
mocha --require esm

clean:
-rm $(DTS) $(MJS) $(JS)
-rm $(DTS) $(MJS) $(JS) $(COMMONJS) $(UMD)

38 changes: 38 additions & 0 deletions commonjs/combinatorics.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,21 @@ class _CBase {
return undefined;
return n;
}
/**
* get the `n`th element of the iterator.
* negative `n` goes backwards
*/
nth(n) { return []; }
;
/**
* pick random element
*/
sample() {
return this.nth(randomInteger(this.length));
}
/**
* an infinite steam of random elements
*/
samples() {
return function* (it) {
while (true)
Expand Down Expand Up @@ -267,6 +277,34 @@ class Combination extends _CBase {
this.comb = combinadic(this.seed.length, this.size);
Object.freeze(this);
}
/**
* returns an iterator which is more efficient
* than the default iterator that uses .nth
*
* @link https://en.wikipedia.org/wiki/Combinatorial_number_system#Applications
*/
bitwiseIterator() {
// console.log('overriding _CBase');
const ctor = this.length.constructor;
const [zero, one, two] = [ctor(0), ctor(1), ctor(2)];
const inc = (x) => {
const u = x & -x;
const v = u + x;
return v + (((v ^ x) / u) >> two);
};
let x = (one << ctor(this.size)) - one; // 0b11...1
return function* (it, len) {
for (let i = 0; i < len; i++, x = inc(x)) {
var result = [];
for (let y = x, j = 0; zero < y; y >>= one, j++) {
if (y & one)
result.push(it.seed[j]);
}
// console.log(`x = ${x}`);
yield result;
}
}(this, this.length);
}
nth(n) {
n = this._check(n);
if (n === undefined)
Expand Down
38 changes: 38 additions & 0 deletions umd/combinatorics.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,21 @@
return undefined;
return n;
}
/**
* get the `n`th element of the iterator.
* negative `n` goes backwards
*/
nth(n) { return []; }
;
/**
* pick random element
*/
sample() {
return this.nth(randomInteger(this.length));
}
/**
* an infinite steam of random elements
*/
samples() {
return function* (it) {
while (true)
Expand Down Expand Up @@ -276,6 +286,34 @@
this.comb = combinadic(this.seed.length, this.size);
Object.freeze(this);
}
/**
* returns an iterator which is more efficient
* than the default iterator that uses .nth
*
* @link https://en.wikipedia.org/wiki/Combinatorial_number_system#Applications
*/
bitwiseIterator() {
// console.log('overriding _CBase');
const ctor = this.length.constructor;
const [zero, one, two] = [ctor(0), ctor(1), ctor(2)];
const inc = (x) => {
const u = x & -x;
const v = u + x;
return v + (((v ^ x) / u) >> two);
};
let x = (one << ctor(this.size)) - one; // 0b11...1
return function* (it, len) {
for (let i = 0; i < len; i++, x = inc(x)) {
var result = [];
for (let y = x, j = 0; zero < y; y >>= one, j++) {
if (y & one)
result.push(it.seed[j]);
}
// console.log(`x = ${x}`);
yield result;
}
}(this, this.length);
}
nth(n) {
n = this._check(n);
if (n === undefined)
Expand Down

0 comments on commit b362bff

Please sign in to comment.