Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I looking for an efficent method to resize an area in a buffer at both sides. The base buffer should only reallocated if neseccary. Please take a look at the following sample source code and let me know your solutions. Thanks!

C#
class BufferSampleClass
{
   // base buffer
   private Byte[] buffer = { 0, 0, 0, 0, 0, 8, 8, 1, 8, 8, 0, 0, 0, 0, 0 };

   // first valid index of the public accessible buffer area
   private Int32 firstIndex = 5;

   // current index in the public accessible buffer area
   private Int32 index = 7;

   // last valid index of the public accessible buffer area
   private Int32 lastIndex = 9;

   // length of the public accessible buffer area
   private Int32 length = 5;

   // resize the public accessible buffer at both sides
   public void Resize(Int32 frontSide, Int32 backSide)
   {
      Int32 newLength = frontSide + length + backSide;
      if (newLength < 0)
      {
         throw new ArgumentException();
      }
      Int32 newFirstIndex = firstIndex - frontSide;
      Int32 newLastIndex = lastIndex + backSide;

      // if newFirstIndex is lower than 0, move the current content
      // if newLastIndex is greater than buffer.Length, create a new buffer and copy the current content
      // combine both cases

      // update firstIndex and lastIndex
      throw new NotImplementedException();
   }

   // Read, Write, Seek, ... members to manipulate the content
}
Posted

I guess you are attempting to create some sort of resizable memory stream since your last line reads:
Read, Write, Seek, ... members to manipulate the content

Try using the existing MemoryStream[^]. If that's not good enough - try reimplementing the MemoryStream using a list of buffers. Depending on what you are doing this can be quite efficient since you will avoid copying and reallocation.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jan-11 17:21pm    
Oh yes, this is a very good suggestion - my 5.
Further development of this idea: you could wrap it in a generic class and provide some array-like interface upgraded to advances operations like those OP wanted. Agree?

I think such technique might pay off if the typical length of the "array" and underlying memory stream is big enough. For relatively small array the "usual" technique I show might be better.

I think there is a far opposite pole in the scales: huge "arrays" that challenge the memory restrictions. In this case, a memory-mapped file can help.

Thank you.
--SA
Espen Harlinn 29-Jan-11 2:28am    
Sounds like a good approach :)
Sergey Alexandrovich Kryukov 30-Jan-11 14:29pm    
Still, I think when working away of extremes, the "trivial" approach I've shown in my answer should prevail, just to follow useful K.I.S.S. principle. Don't you think?
--SA
Espen Harlinn 30-Jan-11 14:37pm    
Seems like he wanted a Stream implmentation :)
Sergey Alexandrovich Kryukov 30-Jan-11 20:05pm    
Maybe, hard to tell exactly based on given information.
--SA
I would say,

C#
byte[] newbuffer = new byte[newLength];
Array.Copy(buffer, newFirstIndex, newbuffer, 0, newLength);
buffer = newbuffer;


I think that's all you can do; you can't arbitrarily "deallocate" trailing and leading parts, even using unsafe methods.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900