mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-01-25 15:31:33 +01:00
format
This commit is contained in:
parent
bcc53c48a7
commit
c88d3bcf85
@ -1,5 +1,4 @@
|
||||
export function HammingEncode(data: number): string {
|
||||
|
||||
const enc: Array<number> = [0];
|
||||
const data_bits: Array<any> = data.toString(2).split("").reverse();
|
||||
|
||||
@ -11,8 +10,8 @@ export function HammingEncode(data: number): string {
|
||||
|
||||
/* NOTE: writing the data like this flips the endianness, this is what the
|
||||
* original implementation by Hedrauta did so I'm keeping it like it was. */
|
||||
for(let i = 1; k > 0; i++) {
|
||||
if((i & (i - 1)) != 0) {
|
||||
for (let i = 1; k > 0; i++) {
|
||||
if ((i & (i - 1)) != 0) {
|
||||
enc[i] = data_bits[--k];
|
||||
} else {
|
||||
enc[i] = 0;
|
||||
@ -22,26 +21,26 @@ export function HammingEncode(data: number): string {
|
||||
let parity: any = 0;
|
||||
|
||||
/* Figure out the subsection parities */
|
||||
for(let i = 0; i < enc.length; i++) {
|
||||
if(enc[i]) {
|
||||
for (let i = 0; i < enc.length; i++) {
|
||||
if (enc[i]) {
|
||||
parity ^= i;
|
||||
}
|
||||
}
|
||||
|
||||
parity = parity.toString(2).split("").reverse();
|
||||
parity.forEach((e: any, i: any , a: any) => {
|
||||
parity.forEach((e: any, i: any, a: any) => {
|
||||
a[i] = parseInt(e);
|
||||
});
|
||||
|
||||
/* Set the parity bits accordingly */
|
||||
for(let i = 0; i < parity.length; i++) {
|
||||
for (let i = 0; i < parity.length; i++) {
|
||||
enc[2 ** i] = parity[i] ? 1 : 0;
|
||||
}
|
||||
|
||||
parity = 0;
|
||||
/* Figure out the overall parity for the entire block */
|
||||
for(let i = 0; i < enc.length; i++) {
|
||||
if(enc[i]) {
|
||||
for (let i = 0; i < enc.length; i++) {
|
||||
if (enc[i]) {
|
||||
parity++;
|
||||
}
|
||||
}
|
||||
@ -61,12 +60,12 @@ export function HammingEncodeProperly(data: number): string {
|
||||
|
||||
let m = 1;
|
||||
|
||||
while((2 ** ((2 ** m) - m - 1)) < data) {
|
||||
while (2 ** (2 ** m - m - 1) < data) {
|
||||
m++;
|
||||
}
|
||||
|
||||
const n: number = (2 ** m);
|
||||
const k: number = (2 ** m) - m - 1;
|
||||
const n: number = 2 ** m;
|
||||
const k: number = 2 ** m - m - 1;
|
||||
|
||||
const enc: Array<number> = [0];
|
||||
const data_bits: Array<any> = data.toString(2).split("").reverse();
|
||||
@ -78,8 +77,8 @@ export function HammingEncodeProperly(data: number): string {
|
||||
/* Flip endianness as in the original implementation by Hedrauta
|
||||
* and write the data back to front
|
||||
* XXX why do we do this? */
|
||||
for(let i = 1, j = k; i < n; i++) {
|
||||
if((i & (i - 1)) != 0) {
|
||||
for (let i = 1, j = k; i < n; i++) {
|
||||
if ((i & (i - 1)) != 0) {
|
||||
enc[i] = data_bits[--j] ? data_bits[j] : 0;
|
||||
}
|
||||
}
|
||||
@ -87,26 +86,26 @@ export function HammingEncodeProperly(data: number): string {
|
||||
let parity: any = 0;
|
||||
|
||||
/* Figure out the subsection parities */
|
||||
for(let i = 0; i < n; i++) {
|
||||
if(enc[i]) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (enc[i]) {
|
||||
parity ^= i;
|
||||
}
|
||||
}
|
||||
|
||||
parity = parity.toString(2).split("").reverse();
|
||||
parity.forEach((e: any, i: any , a: any) => {
|
||||
parity.forEach((e: any, i: any, a: any) => {
|
||||
a[i] = parseInt(e);
|
||||
});
|
||||
|
||||
/* Set the parity bits accordingly */
|
||||
for(let i = 0; i < m; i++) {
|
||||
for (let i = 0; i < m; i++) {
|
||||
enc[2 ** i] = parity[i] ? 1 : 0;
|
||||
}
|
||||
|
||||
parity = 0;
|
||||
/* Figure out the overall parity for the entire block */
|
||||
for(let i = 0; i < n; i++) {
|
||||
if(enc[i]) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (enc[i]) {
|
||||
parity++;
|
||||
}
|
||||
}
|
||||
@ -122,17 +121,17 @@ export function HammingDecode(data: string): number {
|
||||
const bits: Array<number> = [];
|
||||
|
||||
/* TODO why not just work with an array of digits from the start? */
|
||||
for(const i in data.split("")) {
|
||||
for (const i in data.split("")) {
|
||||
const bit = parseInt(data[i]);
|
||||
bits[i] = bit;
|
||||
|
||||
if(bit) {
|
||||
if (bit) {
|
||||
err ^= +i;
|
||||
}
|
||||
}
|
||||
|
||||
/* If err != 0 then it spells out the index of the bit that was flipped */
|
||||
if(err) {
|
||||
if (err) {
|
||||
/* Flip to correct */
|
||||
bits[err] = bits[err] ? 0 : 1;
|
||||
}
|
||||
@ -141,11 +140,11 @@ export function HammingDecode(data: string): number {
|
||||
* which we don't care about). Each bit at an index that is a power of 2 is
|
||||
* a parity bit and not part of the actual message. */
|
||||
|
||||
let ans = '';
|
||||
let ans = "";
|
||||
|
||||
for(let i = 1; i < bits.length; i++) {
|
||||
for (let i = 1; i < bits.length; i++) {
|
||||
/* i is not a power of two so it's not a parity bit */
|
||||
if((i & (i - 1)) != 0) {
|
||||
if ((i & (i - 1)) != 0) {
|
||||
ans += bits[i];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user