Ejemplo n.º 1
0
        public static void EnsureCreated <TLink>(this ILinks <TLink> links, Func <TLink> creator, params TLink[] addresses)
        {
            var addressToUInt64Converter = CheckedConverter <TLink, ulong> .Default;
            var uInt64ToAddressConverter = CheckedConverter <ulong, TLink> .Default;
            var nonExistentAddresses     = new HashSet <TLink>(addresses.Where(x => !links.Exists(x)));

            if (nonExistentAddresses.Count > 0)
            {
                var max = nonExistentAddresses.Max();
                max = uInt64ToAddressConverter.Convert(System.Math.Min(addressToUInt64Converter.Convert(max), addressToUInt64Converter.Convert(links.Constants.InternalReferencesRange.Maximum)));
                var   createdLinks     = new List <TLink>();
                var   equalityComparer = EqualityComparer <TLink> .Default;
                TLink createdLink      = creator();
                while (!equalityComparer.Equals(createdLink, max))
                {
                    createdLinks.Add(createdLink);
                }
                for (var i = 0; i < createdLinks.Count; i++)
                {
                    if (!nonExistentAddresses.Contains(createdLinks[i]))
                    {
                        links.Delete(createdLinks[i]);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public static void EnsureInnerReferenceExists <TLink>(this ILinks <TLink> links, TLink reference, string argumentName)
 {
     if (links.Constants.IsInternalReference(reference) && !links.Exists(reference))
     {
         throw new ArgumentLinkDoesNotExistsException <TLink>(reference, argumentName);
     }
 }
Ejemplo n.º 3
0
 public static void EnsureDoesNotExists <TLink>(this ILinks <TLink> links, TLink source, TLink target)
 {
     if (links.Exists(source, target))
     {
         throw new LinkWithSameValueAlreadyExistsException();
     }
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 public static void EnsureLinkExists <TLinkAddress, TConstants>(this ILinks <TLinkAddress, TConstants> links, TLinkAddress link, string argumentName)
     where TConstants : LinksConstants <TLinkAddress>
 {
     if (!links.Exists(link))
     {
         throw new ArgumentLinkDoesNotExistsException <TLinkAddress>(link, argumentName);
     }
 }
Ejemplo n.º 6
0
        public static void EnsureLinkIsItselfOrExists <TLink>(this ILinks <TLink> links, TLink link, string argumentName)
        {
            var equalityComparer = EqualityComparer <TLink> .Default;

            if (!equalityComparer.Equals(link, links.Constants.Itself) && !links.Exists(link))
            {
                throw new ArgumentLinkDoesNotExistsException <TLink>(link, argumentName);
            }
        }
Ejemplo n.º 7
0
 public static void EnsureLinkExists <TLink>(this ILinks <TLink> links, IList <TLink> restrictions)
 {
     for (var i = 0; i < restrictions.Count; i++)
     {
         if (!links.Exists(restrictions[i]))
         {
             throw new ArgumentLinkDoesNotExistsException <TLink>(restrictions[i], $"sequence[{i}]");
         }
     }
 }
 private bool ObjectContainsAllTags(uint @object, IList <uint> tags)
 {
     for (var i = 0; i < tags.Count; i++)
     {
         if (!_links.Exists(@object, tags[i]))
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 9
0
        public static void EnsureLinkIsAnyOrExists <TLink>(this ILinks <TLink> links, IList <TLink> restrictions)
        {
            var equalityComparer = EqualityComparer <TLink> .Default;
            var any = links.Constants.Any;

            for (var i = 0; i < restrictions.Count; i++)
            {
                if (!equalityComparer.Equals(restrictions[i], any) && !links.Exists(restrictions[i]))
                {
                    throw new ArgumentLinkDoesNotExistsException <TLink>(restrictions[i], $"sequence[{i}]");
                }
            }
        }
Ejemplo n.º 10
0
        public static string FromSequenceLinkToString(ulong link, ILinks<ulong> links)
        {
            var sb = new StringBuilder();

            if (links.Exists(link))
            {
                StopableSequenceWalker.WalkRight(link, links.GetSource, links.GetTarget,
                    x => x <= MapSize || links.GetSource(x) == x || links.GetTarget(x) == x, element =>
                    {
                        sb.Append(FromLinkToChar(element));
                        return true;
                    });
            }

            return sb.ToString();
        }
Ejemplo n.º 11
0
        // TODO: Реализовать компактификацию при чтении
        //public bool EnforceSingleSequenceVersionOnRead; 
        //public bool UseRequestMarker;
        //public bool StoreRequestResults;

        public void InitOptions(ILinks<ulong> links)
        {
            if (UseSequenceMarker)
            {
                if (SequenceMarkerLink == Constants.Null)
                    SequenceMarkerLink = links.CreatePoint();
                else
                {
                    if (!links.Exists(SequenceMarkerLink))
                    {
                        var link = links.CreatePoint();
                        if(link != SequenceMarkerLink)
                            throw new Exception("Cannot recreate sequence marker link.");
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public static void TestMultipleRandomCreationsAndDeletions<TLink>(this ILinks<TLink> links, int maximumOperationsPerCycle)
 {
     var comparer = Comparer<TLink>.Default;
     var addressToUInt64Converter = CheckedConverter<TLink, ulong>.Default;
     var uInt64ToAddressConverter = CheckedConverter<ulong, TLink>.Default;
     for (var N = 1; N < maximumOperationsPerCycle; N++)
     {
         var random = new System.Random(N);
         var created = 0UL;
         var deleted = 0UL;
         for (var i = 0; i < N; i++)
         {
             var linksCount = addressToUInt64Converter.Convert(links.Count());
             var createPoint = random.NextBoolean();
             if (linksCount >= 2 && createPoint)
             {
                 var linksAddressRange = new Range<ulong>(1, linksCount);
                 TLink source = uInt64ToAddressConverter.Convert(random.NextUInt64(linksAddressRange));
                 TLink target = uInt64ToAddressConverter.Convert(random.NextUInt64(linksAddressRange)); //-V3086
                 var resultLink = links.GetOrCreate(source, target);
                 if (comparer.Compare(resultLink, uInt64ToAddressConverter.Convert(linksCount)) > 0)
                 {
                     created++;
                 }
             }
             else
             {
                 links.Create();
                 created++;
             }
         }
         Assert.True(created == addressToUInt64Converter.Convert(links.Count()));
         for (var i = 0; i < N; i++)
         {
             TLink link = uInt64ToAddressConverter.Convert((ulong)i + 1UL);
             if (links.Exists(link))
             {
                 links.Delete(link);
                 deleted++;
             }
         }
         Assert.True(addressToUInt64Converter.Convert(links.Count()) == 0L);
     }
 }
Ejemplo n.º 13
0
 private TLinkAddress GetOrCreateMeaningRoot(TLinkAddress meaningRootIndex) => _links.Exists(meaningRootIndex) ? meaningRootIndex : _links.CreatePoint();
Ejemplo n.º 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);
     }
 }