Ejemplo n.º 1
0
        public IEnumerable <Path> SplitBy(Segment segment)
        {
            var list = new StackListQueue <Path>();
            StackListQueue <int> indexes;

            try
            {
                int[,] matrix;
                lock (CudafySequencies.Semaphore)
                {
                    CudafySequencies.SetSequencies(
                        segment.Select(GetInts).Select(item => item.ToArray()).ToArray(),
                        GetRange(1, Count - 2).Select(GetInts).Select(item => item.ToArray()).ToArray()
                        );
                    CudafySequencies.Execute("Compare");
                    matrix = CudafySequencies.GetMatrix();
                }
                lock (CudafyMatrix.Semaphore)
                {
                    CudafyMatrix.SetMatrix(matrix);
                    CudafyMatrix.ExecuteRepeatZeroIndexOfZero();
                    indexes = new StackListQueue <int>(CudafyMatrix.GetIndexes()
                                                       .Where(index => index >= 0)
                                                       .Select(index => index + 1));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                indexes = new StackListQueue <int>(GetRange(1, Count - 2).Intersect(segment).Select(v => IndexOf(v)));
            }

            indexes.Sort();
            indexes.Prepend(0);
            indexes.Append(Count - 1);
            for (int prev = indexes.Dequeue(); indexes.Any(); prev = indexes.Dequeue())
            {
                if (((prev + 1) == indexes[0]) &&
                    segment.Contains(this[prev]) &&
                    segment.Contains(this[indexes[0]]))
                {
                    continue;
                }
                list.Add(new Path(GetRange(prev, indexes[0] - prev + 1)));
            }
            Debug.WriteLineIf(list.Any(), this + " split by " + segment + " is " +
                              string.Join(",", list.Select(item => item.ToString())));
            return(list);
        }
Ejemplo n.º 2
0
 public IEnumerable <T> Distinct()
 {
     if (Settings.EnableCudafy)
     {
         try
         {
             if (Count == 0)
             {
                 return(new StackListQueue <T>());
             }
             IEnumerable <IEnumerable <int> > list = this.Select(GetInts);
             int[,] matrix;
             int[] indexes;
             lock (CudafySequencies.Semaphore)
             {
                 int[][] arr = this.Select(GetInts).Select(item => item.ToArray()).ToArray();
                 CudafySequencies.SetSequencies(arr, arr);
                 CudafySequencies.Execute("Compare");
                 matrix = CudafySequencies.GetMatrix();
             }
             lock (CudafyMatrix.Semaphore)
             {
                 CudafyMatrix.SetMatrix(matrix);
                 CudafyMatrix.ExecuteRepeatZeroIndexOfZero();
                 indexes = CudafyMatrix.GetIndexes();
             }
             return(indexes.Where((value, index) => value == index)
                    .Select(index => this[index]));
         }
         catch (Exception ex)
         {
             Debug.WriteLine(ex.ToString());
             return(this.ToList().Distinct());
         }
     }
     return(this.ToList().Distinct());
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Разбиение пути в точках перечечения пути с графом на отдельные подпути
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public IEnumerable <Path> Split(Path path)
        {
            Debug.Assert(Count >= 2);
            var list = new StackListQueue <Path>();
            StackListQueue <int> indexes;

            if (Settings.EnableCudafy)
            {
                try
                {
                    int[,] matrix;
                    lock (CudafySequencies.Semaphore)
                    {
                        CudafySequencies.SetSequencies(
                            Vertices.Select(path.GetInts).Select(item => item.ToArray()).ToArray(),
                            path.GetRange(1, Count - 2).Select(path.GetInts).Select(item => item.ToArray()).ToArray()
                            );
                        CudafySequencies.Execute("Compare");
                        matrix = CudafySequencies.GetMatrix();
                    }
                    lock (CudafyMatrix.Semaphore)
                    {
                        CudafyMatrix.SetMatrix(matrix);
                        CudafyMatrix.ExecuteRepeatZeroIndexOfZero();
                        indexes = new StackListQueue <int>(CudafyMatrix.GetIndexes()
                                                           .Where(index => index >= 0)
                                                           .Select(index => index + 1));
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    indexes =
                        new StackListQueue <int>(
                            path.GetRange(1, Count - 2)
                            .Intersect(Vertices)
                            .Select(v => path.IndexOf(v)));
                }
            }
            else
            {
                indexes =
                    new StackListQueue <int>(
                        path.GetRange(1, Count - 2)
                        .Intersect(Vertices)
                        .Select(v => path.IndexOf(v)));
            }
            indexes.Sort();
            indexes.Prepend(0);
            indexes.Append(Count - 1);
            Dictionary <Vertex, VertexSortedCollection> children = Children;

            for (int prev = indexes.Dequeue(); indexes.Any(); prev = indexes.Dequeue())
            {
                if (((prev + 1) == indexes[0]) &&
                    children.ContainsKey(path[prev]) &&
                    children[path[prev]].Contains(path[indexes[0]]))
                {
                    continue;
                }
                list.Add(new Path(path.GetRange(prev, indexes[0] - prev + 1)));
            }
            Debug.WriteLineIf(list.Any(), path + " split by " + this + " is " +
                              string.Join(",", list.Select(item => item.ToString())));
            return(list);
        }
Ejemplo n.º 4
0
        public BooleanVector GetVector(Circle circle)
        {
            Debug.Assert(circle.Any());
            int count      = circle.Count;
            var collection = new SegmentCollection();

            for (int i = 0; i < count; i++)
            {
                collection.Add(new Segment(circle[i], circle[(i + 1) % count]));
            }
            Debug.Assert(collection.All(Contains));
            List <int> indexes;

            if (Settings.EnableCudafy)
            {
                try
                {
                    IEnumerable <IEnumerable <int> > list1 = collection.Select(GetInts);
                    IEnumerable <IEnumerable <int> > list2 = this.Select(GetInts);
                    int[,] matrix;
                    lock (CudafySequencies.Semaphore)
                    {
                        CudafySequencies.SetSequencies(
                            list1.Select(item => item.ToArray()).ToArray(),
                            list2.Select(item => item.ToArray()).ToArray()
                            );
                        CudafySequencies.Execute("Compare");
                        matrix = CudafySequencies.GetMatrix();
                    }
                    lock (CudafyMatrix.Semaphore)
                    {
                        CudafyMatrix.SetMatrix(matrix);
                        CudafyMatrix.ExecuteRepeatZeroIndexOfZero();
                        indexes = CudafyMatrix.GetIndexes().ToList();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    indexes = collection.Select(segment => IndexOf(segment)).ToList();
                }
            }
            else
            {
                indexes = collection.Select(segment => IndexOf(segment)).ToList();
            }
            indexes.Sort();
            var booleanVector = new BooleanVector();

            if (indexes[0] > 0)
            {
                booleanVector.AddRange(Enumerable.Repeat(false, indexes[0]));
            }
            booleanVector.Add(true);
            for (int i = 1; i < indexes.Count; i++)
            {
                if (indexes[i] - indexes[i - 1] > 1)
                {
                    booleanVector.AddRange(Enumerable.Repeat(false, indexes[i] - indexes[i - 1] - 1));
                }
                booleanVector.Add(true);
            }
            return(booleanVector);
        }
Ejemplo n.º 5
0
 public new IEnumerable <Path> Distinct()
 {
     if (Count == 0)
     {
         return(new MyLibrary.Collections.StackListQueue <Path>());
     }
     if (Settings.EnableCudafy)
     {
         try
         {
             var list =
                 new StackListQueue <StackListQueue <int> >(
                     this.Select(path => new StackListQueue <int>(path.Select(vertex => vertex.Id))));
             int[][] arr = list.Select(item => item.ToArray()).ToArray();
             Debug.Assert(Count == arr.Length);
             int[,] matrix;
             int[] indexes;
             lock (CudafySequencies.Semaphore)
             {
                 CudafySequencies.SetSequencies(arr, arr);
                 CudafySequencies.Execute("Compare");
                 matrix = CudafySequencies.GetMatrix();
             }
             Debug.Assert(matrix.GetLength(0) == arr.Length);
             Debug.Assert(matrix.GetLength(1) == arr.Length);
             lock (CudafyMatrix.Semaphore)
             {
                 CudafyMatrix.SetMatrix(matrix);
                 CudafyMatrix.ExecuteRepeatZeroIndexOfZero();
                 indexes = CudafyMatrix.GetIndexes();
             }
             Debug.Assert(indexes.GetLength(0) == arr.Length);
             IEnumerable <Path> paths1 = indexes.Where((value, index) => value == index)
                                         .Select(this.ElementAt);
             var list1 =
                 new StackListQueue <StackListQueue <int> >(
                     paths1.Select(path => new StackListQueue <int>(path.Select(vertex => vertex.Id))));
             var list2 =
                 new StackListQueue <StackListQueue <int> >(
                     paths1.Select(path => new StackListQueue <int>(path.GetReverse().Select(vertex => vertex.Id))));
             lock (CudafySequencies.Semaphore)
             {
                 CudafySequencies.SetSequencies(
                     list1.Select(item => item.ToArray()).ToArray(),
                     list2.Select(item => item.ToArray()).ToArray()
                     );
                 CudafySequencies.Execute("Compare");
                 matrix = CudafySequencies.GetMatrix();
             }
             Debug.Assert(matrix.GetLength(0) == paths1.Count());
             Debug.Assert(matrix.GetLength(1) == paths1.Count());
             lock (CudafyMatrix.Semaphore)
             {
                 CudafyMatrix.SetMatrix(matrix);
                 CudafyMatrix.ExecuteRepeatZeroIndexOfZero();
                 indexes = CudafyMatrix.GetIndexes();
             }
             Debug.Assert(indexes.GetLength(0) == paths1.Count());
             return
                 (new PathCollection(
                      paths1.Where((value, index) => indexes[index] == -1 || indexes[index] >= index)));
         }
         catch (Exception ex)
         {
             return(new PathCollection(base.Distinct()));
         }
     }
     {
         return(new PathCollection(base.Distinct()));
     }
 }