Exemplo n.º 1
0
        public static IList <TLink> SingleOrDefault <TLink>(this ILinks <TLink> links, IList <TLink> query)
        {
            IList <TLink> result    = null;
            var           count     = 0;
            var           constants = links.Constants;
            var           @continue = constants.Continue;
            var           @break    = constants.Break;

            links.Each(linkHandler, query);
            return(result);

            TLink linkHandler(IList <TLink> link)
            {
                if (count == 0)
                {
                    result = link;
                    count++;
                    return(@continue);
                }
                else
                {
                    result = null;
                    return(@break);
                }
            }
        }
Exemplo n.º 2
0
        public static TLink SearchOrDefault <TLink>(this ILinks <TLink> links, TLink source, TLink target)
        {
            var contants = links.Constants;
            var setter   = new Setter <TLink, TLink>(contants.Continue, contants.Break, default);

            links.Each(setter.SetFirstAndReturnFalse, contants.Any, source, target);
            return(setter.Result);
        }
        public static IList <TLink> ToList <TLink>(this ILinks <TLink> sequences, TLink sequence)
        {
            var list   = new List <TLink>();
            var filler = new ListFiller <TLink, TLink>(list, sequences.Constants.Break);

            sequences.Each(filler.AddSkipFirstAndReturnConstant, new LinkAddress <TLink>(sequence));
            return(list);
        }
        private List <uint> GetTagsByObject(uint @object)
        {
            var tags      = new List <uint>();
            var any       = _links.Constants.Any;
            var @continue = _links.Constants.Continue;
            var target    = _links.Constants.TargetPart;
            var query     = new Link <uint>(any, @object, any);

            _links.Each(link =>
            {
                // Ignore objects itself
                if (Point <uint> .IsPartialPoint(link))
                {
                    return(@continue);
                }
                tags.Add(link[target]);
                return(@continue);
            }, query);
            return(tags);
        }
Exemplo n.º 5
0
        public IList <BlogPost> GetBlogPosts()
        {
            var list       = new List <IList <TLinkAddress> >();
            var listFiller = new ListFiller <IList <TLinkAddress>, TLinkAddress>(list, _links.Constants.Continue);
            // Load all links that match the query: (any: _blogPostMarker any) it means, link with any address, _blogPostMarker as source and any link as target.
            // All links that match this query are BlogPosts.
            var any   = _links.Constants.Any;
            var query = new Link <TLinkAddress>(any, _blogPostMarker, any);

            _links.Each(listFiller.AddAndReturnConstant, query);
            return(list.Select(LoadBlogPost).ToList());
        }
Exemplo n.º 6
0
        private void CountCore(TLink link)
        {
            var any = _links.Constants.Any;

            if (_equalityComparer.Equals(_links.Count(any, link), default))
            {
                CountSequenceSymbolFrequency(link);
            }
            else
            {
                _links.Each(EachElementHandler, any, link);
            }
        }
Exemplo n.º 7
0
        public static IList <TLinkAddress> GetLink <TLinkAddress, TConstants>(this ILinks <TLinkAddress, TConstants> links, TLinkAddress link)
            where TConstants : LinksConstants <TLinkAddress>
        {
            var constants = links.Constants;

            if (constants.IsExternalReference(link))
            {
                return(new Point <TLinkAddress>(link, constants.TargetPart + 1));
            }
            var linkPartsSetter = new Setter <IList <TLinkAddress>, TLinkAddress>(constants.Continue, constants.Break);

            links.Each(linkPartsSetter.SetAndReturnTrue, link);
            return(linkPartsSetter.Result);
        }
Exemplo n.º 8
0
        public static IList <TLink> AllIndices <TLink>(this ILinks <TLink> links, params TLink[] restrictions)
        {
            var arraySize = CheckedConverter <TLink, ulong> .Default.Convert(links.Count(restrictions));

            if (arraySize > 0)
            {
                var array  = new TLink[arraySize];
                var filler = new ArrayFiller <TLink, TLink>(array, links.Constants.Continue);
                links.Each(filler.AddFirstAndReturnConstant, restrictions);
                return(array);
            }
            else
            {
                return(Array.Empty <TLink>());
            }
        }
        private static void TestNonexistentReferences(this ILinks <ulong> memoryAdapter)
        {
            var link = memoryAdapter.Create();

            memoryAdapter.Update(link, ulong.MaxValue, ulong.MaxValue);
            var resultLink = _constants.Null;

            memoryAdapter.Each(foundLink =>
            {
                resultLink = foundLink[_constants.IndexPart];
                return(_constants.Break);
            }, _constants.Any, ulong.MaxValue, ulong.MaxValue);
            Assert.True(resultLink == link);
            Assert.True(memoryAdapter.Count(ulong.MaxValue) == 0);
            memoryAdapter.Delete(link);
        }
Exemplo n.º 10
0
        private void CalculateCore(ILinks<ulong> links, ulong current, int stepsLeft, Counter counter)
        {
            links.Each(link =>
                       {
                           if (links.IsFullPoint(link[links.Constants.IndexPart]))
                               return links.Constants.Continue;

                           if (stepsLeft == 0)
                               counter.Count++;
                           else
                           {
                               var target = link[links.Constants.TargetPart];
                               CalculateCore(links, target, stepsLeft - 1, counter);
                           }

                           return links.Constants.Continue;
                       }, links.Constants.Any, current, links.Constants.Any);
        }
Exemplo n.º 11
0
        public static TLink First <TLink>(this ILinks <TLink> links)
        {
            TLink firstLink        = default;
            var   equalityComparer = EqualityComparer <TLink> .Default;

            if (equalityComparer.Equals(links.Count(), default))
            {
                throw new InvalidOperationException("В хранилище нет связей.");
            }
            links.Each(links.Constants.Any, links.Constants.Any, link =>
            {
                firstLink = link[links.Constants.IndexPart];
                return(links.Constants.Break);
            });
            if (equalityComparer.Equals(firstLink, default))
            {
                throw new InvalidOperationException("В процессе поиска по хранилищу не было найдено связей.");
            }
            return(firstLink);
        }
Exemplo n.º 12
0
        public void Recognize()
        {
            var width  = _image.Width;
            var height = _image.Height;

            KeyValuePair <ulong[], ulong[]>[,] matrix = new KeyValuePair <ulong[], ulong[]> [width, height];

            for (var y = 0; y < height; y++)
            {
                IndexRow(y, width);
            }
            for (int x = 0; x < width; x++)
            {
                IndexColumn(x, height);
            }
            for (var y = 0; y < height; y++)
            {
                if (y == 574)
                {
                    var roww    = GetRow(y, width);
                    var levelss = _sequenceToItsLocalElementLevelsConverter.Convert(roww);
                    var l276    = levelss[276];
                    var l277    = levelss[277];
                    var l278    = levelss[278];
                }

                var row = SaveRow(y, width);

                var x     = 0;
                var stack = new Stack <ulong>();
                StopableSequenceWalker.WalkRight(row, _links.GetSource, _links.GetTarget, _links.IsPartialPoint,
                                                 enteredElement =>
                {
                    stack.Push(enteredElement);
                },
                                                 exitedElement =>
                {
                    stack.Pop();
                },
                                                 checkedElement => true,
                                                 element =>
                {
                    stack.Push(element);
                    var pair = new KeyValuePair <ulong[], ulong[]>(stack.ToArray(), default);
                    stack.Pop();
                    matrix[x++, y] = pair;
                    return(true);
                });
            }
            for (int x = 0; x < width; x++)
            {
                var column = SaveColumn(x, height);

                var y     = 0;
                var stack = new Stack <ulong>();
                StopableSequenceWalker.WalkRight(column, _links.GetSource, _links.GetTarget, _links.IsPartialPoint,
                                                 enteredElement =>
                {
                    stack.Push(enteredElement);
                },
                                                 exitedElement =>
                {
                    stack.Pop();
                },
                                                 checkedElement => true,
                                                 element =>
                {
                    var pair = matrix[x, y];
                    stack.Push(element);
                    pair = new KeyValuePair <ulong[], ulong[]>(pair.Key, stack.ToArray());
                    stack.Pop();
                    matrix[x, y++] = pair;
                    return(true);
                });
            }

            // Sort sequences by usages and frequency

            var linksByUsages    = new SortedDictionary <ulong, List <ulong> >();
            var linksByFrequency = new SortedDictionary <ulong, List <ulong> >();

            var any       = _links.Constants.Any;
            var @continue = _links.Constants.Continue;
            var query     = new Link <ulong>(any, any, any);

            _links.Each(link =>
            {
                var linkIndex = _links.GetIndex(link);
                var usages    = _links.Count(new ulong[] { any, linkIndex });
                if (!linksByUsages.TryGetValue(usages, out List <ulong> linksByUsageList))
                {
                    linksByUsageList = new List <ulong>();
                    linksByUsages.Add(usages, linksByUsageList);
                }
                linksByUsageList.Add(linkIndex);
                ulong frequency = GetFrequency(link);
                if (!linksByFrequency.TryGetValue(frequency, out List <ulong> linksByFrequencyList))
                {
                    linksByFrequencyList = new List <ulong>();
                    linksByFrequency.Add(frequency, linksByFrequencyList);
                }
                linksByFrequencyList.Add(linkIndex);
                return(@continue);
            }, query);

            // Build matrix of levels on 2D plane (as in optimal variant algorithm)

            var levels = new ulong[width, height];

            var topBottom = 0UL;
            var leftRight = 0UL;
            var bottomTop = 0UL;
            var rightLeft = 0UL;

            var lastX = width - 1;
            var lastY = height - 1;

            // We do not calculate edges to simplify the algorithm
            for (int y = 1; y < lastY; y++)
            {
                for (int x = 1; x < lastX; x++)
                {
                    topBottom = GetFrequency(matrix[x, y - 1].Key[0], matrix[x, y].Key[0]);
                    leftRight = GetFrequency(matrix[x - 1, y].Key[0], matrix[x, y].Key[0]);
                    bottomTop = GetFrequency(matrix[x, y].Key[0], matrix[x, y + 1].Key[0]);
                    rightLeft = GetFrequency(matrix[x, y].Key[0], matrix[x + 1, y].Key[0]);

                    levels[x, y] = Math.Max(Math.Max(topBottom, leftRight), Math.Max(bottomTop, rightLeft));
                }
            }

            // Print levels matrix

            //for (int y = 1; y < lastY; y++)
            //{
            //    for (int x = 1; x < lastX; x++)
            //    {
            //        Console.Write("{0:0000}", levels[x, y]);
            //        Console.Write(' ');
            //    }
            //    Console.WriteLine();
            //}

            // Black and white (split to two colors)

            var contrastedLevels = new ulong[width, height];

            var minimum = ulong.MaxValue;
            var maximum = ulong.MinValue;

            for (int y = 1; y < lastY; y++)
            {
                for (int x = 1; x < lastX; x++)
                {
                    var level = levels[x, y];
                    minimum = minimum > level ? level : minimum;
                }
            }

            for (int y = 1; y < lastY; y++)
            {
                for (int x = 1; x < lastX; x++)
                {
                    var level = levels[x, y];
                    maximum = maximum < level ? level : maximum;
                }
            }

            var middle = (maximum + minimum) / 2;

            for (int y = 1; y < lastY; y++)
            {
                for (int x = 1; x < lastX; x++)
                {
                    contrastedLevels[x, y] = levels[x, y] > middle ? 0UL : 1UL; // the most frequent should be background (zero)
                }
            }

            // Print contrasted levels matrix

            //for (int y = 1; y < lastY; y++)
            //{
            //    for (int x = 1; x < lastX; x++)
            //    {
            //        Console.Write("{0:0}", contrastedLevels[x, y]);
            //    }
            //    Console.WriteLine();
            //}

            var outputImagePath = Path.ChangeExtension(_sourceImagePath, ".bf.png");

            using (var outputImage = new MagickImage(MagickColor.FromRgba(0, 0, 0, 255), width, height))
            {
                var pixels = outputImage.GetPixels();
                for (int y = 1; y < lastY; y++)
                {
                    for (int x = 1; x < lastX; x++)
                    {
                        if (contrastedLevels[x, y] > 0)
                        {
                            pixels.SetPixel(x, y, new byte[] { 255, 255, 255, 255 });
                        }
                    }
                    Console.WriteLine();
                }
                outputImage.Write(outputImagePath);
            }

            // Get the largest repeated patterns with lowest frequency (level)
        }
Exemplo n.º 13
0
 public static TLinkAddress Each <TLinkAddress, TConstants>(this ILinks <TLinkAddress, TConstants> links, Func <IList <TLinkAddress>, TLinkAddress> handler, params TLinkAddress[] restrictions)
     where TConstants : LinksConstants <TLinkAddress>
 => links.Each(handler, restrictions);
Exemplo n.º 14
0
        public static void TestRawNumbersCRUDOperations<T>(this ILinks<T> links)
        {
            // Constants
            var constants = links.Constants;
            var equalityComparer = EqualityComparer<T>.Default;

            var zero = default(T);
            var one = Arithmetic.Increment(zero);
            var two = Arithmetic.Increment(one);

            var h106E = new Hybrid<T>(106L, isExternal: true);
            var h107E = new Hybrid<T>(-char.ConvertFromUtf32(107)[0]);
            var h108E = new Hybrid<T>(-108L);

            Assert.Equal(106L, h106E.AbsoluteValue);
            Assert.Equal(107L, h107E.AbsoluteValue);
            Assert.Equal(108L, h108E.AbsoluteValue);

            // Create Link (External -> External)
            var linkAddress1 = links.Create();

            links.Update(linkAddress1, h106E, h108E);

            var link1 = new Link<T>(links.GetLink(linkAddress1));

            Assert.True(equalityComparer.Equals(link1.Source, h106E));
            Assert.True(equalityComparer.Equals(link1.Target, h108E));

            // Create Link (Internal -> External)
            var linkAddress2 = links.Create();

            links.Update(linkAddress2, linkAddress1, h108E);

            var link2 = new Link<T>(links.GetLink(linkAddress2));

            Assert.True(equalityComparer.Equals(link2.Source, linkAddress1));
            Assert.True(equalityComparer.Equals(link2.Target, h108E));

            // Create Link (Internal -> Internal)
            var linkAddress3 = links.Create();

            links.Update(linkAddress3, linkAddress1, linkAddress2);

            var link3 = new Link<T>(links.GetLink(linkAddress3));

            Assert.True(equalityComparer.Equals(link3.Source, linkAddress1));
            Assert.True(equalityComparer.Equals(link3.Target, linkAddress2));

            // Search for created link
            var setter1 = new Setter<T>(constants.Null);
            links.Each(h106E, h108E, setter1.SetAndReturnFalse);

            Assert.True(equalityComparer.Equals(setter1.Result, linkAddress1));

            // Search for nonexistent link
            var setter2 = new Setter<T>(constants.Null);
            links.Each(h106E, h107E, setter2.SetAndReturnFalse);

            Assert.True(equalityComparer.Equals(setter2.Result, constants.Null));

            // Update link to reference null (prepare for delete)
            var updated = links.Update(linkAddress3, constants.Null, constants.Null);

            Assert.True(equalityComparer.Equals(updated, linkAddress3));

            link3 = new Link<T>(links.GetLink(linkAddress3));

            Assert.True(equalityComparer.Equals(link3.Source, constants.Null));
            Assert.True(equalityComparer.Equals(link3.Target, constants.Null));

            // Delete link
            links.Delete(linkAddress3);

            Assert.True(equalityComparer.Equals(links.Count(), two));

            var setter3 = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter3.SetAndReturnTrue);

            Assert.True(equalityComparer.Equals(setter3.Result, linkAddress2));
        }
Exemplo n.º 15
0
 public static bool Each <TLink>(this ILinks <TLink> links, TLink source, TLink target, Func <IList <TLink>, TLink> handler) => links.Each(handler, links.Constants.Any, source, target);
Exemplo n.º 16
0
        public static bool Each <TLink>(this ILinks <TLink> links, TLink source, TLink target, Func <TLink, bool> handler)
        {
            var constants = links.Constants;

            return(links.Each(link => handler(link[constants.IndexPart]) ? constants.Continue : constants.Break, constants.Any, source, target));
        }
Exemplo n.º 17
0
 public static bool Each <TLink>(this ILinks <TLink> links, Func <IList <TLink>, TLink> handler, params TLink[] restrictions)
 => EqualityComparer <TLink> .Default.Equals(links.Each(handler, restrictions), links.Constants.Continue);
Exemplo n.º 18
0
        public static void TestCRUDOperations<T>(this ILinks<T> links)
        {
            var constants = links.Constants;

            var equalityComparer = EqualityComparer<T>.Default;

            var zero = default(T);
            var one = Arithmetic.Increment(zero);

            // Create Link
            Assert.True(equalityComparer.Equals(links.Count(), zero));

            var setter = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter.SetAndReturnTrue);

            Assert.True(equalityComparer.Equals(setter.Result, constants.Null));

            var linkAddress = links.Create();

            var link = new Link<T>(links.GetLink(linkAddress));

            Assert.True(link.Count == 3);
            Assert.True(equalityComparer.Equals(link.Index, linkAddress));
            Assert.True(equalityComparer.Equals(link.Source, constants.Null));
            Assert.True(equalityComparer.Equals(link.Target, constants.Null));

            Assert.True(equalityComparer.Equals(links.Count(), one));

            // Get first link
            setter = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter.SetAndReturnFalse);

            Assert.True(equalityComparer.Equals(setter.Result, linkAddress));

            // Update link to reference itself
            links.Update(linkAddress, linkAddress, linkAddress);

            link = new Link<T>(links.GetLink(linkAddress));

            Assert.True(equalityComparer.Equals(link.Source, linkAddress));
            Assert.True(equalityComparer.Equals(link.Target, linkAddress));

            // Update link to reference null (prepare for delete)
            var updated = links.Update(linkAddress, constants.Null, constants.Null);

            Assert.True(equalityComparer.Equals(updated, linkAddress));

            link = new Link<T>(links.GetLink(linkAddress));

            Assert.True(equalityComparer.Equals(link.Source, constants.Null));
            Assert.True(equalityComparer.Equals(link.Target, constants.Null));

            // Delete link
            links.Delete(linkAddress);

            Assert.True(equalityComparer.Equals(links.Count(), zero));

            setter = new Setter<T>(constants.Null);
            links.Each(constants.Any, constants.Any, setter.SetAndReturnTrue);

            Assert.True(equalityComparer.Equals(setter.Result, constants.Null));
        }