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);
}
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;
}
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;
}
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();
}
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);
}
Aggregations