Search in sources :

Example 6 with IntIntPair

use of suite.primitive.adt.pair.IntIntPair 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);
}
Also used : Arrays(java.util.Arrays) List(java.util.List) IntSink(suite.primitive.IntPrimitives.IntSink) IntIntPair(suite.primitive.adt.pair.IntIntPair) Ints_(suite.primitive.Ints_) PriorityQueue(suite.adt.PriorityQueue) IntSink(suite.primitive.IntPrimitives.IntSink) PriorityQueue(suite.adt.PriorityQueue) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 7 with IntIntPair

use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.

the class Grapher method graph_.

private int graph_(Map<IdentityKey<Node>, Integer> ids, Node node) {
    IdentityKey<Node> key = IdentityKey.of(node);
    Integer id = ids.get(key);
    if (id == null) {
        ids.put(key, id = gns.size());
        gns.add(null);
        NodeRead nr = NodeRead.of(node);
        List<IntIntPair> children = // 
        Read.from(// 
        nr.children).map(// 
        p -> IntIntPair.of(graph_(ids, p.t0), graph_(ids, p.t1))).toList();
        gns.set(id, new GN(nr.type, nr.terminal, nr.op, children));
    }
    return id;
}
Also used : DataInputStream(java.io.DataInputStream) Read(suite.streamlet.Read) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Reference(suite.node.Reference) Trail(suite.lp.Trail) IntIntPair(suite.primitive.adt.pair.IntIntPair) Set(java.util.Set) IOException(java.io.IOException) NodeRead(suite.node.io.Rewrite_.NodeRead) NodeHead(suite.node.io.Rewrite_.NodeHead) IntObjMap(suite.primitive.adt.map.IntObjMap) Tree(suite.node.Tree) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Entry(java.util.Map.Entry) As(suite.streamlet.As) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) NodeRead(suite.node.io.Rewrite_.NodeRead) Node(suite.node.Node) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 8 with IntIntPair

use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.

the class Grapher method bind.

public static boolean bind(Node n0, Node n1, Trail trail) {
    Map<IdentityKey<Node>, Integer> mapn0 = new HashMap<>();
    Map<IdentityKey<Node>, Integer> mapn1 = new HashMap<>();
    Grapher g0 = new Grapher();
    Grapher g1 = new Grapher();
    g0.id = g0.graph_(mapn0, n0);
    g1.id = g1.graph_(mapn1, n1);
    IntObjMap<IdentityKey<Node>> mapi0 = new IntObjMap<>();
    IntObjMap<IdentityKey<Node>> mapi1 = new IntObjMap<>();
    for (Entry<IdentityKey<Node>, Integer> e : mapn0.entrySet()) mapi0.put(e.getValue(), e.getKey());
    for (Entry<IdentityKey<Node>, Integer> e : mapn1.entrySet()) mapi1.put(e.getValue(), e.getKey());
    Set<IntIntPair> set = new HashSet<>();
    Deque<IntIntPair> deque = new ArrayDeque<>();
    deque.add(IntIntPair.of(g0.id, g1.id));
    IntIntPair pair;
    while ((pair = deque.pollLast()) != null) if (set.add(pair)) {
        GN gn0 = g0.gns.get(pair.t0);
        GN gn1 = g1.gns.get(pair.t1);
        if (// 
        gn0.type == ReadType.TERM && // 
        gn0.terminal instanceof Reference && Binder.bind(gn0.terminal, mapi1.get(pair.t1).key, trail))
            ;
        else if (// 
        gn1.type == ReadType.TERM && // 
        gn1.terminal instanceof Reference && Binder.bind(gn1.terminal, mapi0.get(pair.t0).key, trail))
            ;
        else if (gn0.type == gn1.type && Objects.equals(gn0.terminal, gn1.terminal) && gn0.op == gn1.op) {
            List<IntIntPair> children0 = gn0.children;
            List<IntIntPair> children1 = gn1.children;
            int size0 = children0.size();
            int size1 = children1.size();
            if (size0 == size1)
                for (int i = 0; i < size0; i++) {
                    IntIntPair p0 = children0.get(i);
                    IntIntPair p1 = children1.get(i);
                    deque.addLast(IntIntPair.of(p0.t0, p1.t0));
                    deque.addLast(IntIntPair.of(p0.t1, p1.t1));
                }
            else
                return false;
        } else
            return false;
    }
    return true;
}
Also used : IdentityKey(suite.adt.IdentityKey) HashMap(java.util.HashMap) Reference(suite.node.Reference) IntObjMap(suite.primitive.adt.map.IntObjMap) ArrayDeque(java.util.ArrayDeque) ArrayList(java.util.ArrayList) List(java.util.List) IntIntPair(suite.primitive.adt.pair.IntIntPair) HashSet(java.util.HashSet)

Example 9 with IntIntPair

use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.

the class HtmlUtil method parse.

public HtmlNode parse(String in) {
    List<IntIntPair> pairs = new ArrayList<>();
    int pos0, pos1 = 0;
    while (0 <= (pos0 = in.indexOf("<", pos1)) && 0 <= (pos1 = in.indexOf(">", pos0 + 1))) pairs.add(IntIntPair.of(pos0, ++pos1));
    Fun<String, IntObjPair<String>> getNameFun = tag -> {
        int p0 = 1, p1 = p0 + 1, px = tag.length() - 1;
        char first = tag.charAt(p1);
        char last = tag.charAt(px - 1);
        int d;
        if (first == '!')
            return IntObjPair.of(0, tag);
        else {
            if (first == '/') {
                p1++;
                d = -1;
            } else if (last == '/') {
                px--;
                d = 0;
            } else
                d = 1;
            int ps0 = tag.indexOf(' ');
            int ps1 = 0 <= ps0 ? ps0 : px;
            return IntObjPair.of(d, tag.substring(p1, ps1));
        }
    };
    Deque<HtmlNode> deque = new ArrayDeque<>(List.of(new HtmlNode(null)));
    int prevp = 0;
    for (IntIntPair pair : pairs) {
        HtmlNode htmlNode = deque.element(), htmlNode1;
        int p0 = pair.t0;
        int px = pair.t1;
        htmlNode.children.add(new HtmlNode(in.substring(prevp, p0)));
        String tag = in.substring(p0, px);
        IntObjPair<String> dn = getNameFun.apply(tag);
        int d = dn.t0;
        String name = dn.t1;
        if (d == -1)
            while (!deque.isEmpty() && !String_.equals(getNameFun.apply(deque.pop().tag).t1, name)) ;
        else {
            htmlNode.children.add(htmlNode1 = new HtmlNode(tag));
            if (d == 1)
                deque.push(htmlNode1);
        }
        prevp = px;
    }
    return deque.pop();
}
Also used : List(java.util.List) IntObjPair(suite.primitive.adt.pair.IntObjPair) BiMap(suite.adt.map.BiMap) IntIntPair(suite.primitive.adt.pair.IntIntPair) HashBiMap(suite.adt.map.HashBiMap) ArrayDeque(java.util.ArrayDeque) Deque(java.util.Deque) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) IntObjPair(suite.primitive.adt.pair.IntObjPair) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 10 with IntIntPair

use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.

the class IntIntMapTest method test.

@Test
public void test() {
    IntIntMap map = new IntIntMap();
    map.put(1, 2);
    map.put(3, 4);
    map.put(5, 6);
    assertEquals(2, map.get(1));
    assertEquals(4, map.get(3));
    assertEquals(6, map.get(5));
    Set<String> expected = new HashSet<>();
    expected.add("1:2");
    expected.add("3:4");
    expected.add("5:6");
    Set<String> actual = new HashSet<>();
    IntIntSource source = map.source();
    IntIntPair pair = IntIntPair.of(0, 0);
    while (source.source2(pair)) actual.add(pair.t0 + ":" + pair.t1);
    assertEquals(expected, actual);
}
Also used : IntIntMap(suite.primitive.adt.map.IntIntMap) IntIntSource(suite.primitive.IntIntSource) HashSet(java.util.HashSet) IntIntPair(suite.primitive.adt.pair.IntIntPair) Test(org.junit.Test)

Aggregations

IntIntPair (suite.primitive.adt.pair.IntIntPair)13 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ArrayDeque (java.util.ArrayDeque)4 HashSet (java.util.HashSet)4 Reference (suite.node.Reference)4 IntIntSource (suite.primitive.IntIntSource)4 Deque (java.util.Deque)3 HashMap (java.util.HashMap)3 IdentityKey (suite.adt.IdentityKey)3 Pair (suite.adt.pair.Pair)3 IntObjMap (suite.primitive.adt.map.IntObjMap)3 Read (suite.streamlet.Read)3 Fail (suite.util.Fail)3 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2