예제 #1
0
        EdgeId NewEdgeId(Graph g)
        {
            Range <EdgeId> range;
            EdgeId         eId = 1;

            switch (edgeRanges.Count)
            {
            case 0:
                range = new Range <EdgeId>(1, 1);
                edgeRanges.Add(range);
                break;

            case 1:
                range = edgeRanges.First();

                if (range.Min == 1)
                {
                    eId   = range.Max + 1;
                    range = new Range <EdgeId>(1, eId);
                }
                else
                {
                    eId   = range.Min - 1;
                    range = new Range <EdgeId>(eId, range.Max);
                }
                edgeRanges[0] = range;
                break;

            default:
            {
                range = edgeRanges.First();
                if (range.Min > 1)
                {
                    eId   = range.Min - 1;
                    range = new Range <VertexId>(eId, range.Max);
                }
                else
                {
                    Range <VertexId> nextRange = edgeRanges[1];
                    if (range.Max + 2 == nextRange.Min)
                    {
                        edgeRanges.Remove(range);
                        eId   = range.Max + 1;
                        range = new Range <VertexId>(range.Min, nextRange.Max);
                    }
                    else
                    {
                        range = new Range <VertexId>(range.Min, range.Max + 1);
                        eId   = range.Max + 1;
                    }
                    edgeRanges.Add(range);
                }
            }
            break;
            }
            return(eId);
        }
예제 #2
0
        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);
        }