public virtual T this[int index] { get { BaseStream.Position = GetItemOffset(index); return(ReadItem(index)); } set { long offset = BaseStream.Position = GetItemOffset(index); int oldSize = FixedItemSize.HasValue ? FixedItemSize.Value : ReadItemSize(index); int newSize = FixedItemSize.HasValue ? FixedItemSize.Value : GetItemSize(value); if (oldSize != newSize) { _stamp++; BaseStream.Position = offset + oldSize; long length = BaseStream.Length - (offset + oldSize); BaseStream.Move(offset + newSize, length, 1024, ExpandMethod); BaseStream.SetLength(offset + newSize + length); // Update offsets if (_offsets != null) { if (_offsets.Count > index + 1) { _offsets.RemoveRange(index + 1, _offsets.Count - index - 1); } } } BaseStream.Position = offset; WriteItem(index, value); } }
public virtual void RemoveAt(int index) { if (index < 0) { throw new ArgumentOutOfRangeException("index"); } int total = ReadCount(); if (index >= total) { throw new ArgumentOutOfRangeException("index"); } _stamp++; long offset = BaseStream.Position = GetItemOffset(index); int size = ReadItemSize(index); long length = BaseStream.Length - offset - size; BaseStream.Position = offset + size; BaseStream.Move(offset, length, 1024, ExpandMethod); BaseStream.SetLength(offset + length); WriteCount(total - 1); // Update offsets cache if (_offsets != null) { if (_offsets.Count > index + (index == total - 1 ? 0 : 1)) { _offsets.RemoveRange(index + (index == total - 1 ? 0 : 1), _offsets.Count - index - (index == total - 1 ? 0 : 1)); } } }
public virtual void SetRepeat(int index, T value, int count) { if (index < 0) { throw new ArgumentOutOfRangeException("index"); } int total = ReadCount(); if (index > total) { throw new ArgumentOutOfRangeException("index"); } if (count > total - index) { throw new ArgumentOutOfRangeException("coll"); } if (count == 0) { return; } _stamp++; // Update offsets cache if (_offsets != null) { if (_offsets.Count > index + 1) { _offsets.RemoveRange(index + 1, _offsets.Count - index - 1); } } // long newSize = (FixedItemSize.HasValue ? FixedItemSize.Value : GetItemSize(value)) * count; long offset = GetItemOffset(index); BaseStream.Position = GetItemOffset(index + count); long length = BaseStream.Length - BaseStream.Position; BaseStream.Move(offset + newSize, length, 1024, ExpandMethod); BaseStream.SetLength(offset + newSize + length); BaseStream.Position = offset; // for (int i = 0; i < count; i++) { // Update offsets cache if (_offsets != null) { if (i > 0 && _offsets.Count >= index) { _offsets.Insert(index + i, BaseStream.Position); } } // WriteItem(index + i, value); } }
public virtual void InsertRange(int index, IEnumerable <T> coll) { if (coll == null) { throw new ArgumentNullException("coll"); } if (index < 0) { throw new ArgumentOutOfRangeException("index"); } int total = ReadCount(); if (index > total) { throw new ArgumentOutOfRangeException("index"); } _stamp++; // Update offsets cache if (_offsets != null) { if (_offsets.Count > index + 1) { _offsets.RemoveRange(index + 1, _offsets.Count - index - 1); } } // long newSize = coll.Aggregate(0L, (a, t) => a += FixedItemSize.HasValue ? FixedItemSize.Value : GetItemSize(t)); long offset = BaseStream.Position = GetItemOffset(index); long length = BaseStream.Length - offset; BaseStream.Move(offset + newSize, length, 1024, ExpandMethod); BaseStream.SetLength(offset + newSize + length); BaseStream.Position = offset; int i = 0; foreach (var item in coll) { // Update offsets cache if (_offsets != null) { if ((_offsets.Count == 0 || i > 0) && _offsets.Count == (index + 1)) { _offsets.Insert(index + i, BaseStream.Position); } } // WriteItem(index + i++, item); } WriteCount(total + i); }
public virtual void Insert(int index, T value) { if (index < 0) { throw new ArgumentOutOfRangeException("index"); } int total = ReadCount(); if (index > total) { throw new ArgumentOutOfRangeException("index"); } _stamp++; // Update offsets cache if (_offsets != null) { if (_offsets.Count > index + 1) { _offsets.RemoveRange(index + 1, _offsets.Count - index - 1); } } // int newSize = FixedItemSize.HasValue ? FixedItemSize.Value : GetItemSize(value); long offset = BaseStream.Position = GetItemOffset(index); long length = BaseStream.Length - offset; BaseStream.Move(offset + newSize, length, 1024, ExpandMethod); BaseStream.SetLength(offset + newSize + length); BaseStream.Position = offset; WriteItem(index, value); WriteCount(total + 1); // Update offsets cache if (_offsets != null) { if (_offsets.Count == index) { _offsets.Insert(index, offset); } } }
public virtual void RemoveRange(int index, int count) { if (index < 0) { throw new ArgumentOutOfRangeException("index"); } int total = ReadCount(); if (index > total) { throw new ArgumentOutOfRangeException("index"); } if (count < 0 || count > total - index) { throw new ArgumentOutOfRangeException("count"); } if (count == 0) { return; } _stamp++; long offset = GetItemOffset(index); BaseStream.Position = GetItemOffset(index + count); long length = BaseStream.Length - BaseStream.Position; BaseStream.Move(offset, length, 1024, ExpandMethod); BaseStream.SetLength(offset + length); WriteCount(total - count); // Update offsets cache if (_offsets != null) { if (_offsets.Count > index + (index + count == total ? 0 : 1)) { _offsets.RemoveRange(index + (index + count == total ? 0 : 1), _offsets.Count - index - (index + count == total ? 0 : 1)); } } }