private void CombineChunks(int chunkIndex, DocumentChunk chunk)
 {
     if (chunkIndex > 0)
     {
         if (chunks[chunkIndex - 1].IsTooShort)
         {
             chunks[chunkIndex - 1].Append(chunk);
             chunks.RemoveAt(chunkIndex);
             chunkIndex--;
             chunk = chunks[chunkIndex];
             updatedToChunkIndex = Math.Min(updatedToChunkIndex, chunkIndex);
         }
     }
     if (!chunk.IsTooShort)
     {
         return;
     }
     if (chunkIndex < chunks.Count - 1)
     {
         if (chunks[chunkIndex + 1].IsTooShort)
         {
             chunk.Append(chunks[chunkIndex + 1]);
             chunks.RemoveAt(chunkIndex + 1);
         }
     }
 }
        public void AddAt(string text, Cindex cindex)
        {
            int           chunkIndex = GetChunkIndexByCindex(cindex);
            DocumentChunk chunk      = chunks[chunkIndex];

            chunk.InsertText(text, cindex);
            updatedToChunkIndex = Math.Min(updatedToChunkIndex, chunkIndex);
            CheckChunkLength(chunkIndex, chunk);
        }
 public char this[int cindex]
 {
     get
     {
         int           chunkIndex = GetChunkIndexByCindex(cindex);
         DocumentChunk chunk      = chunks[chunkIndex];
         return(chunk[cindex]);
     }
 }
        private int RemoveAtPartial(Cindex cindex, int length)
        {
            int           chunkIndex      = GetChunkIndexByCindex(cindex);
            DocumentChunk chunk           = chunks[chunkIndex];
            int           availableLength = Math.Min(length, chunk.End + 1 - cindex);

            if (availableLength == 0)
            {
                return(0);
            }
            chunk.RemoveText(cindex, availableLength);
            updatedToChunkIndex = chunkIndex;
            CheckChunkLength(chunkIndex, chunk);
            return(length - availableLength);
        }
        private void SplitChunk(int chunkIndex, DocumentChunk chunk)
        {
            List <DocumentChunk> chunksToCheck = new List <DocumentChunk>();

            while (chunk.IsTooLong)
            {
                DocumentChunk secondChunk = chunk.Halve();
                chunks.Insert(chunkIndex + 1, secondChunk);
                chunksToCheck.Add(secondChunk);
            }
            foreach (DocumentChunk chunkToCheck in chunksToCheck)
            {
                SplitChunk(chunks.IndexOf(chunkToCheck), chunkToCheck);
            }
        }
/*		private DocumentChunk GetChunkByCindex(Cindex cindex)
 *              {
 *                      UpdateChunksToCindex(cindex);
 *                      foreach(DocumentChunk chunk in chunks)
 *                      {
 *                              if(chunk.Start >= cindex && chunk.End <= cindex)
 *                                      return chunk;
 *                      }
 *                      if(cindex == Length)
 *                              return chunks.Last();
 *                      throw new Exception(String.Format("Cindex {0} not found in DocumentChunkCollection of length {1}.", cindex, Length));
 *              }	*/

        private void CheckChunkLength(int chunkIndex, DocumentChunk chunk)
        {
            if (chunk.IsEmpty)
            {
                if (chunks.Count > 1)
                {
                    chunks.RemoveAt(chunkIndex);
                    updatedToChunkIndex--;
                }
            }
            else if (chunk.IsTooLong)
            {
                SplitChunk(chunkIndex, chunk);
            }
            else if (chunk.IsTooShort)
            {
                CombineChunks(chunkIndex, chunk);
            }
        }