Search in sources :

Example 1 with PriorityQueue

use of suite.adt.PriorityQueue 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)

Aggregations

Arrays (java.util.Arrays)1 List (java.util.List)1 PriorityQueue (suite.adt.PriorityQueue)1 IntSink (suite.primitive.IntPrimitives.IntSink)1 Ints_ (suite.primitive.Ints_)1 IntIntPair (suite.primitive.adt.pair.IntIntPair)1