// Godin: create one group per unique hash
// TODO Godin: can we create map with expected size?
Map<ByteArray, BlocksGroup> groupsByHash = Maps.newHashMap();
for (Block fileBlock : fileBlocks) {
ByteArray hash = fileBlock.getBlockHash();
BlocksGroup sameHash = groupsByHash.get(hash);
if (sameHash == null) {
sameHash = BlocksGroup.empty();
groupsByHash.put(hash, sameHash);
}
sameHash.blocks.add(fileBlock);
}
// Godin: retrieve blocks from index
for (Map.Entry<ByteArray, BlocksGroup> entry : groupsByHash.entrySet()) {
ByteArray hash = entry.getKey();
BlocksGroup group = entry.getValue();
for (Block blockFromIndex : cloneIndex.getBySequenceHash(hash)) {
// Godin: skip blocks for this file if they come from index
if (!originResourceId.equals(blockFromIndex.getResourceId())) {
group.blocks.add(blockFromIndex);
}
}
Collections.sort(group.blocks, BlocksGroup.BlockComparator.INSTANCE);
}
// 3: let c be a list with c(0) = empty
BlocksGroup[] sameHashBlocksGroups = new BlocksGroup[size + 2];
sameHashBlocksGroups[0] = BlocksGroup.empty();
// 4: for i := 1 to length(f) do
for (Block fileBlock : fileBlocks) {
ByteArray hash = fileBlock.getBlockHash();
int i = fileBlock.getIndexInFile() + 1;
// 5: retrieve tuples with same sequence hash as f(i)
// 6: store this set as c(i)
sameHashBlocksGroups[i] = groupsByHash.get(hash);
}