use of suite.primitive.Ints_ in project suite by stupidsing.
the class DevMain method run.
private void run() {
String input = FileUtil.read("src/main/java/suite/dev/DevMain.java");
Text inputText = text(IRope.ropeList(input));
try (Termios termios = new Termios(libc)) {
termios.clear();
Keyboard keyboard = new Keyboard(libc);
Sink<State> redraw = state -> state.apply((st, undo, redo, text, oc, cc) -> cc.apply((cx, cy) -> oc.apply((ox, oy) -> {
String[] lines = //
Ints_.range(//
viewSizeY).map(//
screenY -> text.get(ox, oy + screenY, viewSizeX).replace('\t', ' ')).toArray(String.class);
termios.cursor(false);
for (int screenY = 0; screenY < viewSizeY; screenY++) {
termios.gotoxy(0, screenY);
termios.puts(lines[screenY]);
}
termios.gotoxy(cx - ox, cy - oy);
termios.cursor(true);
return null;
})));
State state0 = new State(null, null, inputText, c(0, 0), c(0, 0));
redraw.sink(state0);
FixieFun3<VK, Character, State, State> mutate = (vk, ch, state) -> //
state.apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
int ci = text.index(cx, cy);
if (vk == VK.LEFT_)
return st.cursor(ci - 1);
else if (vk == VK.RIGHT)
return st.cursor(ci + 1);
else if (vk == VK.UP___)
return st.cursor(cx, cy - 1);
else if (vk == VK.DOWN_)
return st.cursor(cx, cy + 1);
else if (vk == VK.PGUP_)
return st.cursor(cx, cy - viewSizeY);
else if (vk == VK.PGDN_)
return st.cursor(cx, cy + viewSizeY);
else if (vk == VK.HOME_)
return st.cursor(text.startOfLine(ci));
else if (vk == VK.END__)
return st.cursor(text.endOfLine(ci));
else if (vk == VK.CTRL_HOME_)
return st.cursor(0);
else if (vk == VK.CTRL_END__)
return st.cursor(text.length());
else if (vk == VK.CTRL_LEFT_)
return st.cursor(text.scanNext(ci, -1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
else if (vk == VK.CTRL_RIGHT)
return st.cursor(text.scanNext(ci, 1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
else if (vk == VK.CTRL_UP___)
return st.offset(ox, oy - 1).cursor(cx, cy - 1);
else if (vk == VK.CTRL_DOWN_)
return st.offset(ox, oy + 1).cursor(cx, cy + 1);
else if (vk == VK.F7___)
return st.offset(ox, max(cy - viewSizeY + 1, 0));
else if (vk == VK.F8___)
return st.offset(ox, min(cy, text.nLines()));
else if (vk == VK.ALT_J____) {
int index = text.endOfLine(ci);
Text text1 = text.splice(index, index + 1, empty);
return st.text(text1).cursor(index);
} else if (vk == VK.BKSP_) {
int index = ci;
return 0 < index ? st.splice(index - 1, index, empty) : st;
} else if (vk == VK.ALT_UP___) {
int i1 = text.startOfLine(ci);
if (0 < i1) {
int i0 = text.prevLine(i1);
int i2 = text.nextLine(i1);
return st.splice(i2, i2, text.text.subList(i0, i1)).splice(i0, i1, empty);
} else
return st;
} else if (vk == VK.ALT_DOWN_) {
int i0 = text.startOfLine(ci);
int i1 = text.nextLine(i0);
if (i1 < text.length()) {
int i2 = text.nextLine(i1);
return st.splice(i1, i2, empty).splice(i0, i0, text.text.subList(i1, i2));
} else
return st;
} else if (vk == VK.DEL__)
return st.splice(1, empty);
else if (vk == VK.CTRL_K____)
return st.splice(ci, text.endOfLine(ci), empty);
else if (vk == VK.CTRL_U____)
return st.splice(text.startOfLine(ci), ci, empty);
else if (vk == VK.CTRL_D____)
return st.splice(text.startOfLine(ci), text.nextLine(ci), empty);
else if (vk == VK.CTRL_Y____)
return redo != null ? redo : st;
else if (vk == VK.CTRL_Z____) {
State undo1 = undo != null ? undo : st;
return new State(undo1.undo, st, undo1.text, oc, undo1.cursorCoord);
} else if (vk == VK.CTRL_C____)
return Fail.t();
else if (ch != null)
if (ch == 13) {
int i0 = text.startOfLine(ci);
int ix = i0;
char ch_;
while ((ch_ = text.at(ix)) == ' ' || ch_ == '\t') ix++;
return st.splice(0, IRope.ropeList("\n").concat(text.text.subList(i0, ix)));
} else
return st.splice(0, IRope.ropeList(Character.toString(ch)));
else
return st;
}))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
IntIntPair cc_ = text.coord(sat(text.index(cx, cy), 0, text.length()));
return st.cursor(cc_.t0, cc_.t1);
}))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
int x0 = Math.max(0, cx - viewSizeX + 1);
int y0 = Math.max(0, cy - viewSizeY + 1);
int ox_ = sat(ox, x0, cx);
int oy_ = sat(oy, y0, cy);
return st.offset(ox_, oy_);
})));
keyboard.loop(signal -> //
signal.fold(state0, //
(state, pair_) -> pair_.map((vk, ch) -> mutate.apply(vk, ch, state))).wire(redraw));
}
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class LibraryMain method run.
protected boolean run(String[] args) {
Pair<Streamlet2<Path, Long>, Streamlet2<Path, Long>> partition = //
FileUtil.findPaths(Paths.get(inputDir)).filter(//
path -> fileExtensions.contains(FileUtil.getFileExtension(path))).map2(//
path -> Rethrow.ex(() -> Files.size(path))).partition((path, size) -> 0 < size);
// remove empty files
partition.t1.sink((path, size) -> {
try {
Files.delete(path);
} catch (IOException ex) {
Fail.t(ex);
}
});
Streamlet2<Path, FileInfo> path_fileInfos = //
partition.t0.map2((path, size) -> {
BasicFileAttributes attrs = Rethrow.ex(() -> Files.readAttributes(path, BasicFileAttributes.class));
// get all file information
List<String> tags = //
Ints_.range(//
path.getNameCount()).map(//
i -> path.getName(i).toString()).cons(//
To.string(attrs.lastModifiedTime().toInstant())).toList();
FileInfo fileInfo = new FileInfo();
fileInfo.md5 = Rethrow.ex(() -> Md5Crypt.md5Crypt(Files.readAllBytes(path)));
fileInfo.tags = tags;
return fileInfo;
});
// construct file listing
try (OutputStream os = FileUtil.out(inputDir + ".listing");
PrintWriter pw = new PrintWriter(os)) {
for (Pair<Path, FileInfo> path_fileInfo : path_fileInfos) pw.println(path_fileInfo.t0 + path_fileInfo.t1.md5);
} catch (IOException ex) {
Fail.t(ex);
}
//
path_fileInfos.map2((path, fileInfo) -> {
// move file to library, by md5
Path path1 = Paths.get(libraryDir, fileInfo.md5.substring(0, 2), fileInfo.md5);
FileUtil.mkdir(path1.getParent());
Rethrow.ex(() -> Files.move(path, path1, StandardCopyOption.REPLACE_EXISTING));
return fileInfo;
}).concatMap((path, fileInfo) -> Read.from(fileInfo.tags).map(tag -> {
// add to tag indices
Path path1 = Paths.get(tagsDir, tag, fileInfo.md5);
return Rethrow.ex(() -> {
Files.newOutputStream(path1).close();
return Pair.of(tag, fileInfo);
});
}));
return true;
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class Sha2 method sha256.
public byte[] sha256(byte[] bs0) {
int h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a;
int h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
int L0 = bs0.length * 8;
int L1 = L0 + 1;
int L2 = L1 + 64;
int L3 = L2 + 511 & 0xFFFFFE00;
int K = L3 - L2;
int[] is = Ints_.toArray(bs0.length / 4, i -> {
int i4 = i * 4;
return bs0[i4] + (bs0[i4 + 1] << 8) + (bs0[i4 + 2] << 16) + (bs0[i4 + 3] << 24);
});
IntIntSink set = (pos, b) -> is[pos / 32] |= b << pos % 32;
set.sink2(L1, 1);
for (int i = 0; i < 64; i++) set.sink2(L1 + K, L0 >> (63 - i) & 1);
for (int pos = 0; pos < is.length; ) {
int i0 = pos;
int[] w = Arrays.copyOfRange(is, i0, pos += 512 / 32);
for (int i = 16; i < 64; i++) {
int wi2 = w[i - 2];
int wi15 = w[i - 15];
int s0 = Integer.rotateRight(wi15, 7) ^ Integer.rotateRight(wi15, 18) ^ wi15 >> 3;
int s1 = Integer.rotateRight(wi2, 17) ^ Integer.rotateRight(wi2, 19) ^ wi2 >> 10;
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
int a = h0, b = h1, c = h2, d = h3;
int e = h4, f = h5, g = h6, h = h7;
for (int i = 0; i < 64; i++) {
int s1 = Integer.rotateRight(e, 6) ^ Integer.rotateRight(e, 11) ^ Integer.rotateRight(e, 25);
int ch = e & f ^ ~e & g;
int temp1 = h + s1 + ch + k[i] + w[i];
int s0 = Integer.rotateRight(a, 2) ^ Integer.rotateRight(a, 13) ^ Integer.rotateRight(a, 22);
int maj = a & b ^ a & c ^ b & c;
int temp2 = s0 + maj;
h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
h0 += a;
h1 += b;
h2 += c;
h3 += d;
h4 += e;
h5 += f;
h6 += g;
h7 += h;
}
byte[] bs1 = new byte[256];
ByteBuffer bb = ByteBuffer.wrap(bs1);
bb.putInt(h0);
bb.putInt(h1);
bb.putInt(h2);
bb.putInt(h3);
bb.putInt(h4);
bb.putInt(h5);
bb.putInt(h6);
bb.putInt(h7);
return bs1;
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class SparseMatrix method transpose.
public SparseMatrix transpose() {
PriorityQueue<IntIntPair> pq = new PriorityQueue<>(IntIntPair.class, height, (p0, p1) -> Integer.compare(p0.t1, p1.t1));
List<Spans> matrix1 = Ints_.range(width_).map(i -> new Spans()).toList();
int[] js = new int[height];
IntSink enqRow = r -> {
int j = js[r];
if (j < matrix.get(r).size)
pq.insert(IntIntPair.of(r, j));
};
for (int r = 0; r < height; r++) enqRow.sink(r);
while (!pq.isEmpty()) {
IntIntPair pair = pq.extractMin();
int r = pair.t0;
int j = js[r]++;
Spans spans = matrix.get(r);
matrix1.get(spans.columns[j]).add(r, spans.values[j]);
enqRow.sink(r);
}
return new SparseMatrix(width_, height, matrix1);
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class UctSearch method search.
public Move search() {
for (Move move : visitor.getAllMoves()) {
nRaveWins.put(move, new AtomicInteger());
nRaveVisits.put(move, new AtomicInteger());
}
root = new UctNode<>();
AtomicInteger count = new AtomicInteger();
long end = System.currentTimeMillis() + boundedTime;
List<Thread> threads = //
Ints_.range(//
numberOfThreads).map(i -> Thread_.newThread(() -> {
int j = 0;
while (count.getAndIncrement() < numberOfSimulations) {
playSimulation(visitor.cloneVisitor(), root, 0);
if (100 < ++j) {
j = 0;
if (end < System.currentTimeMillis())
break;
}
}
})).toList();
Thread_.startJoin(threads);
// finds best node
best = root.bestChild;
return best != null ? best.move : null;
}
Aggregations