Beispiel #1
0
        public static bool CheckPathExistance <TLink>(this ILinks <TLink> links, params TLink[] path)
        {
            var current = path[0];

            //EnsureLinkExists(current, "path");
            if (!links.Exists(current))
            {
                return(false);
            }
            var equalityComparer = EqualityComparer <TLink> .Default;
            var constants        = links.Constants;

            for (var i = 1; i < path.Length; i++)
            {
                var next   = path[i];
                var values = links.GetLink(current);
                var source = values[constants.SourcePart];
                var target = values[constants.TargetPart];
                if (equalityComparer.Equals(source, target) && equalityComparer.Equals(source, next))
                {
                    //throw new InvalidOperationException(string.Format("Невозможно выбрать путь, так как и Source и Target совпадают с элементом пути {0}.", next));
                    return(false);
                }
                if (!equalityComparer.Equals(next, source) && !equalityComparer.Equals(next, target))
                {
                    //throw new InvalidOperationException(string.Format("Невозможно продолжить путь через элемент пути {0}", next));
                    return(false);
                }
                current = next;
            }
            return(true);
        }
Beispiel #2
0
        public static bool Equals <TLink>(this ILinks <TLink> links, TLink link, TLink source, TLink target)
        {
            var constants        = links.Constants;
            var values           = links.GetLink(link);
            var equalityComparer = EqualityComparer <TLink> .Default;

            return(equalityComparer.Equals(values[constants.SourcePart], source) && equalityComparer.Equals(values[constants.TargetPart], target));
        }
Beispiel #3
0
 public static bool IsPartialPoint <TLinkAddress, TConstants>(this ILinks <TLinkAddress, TConstants> links, TLinkAddress link)
     where TConstants : LinksConstants <TLinkAddress>
 {
     if (links.Constants.IsExternalReference(link))
     {
         return(true);
     }
     links.EnsureLinkExists(link);
     return(Point <TLinkAddress> .IsPartialPoint(links.GetLink(link)));
 }
Beispiel #4
0
        public static TLink GetByKeys <TLink>(this ILinks <TLink> links, TLink root, params int[] path)
        {
            links.EnsureLinkExists(root, "root");
            var currentLink = root;

            for (var i = 0; i < path.Length; i++)
            {
                currentLink = links.GetLink(currentLink)[path[i]];
            }
            return(currentLink);
        }
Beispiel #5
0
        public static TLink CountUsages <TLink>(this ILinks <TLink> links, TLink link)
        {
            var   constants        = links.Constants;
            var   values           = links.GetLink(link);
            TLink usagesAsSource   = links.Count(new Link <TLink>(constants.Any, link, constants.Any));
            var   equalityComparer = EqualityComparer <TLink> .Default;

            if (equalityComparer.Equals(values[constants.SourcePart], link))
            {
                usagesAsSource = Arithmetic <TLink> .Decrement(usagesAsSource);
            }
            TLink usagesAsTarget = links.Count(new Link <TLink>(constants.Any, constants.Any, link));

            if (equalityComparer.Equals(values[constants.TargetPart], link))
            {
                usagesAsTarget = Arithmetic <TLink> .Decrement(usagesAsTarget);
            }
            return(Arithmetic <TLink> .Add(usagesAsSource, usagesAsTarget));
        }
Beispiel #6
0
        public static TLink GetSquareMatrixSequenceElementByIndex <TLink>(this ILinks <TLink> links, TLink root, ulong size, ulong index)
        {
            var constants = links.Constants;
            var source    = constants.SourcePart;
            var target    = constants.TargetPart;

            if (!Platform.Numbers.Math.IsPowerOfTwo(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), "Sequences with sizes other than powers of two are not supported.");
            }
            var path   = new BitArray(BitConverter.GetBytes(index));
            var length = Bit.GetLowestPosition(size);

            links.EnsureLinkExists(root, "root");
            var currentLink = root;

            for (var i = length - 1; i >= 0; i--)
            {
                currentLink = links.GetLink(currentLink)[path[i] ? target : source];
            }
            return(currentLink);
        }
Beispiel #7
0
        private static void AppendPattern(ILinks <ulong> links, LinksConstants <ulong> constants, ulong start, ulong patternMarker, Dictionary <ulong, char> chars, ulong any, ulong @continue, StringBuilder sb, ulong initialPosition, RawNumberToAddressConverter <ulong> rawNumberToAddressConverter)
        {
            sb.Append('(');
            var alternatives = 0;

            links.Each(linkParts =>
            {
                var link = new UInt64Link(linkParts);
                if (patternMarker == link.Target)
                {
                    if (alternatives > 0)
                    {
                        sb.Append('|');
                    }
                    alternatives++;
                }
                else if (!constants.IsExternalReference(link.Target))
                {
                    var charPosition = new UInt64Link(links.GetLink(link.Target));
                    if (constants.IsExternalReference(charPosition.Source) && chars.TryGetValue(charPosition.Target, out char targetSymbol))
                    {
                        var position = rawNumberToAddressConverter.Convert(charPosition.Source) - 10;
                        if (position == initialPosition)
                        {
                            if (alternatives > 0)
                            {
                                sb.Append('|');
                            }
                            sb.Append(targetSymbol);
                            AppendPattern(links, constants, link.Target, patternMarker, chars, any, @continue, sb, initialPosition + 1, rawNumberToAddressConverter);
                            alternatives++;
                        }
                    }
                }
                return(@continue);
            }, new UInt64Link(any, start, any));
            sb.Append(')');
        }
Beispiel #8
0
 private static string LinkToString(ILinks <ulong> links, LinksConstants <ulong> constants, ulong linkAddress, Dictionary <ulong, char> chars, RawNumberToAddressConverter <ulong> rawNumberToAddressConverter)
 {
     if (chars.TryGetValue(linkAddress, out char @char))
     {
         return($"'{@char.ToString()}'");
     }
     else if (constants.IsExternalReference(linkAddress))
     {
         return(rawNumberToAddressConverter.Convert(linkAddress).ToString());
     }
     else
     {
         var link = new UInt64Link(links.GetLink(linkAddress));
         if (constants.IsExternalReference(link.Source) && chars.TryGetValue(link.Target, out char targetChar))
         {
             return($"[{rawNumberToAddressConverter.Convert(link.Source)}]'{targetChar}'");
         }
         else
         {
             return(linkAddress.ToString());
         }
     }
 }
Beispiel #9
0
 public static TLink GetTarget <TLink>(this ILinks <TLink> links, TLink link) => links.GetLink(link)[links.Constants.TargetPart];
Beispiel #10
0
 public static TLink GetSource <TLink>(this ILinks <TLink> links, TLink link) => links.GetLink(link)[links.Constants.SourcePart];
        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));
        }
        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));
        }
Beispiel #13
0
 private ulong GetFrequency(ulong link) => GetFrequency(_links.GetLink(link));
Beispiel #14
0
 public static void AppendStructure(this ILinks <ulong> links, StringBuilder sb, HashSet <ulong> visited, ulong linkIndex, Func <Link <ulong>, bool> isElement, Action <StringBuilder, Link <ulong> > appendElement, bool renderIndex = false, bool renderDebug = false)
 {
     if (sb == null)
     {
         throw new ArgumentNullException(nameof(sb));
     }
     if (linkIndex == Constants.Null || linkIndex == Constants.Any || linkIndex == Constants.Itself)
     {
         return;
     }
     if (links.Exists(linkIndex))
     {
         if (visited.Add(linkIndex))
         {
             sb.Append('(');
             var link = new Link <ulong>(links.GetLink(linkIndex));
             if (renderIndex)
             {
                 sb.Append(link.Index);
                 sb.Append(':');
             }
             if (link.Source == link.Index)
             {
                 sb.Append(link.Index);
             }
             else
             {
                 var source = new Link <ulong>(links.GetLink(link.Source));
                 if (isElement(source))
                 {
                     appendElement(sb, source);
                 }
                 else
                 {
                     links.AppendStructure(sb, visited, source.Index, isElement, appendElement, renderIndex);
                 }
             }
             sb.Append(' ');
             if (link.Target == link.Index)
             {
                 sb.Append(link.Index);
             }
             else
             {
                 var target = new Link <ulong>(links.GetLink(link.Target));
                 if (isElement(target))
                 {
                     appendElement(sb, target);
                 }
                 else
                 {
                     links.AppendStructure(sb, visited, target.Index, isElement, appendElement, renderIndex);
                 }
             }
             sb.Append(')');
         }
         else
         {
             if (renderDebug)
             {
                 sb.Append('*');
             }
             sb.Append(linkIndex);
         }
     }
     else
     {
         if (renderDebug)
         {
             sb.Append('~');
         }
         sb.Append(linkIndex);
     }
 }