use of suite.adt.pair.Pair in project suite by stupidsing.
the class LazyIbTreeExtentFilePersister method save_.
private Extent save_(List<Slot<T>> slots) {
IdentityKey<List<Slot<T>>> key = IdentityKey.of(slots);
Extent extent = slotsByExtent.inverse().get(key);
if (extent == null) {
List<Pair<T, Extent>> pairs = //
Read.from(//
slots).map(//
slot -> Pair.of(slot.pivot, save_(slot.readSlots()))).toList();
slotsByExtent.put(extent = saveSlot(nPages, new PersistSlot<>(pairs)), key);
nPages = extent.end;
}
return extent;
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class LazyIbTreeExtentFilePersister method gc.
@Override
public Map<Extent, Extent> gc(List<Extent> roots, int back) {
synchronized (writeLock) {
int end = nPages;
int start = max(0, end - back);
Set<Extent> isInUse = new HashSet<>();
Sink<List<Extent>> use = extents_ -> {
for (Extent extent : extents_) if (start <= extent.start)
isInUse.add(extent);
};
use.sink(roots);
List<Extent> extents = extentFile.scan(start, end);
for (Extent extent : Read.from(extents).reverse()) if (isInUse.contains(extent))
use.sink(Read.from(loadSlot(extent).pairs).map(Pair::second).toList());
Map<Extent, Extent> map = new HashMap<>();
if (!extents.isEmpty()) {
int pointer = extents.get(0).start;
for (Extent extent0 : extents) if (isInUse.contains(extent0)) {
PersistSlot<T> ps0 = loadSlot(extent0);
List<Pair<T, Extent>> pairs0 = ps0.pairs;
List<Pair<T, Extent>> pairsx = Read.from(pairs0).map(Pair.map1(p -> map.getOrDefault(p, p))).toList();
PersistSlot<T> psx = new PersistSlot<>(pairsx);
Extent extentx = saveSlot(pointer, psx);
pointer = extentx.end;
map.put(extent0, extentx);
}
nPages = pointer;
slotsByExtent.clear();
}
return map;
}
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class SewingGeneralizerImpl method generalizer.
@Override
public Generalize_ generalizer(Node node) {
List<Generalize_> funs = new ArrayList<>();
Generalize_ fun;
while (true) {
Node node0 = node;
Tree tree;
if (node0 instanceof Atom) {
Atom atom = (Atom) node0;
String name = atom.name;
if (ProverConstant.isCut(node0) || ProverConstant.isVariable(name)) {
int index = vm.computeIndex(atom);
fun = env -> env.get(index);
} else if (ProverConstant.isWildcard(name))
fun = env -> new Reference();
else
fun = env -> node0;
} else if (node0 instanceof Dict) {
Generalize_[][] array = //
Read.from2(//
((Dict) node0).map).map(//
(key, value) -> new Generalize_[] { generalizer(key), generalizer(value) }).toArray(Generalize_[].class);
int length = array.length;
fun = env -> {
@SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
return Dict.of(pairs);
};
} else if ((tree = Tree.decompose(node0)) != null) {
Operator operator = tree.getOperator();
if (operator != TermOp.OR____) {
Generalize_ f = generalizer(tree.getLeft());
funs.add(env -> Tree.of(operator, f.apply(env), null));
node = tree.getRight();
continue;
} else {
// delay generalizing for performance
Generalize_ lf = generalizer(tree.getLeft());
Generalize_ rf = generalizer(tree.getRight());
fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
}
} else if (node0 instanceof Tuple) {
Generalize_[] fs = Read.from(((Tuple) node0).nodes).map(this::generalizer).toArray(Generalize_.class);
int length = fs.length;
fun = env -> {
Node[] array = new Node[length];
for (int i = 0; i < length; i++) array[i] = fs[i].apply(env);
return Tuple.of(array);
};
} else
fun = env -> node0;
funs.add(fun);
break;
}
if (1 < funs.size())
return env -> {
Tree t = Tree.of(null, null, null);
Node node_ = t;
for (Generalize_ fun_ : funs) {
Tree t_ = Tree.decompose(node_);
Tree.forceSetRight(t_, fun_.apply(env));
node_ = t_.getRight();
}
return t.getRight();
};
else
return funs.get(0);
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class SewingProverImpl method compileAll.
private void compileAll() {
isHasCutByPrototype = rules.listEntries().mapValue(this::isHasCut).toMap();
for (Pair<Prototype, List<Rule>> e : rules.listEntries()) {
Prototype prototype = e.t0;
List<Rule> rules = new ArrayList<>(e.t1);
TraceLevel traceLevel = traceLevel(prototype);
// second-level indexing optimization
Map<Prototype, List<Rule>> rulesByProto1;
if (6 <= rules.size()) {
Map<Prototype, List<Rule>> rulesByProto_ = Read.from(rules).toListMap(rule -> Prototype.of(rule, 1));
rulesByProto1 = !rulesByProto_.containsKey(null) ? rulesByProto_ : null;
} else
rulesByProto1 = null;
if (isHasCutByPrototype.get(prototype)) {
Trampoline tr0 = compileTrRules(prototype, rules, traceLevel);
Trampoline tr;
if (rulesByProto1 != null) {
Map<Prototype, Trampoline> trByProto1 = //
Read.from2(//
rulesByProto1).mapValue(//
rules_ -> compileTrRules(prototype, rules_, traceLevel)).toMap();
tr = rt -> {
Prototype proto = Prototype.of(rt.query, 1);
if (proto != null) {
Trampoline tr_ = trByProto1.get(proto);
return tr_ != null ? tr_ : fail;
} else
return tr0;
};
} else
tr = tr0;
getTrampolineByPrototype(prototype).set(tr);
} else {
Cps cps0 = compileCpsRules(prototype, rules, traceLevel);
Cps cps;
if (rulesByProto1 != null) {
Map<Prototype, Cps> cpsByProto1 = //
Read.from2(//
rulesByProto1).mapValue(//
rules_ -> compileCpsRules(prototype, rules_, traceLevel)).toMap();
cps = rt -> {
Prototype proto = Prototype.of(rt.query, 1);
return proto != null ? cpsByProto1.get(proto) : cps0;
};
} else
cps = cps0;
getCpsByPrototype(prototype).set(cps);
}
}
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class DivisiblePolynomial method format.
public Node format(Poly<N> poly) {
Express ex = new Express();
OpGroup add = ex.add;
OpGroup mul = ex.mul;
Int_Obj<Node> powerFun = p -> {
Node power = mul.identity();
for (int i = 0; i < p; i++) power = mul.apply(x, power);
return power;
};
Node sum = format_.apply(n0);
for (IntObjPair<N> pair : poly.streamlet().sortByKey(Integer::compare)) {
int p = pair.t0;
Node power = p < 0 ? mul.inverse(powerFun.apply(-p)) : powerFun.apply(p);
sum = add.apply(mul.apply(format_.apply(pair.t1), power), sum);
}
return sum;
}
Aggregations