Skip to content

Commit

Permalink
typescript first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
you21979 committed Mar 28, 2018
1 parent 161c8a4 commit f20799e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 147 deletions.
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.

21 changes: 12 additions & 9 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,12 +17,14 @@
"binary"
],
"devDependencies": {
"istanbul":"",
"coveralls":"",
"power-assert":"",
"mocha":""
"@types/node": "",
"power-assert": "",
"istanbul": "",
"coveralls": "",
"mocha": ""
},
"scripts": {
"build": "tsc -p ./",
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
},
Expand Down
118 changes: 118 additions & 0 deletions src/binaryarray.ts
@@ -0,0 +1,118 @@
import assert = require('assert');
import util = require('./util');

export class BinaryArray{
private maxnum: number;
private 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) => { return w[spec[k]] })
.map((k) => { return 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 : Object, off_list : Object) : 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) => { return [v, i] })
.filter((v) => { return 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;
}
}
3 changes: 3 additions & 0 deletions src/index.ts
@@ -0,0 +1,3 @@
import { BinaryArray } from './binaryarray'
module.exports = BinaryArray

42 changes: 23 additions & 19 deletions lib/util.js → src/util.ts
@@ -1,30 +1,34 @@
"use strict";

// 配列一つに入るビットの数
var BITS = 32;
const BITS : number = 32;

// フラグ数から配列のサイズを求める
var getArraySize = exports.getArraySize = function(flagmax){
export const getArraySize = (flagmax : number) : number => {
return Math.ceil(flagmax/BITS);
};
}

// フラグ番号から配列の位置を求める
var getArrayIndex = exports.getArrayIndex = function(no){
export const getArrayIndex = (no : number) : number => {
return Math.floor(no/BITS);
};
}

// フラグ番号から実際のフラグの位置を求める
var getFlagPos = exports.getFlagPos = function(no){
export const getFlagPos = (no : number) : number => {
return no % BITS;
};
var digitfix = exports.digitfix = function(str, b){
}


export const digitfix = (str : string, b : number) : string => {
if(str.length < b ){
var len = b - str.length;
for(var i = 0; i < len; ++i){
const len = b - str.length;
for(let i = 0; i < len; ++i){
str = '0' + str;
}
}
return str;
};
var hexconv = exports.hexconv = function(n){

export const hexconv = (n : number) : string => {
switch (n) {
case 10: return 'A';
case 11: return 'B';
Expand All @@ -35,10 +39,11 @@ var hexconv = exports.hexconv = function(n){
}
return n.toString();
};

// 数値からヘックスの文字列にする
var NumberToHexString = exports.NumberToHexString = function(num, digit){
var remainder = 0;
var str = '';
export const NumberToHexString = (num : number, digit : number) : string => {
let remainder = 0;
let str = '';
while (num > 0) {
remainder = num % 16;
num = (num - remainder) / 16;
Expand All @@ -48,10 +53,9 @@ var NumberToHexString = exports.NumberToHexString = function(num, digit){
}

// 初期化済みの配列を作成する
var createArray = exports.createArray = function(size, init_val){
init_val = init_val || 0;
var w = new Uint32Array(size);
for(var i = 0; i < size; ++i){
export const createArray = (size : number, init_val : number = 0) : Uint32Array => {
const w = new Uint32Array(size);
for(let i = 0; i < size; ++i){
w[i] = init_val;
}
return w;
Expand Down
2 changes: 1 addition & 1 deletion test/test_binaryarray.js
@@ -1,5 +1,5 @@
var assert = require('power-assert');
var BinaryArray = require('../');
var BinaryArray = require('..');

describe('test', function() {
it('array length test', function() {
Expand Down
20 changes: 20 additions & 0 deletions tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"rootDir": "./src",
"outDir": "./dist",
"noImplicitAny": false,
"sourceMap": false,
"removeComments": true,
"moduleResolution": "node",
"declaration": true
},
"exclude": [
"node_modules",
"src/**/*.spec.*"
],
"include": [
"src/**/*"
]
}

0 comments on commit f20799e

Please sign in to comment.