Exemplo n.º 1
0
        public IEnumerable <string> Serialize(ListRand list)
        {
            var idMappings = GenerateIdMappings(list);

            var converter      = new ListRandConverter(idMappings);
            var serializedList = SerializeListAsStream(list, idMappings);

            return(serializedList);
        }
Exemplo n.º 2
0
        private IEnumerable <string> SerializeListAsStream(ListRand list, IReadOnlyDictionary <ListNode, int> idMappings)
        {
            var listConverter = new ListRandConverter(idMappings);

            yield return(listConverter.ToString(list));

            var serializedNodes = SerializeNodesAsStream(idMappings, list.Head);

            foreach (var serializedNode in serializedNodes)
            {
                yield return(serializedNode);
            }

            yield return(listConverter.GetStringOfClosePart());
        }
Exemplo n.º 3
0
        public string ToString(ListRand source)
        {
            var result = new StringBuilder();

            result.AppendLine(Constants.ObjectSymbols.Start);
            var headStr = LinkToNodeToString($"{nameof(source.Head)}:{{0}}", source.Head);

            result.AppendLine(headStr);
            var tailStr = LinkToNodeToString($"{nameof(source.Tail)}:{{0}}", source.Tail);

            result.AppendLine(tailStr);
            result.AppendLine($"{nameof(source.Count)}:" + source.Count);
            result.AppendLine($"{Constants.ObjectSymbols.ListRand.Nodes}: {Constants.ArraySymbols.Start}");

            return(result.ToString());
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var headNode = new ListNode()
            {
                Data = "Head \r\n \"Hello world\" \r\nprev: 0\r\n node"
            };

            var node2 = new ListNode()
            {
                Data = "Second node"
            };

            headNode.Next = node2;
            node2.Prev    = headNode;

            var tailNode = new ListNode()
            {
                Data = "Tail node"
            };

            node2.Next    = tailNode;
            tailNode.Prev = node2;

            headNode.Rand = tailNode;
            tailNode.Rand = node2;

            var list = new ListRand()
            {
                Head  = headNode,
                Tail  = tailNode,
                Count = 3
            };

            using (var fs = File.Open("test.txt", FileMode.OpenOrCreate))
            {
                list.Serialize(fs);
            }

            var deserialized = new ListRand();

            using (var fs = File.Open("test.txt", FileMode.Open))
            {
                deserialized.Deserialize(fs);
            }
        }
Exemplo n.º 5
0
        private IReadOnlyDictionary <ListNode, int> GenerateIdMappings(ListRand list)
        {
            var headNode   = list.Head;
            var startId    = 0;
            var idMappings = new Dictionary <ListNode, int>(list.Count);
            var currNode   = headNode;

            while (currNode != null)
            {
                var refs = new List <ListNode> {
                    headNode, headNode.Rand, headNode.Prev, headNode.Next
                }
                .Where(x => x != null)
                .ToList();
                startId = refs.Aggregate(startId, (currId, x) => GetOrAddIdMapping(idMappings, x, currId));

                currNode = currNode.Next;
            }

            return(idMappings);
        }
Exemplo n.º 6
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);
        }