Skip to content

Commit

Permalink
Coremask
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Jan 12, 2024
1 parent 3fcab5e commit 0d2ff2c
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 50 deletions.
25 changes: 0 additions & 25 deletions .eslintrc.js

This file was deleted.

11 changes: 11 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"parser": "@typescript-eslint/parser",
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
// Add custom rules here
}
}
44 changes: 22 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
name: coretime-utils CI

on:
push:
branches: [main]
pull_request:
branches: [main]
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

# Setup Node.js environment
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '14' # You can specify your Node.js version here
# Setup Node.js environment
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '14' # You can specify your Node.js version here

# Install dependencies
- name: Install dependencies
run: npm install
# Install dependencies
- name: Install dependencies
run: npm install

# Run linter
- name: Run Linter
run: npm run lint
# Run linter
- name: Run Linter
run: npm run lint

# Check Prettier
- name: Check Prettier
run: npm run prettier-check
# Check Prettier
- name: Check Prettier
run: npm run prettier-check

# Run tests
- name: Run Tests
run: npm test
# Run tests
- name: Run Tests
run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# coretime-utils

An npm package containing types and constants that are commonly reused in regarding to Coretime
52 changes: 52 additions & 0 deletions __tests__/coremask.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { CoreMask, COMPLETE_MASK, VOID_MASK } from '../src';

describe('CoreMask utils work', () => {
test('constructor works', () => {
expect(() => new CoreMask(COMPLETE_MASK)).not.toThrow();
expect(() => new CoreMask(VOID_MASK)).not.toThrow();
expect(() => new CoreMask('foo')).toThrow();
expect(() => new CoreMask(VOID_MASK + '0')).toThrow();
});

test('fromChunk works', () => {
expect(CoreMask.fromChunk(40, 60).getMask()).toEqual('0x0000000000fffff00000');
});

test('fromBin works', () => {
expect(
CoreMask.fromBin(
'11111111111111111111111111111111111111111111111111111111111111111111111111111111',
).getMask(),
).toEqual(COMPLETE_MASK);

expect(
CoreMask.fromBin(
'00000000000000000000000000000000000000000000000000000000000000000000000000000000',
).getMask(),
).toEqual(VOID_MASK);
});

test('countOnes works', () => {
expect(CoreMask.voidMask().countOnes()).toEqual(0);
expect(CoreMask.completeMask().countOnes()).toEqual(80);
expect(CoreMask.fromChunk(0, 20).countOnes()).toEqual(20);
});

test('countZeros works', () => {
expect(CoreMask.voidMask().countZeros()).toEqual(80);
expect(CoreMask.completeMask().countZeros()).toEqual(0);
expect(CoreMask.fromChunk(0, 20).countZeros()).toEqual(60);
});

test('toBin works', () => {
expect(CoreMask.voidMask().toBin()).toEqual(
'00000000000000000000000000000000000000000000000000000000000000000000000000000000',
);
expect(CoreMask.completeMask().toBin()).toEqual(
'11111111111111111111111111111111111111111111111111111111111111111111111111111111',
);
expect(CoreMask.fromChunk(0, 5).toBin()).toEqual(
'11111000000000000000000000000000000000000000000000000000000000000000000000000000',
);
});
});
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"name": "coretime-utils",
"version": "1.0.0",
"description": "",
"main": "index.js",
"version": "0.1.0",
"description": "An npm package containing types and constants that are commonly reused in regarding Coretime.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"dev": "npx ts-node src/main.ts",
"prettier": "prettier --write .",
"lint": "eslint '**/*.ts'",
"test": "jest"
},
"files": [
"dist/**/*"
],
"keywords": [],
"author": "",
"license": "ISC",
Expand Down
89 changes: 89 additions & 0 deletions src/coreMask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
export const VOID_MASK = '0x00000000000000000000'; // hex encoded 80 bit bitmap.
export const COMPLETE_MASK = '0xffffffffffffffffffff'; // hex encoded 80 bit bitmap.

export class CoreMask {
private mask: string;

getMask(): string {
return this.mask;
}

constructor(mask: string) {
// 10 hex characters plus the '0x' at the start.
if (isHex(mask) && mask.length === 22) {
this.mask = mask;
} else {
throw new Error('Invalid mask');
}
}

static completeMask(): CoreMask {
return new CoreMask(COMPLETE_MASK);
}

static voidMask(): CoreMask {
return new CoreMask(VOID_MASK);
}

static fromChunk(from: number, to: number): CoreMask {
if (from < 0 || to >= 80 || from > to) {
throw new Error('Invalid bit range');
}

let mask = 0n;
for (let i = from; i < to; i++) {
mask |= 1n << BigInt(79 - i);
}

return new CoreMask('0x' + mask.toString(16).padStart(20, '0'));
}

static fromBin(bin: string): CoreMask {
let hexMask = '';
for (let i = 0; i < bin.length; i += 4) {
const v = parseInt(bin.slice(i, i + 4), 2);
hexMask += v.toString(16);
}
return new CoreMask(`0x${hexMask}`);
}

countZeros(): number {
let count = 0;
for (let i = 2; i < this.mask.length; ++i) {
let v = parseInt(this.mask.slice(i, i + 1), 16);
for (let j = 0; j < 4; ++j) {
if ((v & 1) === 0) ++count;
v >>= 1;
}
}
return count;
}

countOnes(): number {
let count = 0;
for (let i = 2; i < this.mask.length; ++i) {
let v = parseInt(this.mask.slice(i, i + 1), 16);
while (v > 0) {
if (v & 1) ++count;
v >>= 1;
}
}
return count;
}

toBin(): string {
let bin = '';
for (let i = 2; i < this.mask.length; ++i) {
const v = parseInt(this.mask.slice(i, i + 1), 16);
for (let j = 3; j >= 0; --j) {
bin += v & (1 << j) ? '1' : '0';
}
}
return bin;
}
}

function isHex(str: string) {
const regex = /^0x[0-9a-fA-F]+$/;
return regex.test(str);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './coreMask';
export * from './types';
Empty file removed src/main.ts
Empty file.
33 changes: 33 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { CoreMask } from '.';

export type Timeslice = number;
export type CoreIndex = number;

export type Balance = number;

export type RegionId = {
begin: Timeslice;
core: CoreIndex;
mask: CoreMask;
};

export type RegionRecord = {
end: Timeslice;
owner: string;
paid: null | Balance;
};

export type Region = {
regionId: RegionId;
regionRecord: RegionRecord;
};

export const Id = {
_enum: {
U8: 'u8',
U16: 'u16',
U32: 'u32',
U64: 'u64',
U128: 'u128',
},
};
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist",
"declaration": true,
"target": "ES2020"
}
}

0 comments on commit 0d2ff2c

Please sign in to comment.