Differential D4006 Diff 14136 java/src/main/java/org/softwareheritage/graph/webgraph/FixedWidthSwhLabelList.java
Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/webgraph/FixedWidthSwhLabelList.java
- This file was added.
package org.softwareheritage.graph.webgraph; | |||||
import it.unimi.dsi.big.webgraph.labelling.Label; | |||||
import it.unimi.dsi.io.InputBitStream; | |||||
import it.unimi.dsi.io.OutputBitStream; | |||||
import org.softwareheritage.graph.SwhLabel; | |||||
import java.io.IOException; | |||||
import java.util.Arrays; | |||||
/** | |||||
* List of {@link SwhLabel} represented in fixed width and following Webgraph labels convention. | |||||
* | |||||
* @author The Software Heritage developers | |||||
*/ | |||||
public class FixedWidthSwhLabelList extends AbstractSwhLabelList { | |||||
private final int width; | |||||
private static final SwhLabel[] EMPTY_ARRAY = {}; | |||||
public FixedWidthSwhLabelList(String key, int width, SwhLabel[] value) { | |||||
super(key, value); | |||||
for (int i = 0; i < value.length; i++) { | |||||
long valueEncoded = SwhLabel.toEncoded(value[i]); | |||||
if (valueEncoded < 0 || valueEncoded >= 1L << width) | |||||
throw new IllegalArgumentException("Value out of range: " + valueEncoded); | |||||
} | |||||
this.width = width; | |||||
} | |||||
public FixedWidthSwhLabelList(String key, int width) { | |||||
this(key, width, EMPTY_ARRAY); | |||||
} | |||||
public FixedWidthSwhLabelList(String... arg) { | |||||
this(arg[0], Integer.parseInt(arg[1])); | |||||
} | |||||
@Override | |||||
public Label copy() { | |||||
return new FixedWidthSwhLabelList(key, width, value.clone()); | |||||
} | |||||
@Override | |||||
public int fromBitStream(InputBitStream inputBitStream, final long sourceUnused) throws IOException { | |||||
long readBits = inputBitStream.readBits(); | |||||
value = new SwhLabel[inputBitStream.readGamma()]; | |||||
for(int i = 0; i < value.length; i++) | |||||
value[i] = SwhLabel.fromEncoded(inputBitStream.readLong(width)); | |||||
return (int) (inputBitStream.readBits() - readBits); | |||||
} | |||||
@Override | |||||
public int toBitStream(OutputBitStream outputBitStream, final long sourceUnused) throws IOException { | |||||
int bits = outputBitStream.writeGamma(value.length); | |||||
for(int i = 0; i < value.length; i++) | |||||
bits += outputBitStream.writeLong(SwhLabel.toEncoded(value[i]), width); | |||||
return bits; | |||||
} | |||||
@Override | |||||
public int fixedWidth() { | |||||
return -1; | |||||
} | |||||
@Override | |||||
public String toString() { | |||||
return key + ":" + Arrays.toString(value) + " (width:" + width + ")"; | |||||
} | |||||
@Override | |||||
public String toSpec() { | |||||
return this.getClass().getName() + "(" + key + "," + width + ")"; | |||||
} | |||||
} | |||||
No newline at end of file |