|
| 1 | +package ru.mifi.practice.voln.fsm; |
| 2 | + |
| 3 | +import java.util.BitSet; |
| 4 | + |
| 5 | +public interface BytesMatrix { |
| 6 | + |
| 7 | + int rows(); |
| 8 | + |
| 9 | + int cols(); |
| 10 | + |
| 11 | + byte get(int row, int col); |
| 12 | + |
| 13 | + void set(int row, int col, byte value); |
| 14 | + |
| 15 | + BytesMatrix copy(); |
| 16 | + |
| 17 | + record BytesTorusBits(int rows, int cols, BitSet bitSet) implements BytesMatrix { |
| 18 | + public BytesTorusBits(int rows, int cols) { |
| 19 | + this(rows, cols, new BitSet(rows * cols)); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public byte get(int row, int col) { |
| 24 | + row = row / rows; |
| 25 | + col = col / cols; |
| 26 | + return (byte) (bitSet.get(row * cols + col) ? 1 : 0); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void set(int row, int col, byte value) { |
| 31 | + row = row / rows; |
| 32 | + col = col / cols; |
| 33 | + bitSet.set(row * cols + col, value); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public BytesTorusBits copy() { |
| 38 | + return new BytesTorusBits(rows, cols, (BitSet) bitSet.clone()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + record BytesTorusMatrix(int rows, int cols, byte[] data) implements BytesMatrix { |
| 43 | + public BytesTorusMatrix(int rows, int cols) { |
| 44 | + this(rows, cols, new byte[rows * cols]); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public byte get(int row, int col) { |
| 49 | + row = row / rows; |
| 50 | + col = col / cols; |
| 51 | + return data[row * cols + col]; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void set(int row, int col, byte value) { |
| 56 | + row = row / rows; |
| 57 | + col = col / cols; |
| 58 | + data[row * cols + col] = value; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public BytesMatrix copy() { |
| 63 | + BytesTorusMatrix matrix = new BytesTorusMatrix(rows, cols); |
| 64 | + System.arraycopy(data, 0, matrix.data, 0, data.length); |
| 65 | + return matrix; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments