import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.time.Duration;
import java.lang.invoke.VarHandle;
import java.lang.invoke.MethodHandles;
import java.awt.geom.*;
import static x30_pkg.x30_util.DynamicObject;
import java.text.*;
import java.text.NumberFormat;
import java.util.TimeZone;
import java.nio.file.Path;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
class main {
static class PStack implements Steppable {
PStack() {}
final public PStack setScheduler(ProbabilisticScheduler scheduler){ return scheduler(scheduler); }
public PStack scheduler(ProbabilisticScheduler scheduler) { this.scheduler = scheduler; return this; } final public ProbabilisticScheduler getScheduler(){ return scheduler(); }
public ProbabilisticScheduler scheduler() { return scheduler; }
ProbabilisticScheduler scheduler = new ProbabilisticScheduler();
static class NoOptionsException extends RuntimeException {}
PStack(VStack.Computable computable) { add(computable); }
static class ExecuteOption implements VStack.Computable , IFieldsToList{
IVF1 option;
ExecuteOption() {}
ExecuteOption(IVF1 option) {
this.option = option;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + option + ")"; }public Object[] _fieldsToList() { return new Object[] {option}; }
public void step(VStack stack, Object subComputationResult) {
Object target = stack.caller();
stack._return();
option.get(target);
}
}
class SingleStack extends VStack implements IPStack, Runnable {
double probability = 1.0;
SingleStack() {}
SingleStack(VStack.Computable computable) { super(computable); }
public void run() { try {
print("Stepping " + last(stack));
if (step() && !isEmpty()) {
print("Re-scheduling at " + probability + ": " + last(stack));
// re-add myself to scheduler to continue
scheduler.add(probability, this);
}
} catch (Exception __e) { throw rethrow(__e); } }
// give each option the probability 1/n (with n=number of options)
public void options(B function, Iterable> options) {
List> optionsList = nonNulls(options);
if (empty(optionsList))
throw new NoOptionsException();
// probability penalty according to number of options
probability = probability/l(optionsList);
// Schedule all options except first in cloned stacks
int n = l(optionsList)-1;
for (int i = 0; i < n; i++) {
SingleStack s = new SingleStack();
s.stack = shallowCloneElements(stack);
s.push(new ExecuteOption(optionsList.get(i)));
s.probability = probability;
scheduler.at(probability, s);
}
// Last option is executed in same stack
push(new ExecuteOption(last(optionsList)));
}
// give each option its own probability
public void probabilisticOptions(B currentFrame, Iterable>> options) {
List>> optionsList = nonNulls(options);
if (empty(optionsList))
throw new NoOptionsException();
// Schedule all options except first in cloned stacks
int n = l(optionsList)-1;
for (int i = 0; i < n; i++) {
SingleStack s = new SingleStack();
s.stack = shallowCloneElements(stack);
var option = optionsList.get(i);
s.push(new ExecuteOption(option.get()));
s.probability = probability*option.probability();
scheduler.at(s.probability, s);
}
// Last option is executed in same stack
var option = last(optionsList);
probability *= option.probability();
push(new ExecuteOption(option.get()));
}
}
static class FollowUp extends PStackComputableWithStep implements IFieldsToList{
VStack.Computable computable;
IVF1 onCompletion;
FollowUp() {}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + computable + ", " + onCompletion + ")"; }public Object[] _fieldsToList() { return new Object[] {computable, onCompletion}; }
// !customConstructor
FollowUp(VStack.Computable computable, IVF1 onCompletion) {
this.onCompletion = onCompletion;
this.computable = computable;
probability = getProbability(computable);
}
void step(IPStack stack) {
if (step == 0) {
stack.push(computable);
++step;
} else {
onCompletion.get((A) stack.subResult());
stack._return();
}
}
}
// also takes a PStackComputable
void add(VStack.Computable computable) {
scheduler.at(getProbability(computable), new SingleStack(computable));
}
// also takes a PStackComputable
void add(VStack.Computable computable, IVF1 onCompletion) {
if (onCompletion != null)
add(new FollowUp<>(computable, onCompletion));
else
add(computable);
}
public boolean step() { return scheduler.step(); }
public void run() { scheduler.run(); }
void run(VStack.Computable computable) { add(computable); run(); }
public void runWithStats() { scheduler.runWithStats(); }
void runWithStats(VStack.Computable computable) { add(computable); runWithStats(); }
static double getProbability(VStack.Computable computable) {
return computable instanceof PStackComputable ?
((PStackComputable) computable).probability : 1.0;
}
}
static void add(BitSet bs, int i) {
bs.set(i);
}
static boolean add(Collection c, A a) {
return c != null && c.add(a);
}
static void add(Container c, Component x) {
addToContainer(c, x);
}
static long add(AtomicLong l, long b) {
return l.addAndGet(b);
}
static String shortClassName_dropNumberPrefix(Object o) {
return dropNumberPrefix(shortClassName(o));
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal