Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port current version of RSSExpandedReader to javascript #630

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/oned/MultiFormatOneDReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class MultiFormatOneDReader extends OneDReader {
this.readers.push(new RSS14Reader());
}
if (possibleFormats.includes(BarcodeFormat.RSS_EXPANDED)) {
// XXX
console.warn('RSS Expanded reader IS NOT ready for production yet! use at your own risk.');
this.readers.push(new RSSExpandedReader());
}
Expand All @@ -84,7 +85,7 @@ export default class MultiFormatOneDReader extends OneDReader {
this.readers.push(new Code128Reader());
this.readers.push(new ITFReader());
this.readers.push(new RSS14Reader());
// this.readers.push(new RSSExpandedReader());
this.readers.push(new RSSExpandedReader());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/oned/rss/AbstractRSSReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default abstract class AbstractRSSReader extends OneDReader {
this.dataCharacterCounters = new Int32Array(8);
this.oddRoundingErrors = new Array<number>(4);
this.evenRoundingErrors = new Array<number>(4);
this.oddCounts = new Array<number>(this.dataCharacterCounters.length / 2);
this.evenCounts = new Array<number>(this.dataCharacterCounters.length / 2);
this.oddCounts = new Array<number>(Math.trunc(this.dataCharacterCounters.length / 2));
this.evenCounts = new Array<number>(Math.trunc(this.dataCharacterCounters.length / 2));
}

protected getDecodeFinderCounters(): Int32Array {
Expand Down
4 changes: 3 additions & 1 deletion src/core/oned/rss/FinderPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import ResultPoint from '../../ResultPoint';

export default class FinderPattern {

private value: number;
private startEnd: number[];
private resultPoints: Array<ResultPoint>;

public constructor(private value: number, private startEnd: number[], start: number, end: number, rowNumber: number) {
public constructor(value: number, startEnd: number[], start: number, end: number, rowNumber: number) {
this.value = value;
this.startEnd = startEnd;
this.resultPoints = new Array<ResultPoint>();
Expand Down
16 changes: 8 additions & 8 deletions src/core/oned/rss/RSSUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ export default class RSSUtils {
return val;
}

private static combins(n: number, r: number): number {
let maxDenom;
let minDenom;
private static combins(n: number /* int */, r: number /* int */): number /* int */ {
let maxDenom /* int */ = 0;
let minDenom /* int */ = 0;
if (n - r > r) {
minDenom = r;
maxDenom = n - r;
} else {
minDenom = n - r;
maxDenom = r;
}
let val = 1;
let j = 1;
for (let i: number = n; i > maxDenom; i--) {
let val /* int */ = 1;
let j /* int */ = 1;
for (let i = n; i > maxDenom; i--) {
val *= i;
if (j <= minDenom) {
val /= j;
val = Math.trunc(val / j);
j++;
}
}
while ((j <= minDenom)) {
val /= j;
val = Math.trunc(val / j);
j++;
}
return val;
Expand Down
8 changes: 4 additions & 4 deletions src/core/oned/rss/expanded/BitArrayBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ExpandedPair from './ExpandedPair';
export default class BitArrayBuilder {
static buildBitArray(pairs: Array<ExpandedPair>): BitArray {
let charNumber: number = pairs.length * 2 - 1;
if (pairs[pairs.length - 1].getRightChar() == null) {
if (pairs[pairs.length - 1].getRightChar() === null) {
charNumber -= 1;
}

Expand All @@ -23,9 +23,9 @@ export default class BitArrayBuilder {
}

for (let i = 1; i < pairs.length; ++i) {
let currentPair: ExpandedPair = pairs[i];
const currentPair: ExpandedPair = pairs[i];

let leftValue = currentPair.getLeftChar().getValue();
const leftValue = currentPair.getLeftChar().getValue();
for (let j = 11; j >= 0; --j) {
if ((leftValue & (1 << j)) !== 0) {
binary.set(accPos);
Expand All @@ -34,7 +34,7 @@ export default class BitArrayBuilder {
}

if (currentPair.getRightChar() !== null) {
let rightValue = currentPair.getRightChar().getValue();
const rightValue = currentPair.getRightChar().getValue();
for (let j = 11; j >= 0; --j) {
if ((rightValue & (1 << j)) !== 0) {
binary.set(accPos);
Expand Down
70 changes: 34 additions & 36 deletions src/core/oned/rss/expanded/ExpandedPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,56 @@ import DataCharacter from '../../rss/DataCharacter';
import FinderPattern from '../../rss/FinderPattern';

export default class ExpandedPair {
private readonly maybeLast: boolean;
private readonly leftchar: DataCharacter;
private readonly rightchar: DataCharacter;
private readonly finderpattern: FinderPattern;
private readonly leftChar: DataCharacter | null;
private readonly rightChar: DataCharacter | null;
private readonly finderPattern: FinderPattern | null;

constructor(leftChar: DataCharacter, rightChar: DataCharacter, finderPatter: FinderPattern, mayBeLast: boolean) {
this.leftchar = leftChar;
this.rightchar = rightChar;
this.finderpattern = finderPatter;
this.maybeLast = mayBeLast;
constructor(
leftChar: DataCharacter | null,
rightChar: DataCharacter | null,
finderPatter: FinderPattern | null,
) {
this.leftChar = leftChar;
this.rightChar = rightChar;
this.finderPattern = finderPatter;
}

mayBeLast(): boolean {
return this.maybeLast;
getLeftChar(): DataCharacter | null {
return this.leftChar;
}
getLeftChar(): DataCharacter {
return this.leftchar;
}
getRightChar(): DataCharacter {
return this.rightchar;

getRightChar(): DataCharacter | null {
return this.rightChar;
}
getFinderPattern(): FinderPattern {
return this.finderpattern;

getFinderPattern(): FinderPattern | null {
return this.finderPattern;
}
mustBeLast() {
return this.rightchar == null;

mustBeLast(): boolean {
return this.rightChar === null;
}

toString(): String {
return '[ ' + this.leftchar + ', ' + this.rightchar + ' : ' + (this.finderpattern == null ? 'null' : this.finderpattern.getValue()) + ' ]';
return '[ ' + this.leftChar + ', ' + this.rightChar + ' : ' + (this.finderPattern === null ? 'null' : this.finderPattern.getValue()) + ' ]';
}

static equals(o1: any, o2: any): boolean {
if (!(o1 instanceof ExpandedPair)) {
static equals(o1: ExpandedPair | null, o2: any): boolean {
if (o2 === null) return o1 === null;
if (!(o2 instanceof ExpandedPair)) {
return false;
}
return ExpandedPair.equalsOrNull(o1.leftchar, o2.leftchar) &&
ExpandedPair.equalsOrNull(o1.rightchar, o2.rightchar) &&
ExpandedPair.equalsOrNull(o1.finderpattern, o2.finderpattern);
return (o1.leftChar === null ? o2.leftChar === null : o1.leftChar.equals(o2.leftChar)) &&
(o1.rightChar === null ? o2.rightChar === null : o1.rightChar.equals(o2.rightChar)) &&
(o1.finderPattern === null ? o2.finderPattern === null : o1.finderPattern.equals(o2.finderPattern));
}

private static equalsOrNull(o1: any, o2: any): boolean {
return o1 === null ? o2 === null : ExpandedPair.equals(o1, o2);
hashCode(): number {
return ExpandedPair.hashNotNull(this.leftChar) ^ ExpandedPair.hashNotNull(this.rightChar) ^ ExpandedPair.hashNotNull(this.finderPattern);
}

hashCode(): any {
// return ExpandedPair.hashNotNull(leftChar) ^ hashNotNull(rightChar) ^ hashNotNull(finderPattern);
let value = this.leftchar.getValue() ^ this.rightchar.getValue() ^ this.finderpattern.getValue();
return value;
private static hashNotNull(o: { hashCode(): number }): number {
return o === null ? 0 : o.hashCode();
}
// To do - Re check the implementation
// private static hashNotNull(o: ExpandedPair): number {
// return o === null ? 0 : o.hashCode();
// }
}

47 changes: 13 additions & 34 deletions src/core/oned/rss/expanded/ExpandedRow.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import ExpandedPair from './ExpandedPair';


export default class ExpandedRow {
private readonly pairs: Array<ExpandedPair>;
private readonly rowNumber: number;
private readonly wasReversed: boolean;

constructor(pairs: Array<ExpandedPair>, rowNumber: number, wasReversed: boolean) {
this.pairs = pairs;
constructor(pairs: Array<ExpandedPair>, rowNumber: number) {
this.pairs = [...pairs];
this.rowNumber = rowNumber;
this.wasReversed = wasReversed;
}

getPairs(): Array<ExpandedPair> {
Expand All @@ -20,15 +17,9 @@ export default class ExpandedRow {
return this.rowNumber;
}

isReversed(): boolean {
return this.wasReversed;
}
// check implementation

isEquivalent(otherPairs: Array<ExpandedPair>): boolean {
return this.checkEqualitity(this, otherPairs);
return ExpandedRow.listEquals(this.getPairs(), otherPairs);
}
// @Override

public toString(): String {
return '{ ' + this.pairs + ' }';
Expand All @@ -38,31 +29,19 @@ export default class ExpandedRow {
* Two rows are equal if they contain the same pairs in the same order.
*/
// @Override
// check implementation
public equals(o1: ExpandedRow, o2: ExpandedRow): boolean {
if (!(o1 instanceof ExpandedRow)) {
public static equals(o1: ExpandedRow | null, o2: any): boolean {
if (o1 === null) return o2 === null;
if (!(o2 instanceof ExpandedRow)) {
return false;
}
return this.checkEqualitity(o1, o2) && o1.wasReversed === o2.wasReversed;
return ExpandedRow.listEquals(o1.pairs, o2.getPairs());
}
checkEqualitity(pair1: any, pair2: any): boolean {
if (!pair1 || !pair2) return;
let result;
pair1.forEach((e1, i) => {
pair2.forEach(e2 => {
if (e1.getLeftChar().getValue() === e2.getLeftChar().getValue() && e1.getRightChar().getValue() === e2.getRightChar().getValue() && e1.getFinderPatter().getValue() === e2.getFinderPatter().getValue()) {
result = true;
}
});

static listEquals(pairs1: Array<ExpandedPair>, pairs2: Array<ExpandedPair>): boolean {
if (pairs1.length !== pairs2.length) return false;
return pairs1.every((pair1, index) => {
const pair2 = pairs2[index];
return ExpandedPair.equals(pair1, pair2);
});
return result;
}

// @Override
// check implementation
// public int hashCode(): number {
// let hash = this.pairs.values ^ this.wasReversed
// //return pairs.hashCode() ^ Boolean.valueOf(wasReversed).hashCode();
// }

}
Loading