Exemplo n.º 1
0
        static public long ToNumber(Link link)
        {
            if (link == Net.Zero)
            {
                return(0);
            }
            else if (link == Net.One)
            {
                return(1);
            }
            else if (link.IsSum())
            {
                List <Link> numberParts = LinkConverter.ToList(link.Target);

                long number = 0;
                for (int i = 0; i < numberParts.Count; i++)
                {
                    long numberPart;
                    GoDownAndTakeIt(numberParts[i], out numberPart);
                    number += numberPart;
                }

                return(number);
            }
            else
            {
                throw new ArgumentOutOfRangeException("link", "Specified link is not a number.");
            }
        }
Exemplo n.º 2
0
        static public Link FromNumber(long number)
        {
            if (number == 0)
            {
                return(Net.Zero);
            }
            else if (number == 1)
            {
                return(Net.One);
            }
            else
            {
                Link[] links = new Link[CountBits(number)];

                if (number >= 0)
                {
                    for (long key = 1, powerOf2 = 0, i = 0; key <= number; key += key, powerOf2++)
                    {
                        if ((number & key) == key)
                        {
                            links[i++] = FromPowerOf2(powerOf2);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("Negative numbers are not supported yet.");
                }

                Link sum = Link.Create(Net.Sum, Net.Of, LinkConverter.FromList(links));
                return(sum);
            }
        }
Exemplo n.º 3
0
        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.

            int firstLetterIndex = 0;

            for (int i = firstLetterIndex; i < letters.Length; i++)
            {
                letters[i] = Net.CreateThing();
            }

            int 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]);

            int secondLetterIndex = firstLetterIndex + 1;

            for (int 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));
        }
Exemplo n.º 4
0
        static public bool TryGetName(this Link link, out string str)
        {
            if (!LinksWithNamesGatheringProcess.Add(link))
            {
                str = "...";
                return(true);
            }
            else
            {
                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 (TryGetSpecificName(link, out str))
                        {
                            return(true);
                        }

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

                        string sourceName, linkerName, targetName;
                        if (TryGetName(link.Source, out sourceName) && TryGetName(link.Linker, out linkerName) && TryGetName(link.Target, out 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);
                }
            }
        }
Exemplo n.º 5
0
        private static void RegisterExistingLetter(Link letter)
        {
            letter.WalkThroughReferersBySource(referer =>
            {
                if (referer.Linker == Net.Has)
                {
                    Link target = referer.Target;
                    if (target.Source == Net.Code && target.Linker == Net.ThatIsRepresentedBy)
                    {
                        char charCode = (char)LinkConverter.ToNumber(target.Target);

                        CharactersToLinks[charCode] = letter;
                        LinksToCharacters[letter]   = charCode;
                    }
                }
            });
        }
Exemplo n.º 6
0
        static public bool TryGetSpecificName(this Link link, out string name)
        {
            string nameLocal = null;

            if (Net.Name.ReferersBySourceCount < link.ReferersBySourceCount)
            {
                Net.Name.WalkThroughReferersBySource(referer =>
                {
                    if (referer.Linker == Net.ThatIsRepresentedBy)
                    {
                        if (Link.Exists(link, Net.Has, referer))
                        {
                            nameLocal = LinkConverter.ToString(referer.Target);
                            return(false);                            // Останавливаем проход
                        }
                    }
                    return(true);
                });
            }
            else
            {
                link.WalkThroughReferersBySource(referer =>
                {
                    if (referer.Linker == Net.Has)
                    {
                        Link 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);
        }
Exemplo n.º 7
0
 static private Link CreateSimpleCharacterLink(char character)
 {
     return(Link.Create(Net.Character, Net.ThatHas, Link.Create(Net.Code, Net.ThatIsRepresentedBy, LinkConverter.FromNumber(character))));
 }
Exemplo n.º 8
0
        static private void SetLetterCodes(Link letter, char lowerCaseCharacter, out Link lowerCase, out Link upperCase)
        {
            char upperCaseCharacter = char.ToUpper(lowerCaseCharacter);

            if (upperCaseCharacter != lowerCaseCharacter)
            {
                lowerCase = Link.Create(Net.LowerCase, Net.Of, letter);
                Link lowerCaseCharacterCode = Link.Create(Net.Code, Net.ThatIsRepresentedBy, LinkConverter.FromNumber(lowerCaseCharacter));

                Link.Create(lowerCase, Net.Has, lowerCaseCharacterCode);

                upperCase = Link.Create(Net.UpperCase, Net.Of, letter);
                Link 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)));
            }
        }
Exemplo n.º 9
0
 static public Link SetName(this Link link, string name)
 {
     Link.Create(link, Net.Has, Link.Create(Net.Name, Net.ThatIsRepresentedBy, LinkConverter.FromString(name)));
     return(link);            // Chaining
 }