use of suite.util.Streamlet_.forInt in project suite by stupidsing.
the class SparseMatrix method transpose.
public SparseMatrix transpose() {
var pq = new PriorityQueue<>(IntIntPair.class, height, Comparator.comparingInt(IntIntPair::snd));
var matrix1 = forInt(width_).map(i -> new Spans()).toList();
var js = new int[height];
IntSink enqRow = r -> {
var j = js[r];
if (j < matrix.get(r).size)
pq.add(IntIntPair.of(r, j));
};
for (var r = 0; r < height; r++) enqRow.f(r);
while (!pq.isEmpty()) {
var pair = pq.extractMin();
var r = pair.t0;
var j = js[r]++;
var spans = matrix.get(r);
matrix1.get(spans.columns[j]).add(r, spans.values[j]);
enqRow.f(r);
}
return new SparseMatrix(width_, height, matrix1);
}
use of suite.util.Streamlet_.forInt in project suite by stupidsing.
the class UctSearch method search.
public Move search() {
for (var move : visitor.getAllMoves()) {
nRaveWins.put(move, new AtomicInteger());
nRaveVisits.put(move, new AtomicInteger());
}
root = new UctNode<>();
var count = new AtomicInteger();
var end = System.currentTimeMillis() + boundedTime;
forInt(numberOfThreads).collect(As.executeThreadsByInt(i -> {
var j = 0;
while (count.getAndIncrement() < numberOfSimulations) {
playSimulation(visitor.cloneVisitor(), root, 0);
if (100 < ++j) {
j = 0;
if (end < System.currentTimeMillis())
break;
}
}
}));
// finds best node
best = root.bestChild;
return best != null ? best.move : null;
}
use of suite.util.Streamlet_.forInt in project suite by stupidsing.
the class Render method renderPixels.
public Image renderPixels(int width, int height, IntInt_Obj<R3> f) {
var nThreads = Defaults.nThreads;
var txs = NewInt.array(nThreads + 1, i -> width * i / nThreads);
var pixels = new R3[width][height];
forInt(nThreads).collect(As.executeThreadsByInt(t -> {
for (var x = txs[t]; x < txs[t + 1]; x++) for (var y = 0; y < height; y++) pixels[x][y] = f.apply(x, y);
}));
var image = new Image(width, height, BufferedImage.TYPE_INT_RGB);
for (var x = 0; x < width; x++) for (var y = 0; y < height; y++) {
var pixel = limit(pixels[x][y]);
image.setRGB(x, y, new Color(pixel.x, pixel.y, pixel.z).getRGB());
}
return image;
}
use of suite.util.Streamlet_.forInt in project suite by stupidsing.
the class Trade_ method collectBrokeredTrades.
public static Streamlet<Trade> collectBrokeredTrades(Puller<Trade> puller) {
var trades0 = puller.toArray(Trade.class);
var trades1 = new ArrayList<Trade>();
var length0 = trades0.length;
var i0 = 0;
IntIntSink tx = (i0_, i1_) -> {
if (forInt(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
while (i0_ < i1_) {
var trade0 = trades0[i0_++];
if (!Equals.string(trade0.remark, "#"))
trades1.add(trade0);
}
};
for (var i = 1; i < length0; i++) {
var trade0 = trades0[i0];
var trade1 = trades0[i];
var isGroup = //
true && //
Equals.string(trade0.date, trade1.date) && //
Equals.string(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
if (!isGroup) {
tx.sink2(i0, i);
i0 = i;
}
}
tx.sink2(i0, length0);
return Read.from(trades1);
}
use of suite.util.Streamlet_.forInt in project suite by stupidsing.
the class NioClusterMapTest method testClusterMap.
@Test
public void testClusterMap() throws IOException {
var nNodes = 3;
var peers = forInt(nNodes).map2(i -> "NODE" + i, i -> new InetSocketAddress(localHost, 3000 + i)).toMap();
var clusters = //
Read.from2(//
peers).keys().map2(name -> name, //
name -> ex(() -> new NioCluster(name, peers))).toMap();
for (var cluster : clusters.values()) cluster.start();
var peerNames = new ArrayList<>(peers.keySet());
var clMap = //
Read.from2(//
peers).keys().map2(name -> name, //
name -> new NioClusterMap<Integer, String>(clusters.get(name))).toMap();
Sleep.quietly(5 * 1000);
System.out.println("=== CLUSTER FORMED (" + LocalDateTime.now() + ") ===\n");
Source<NioClusterMap<Integer, String>> peerf = () -> clMap.get(peerNames.get(random.nextInt(nNodes)));
Int_Obj<Sink<Runnable>> setf = i -> cont -> peerf.g().set(i, Integer.toString(i), v0 -> cont.run(), fail);
Int_Obj<Sink<Runnable>> getf = i -> cont -> peerf.g().get(i, v -> {
assertEquals(Integer.toString(i), v);
cont.run();
}, fail);
Fun<NioCluster, Sink<Runnable>> closef = cluster -> cont -> {
try {
cluster.stop();
System.out.println("=== CLUSTER STOPPED (" + LocalDateTime.now() + ") ===\n");
} catch (IOException ex) {
fail(ex);
}
cont.run();
};
var sinks = //
Streamlet.concat(//
forInt(9).map(setf), //
forInt(9).map(getf), Read.from2(clusters).values().map(closef)).toList();
new Object() {
public void run(int i) {
if (i < sinks.size())
sinks.get(i).f(() -> run(i + 1));
}
}.run(0);
Read.from2(clusters).values().map(cluster -> New.thread(cluster::run)).collect(Start::thenJoin);
for (var cluster : clusters.values()) cluster.close();
}
Aggregations