/// <summary> /// Writes data to a reflexive, reallocating the original. /// </summary> /// <param name="entries">The entries to write.</param> /// <param name="oldCount">The old count.</param> /// <param name="oldAddress">The old address.</param> /// <param name="newCount">The number of entries to write.</param> /// <param name="layout">The layout of the data to write.</param> /// <param name="metaArea">The meta area of the cache file.</param> /// <param name="allocator">The cache file's meta allocator.</param> /// <param name="stream">The stream to manipulate.</param> /// <returns>The address of the new reflexive, or 0 if the entry list is empty and the reflexive was freed.</returns> public static uint WriteReflexive(IEnumerable <StructureValueCollection> entries, int oldCount, uint oldAddress, int newCount, StructureLayout layout, FileSegmentGroup metaArea, MetaAllocator allocator, IStream stream) { if (newCount == 0) { // Free the old reflexive and return if (oldCount > 0 && oldAddress != 0) { allocator.Free(oldAddress, oldCount * layout.Size); } return(0); } uint newAddress = oldAddress; if (newCount != oldCount) { // Reallocate the reflexive int oldSize = oldCount * layout.Size; int newSize = newCount * layout.Size; if (oldCount > 0 && oldAddress != 0) { newAddress = allocator.Reallocate(oldAddress, oldSize, newSize, stream); } else { newAddress = allocator.Allocate(newSize, stream); } } // Write the new values WriteReflexive(entries.Take(newCount), newAddress, layout, metaArea, stream); return(newAddress); }
/// <summary> /// Writes data to a block, reallocating the original. /// </summary> /// <param name="elements">The entries to write.</param> /// <param name="oldCount">The old count.</param> /// <param name="oldAddress">The old address.</param> /// <param name="newCount">The number of entries to write.</param> /// <param name="layout">The layout of the data to write.</param> /// <param name="metaArea">The meta area of the cache file.</param> /// <param name="allocator">The cache file's meta allocator.</param> /// <param name="stream">The stream to manipulate.</param> /// <returns>The address of the new tag block, or 0 if the entry list is empty and the tag block was freed.</returns> public static long WriteTagBlock(IEnumerable <StructureValueCollection> elements, int oldCount, long oldAddress, int newCount, StructureLayout layout, FileSegmentGroup metaArea, MetaAllocator allocator, IStream stream) { if (newCount == 0) { // Free the old block and return if (oldCount > 0 && oldAddress != 0) { allocator.Free(oldAddress, oldCount * layout.Size); } return(0); } long newAddress = oldAddress; if (newCount != oldCount) { // Reallocate the block int oldSize = oldCount * layout.Size; int newSize = newCount * layout.Size; if (oldCount > 0 && oldAddress != 0) { newAddress = allocator.Reallocate(oldAddress, oldSize, newSize, stream); } else { newAddress = allocator.Allocate(newSize, stream); } } // Write the new values WriteTagBlock(elements.Take(newCount), newAddress, layout, metaArea, stream); return(newAddress); }
private void SaveBitArray(BitArray bits, string countName, string addressName, MetaAllocator allocator, IStream stream, ReflexiveCache<int> cache, StructureValueCollection values) { if (bits.Length == 0) { values.SetInteger(countName, 0); values.SetInteger(addressName, 0); return; } var ints = new int[((bits.Length + 31) & ~31)/32]; bits.CopyTo(ints, 0); // If the address isn't cached, then allocate space and write a new array uint newAddress; if (!cache.TryGetAddress(ints, out newAddress)) { newAddress = allocator.Allocate(ints.Length*4, stream); stream.SeekTo(_metaArea.PointerToOffset(newAddress)); foreach (int i in ints) stream.WriteInt32(i); cache.Add(newAddress, ints); } values.SetInteger(countName, (uint)ints.Length); values.SetInteger(addressName, newAddress); }
/// <summary> /// Writes data to a reflexive, reallocating the original. /// </summary> /// <param name="entries">The entries to write.</param> /// <param name="oldCount">The old count.</param> /// <param name="oldAddress">The old address.</param> /// <param name="newCount">The number of entries to write.</param> /// <param name="layout">The layout of the data to write.</param> /// <param name="metaArea">The meta area of the cache file.</param> /// <param name="allocator">The cache file's meta allocator.</param> /// <param name="stream">The stream to manipulate.</param> /// <returns>The address of the new reflexive, or 0 if the entry list is empty and the reflexive was freed.</returns> public static uint WriteReflexive(IEnumerable<StructureValueCollection> entries, int oldCount, uint oldAddress, int newCount, StructureLayout layout, FileSegmentGroup metaArea, MetaAllocator allocator, IStream stream) { if (newCount == 0) { // Free the old reflexive and return if (oldCount > 0 && oldAddress != 0) allocator.Free(oldAddress, oldCount*layout.Size); return 0; } uint newAddress = oldAddress; if (newCount != oldCount) { // Reallocate the reflexive int oldSize = oldCount*layout.Size; int newSize = newCount*layout.Size; if (oldCount > 0 && oldAddress != 0) newAddress = allocator.Reallocate(oldAddress, oldSize, newSize, stream); else newAddress = allocator.Allocate(newSize, stream); } // Write the new values WriteReflexive(entries.Take(newCount), newAddress, layout, metaArea, stream); return newAddress; }