Example #1
0
        public ListRand Deserialize(IEnumerable <string> fileLines)
        {
            var idMappings      = new Dictionary <int, ListNode>();
            var fileLinesStream = new FileLinesStream(fileLines);
            var converter       = new ListRandConverter();
            var list            = converter.ToListRand(fileLinesStream, idMappings);

            return(list);
        }
Example #2
0
        public ListNode ToNode(FileLinesStream fileLines, Dictionary <int, ListNode> idMappings)
        {
            var line = fileLines.Peek();

            if (line != Constants.ObjectSymbols.Start)
            {
                return(null);
            }

            var node = new ListNode();

            line = fileLines.Next();
            while (line != null)
            {
                var(name, value) = Split(line);

                switch (name)
                {
                case Constants.ObjectSymbols.ListNode.Id:
                    if (string.IsNullOrEmpty(value))
                    {
                        throw new FileHasIncorrectFormat();
                    }
                    node = ToLinkFromString(value, idMappings);
                    break;

                case nameof(node.Prev):
                    node.Prev = ToLinkFromString(value, idMappings);
                    break;

                case nameof(node.Next):
                    node.Next = ToLinkFromString(value, idMappings);
                    break;

                case nameof(node.Rand):
                    node.Rand = ToLinkFromString(value, idMappings);
                    break;

                case nameof(node.Data):
                    node.Data = value.FromLiteral();
                    break;

                case Constants.ObjectSymbols.End:
                    fileLines.Next();
                    return(node);

                default:
                    throw new FileHasIncorrectFormat();
                }

                line = fileLines.Next();
            }

            return(node);
        }
Example #3
0
        public ListRand ToListRand(FileLinesStream fileLines, Dictionary <int, ListNode> idMappings)
        {
            var element = fileLines.Peek();

            if (element != Constants.ObjectSymbols.Start)
            {
                return(null);
            }

            var list = new ListRand();

            var line = fileLines.Next();

            while (line != null)
            {
                var(name, value) = Split(line);

                switch (name)
                {
                case nameof(list.Head):
                    list.Head = ToLink(value, idMappings);
                    break;

                case nameof(list.Tail):
                    list.Tail = ToLink(value, idMappings);
                    break;

                case nameof(list.Count):
                    list.Count = int.Parse(value);
                    break;

                case Constants.ObjectSymbols.ListRand.Nodes:
                    if (value != Constants.ArraySymbols.Start)
                    {
                        throw new FileHasIncorrectFormat();
                    }

                    fileLines.Next();
                    ListNode node = null;
                    do
                    {
                        var nodeConverter = new ListNodeConverter();
                        node = nodeConverter.ToNode(fileLines, idMappings);
                    } while (node != null);

                    var command = fileLines.Peek();
                    if (command != Constants.ArraySymbols.End)
                    {
                        throw new FileHasIncorrectFormat();
                    }
                    break;

                case Constants.ObjectSymbols.End:
                    fileLines.Next();
                    return(list);

                default:
                    throw new FileHasIncorrectFormat();
                }

                line = fileLines.Next();
            }

            return(list);
        }