Ejemplo n.º 1
0
        /// <summary>
        ///   Writes the vertex.
        /// </summary>
        /// <param name='vertex'> Vertex. </param>
        /// <param name='writer'> Writer. </param>
        private static void WriteVertex(VertexModel vertex, SerializationWriter writer)
        {
            writer.WriteOptimized(SerializedVertex);
            WriteAGraphElement(vertex, writer);

            #region edges

            var outgoingEdges = vertex.GetOutgoingEdges();
            if (outgoingEdges == null)
            {
                writer.WriteOptimized(0);
            }
            else
            {
                writer.WriteOptimized(outgoingEdges.Count);
                foreach (var aOutEdgeProperty in outgoingEdges)
                {
                    writer.Write(aOutEdgeProperty.EdgePropertyId);
                    writer.WriteOptimized(aOutEdgeProperty.Edges.Count);
                    foreach (var aOutEdge in aOutEdgeProperty.Edges)
                    {
                        writer.Write(aOutEdge.Id);
                    }
                }
            }

            var incomingEdges = vertex.GetIncomingEdges();
            if (incomingEdges == null)
            {
                writer.WriteOptimized(0);
            }
            else
            {
                writer.WriteOptimized(incomingEdges.Count);
                foreach (var aIncEdgeProperty in incomingEdges)
                {
                    writer.Write(aIncEdgeProperty.EdgePropertyId);
                    writer.WriteOptimized(aIncEdgeProperty.Edges.Count);
                    foreach (var aIncEdge in aIncEdgeProperty.Edges)
                    {
                        writer.Write(aIncEdge.Id);
                    }
                }
            }

            #endregion
        }
        /// <summary>
        /// Gets the frontier elements on an outgoing edge
        /// </summary>
        /// <param name="vertex">The vertex behind the frontier</param>
        /// <param name="edgepropertyFilter">The edge property filter</param>
        /// <param name="edgeFilter">The edge filter</param>
        /// <param name="vertexFilter">The vertex filter</param>
        /// <param name="alreadyVisited">The vertices that have been visited already</param>
        /// <returns>A number of frontier elements</returns>
        private static IEnumerable <FrontierElement> GetValidOutgoingEdges(
            VertexModel vertex,
            PathDelegates.EdgePropertyFilter edgepropertyFilter,
            PathDelegates.EdgeFilter edgeFilter,
            PathDelegates.VertexFilter vertexFilter,
            BigBitArray alreadyVisited)
        {
            var edgeProperties = vertex.GetOutgoingEdges();
            var result         = new List <FrontierElement>();

            if (edgeProperties != null)
            {
                foreach (var edgeContainer in edgeProperties)
                {
                    if (edgepropertyFilter != null && !edgepropertyFilter(edgeContainer.EdgePropertyId, Direction.OutgoingEdge))
                    {
                        continue;
                    }

                    if (edgeFilter != null)
                    {
                        for (var i = 0; i < edgeContainer.Edges.Count; i++)
                        {
                            var aEdge = edgeContainer.Edges[i];
                            if (edgeFilter(aEdge, Direction.OutgoingEdge))
                            {
                                if (alreadyVisited.SetValue(aEdge.TargetVertex.Id, true))
                                {
                                    if (vertexFilter != null)
                                    {
                                        if (vertexFilter(aEdge.TargetVertex))
                                        {
                                            result.Add(new FrontierElement
                                            {
                                                EdgeDirection = Direction.OutgoingEdge,
                                                EdgeLocation  = new EdgeLocation
                                                {
                                                    Edge           = aEdge,
                                                    EdgePropertyId =
                                                        edgeContainer.EdgePropertyId
                                                },
                                                FrontierVertex = aEdge.TargetVertex
                                            });
                                        }
                                    }
                                    else
                                    {
                                        result.Add(new FrontierElement
                                        {
                                            EdgeDirection = Direction.OutgoingEdge,
                                            EdgeLocation  = new EdgeLocation
                                            {
                                                Edge           = aEdge,
                                                EdgePropertyId =
                                                    edgeContainer.EdgePropertyId
                                            },
                                            FrontierVertex = aEdge.TargetVertex
                                        });
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (vertexFilter != null)
                        {
                            for (var i = 0; i < edgeContainer.Edges.Count; i++)
                            {
                                var aEdge = edgeContainer.Edges[i];

                                if (alreadyVisited.SetValue(aEdge.TargetVertex.Id, true))
                                {
                                    if (vertexFilter(aEdge.TargetVertex))
                                    {
                                        result.Add(new FrontierElement
                                        {
                                            EdgeDirection = Direction.OutgoingEdge,
                                            EdgeLocation  = new EdgeLocation
                                            {
                                                Edge           = aEdge,
                                                EdgePropertyId =
                                                    edgeContainer.EdgePropertyId
                                            },
                                            FrontierVertex = aEdge.TargetVertex
                                        });
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (var i = 0; i < edgeContainer.Edges.Count; i++)
                            {
                                var aEdge = edgeContainer.Edges[i];
                                if (alreadyVisited.SetValue(aEdge.TargetVertex.Id, true))
                                {
                                    result.Add(new FrontierElement
                                    {
                                        EdgeDirection = Direction.OutgoingEdge,
                                        EdgeLocation  = new EdgeLocation
                                        {
                                            Edge           = aEdge,
                                            EdgePropertyId =
                                                edgeContainer.EdgePropertyId
                                        },
                                        FrontierVertex = aEdge.TargetVertex
                                    });
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the valid edges of a vertex
        /// </summary>
        /// <param name="vertex">The vertex.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="edgepropertyFilter">The edge property filter.</param>
        /// <param name="edgeFilter">The edge filter.</param>
        /// <param name="vertexFilter">The target vertex filter.</param>
        /// <returns>Valid edges</returns>
        public static List <Tuple <UInt16, IEnumerable <EdgeModel> > > GetValidEdges(
            VertexModel vertex,
            Direction direction,
            PathDelegates.EdgePropertyFilter edgepropertyFilter,
            PathDelegates.EdgeFilter edgeFilter,
            PathDelegates.VertexFilter vertexFilter)
        {
            var edgeProperties = direction == Direction.IncomingEdge ? vertex.GetIncomingEdges() : vertex.GetOutgoingEdges();
            var result         = new List <Tuple <ushort, IEnumerable <EdgeModel> > >();

            if (edgeProperties != null)
            {
                foreach (var edgeContainer in edgeProperties)
                {
                    if (edgepropertyFilter != null && !edgepropertyFilter(edgeContainer.EdgePropertyId, direction))
                    {
                        continue;
                    }

                    if (edgeFilter != null)
                    {
                        var validEdges = new List <EdgeModel>();

                        for (var i = 0; i < edgeContainer.Edges.Count; i++)
                        {
                            var aEdge = edgeContainer.Edges[i];
                            if (edgeFilter(aEdge, direction))
                            {
                                if (vertexFilter != null)
                                {
                                    if (
                                        vertexFilter(direction == Direction.IncomingEdge
                                                         ? aEdge.SourceVertex
                                                         : aEdge.TargetVertex))
                                    {
                                        validEdges.Add(aEdge);
                                    }
                                }
                                else
                                {
                                    validEdges.Add(aEdge);
                                }
                            }
                        }
                        result.Add(new Tuple <ushort, IEnumerable <EdgeModel> >(edgeContainer.EdgePropertyId, validEdges));
                    }
                    else
                    {
                        if (vertexFilter != null)
                        {
                            var validEdges = new List <EdgeModel>();

                            for (var i = 0; i < edgeContainer.Edges.Count; i++)
                            {
                                var aEdge = edgeContainer.Edges[i];
                                if (
                                    vertexFilter(direction == Direction.IncomingEdge
                                                     ? aEdge.SourceVertex
                                                     : aEdge.TargetVertex))
                                {
                                    validEdges.Add(aEdge);
                                }
                            }
                            result.Add(new Tuple <ushort, IEnumerable <EdgeModel> >(edgeContainer.EdgePropertyId, validEdges));
                        }
                        else
                        {
                            result.Add(new Tuple <ushort, IEnumerable <EdgeModel> >(edgeContainer.EdgePropertyId, edgeContainer.Edges));
                        }
                    }
                }
            }

            return(result);
        }