|
19 | 19 | */
|
20 | 20 | package org.neo4j.graphalgo.core.utils.partition;
|
21 | 21 |
|
| 22 | +import com.carrotsearch.hppc.AbstractIterator; |
22 | 23 | import org.neo4j.graphalgo.api.Graph;
|
23 | 24 | import org.neo4j.graphalgo.core.concurrency.ParallelUtil;
|
24 | 25 | import org.neo4j.graphalgo.core.utils.BitUtil;
|
25 | 26 | import org.neo4j.graphalgo.core.utils.collection.primitive.PrimitiveLongIterator;
|
| 27 | +import org.neo4j.graphalgo.core.utils.paged.HugeCursor; |
| 28 | +import org.neo4j.graphalgo.core.utils.paged.HugeLongArray; |
26 | 29 |
|
27 | 30 | import java.util.ArrayList;
|
| 31 | +import java.util.Iterator; |
28 | 32 | import java.util.List;
|
29 | 33 | import java.util.function.Function;
|
30 | 34 |
|
@@ -123,4 +127,80 @@ private static <TASK> List<TASK> tasks(
|
123 | 127 | }
|
124 | 128 | return result;
|
125 | 129 | }
|
| 130 | + |
| 131 | + public static <TASK> Iterator<TASK> blockAlignedPartitioning( |
| 132 | + HugeLongArray sortedIds, |
| 133 | + int blockShift, |
| 134 | + Function<Partition, TASK> taskCreator |
| 135 | + ) { |
| 136 | + return new BlockAlignedPartitionIterator<>(sortedIds, blockShift, taskCreator); |
| 137 | + } |
| 138 | + |
| 139 | + private static class BlockAlignedPartitionIterator<TASK> extends AbstractIterator<TASK> { |
| 140 | + private final HugeCursor<long[]> cursor; |
| 141 | + private final long size; |
| 142 | + private final int blockShift; |
| 143 | + private final Function<Partition, TASK> taskCreator; |
| 144 | + |
| 145 | + private int prevBlockId; |
| 146 | + private long blockStart; |
| 147 | + private boolean done; |
| 148 | + private int lastIndex; |
| 149 | + |
| 150 | + BlockAlignedPartitionIterator( |
| 151 | + HugeLongArray sortedIds, |
| 152 | + int blockShift, |
| 153 | + Function<Partition, TASK> taskCreator |
| 154 | + ) { |
| 155 | + this.size = sortedIds.size(); |
| 156 | + this.blockShift = blockShift; |
| 157 | + this.taskCreator = taskCreator; |
| 158 | + this.cursor = sortedIds.initCursor(sortedIds.newCursor()); |
| 159 | + this.prevBlockId = 0; |
| 160 | + this.blockStart = 0L; |
| 161 | + this.done = false; |
| 162 | + this.lastIndex = Integer.MAX_VALUE; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + protected TASK fetch() { |
| 167 | + if (this.done) { |
| 168 | + return done(); |
| 169 | + } |
| 170 | + |
| 171 | + long base = cursor.base; |
| 172 | + int limit = cursor.limit; |
| 173 | + long[] array = cursor.array; |
| 174 | + int prevBlockId = this.prevBlockId; |
| 175 | + int blockShift = this.blockShift; |
| 176 | + |
| 177 | + for (int i = lastIndex; i < limit; i++) { |
| 178 | + long originalId = array[i]; |
| 179 | + int blockId = (int) (originalId >>> blockShift); |
| 180 | + if (blockId != prevBlockId) { |
| 181 | + long internalId = base + i; |
| 182 | + prevBlockId = blockId; |
| 183 | + |
| 184 | + if (internalId > 0) { |
| 185 | + var partition = ImmutablePartition.of(blockStart, internalId - blockStart); |
| 186 | + this.blockStart = internalId; |
| 187 | + this.prevBlockId = prevBlockId; |
| 188 | + this.lastIndex = i; |
| 189 | + return taskCreator.apply(partition); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (cursor.next()) { |
| 195 | + this.prevBlockId = prevBlockId; |
| 196 | + this.lastIndex = cursor.offset; |
| 197 | + return fetch(); |
| 198 | + } |
| 199 | + |
| 200 | + var partition = ImmutablePartition.of(blockStart, size - blockStart); |
| 201 | + this.done = true; |
| 202 | + |
| 203 | + return taskCreator.apply(partition); |
| 204 | + } |
| 205 | + } |
126 | 206 | }
|
0 commit comments