Skip to content

Commit

Permalink
Merge pull request #3 from you21979/typescript-pr
Browse files Browse the repository at this point in the history
typescript rewrite
  • Loading branch information
you21979 committed Mar 28, 2018
2 parents 161c8a4 + 5bbc323 commit b6eec7f
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 241 deletions.
26 changes: 26 additions & 0 deletions .npmignore
@@ -0,0 +1,26 @@
built
doc
Gulpfile.ts
internal
issue_template.md
jenkins.sh
lib/README.md
lib/enu
netci.groovy
pull_request_template.md
scripts
src
tests
tslint.json
Jakefile.js
.editorconfig
.gitattributes
.gitmodules
.settings/
.travis.yml
.circleci
.vscode/
.parallelperf.json
test.config
package-lock.json
yarn.lock
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -4,6 +4,8 @@ node_js:
- "8"
- "6"
- "4"
script: npm test --coverage
script:
- npm run build
- npm test --coverage
after_success:
- npm run coveralls
1 change: 0 additions & 1 deletion index.js

This file was deleted.

117 changes: 0 additions & 117 deletions lib/binaryarray.js

This file was deleted.

24 changes: 14 additions & 10 deletions package.json
@@ -1,11 +1,12 @@
{
"name": "binaryarray",
"version": "0.0.6",
"version": "0.1.0",
"description": "the binary array",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/"
],
"repository": {
"type": "git",
"url": "git+https://github.com/you21979/node-binaryarray.git"
Expand All @@ -16,13 +17,16 @@
"binary"
],
"devDependencies": {
"istanbul":"",
"coveralls":"",
"power-assert":"",
"mocha":""
"@types/node": "",
"@types/mocha": "",
"typescript": "",
"istanbul": "",
"coveralls": "",
"mocha": ""
},
"scripts": {
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha",
"build": "./node_modules/.bin/tsc -p ./",
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha ./dist/test",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
},
"author": "Yuki Akiyama",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
@@ -0,0 +1,3 @@
import { BinaryArray } from './lib/binaryarray'
export = BinaryArray

118 changes: 118 additions & 0 deletions src/lib/binaryarray.ts
@@ -0,0 +1,118 @@
import assert = require('assert');
import util = require('./util');

export class BinaryArray{
maxnum: number;
storage: Uint32Array;
constructor(maxnum : number){
this.maxnum = maxnum
this.storage = util.createArray(util.getArraySize(maxnum), 0)
}
bitOn(no : number) : BinaryArray{
assert(this.maxnum > no, 'on:over flagmax');
const idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'on:over idx range');
const pos = util.getFlagPos(no);
const flag = 1 << pos;
this.storage[idx] |= flag;
return this;
}
bitOff(no : number) : BinaryArray{
assert(this.maxnum > no, 'off:over flagmax');
const idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'off:over idx range');
const pos = util.getFlagPos(no);
const flag = 1 << pos;
if(this.storage[idx] & flag){
this.storage[idx] ^= flag;
}
return this;
}
at(no : number) : number{
assert(this.maxnum > no, 'get:over flagmax');
const idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'is:over idx range');
const pos = util.getFlagPos(no);
const flag = 1 << pos;
return this.storage[idx] & flag ? 1 : 0;
}
toArray() : Array<number>{
const w = [];
const max = this.maxnum;
for(let i = 0; i < max; ++i ){
w.push(this.at(i));
}
return w;
}
serialize(spec : Object) : Array<string>{
assert(spec instanceof Object, 'spec is must be Object')
const w = this.toArray();
return Object.keys(spec)
.filter((k) => w[spec[k]])
.map((k) => k)
}
toJSON() : string{
return JSON.stringify(this.toArray())
}
isRange(no : number) : boolean{
if(!(this.maxnum > no)){
return false;
}
const idx = util.getArrayIndex(no);
if(!(idx >= 0 && idx < this.storage.length)){
return false;
}
return true;
}
rangeOf(xs : any) : Object{
if(!(xs instanceof Array)){
xs = [xs];
}
return xs.reduce((r, v) => {
r[v] = this.at(v)
return r;
}, {})
}
check(on_list : Array<number>, off_list : Array<number> = []) : boolean{
const on = this.rangeOf(on_list);
const off = this.rangeOf(off_list);
const x = Object.keys(on).reduce((r, v) => { return r & on[v] }, 1)
const y = Object.keys(off).reduce((r, v) => { return r & ~off[v] }, 1)
return (x & y) ? true : false
}
toHexString() : string{
let str = '';
const n = this.storage.length;
for(let i=n-1;i>=0;--i){
str = str + util.NumberToHexString(this.storage[i], 8);
}
return str;
}
static loadFromHexString(maxnum : number, str : string) : BinaryArray{
const ba = new BinaryArray(maxnum);
const s = 8;
let b = str.length - s;
let e = str.length - 0;
const n = str.length / s;
for(let i = 0; i < n; ++i){
ba.storage[i] = parseInt(str.substring(b,e), 16);
b -= s;
e -= s;
}
return ba;
}
static loadFromArray(xs : Array<number>) : BinaryArray{
const ba = new BinaryArray(xs.length);
xs.map((v, i) => [v, i])
.filter((v) => v[0])
.forEach((v) => {
ba.bitOn(v[1]);
})
return ba;
}
static deserialize(list : Array<string>, spec : Object, max : number) : BinaryArray{
const ba = new BinaryArray(max);
list.forEach((v) => { ba.bitOn(spec[v]) })
return ba;
}
}

0 comments on commit b6eec7f

Please sign in to comment.