use of com.carrotsearch.hppc.LongArrayList in project titan by thinkaurelius.
the class EdgeSerializer method writeRelation.
public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
assert type == relation.getType() || type.getBaseType().equals(relation.getType());
Direction dir = EdgeDirection.fromPosition(position);
Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
long typeid = type.longId();
DirectionID dirID = getDirID(dir, relation.isProperty() ? RelationCategory.PROPERTY : RelationCategory.EDGE);
DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY);
int valuePosition;
IDHandler.writeRelationType(out, typeid, dirID, type.isInvisibleType());
Multiplicity multiplicity = type.multiplicity();
long[] sortKey = type.getSortKey();
assert !multiplicity.isConstrained() || sortKey.length == 0 : type.name();
int keyStartPos = out.getPosition();
if (!multiplicity.isConstrained()) {
writeInlineTypes(sortKey, relation, out, tx, InlineType.KEY);
}
int keyEndPos = out.getPosition();
long relationId = relation.longId();
//How multiplicity is handled for edges and properties is slightly different
if (relation.isEdge()) {
long otherVertexId = relation.getVertex((position + 1) % 2).longId();
if (multiplicity.isConstrained()) {
if (multiplicity.isUnique(dir)) {
valuePosition = out.getPosition();
VariableLong.writePositive(out, otherVertexId);
} else {
VariableLong.writePositiveBackward(out, otherVertexId);
valuePosition = out.getPosition();
}
VariableLong.writePositive(out, relationId);
} else {
VariableLong.writePositiveBackward(out, otherVertexId);
VariableLong.writePositiveBackward(out, relationId);
valuePosition = out.getPosition();
}
} else {
assert relation.isProperty();
Preconditions.checkArgument(relation.isProperty());
Object value = ((TitanVertexProperty) relation).value();
Preconditions.checkNotNull(value);
PropertyKey key = (PropertyKey) type;
assert key.dataType().isInstance(value);
if (multiplicity.isConstrained()) {
if (multiplicity.isUnique(dir)) {
//Cardinality=SINGLE
valuePosition = out.getPosition();
writePropertyValue(out, key, value);
} else {
//Cardinality=SET
writePropertyValue(out, key, value);
valuePosition = out.getPosition();
}
VariableLong.writePositive(out, relationId);
} else {
assert multiplicity.getCardinality() == Cardinality.LIST;
VariableLong.writePositiveBackward(out, relationId);
valuePosition = out.getPosition();
writePropertyValue(out, key, value);
}
}
//Write signature
long[] signature = type.getSignature();
writeInlineTypes(signature, relation, out, tx, InlineType.SIGNATURE);
//Write remaining properties
LongSet writtenTypes = new LongHashSet(sortKey.length + signature.length);
if (sortKey.length > 0 || signature.length > 0) {
for (long id : sortKey) writtenTypes.add(id);
for (long id : signature) writtenTypes.add(id);
}
LongArrayList remainingTypes = new LongArrayList(8);
for (PropertyKey t : relation.getPropertyKeysDirect()) {
if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) {
remainingTypes.add(t.longId());
}
}
//Sort types before writing to ensure that value is always written the same way
long[] remaining = remainingTypes.toArray();
Arrays.sort(remaining);
for (long tid : remaining) {
PropertyKey t = tx.getExistingPropertyKey(tid);
writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL);
}
assert valuePosition > 0;
StaticArrayEntry entry = new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
return entry;
}
use of com.carrotsearch.hppc.LongArrayList in project titan by thinkaurelius.
the class AbstractLongListUtil method mergeSort.
public static LongArrayList mergeSort(LongArrayList a, LongArrayList b) {
int posa = 0, posb = 0;
LongArrayList result = new LongArrayList(a.size() + b.size());
while (posa < a.size() || posb < b.size()) {
long next;
if (posa >= a.size()) {
next = b.get(posb++);
} else if (posb >= b.size()) {
next = a.get(posa++);
} else if (a.get(posa) <= b.get(posb)) {
next = a.get(posa++);
} else {
next = b.get(posb++);
}
Preconditions.checkArgument(result.isEmpty() || result.get(result.size() - 1) <= next, "The input lists are not sorted");
result.add(next);
}
return result;
}
use of com.carrotsearch.hppc.LongArrayList in project graphhopper by graphhopper.
the class OSMReader method processWay.
/**
* Process properties, encode flags and create edges for the way.
*/
void processWay(ReaderWay way) {
if (way.getNodes().size() < 2)
return;
// ignore multipolygon geometry
if (!way.hasTags())
return;
long wayOsmId = way.getId();
long includeWay = encodingManager.acceptWay(way);
if (includeWay == 0)
return;
long relationFlags = getRelFlagsMap().get(way.getId());
// TODO move this after we have created the edge and know the coordinates => encodingManager.applyWayTags
LongArrayList osmNodeIds = way.getNodes();
// Estimate length of ways containing a route tag e.g. for ferry speed calculation
if (osmNodeIds.size() > 1) {
int first = getNodeMap().get(osmNodeIds.get(0));
int last = getNodeMap().get(osmNodeIds.get(osmNodeIds.size() - 1));
double firstLat = getTmpLatitude(first), firstLon = getTmpLongitude(first);
double lastLat = getTmpLatitude(last), lastLon = getTmpLongitude(last);
if (!Double.isNaN(firstLat) && !Double.isNaN(firstLon) && !Double.isNaN(lastLat) && !Double.isNaN(lastLon)) {
double estimatedDist = distCalc.calcDist(firstLat, firstLon, lastLat, lastLon);
// Add artificial tag for the estimated distance and center
way.setTag("estimated_distance", estimatedDist);
way.setTag("estimated_center", new GHPoint((firstLat + lastLat) / 2, (firstLon + lastLon) / 2));
}
}
if (way.getTag("duration") != null) {
try {
long dur = OSMTagParser.parseDuration(way.getTag("duration"));
// Provide the duration value in seconds in an artificial graphhopper specific tag:
way.setTag("duration:seconds", Long.toString(dur));
} catch (Exception ex) {
LOGGER.warn("Parsing error in way with OSMID=" + way.getId() + " : " + ex.getMessage());
}
}
long wayFlags = encodingManager.handleWayTags(way, includeWay, relationFlags);
if (wayFlags == 0)
return;
List<EdgeIteratorState> createdEdges = new ArrayList<EdgeIteratorState>();
// look for barriers along the way
final int size = osmNodeIds.size();
int lastBarrier = -1;
for (int i = 0; i < size; i++) {
long nodeId = osmNodeIds.get(i);
long nodeFlags = getNodeFlagsMap().get(nodeId);
// barrier was spotted and way is otherwise passable for that mode of travel
if (nodeFlags > 0) {
if ((nodeFlags & wayFlags) > 0) {
// remove barrier to avoid duplicates
getNodeFlagsMap().put(nodeId, 0);
// create shadow node copy for zero length edge
long newNodeId = addBarrierNode(nodeId);
if (i > 0) {
// start at beginning of array if there was no previous barrier
if (lastBarrier < 0)
lastBarrier = 0;
// add way up to barrier shadow node
int length = i - lastBarrier + 1;
LongArrayList partNodeIds = new LongArrayList();
partNodeIds.add(osmNodeIds.buffer, lastBarrier, length);
partNodeIds.set(length - 1, newNodeId);
createdEdges.addAll(addOSMWay(partNodeIds, wayFlags, wayOsmId));
// create zero length edge for barrier
createdEdges.addAll(addBarrierEdge(newNodeId, nodeId, wayFlags, nodeFlags, wayOsmId));
} else {
// run edge from real first node to shadow node
createdEdges.addAll(addBarrierEdge(nodeId, newNodeId, wayFlags, nodeFlags, wayOsmId));
// exchange first node for created barrier node
osmNodeIds.set(0, newNodeId);
}
// remember barrier for processing the way behind it
lastBarrier = i;
}
}
}
// just add remainder of way to graph if barrier was not the last node
if (lastBarrier >= 0) {
if (lastBarrier < size - 1) {
LongArrayList partNodeIds = new LongArrayList();
partNodeIds.add(osmNodeIds.buffer, lastBarrier, size - lastBarrier);
createdEdges.addAll(addOSMWay(partNodeIds, wayFlags, wayOsmId));
}
} else {
// no barriers - simply add the whole way
createdEdges.addAll(addOSMWay(way.getNodes(), wayFlags, wayOsmId));
}
for (EdgeIteratorState edge : createdEdges) {
encodingManager.applyWayTags(way, edge);
}
}
use of com.carrotsearch.hppc.LongArrayList in project titan by thinkaurelius.
the class SimpleVertexQueryProcessor method vertexIds.
/**
* Returns the list of adjacent vertex ids for this query. By reading those ids
* from the entries directly (without creating objects) we get much better performance.
*
* @return
*/
public VertexList vertexIds() {
LongArrayList list = new LongArrayList();
long previousId = 0;
for (Long id : Iterables.transform(this, new Function<Entry, Long>() {
@Nullable
@Override
public Long apply(@Nullable Entry entry) {
return edgeSerializer.readRelation(entry, true, tx).getOtherVertexId();
}
})) {
list.add(id);
if (id >= previousId && previousId >= 0)
previousId = id;
else
previousId = -1;
}
return new VertexLongList(tx, list, previousId >= 0);
}
use of com.carrotsearch.hppc.LongArrayList in project titan by thinkaurelius.
the class VertexLongList method subList.
@Override
public VertexList subList(int fromPosition, int length) {
LongArrayList subList = new LongArrayList(length);
subList.add(vertices.buffer, fromPosition, length);
assert subList.size() == length;
return new VertexLongList(tx, subList, sorted);
}
Aggregations