Example #1
0
 public void AddLast(DataBlock block)
 {
     if (FirstBlock == null)
     {
         AddBlockToEmptyMap(block);
     }
     else
     {
         AddAfterInternal(GetLastBlock(), block);
     }
 }
Example #2
0
 public void AddFirst(DataBlock block)
 {
     if (FirstBlock == null)
     {
         AddBlockToEmptyMap(block);
     }
     else
     {
         AddBeforeInternal(FirstBlock, block);
     }
 }
Example #3
0
        public void InsertBytes(long index, byte[] bs)
        {
            try
            {
                // Find the block affected.
                long      blockOffset;
                DataBlock block = GetDataBlock(index, out blockOffset);

                // If the insertion point is in a memory block, just insert it.
                var memoryBlock = block as MemoryDataBlock;
                if (memoryBlock != null)
                {
                    memoryBlock.InsertBytes(index - blockOffset, bs);
                    return;
                }

                var fileBlock = (FileDataBlock)block;

                // If the insertion point is at the start of a file block, and the previous block is a memory block, append it to that block.
                if (blockOffset == index && block.PreviousBlock != null)
                {
                    var previousMemoryBlock = block.PreviousBlock as MemoryDataBlock;
                    if (previousMemoryBlock != null)
                    {
                        previousMemoryBlock.InsertBytes(previousMemoryBlock.Length, bs);
                        return;
                    }
                }

                // Split the block into a prefix and a suffix and place a memory block in-between.
                FileDataBlock prefixBlock = null;
                if (index > blockOffset)
                {
                    prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset);
                }

                FileDataBlock suffixBlock = null;
                if (index < blockOffset + fileBlock.Length)
                {
                    suffixBlock = new FileDataBlock(
                        fileBlock.FileOffset + index - blockOffset,
                        fileBlock.Length - (index - blockOffset));
                }

                block = _dataMap.Replace(block, new MemoryDataBlock(bs));

                if (prefixBlock != null)
                {
                    _dataMap.AddBefore(block, prefixBlock);
                }

                if (suffixBlock != null)
                {
                    _dataMap.AddAfter(block, suffixBlock);
                }
            }
            finally
            {
                Length += bs.Length;
                OnLengthChanged(EventArgs.Empty);
                OnChanged(EventArgs.Empty);
            }
        }
Example #4
0
 public void Remove(DataBlock block)
 {
     RemoveInternal(block);
 }
Example #5
0
 public DataBlock Replace(DataBlock block, DataBlock newBlock)
 {
     AddAfterInternal(block, newBlock);
     RemoveInternal(block);
     return newBlock;
 }
Example #6
0
 private static void InvalidateBlock(DataBlock block)
 {
     block.Map           = null;
     block.NextBlock     = null;
     block.PreviousBlock = null;
 }
Example #7
0
 public void AddBefore(DataBlock block, DataBlock newBlock)
 {
     AddBeforeInternal(block, newBlock);
 }
Example #8
0
 internal Enumerator(DataMap map)
 {
     _map = map;
     _version = map._version;
     _current = null;
     _index = -1;
 }
Example #9
0
            public bool MoveNext()
            {
                if (_version != _map._version)
                {
                    throw new InvalidOperationException("Collection was modified after the enumerator was instantiated.");
                }

                if (_index >= _map.Count)
                {
                    return false;
                }

                if (++_index == 0)
                {
                    _current = _map.FirstBlock;
                }
                else
                {
                    _current = _current.NextBlock;
                }

                return (_index < _map.Count);
            }
Example #10
0
 private static void InvalidateBlock(DataBlock block)
 {
     block.Map = null;
     block.NextBlock = null;
     block.PreviousBlock = null;
 }
Example #11
0
        private void AddBlockToEmptyMap(DataBlock block)
        {
            block.Map = this;
            block.NextBlock = null;
            block.PreviousBlock = null;

            FirstBlock = block;
            _version++;
            Count++;
        }
Example #12
0
        private void RemoveInternal(DataBlock block)
        {
            DataBlock previousBlock = block.PreviousBlock;
            DataBlock nextBlock = block.NextBlock;

            if (previousBlock != null)
            {
                previousBlock.NextBlock = nextBlock;
            }

            if (nextBlock != null)
            {
                nextBlock.PreviousBlock = previousBlock;
            }

            if (FirstBlock == block)
            {
                FirstBlock = nextBlock;
            }

            InvalidateBlock(block);

            Count--;
            _version++;
        }
Example #13
0
        private void AddBeforeInternal(DataBlock block, DataBlock newBlock)
        {
            newBlock.NextBlock = block;
            newBlock.PreviousBlock = block.PreviousBlock;
            newBlock.Map = this;

            if (block.PreviousBlock != null)
            {
                block.PreviousBlock.NextBlock = newBlock;
            }
            block.PreviousBlock = newBlock;

            if (FirstBlock == block)
            {
                FirstBlock = newBlock;
            }
            _version++;
            Count++;
        }
Example #14
0
        private void AddAfterInternal(DataBlock block, DataBlock newBlock)
        {
            newBlock.PreviousBlock = block;
            newBlock.NextBlock = block.NextBlock;
            newBlock.Map = this;

            if (block.NextBlock != null)
            {
                block.NextBlock.PreviousBlock = newBlock;
            }
            block.NextBlock = newBlock;

            _version++;
            Count++;
        }
Example #15
0
        /// <summary>
        ///     See <see cref="IByteProvider.WriteByte" /> for more information.
        /// </summary>
        public void WriteByte(long index, byte value)
        {
            try
            {
                // Find the block affected.
                long      blockOffset;
                DataBlock block = GetDataBlock(index, out blockOffset);

                // If the byte is already in a memory block, modify it.
                var memoryBlock = block as MemoryDataBlock;
                if (memoryBlock != null)
                {
                    memoryBlock.Data[index - blockOffset] = value;
                    return;
                }

                var fileBlock = (FileDataBlock)block;

                // If the byte changing is the first byte in the block and the previous block is a memory block, extend that.
                if (blockOffset == index && block.PreviousBlock != null)
                {
                    var previousMemoryBlock = block.PreviousBlock as MemoryDataBlock;
                    if (previousMemoryBlock != null)
                    {
                        previousMemoryBlock.AddByteToEnd(value);
                        fileBlock.RemoveBytesFromStart(1);
                        if (fileBlock.Length == 0)
                        {
                            _dataMap.Remove(fileBlock);
                        }
                        return;
                    }
                }

                // If the byte changing is the last byte in the block and the next block is a memory block, extend that.
                if (blockOffset + fileBlock.Length - 1 == index && block.NextBlock != null)
                {
                    var nextMemoryBlock = block.NextBlock as MemoryDataBlock;
                    if (nextMemoryBlock != null)
                    {
                        nextMemoryBlock.AddByteToStart(value);
                        fileBlock.RemoveBytesFromEnd(1);
                        if (fileBlock.Length == 0)
                        {
                            _dataMap.Remove(fileBlock);
                        }
                        return;
                    }
                }

                // Split the block into a prefix and a suffix and place a memory block in-between.
                FileDataBlock prefixBlock = null;
                if (index > blockOffset)
                {
                    prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset);
                }

                FileDataBlock suffixBlock = null;
                if (index < blockOffset + fileBlock.Length - 1)
                {
                    suffixBlock = new FileDataBlock(
                        fileBlock.FileOffset + index - blockOffset + 1,
                        fileBlock.Length - (index - blockOffset + 1));
                }

                block = _dataMap.Replace(block, new MemoryDataBlock(value));

                if (prefixBlock != null)
                {
                    _dataMap.AddBefore(block, prefixBlock);
                }

                if (suffixBlock != null)
                {
                    _dataMap.AddAfter(block, suffixBlock);
                }
            }
            finally
            {
                OnChanged(EventArgs.Empty);
            }
        }
Example #16
0
            void IEnumerator.Reset()
            {
                if (_version != _map._version)
                {
                    throw new InvalidOperationException("Collection was modified after the enumerator was instantiated.");
                }

                _index = -1;
                _current = null;
            }
Example #17
0
 public DataBlock Replace(DataBlock block, DataBlock newBlock)
 {
     AddAfterInternal(block, newBlock);
     RemoveInternal(block);
     return(newBlock);
 }
Example #18
0
 public void AddAfter(DataBlock block, DataBlock newBlock)
 {
     AddAfterInternal(block, newBlock);
 }
Example #19
0
 public void AddAfter(DataBlock block, DataBlock newBlock)
 {
     AddAfterInternal(block, newBlock);
 }
Example #20
0
 public void AddBefore(DataBlock block, DataBlock newBlock)
 {
     AddBeforeInternal(block, newBlock);
 }
Example #21
0
 public void Remove(DataBlock block)
 {
     RemoveInternal(block);
 }
 private FileDataBlock GetNextFileDataBlock(DataBlock block, long dataOffset, out long nextDataOffset)
 {
     // Iterate over the remaining blocks until a file block is encountered.
     nextDataOffset = dataOffset + block.Length;
     block = block.NextBlock;
     while (block != null)
     {
         var fileBlock = block as FileDataBlock;
         if (fileBlock != null)
         {
             return fileBlock;
         }
         nextDataOffset += block.Length;
         block = block.NextBlock;
     }
     return null;
 }