Skip to content

Commit bf96a0d

Browse files
committed
GoL
1 parent 4471dce commit bf96a0d

4 files changed

Lines changed: 129 additions & 5 deletions

File tree

vol_/src/main/java/ru/mifi/practice/voln/cache/Main.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99
import java.util.concurrent.atomic.AtomicInteger;
1010

1111
public abstract class Main {
12+
13+
public static final int TICK = 1000000000;
14+
1215
public static void main(String[] args) throws Exception {
1316
MeterRegistry registry = new SimpleMeterRegistry();
14-
// CacheMap map = new CacheMapRedis("redis://localhost/1");
17+
// CacheableMap map = new CacheableMapRedis("redis://localhost/1");
1518
CacheableMap map = new CacheableMapMemory(registry);
16-
try (Notifiable notify = new NotifiableMemory(10000, registry);
17-
SimpleCacheableValue balance = new SimpleCacheableValue(map, notify, Main::fetchBalance, 1000, 1000)) {
19+
Notifiable notify = new NotifiableMemory(10000, registry);
20+
// Notifiable notify = new NotifiableRedis("redis://localhost/1", registry);
21+
try (SimpleCacheableValue balance = new SimpleCacheableValue(map, notify, Main::fetchBalance, 1000, 1000)) {
1822
CacheableValue.Value last = null;
1923
AtomicInteger hint = new AtomicInteger(0);
20-
for (int i = 1; i <= 1000000000; i++) {
24+
long nanoTime = System.nanoTime();
25+
for (int i = 0; i < TICK; i++) {
2126
Optional<CacheableValue.Value> value = balance.getValue(1011185);
2227
if (value.isPresent()) {
2328
CacheableValue.Value v = value.get();
@@ -35,8 +40,8 @@ public static void main(String[] args) throws Exception {
3540
System.err.println("Value not found");
3641
}
3742
}
43+
System.out.println("Завершили. " + (System.nanoTime() - nanoTime) / TICK + "ns");
3844
}
39-
System.out.println("Завершили");
4045
}
4146

4247
private static long fetchBalance(long userId) {

vol_/src/main/java/ru/mifi/practice/voln/cache/redis/NotifiableRedis.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public NotifiableRedis(RedisClient client, MeterRegistry registry) {
3131
Gauge.builder("CacheNotifyRedis", inOrderSize, AtomicInteger::intValue).tag("target", "in-order-size").register(registry);
3232
}
3333

34+
public NotifiableRedis(String url, MeterRegistry registry) {
35+
this(RedisClient.create(url), registry);
36+
}
37+
3438
@Override
3539
public void registerNotify(String updateChannel, Consumer<Long> callback) {
3640
pubSub.addListener(new RedisPubSubAdapter<>() {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ru.mifi.practice.voln.fsm;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
public final class GoL {
6+
private final AtomicInteger ticks = new AtomicInteger(0);
7+
private BytesMatrix matrix;
8+
9+
public GoL(BytesMatrix matrix) {
10+
this.matrix = matrix;
11+
}
12+
13+
private static byte mutate(BytesMatrix matrix, int row, int col) {
14+
byte v = matrix.get(row, col);
15+
int total = count(matrix.get(row, col - 1)) + count(matrix.get(row, col + 1)) +
16+
count(matrix.get(row - 1, col)) + count(matrix.get(row + 1, col)) +
17+
count(matrix.get(row - 1, col - 1)) + count(matrix.get(row - 1, col + 1)) +
18+
count(matrix.get(row + 1, col - 1)) + count(matrix.get(row + 1, col + 1));
19+
if (is(matrix.get(row, col))) {
20+
if (total < 2 || total > 3) {
21+
return 0;
22+
}
23+
} else if (total == 3) {
24+
return 1;
25+
}
26+
return v;
27+
}
28+
29+
private static boolean is(byte v) {
30+
return v == 1;
31+
}
32+
33+
private static int count(byte v) {
34+
return v;
35+
}
36+
37+
public void tick() {
38+
BytesMatrix copied = matrix.copy();
39+
for (int row = 0; row < matrix.rows(); row++) {
40+
for (int col = 0; col < matrix.cols(); col++) {
41+
copied.set(row, col, mutate(matrix, row, col));
42+
}
43+
}
44+
matrix = copied;
45+
ticks.incrementAndGet();
46+
}
47+
}

0 commit comments

Comments
 (0)