private static void GenerateAlphabetBasis(ref Link alphabet, ref Link letterOfAlphabet, Link[] letters)
        {
            // Принцип, на примере латинского алфавита.
            //latin alphabet: alphabet that consists of a and b and c and ... and z.
            //a: letter of latin alphabet that is before b.
            //b: letter of latin alphabet that is between (a and c).
            //c: letter of latin alphabet that is between (b and e).
            //...
            //y: letter of latin alphabet that is between (x and z).
            //z: letter of latin alphabet that is after y.
            const int firstLetterIndex = 0;

            for (var i = firstLetterIndex; i < letters.Length; i++)
            {
                letters[i] = Net.CreateThing();
            }
            var lastLetterIndex = letters.Length - 1;

            Link.Update(ref letters[firstLetterIndex], letterOfAlphabet, Net.ThatIsBefore, letters[firstLetterIndex + 1]);
            Link.Update(ref letters[lastLetterIndex], letterOfAlphabet, Net.ThatIsAfter, letters[lastLetterIndex - 1]);
            const int secondLetterIndex = firstLetterIndex + 1;

            for (var i = secondLetterIndex; i < lastLetterIndex; i++)
            {
                Link.Update(ref letters[i], letterOfAlphabet, Net.ThatIsBetween, letters[i - 1] & letters[i + 1]);
            }
            Link.Update(ref alphabet, Net.Alphabet, Net.ThatConsistsOf, LinkConverter.FromList(letters));
        }
Ejemplo n.º 2
0
        public static Link FromNumber(long number)
        {
            if (number == 0)
            {
                return(Net.Zero);
            }
            if (number == 1)
            {
                return(Net.One);
            }
            var links = new Link[Bit.Count(number)];

            if (number >= 0)
            {
                for (long key = 1, powerOf2 = 0, i = 0; key <= number; key *= 2, powerOf2++)
                {
                    if ((number & key) == key)
                    {
                        links[i] = FromPowerOf2(powerOf2);
                        i++;
                    }
                }
            }
            else
            {
                throw new NotSupportedException("Negative numbers are not supported yet.");
            }
            var sum = Link.Create(Net.Sum, Net.Of, LinkConverter.FromList(links));

            return(sum);
        }
Ejemplo n.º 3
0
        public static bool TryGetName(this Link link, out string str)
        {
            // Защита от зацикливания
            if (!_linksWithNamesGatheringProcess.Add(link))
            {
                str = "...";
                return(true);
            }
            try
            {
                if (link != null)
                {
                    if (link.Linker == Net.And)
                    {
                        str = SequenceHelpers.FormatSequence(link);
                        return(true);
                    }
                    else if (link.IsGroup())
                    {
                        str = LinkConverter.ToString(LinkConverter.ToList(link.Target));
                        return(true);
                    }
                    else if (link.IsChar())
                    {
                        str = LinkConverter.ToChar(link).ToString();
                        return(true);
                    }
                    else if (link.TryGetSpecificName(out str))
                    {
                        return(true);
                    }

                    if (link.Source == link || link.Linker == link || link.Target == link)
                    {
                        return(false);
                    }

                    if (link.Source.TryGetName(out string sourceName) && link.Linker.TryGetName(out string linkerName) && link.Target.TryGetName(out string targetName))
                    {
                        var sb = new StringBuilder();
                        sb.Append(sourceName).Append(' ').Append(linkerName).Append(' ').Append(targetName);
                        str = sb.ToString();
                        return(true);
                    }
                }
                str = null;
                return(false);
            }
            finally
            {
                _linksWithNamesGatheringProcess.Remove(link);
            }
        }
 private static void RegisterExistingCharacter(Link character)
 {
     if (character.Source == Net.Character && character.Linker == Net.ThatHas)
     {
         var code = character.Target;
         if (code.Source == Net.Code && code.Linker == Net.ThatIsRepresentedBy)
         {
             var charCode = (char)LinkConverter.ToNumber(code.Target);
             _charactersToLinks[charCode]  = character;
             _linksToCharacters[character] = charCode;
         }
     }
 }
 private static void RegisterExistingLetter(Link letter)
 {
     letter.WalkThroughReferersAsSource(referer =>
     {
         if (referer.Linker == Net.Has)
         {
             var target = referer.Target;
             if (target.Source == Net.Code && target.Linker == Net.ThatIsRepresentedBy)
             {
                 var charCode = (char)LinkConverter.ToNumber(target.Target);
                 _charactersToLinks[charCode] = letter;
                 _linksToCharacters[letter]   = charCode;
             }
         }
     });
 }
Ejemplo n.º 6
0
        public static bool TryGetSpecificName(this Link link, out string name)
        {
            string nameLocal = null;

            if (Net.Name.ReferersBySourceCount < link.ReferersBySourceCount)
            {
                Net.Name.WalkThroughReferersAsSource(referer =>
                {
                    if (referer.Linker == Net.ThatIsRepresentedBy)
                    {
                        if (Link.Exists(link, Net.Has, referer))
                        {
                            nameLocal = LinkConverter.ToString(referer.Target);
                            return(false); // Останавливаем проход
                        }
                    }
                    return(true);
                });
            }
            else
            {
                link.WalkThroughReferersAsSource(referer =>
                {
                    if (referer.Linker == Net.Has)
                    {
                        var nameLink = referer.Target;
                        if (nameLink.Source == Net.Name && nameLink.Linker == Net.ThatIsRepresentedBy)
                        {
                            nameLocal = LinkConverter.ToString(nameLink.Target);
                            return(false); // Останавливаем проход
                        }
                    }
                    return(true);
                });
            }

            name = nameLocal;
            return(nameLocal != null);
        }
Ejemplo n.º 7
0
 public static long ToNumber(Link link)
 {
     if (link == Net.Zero)
     {
         return(0);
     }
     if (link == Net.One)
     {
         return(1);
     }
     if (link.IsSum())
     {
         var  numberParts = LinkConverter.ToList(link.Target);
         long number      = 0;
         for (var i = 0; i < numberParts.Count; i++)
         {
             GoDownAndTakeIt(numberParts[i], out long numberPart);
             number += numberPart;
         }
         return(number);
     }
     throw new ArgumentOutOfRangeException(nameof(link), "Specified link is not a number.");
 }
Ejemplo n.º 8
0
 public static Link SetName(this Link link, string name)
 {
     Link.Create(link, Net.Has, Link.Create(Net.Name, Net.ThatIsRepresentedBy, LinkConverter.FromString(name)));
     return(link); // Chaining
 }
 private static Link CreateSimpleCharacterLink(char character) => Link.Create(Net.Character, Net.ThatHas, Link.Create(Net.Code, Net.ThatIsRepresentedBy, LinkConverter.FromNumber(character)));
Ejemplo n.º 10
0
        private static void SetLetterCodes(Link letter, char lowerCaseCharacter, out Link lowerCase, out Link upperCase)
        {
            var upperCaseCharacter = char.ToUpper(lowerCaseCharacter);

            if (upperCaseCharacter != lowerCaseCharacter)
            {
                lowerCase = Link.Create(Net.LowerCase, Net.Of, letter);
                var lowerCaseCharacterCode = Link.Create(Net.Code, Net.ThatIsRepresentedBy, LinkConverter.FromNumber(lowerCaseCharacter));
                Link.Create(lowerCase, Net.Has, lowerCaseCharacterCode);
                upperCase = Link.Create(Net.UpperCase, Net.Of, letter);
                var upperCaseCharacterCode = Link.Create(Net.Code, Net.ThatIsRepresentedBy, LinkConverter.FromNumber(upperCaseCharacter));
                Link.Create(upperCase, Net.Has, upperCaseCharacterCode);
            }
            else
            {
                lowerCase = letter;
                upperCase = null;
                Link.Create(letter, Net.Has, Link.Create(Net.Code, Net.ThatIsRepresentedBy, LinkConverter.FromNumber(lowerCaseCharacter)));
            }
        }