Exemplo n.º 1
0
        public async Task <ListNode> DeepCopy(ListNode head)
        {
            Debug.Assert(head.Previous == null, "Head node should be passed to this method.");
            var nodePacker   = new ListNodePacker();
            var cloningTasks = GetAllNodes(head)
                               .Select(node => nodePacker.ToPackedListNode(node))
                               .Select(node => nodePacker.RestoreNodeAsync(node));
            var clonedNodes = await Task.WhenAll(cloningTasks);

            return(clonedNodes[0]);
        }
Exemplo n.º 2
0
        public async Task <ListNode> Deserialize(Stream s)
        {
            var buffer = await ReadBufferFromStream(s);

            var nodePacker = new ListNodePacker();
            var nodesTasks = ReadPackedNodesFromBuffer(nodePacker, buffer)
                             .Select(node => nodePacker.RestoreNodeAsync(node));
            var nodes = await Task.WhenAll(nodesTasks);

            return(nodes[0]);
        }
Exemplo n.º 3
0
        private static IEnumerable <PackedListNode> ReadPackedNodesFromBuffer(ListNodePacker packer, byte[] buffer)
        {
            var initialOffset = 0;

            while (initialOffset < buffer.Length)
            {
                var packedNode = packer.FromBuffer(buffer, initialOffset);
                yield return(packedNode);

                initialOffset += packedNode.ByteSize;
            }
        }
Exemplo n.º 4
0
        public async Task Serialize(ListNode head, Stream s)
        {
            Debug.Assert(head.Previous == null, "Head node should be passed to this method.");
            var nodePacker = new ListNodePacker();
            var tasks      = GetAllNodes(head)
                             .Select(nodePacker.ToBytesAsync);
            var conversionTask = await Task.WhenAll(tasks);

            var bytes = ByteArrayHelper.Combine(conversionTask);

            s.Write(bytes);
            s.Position = 0;
        }