Search in sources :

Example 6 with Pair

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;
}
Also used : SerializedFileFactory(suite.file.impl.SerializedFileFactory) PageFile(suite.file.PageFile) Read(suite.streamlet.Read) DataInput_(suite.util.DataInput_) DataOutput_(suite.util.DataOutput_) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ExtentFile(suite.file.ExtentFile) Rethrow(suite.util.Rethrow) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) SerializedPageFile(suite.file.SerializedPageFile) FileFactory(suite.file.impl.FileFactory) Serializer(suite.util.Serialize.Serializer) Slot(suite.immutable.LazyIbTree.Slot) Set(java.util.Set) Bytes(suite.primitive.Bytes) IOException(java.io.IOException) HashBiMap(suite.adt.map.HashBiMap) To(suite.util.To) Serialize(suite.util.Serialize) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) BiMap(suite.adt.map.BiMap) Extent(suite.file.ExtentAllocator.Extent) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Comparator(java.util.Comparator) Extent(suite.file.ExtentAllocator.Extent) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 7 with Pair

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;
    }
}
Also used : SerializedFileFactory(suite.file.impl.SerializedFileFactory) PageFile(suite.file.PageFile) Read(suite.streamlet.Read) DataInput_(suite.util.DataInput_) DataOutput_(suite.util.DataOutput_) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ExtentFile(suite.file.ExtentFile) Rethrow(suite.util.Rethrow) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) SerializedPageFile(suite.file.SerializedPageFile) FileFactory(suite.file.impl.FileFactory) Serializer(suite.util.Serialize.Serializer) Slot(suite.immutable.LazyIbTree.Slot) Set(java.util.Set) Bytes(suite.primitive.Bytes) IOException(java.io.IOException) HashBiMap(suite.adt.map.HashBiMap) To(suite.util.To) Serialize(suite.util.Serialize) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) BiMap(suite.adt.map.BiMap) Extent(suite.file.ExtentAllocator.Extent) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Comparator(java.util.Comparator) Extent(suite.file.ExtentAllocator.Extent) HashMap(java.util.HashMap) List(java.util.List) HashSet(java.util.HashSet) Pair(suite.adt.pair.Pair)

Example 8 with Pair

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);
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) GeneralizerFactory(suite.lp.doer.GeneralizerFactory) TermOp(suite.node.io.TermOp) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Suspend(suite.node.Suspend) Tuple(suite.node.Tuple) Operator(suite.node.io.Operator) Dict(suite.node.Dict) Operator(suite.node.io.Operator) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Suspend(suite.node.Suspend) Atom(suite.node.Atom) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 9 with Pair

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);
        }
    }
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Prototype(suite.lp.kb.Prototype) ArrayList(java.util.ArrayList) List(java.util.List) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Rule(suite.lp.kb.Rule)

Example 10 with Pair

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;
}
Also used : Poly(suite.math.sym.Polynomial.Poly) Ring(suite.math.sym.Sym.Ring) Predicate(java.util.function.Predicate) Pattern(suite.BindArrayUtil.Pattern) OpGroup(suite.math.sym.Express.OpGroup) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) Opt(suite.adt.Opt) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) Int_Obj(suite.primitive.IntPrimitives.Int_Obj) Fun2(suite.util.FunUtil2.Fun2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) IntObjPair(suite.primitive.adt.pair.IntObjPair) Fixie3(suite.adt.pair.Fixie_.Fixie3) Field(suite.math.sym.Sym.Field) SwitchNode(suite.node.io.SwitchNode) Int(suite.node.Int) OpGroup(suite.math.sym.Express.OpGroup) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode)

Aggregations

Pair (suite.adt.pair.Pair)36 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Read (suite.streamlet.Read)22 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Node (suite.node.Node)14 Fail (suite.util.Fail)13 Fun (suite.util.FunUtil.Fun)12 Reference (suite.node.Reference)11 Tree (suite.node.Tree)10 TermOp (suite.node.io.TermOp)10 Set (java.util.Set)9 Atom (suite.node.Atom)9 Int (suite.node.Int)9 As (suite.streamlet.As)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Streamlet (suite.streamlet.Streamlet)7 Time (suite.trade.Time)6