Differential D4006 Diff 14145 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.AbstractLabel; | |||||
import it.unimi.dsi.big.webgraph.labelling.FixedWidthLongListLabel; | |||||
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 AbstractLabel { | |||||
private final String key; | |||||
private final int width; | |||||
public SwhLabel[] value; | |||||
// Use existing Webgraph class to represent a list of SwhLabel as a list of encoded long | |||||
private final FixedWidthLongListLabel longList; | |||||
private static final SwhLabel[] EMPTY_ARRAY = {}; | |||||
public FixedWidthSwhLabelList(String key, int width, SwhLabel[] value) { | |||||
this.key = key; | |||||
this.width = width; | |||||
this.value = value; | |||||
long[] valueEncoded = new long[value.length]; | |||||
for (int i = 0; i < value.length; i++) | |||||
valueEncoded[i] = SwhLabel.toEncoded(value[i]); | |||||
this.longList = new FixedWidthLongListLabel(key, width, valueEncoded); | |||||
} | |||||
public FixedWidthSwhLabelList(String key, int width) { | |||||
this(key, width, EMPTY_ARRAY); | |||||
} | |||||
public FixedWidthSwhLabelList(String... arg) { | |||||
this(arg[0], Integer.parseInt(arg[1])); | |||||
} | |||||
@Override | |||||
public int fromBitStream(InputBitStream inputBitStream, final long sourceUnused) throws IOException { | |||||
int ret = longList.fromBitStream(inputBitStream, sourceUnused); | |||||
// Decode values from their internal long representation | |||||
value = new SwhLabel[longList.value.length]; | |||||
for (int i = 0; i < value.length; i++) | |||||
value[i] = SwhLabel.fromEncoded(longList.value[i]); | |||||
return ret; | |||||
} | |||||
@Override | |||||
public int toBitStream(OutputBitStream outputBitStream, final long sourceUnused) throws IOException { | |||||
// Values have already been encoded in the FixedWidthSwhLabelList constructor | |||||
return longList.toBitStream(outputBitStream, sourceUnused); | |||||
} | |||||
@Override | |||||
public String wellKnownAttributeKey() { | |||||
return key; | |||||
} | |||||
@Override | |||||
public String[] attributeKeys() { | |||||
return new String[]{key}; | |||||
} | |||||
@Override | |||||
public Class<?>[] attributeTypes() { | |||||
return new Class[]{SwhLabel[].class}; | |||||
} | |||||
@Override | |||||
public Object get(String s) { | |||||
if (this.key.equals(key)) | |||||
return value; | |||||
throw new IllegalArgumentException(); | |||||
} | |||||
@Override | |||||
public Object get() { | |||||
return value; | |||||
} | |||||
@Override | |||||
public Label copy() { | |||||
return new FixedWidthSwhLabelList(key, width, value.clone()); | |||||
} | |||||
@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 + ")"; | |||||
} | |||||
} |