diff --git a/java-fast/io/nayuki/fastqrcodegen/Memoizer.java b/java-fast/io/nayuki/fastqrcodegen/Memoizer.java index 1df5bc4..4cf9cc9 100644 --- a/java-fast/io/nayuki/fastqrcodegen/Memoizer.java +++ b/java-fast/io/nayuki/fastqrcodegen/Memoizer.java @@ -34,17 +34,25 @@ import java.util.function.Function; // A thread-safe cache based on soft references. final class Memoizer { + /*---- Fields ----*/ + private final Function function; private Map> cache = new ConcurrentHashMap<>(); private Set pending = new HashSet<>(); + + /*---- Constructor ----*/ + // Creates a memoizer based on the given function that takes one input to compute an output. public Memoizer(Function func) { function = func; } + + /*---- Method ----*/ + // Computes function.apply(arg) or returns a cached copy of a previous call. public R get(T arg) { // Non-blocking fast path diff --git a/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java b/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java index 73d59d6..0edc803 100644 --- a/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java +++ b/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java @@ -28,11 +28,16 @@ package io.nayuki.fastqrcodegen; // and does not depend on the data or error correction level or mask. final class QrTemplate { + /*---- Constant ----*/ + // Use this memoizer to get instances of this class. public static final Memoizer MEMOIZER = new Memoizer<>(QrTemplate::new); + + /*---- Fields ----*/ + private final int version; // In the range [1, 40]. private final int size; // Derived from version. @@ -45,6 +50,9 @@ final class QrTemplate { private int[] isFunction; + + /*---- Constructor ----*/ + // Creates a QR Code template for the given version number. private QrTemplate(int ver) { if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION) @@ -61,6 +69,9 @@ final class QrTemplate { } + + /*---- Methods ----*/ + // Reads this object's version field, and draws and marks all function modules. private void drawFunctionPatterns() { // Draw horizontal and vertical timing patterns diff --git a/java-fast/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java b/java-fast/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java index a4b77be..520d6c0 100644 --- a/java-fast/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java +++ b/java-fast/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java @@ -30,16 +30,24 @@ import java.util.Objects; // Computes Reed-Solomon error correction codewords for given data codewords. final class ReedSolomonGenerator { + /*---- Constant ----*/ + // Use this memoizer to get instances of this class. public static final Memoizer MEMOIZER = new Memoizer<>(ReedSolomonGenerator::new); + + /*---- Field ----*/ + // A table of size 256 * degree, where polynomialMultiply[i][j] = multiply(i, coefficients[j]). // 'coefficients' is the temporary array computed in the constructor. private byte[][] polynomialMultiply; + + /*---- Constructor ----*/ + // Creates a Reed-Solomon ECC generator polynomial for the given degree. private ReedSolomonGenerator(int degree) { if (degree < 1 || degree > 255) @@ -72,6 +80,9 @@ final class ReedSolomonGenerator { } + + /*---- Method ----*/ + // Returns the error correction codeword for the given data polynomial and this divisor polynomial. public void getRemainder(byte[] data, int dataOff, int dataLen, byte[] result) { Objects.requireNonNull(data); @@ -89,6 +100,9 @@ final class ReedSolomonGenerator { } + + /*---- Utility function ----*/ + // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8. private static int multiply(int x, int y) {