/// <summary> /// Removing an edge from this edge type /// </summary> /// <param name="edge">an edge to remove</param> public void RemoveEdge(Edge edge) { if (Unrestricted) { UnrestrictedEdge unrestrictedEdge = unrestrictedEdges[edge.EdgeId]; unrestrictedEdge.Unpersist(graph.Session); unrestrictedEdges.Remove(edge.EdgeId); } else { restrictedEdges.Remove(edge.EdgeId); } if (birectional) { edge.Tail.VertexType.RemoveTailToHeadEdge(edge); edge.Head.VertexType.RemoveHeadToTailEdge(edge); } else { edge.Tail.VertexType.RemoveTailToHeadEdge(edge); } foreach (string key in GetPropertyKeys()) { edge.RemoveProperty(key); } Range <EdgeId> range = new Range <EdgeId>(edge.EdgeId, edge.EdgeId); bool isEqual; int pos = edgeRanges.BinarySearch(range, out isEqual); if (pos >= 0) { if (pos == edgeRanges.Count || (pos > 0 && edgeRanges[pos].Min > edge.EdgeId)) { --pos; } range = edgeRanges[pos]; if (range.Min == edge.EdgeId) { if (range.Max == edge.EdgeId) { edgeRanges.RemoveAt(pos); } else { edgeRanges[pos] = new Range <EdgeId>(range.Min + 1, range.Max); } } else if (range.Max == edge.EdgeId) { edgeRanges[pos] = new Range <EdgeId>(range.Min, range.Max + 1); } else { edgeRanges[pos] = new Range <EdgeId>(range.Min, edge.EdgeId - 1); edgeRanges.Insert(pos + 1, new Range <EdgeId>(edge.EdgeId + 1, range.Max)); } } }
internal VertexId AllocateVertexId(VertexId vId = 0, VelocityDbList <Range <VertexId> > vertecis = null) { if (vertecis == null) { if (this.vertecis == null) { return(vId); } vertecis = this.vertecis; } Range <VertexId> range; if (vId != 0) { range = new Range <VertexId>(vId, vId); if (vertecis.Count == 0) { vertecis.Add(range); } else { bool isEqual; int pos = vertecis.BinarySearch(range, out isEqual); int originalPos = pos; if (isEqual) { throw new VertexAllreadyExistException("Vertex with id " + vId + " allready exist"); } Range <VertexId> existingRange = vertecis[pos == vertecis.Count ? pos - 1 : pos]; if (existingRange.Min == 0 && existingRange.Max == 0 || (pos > 0 && existingRange.Min > vId + 1)) { existingRange = vertecis[--pos]; } if (existingRange.Min - 1 == vId) { range = new Range <VertexId>(existingRange.Min - 1, existingRange.Max); if (pos > 0) { Range <VertexId> priorRange = vertecis[pos - 1]; if (priorRange.Max + 1 == range.Min) { range = new Range <VertexId>(priorRange.Min, range.Max); vertecis[pos - 1] = range; vertecis.RemoveAt(pos); } else { vertecis[pos] = range; } } else { vertecis[pos] = range; } } else if (existingRange.Max + 1 == vId) { range = new Range <VertexId>(existingRange.Min, existingRange.Max + 1); if (vertecis.Count > pos) { Range <VertexId> nextRange = vertecis[pos + 1]; if (nextRange.Min == range.Max + 1) { range = new Range <VertexId>(range.Min, nextRange.Max); vertecis.RemoveAt(pos); vertecis[pos] = range; } else { vertecis[pos] = range; } } else { vertecis[pos == vertecis.Count ? pos - 1 : pos] = range; } } else if (vId >= existingRange.Min && vId <= existingRange.Max) { throw new VertexAllreadyExistException("Vertex with id " + vId + " allready exist"); } else { vertecis.Insert(originalPos, range); } #if VERIFY int i = 0; Range <VertexId> p = default(Range <VertexId>); foreach (Range <VertexId> r in vertecis) { if (i++ > 0) { if (p.Min >= r.Min) { throw new UnexpectedException("Wrong order"); } if (p.Max == r.Min + 1) { throw new UnexpectedException("Failed to merge"); } } p = r; } #endif } } else { vId = 1; switch (vertecis.Count) { case 0: range = new Range <VertexId>(1, 1); vertecis.Add(range); break; case 1: range = vertecis.First(); if (range.Min == 1) { vId = range.Max + 1; range = new Range <VertexId>(1, vId); } else { vId = range.Min - 1; range = new Range <VertexId>(vId, range.Max); } vertecis[0] = range; break; default: { range = vertecis.First(); if (range.Min > 1) { vId = range.Min - 1; range = new Range <VertexId>(vId, range.Max); vertecis[0] = range; } else { Range <VertexId> nextRange = vertecis[1]; if (range.Max + 1 == nextRange.Min) { vertecis.RemoveAt(1); vId = nextRange.Min; range = new Range <VertexId>(range.Min, nextRange.Max); vertecis[0] = range; } else { range = new Range <VertexId>(range.Min, range.Max + 1); vId = range.Max; vertecis[0] = range; } } } break; } } return(vId); }