OSDN Git Service

Use std::vector instead of CArray for storing linedata in editor.
authorKimmo Varis <kimmov@gmail.com>
Mon, 29 Jun 2009 07:39:10 +0000 (07:39 +0000)
committerKimmo Varis <kimmov@gmail.com>
Mon, 29 Jun 2009 07:39:10 +0000 (07:39 +0000)
Src/DiffTextBuffer.cpp
Src/GhostTextBuffer.cpp
Src/MergeDoc.cpp
Src/editlib/ccrystaltextbuffer.cpp
Src/editlib/ccrystaltextbuffer.h

index 954eca2..dae116d 100644 (file)
@@ -320,7 +320,7 @@ int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
                CRLFSTYLE nCrlfStyle, const FileTextEncoding & encoding, CString &sError)
 {
        ASSERT(!m_bInit);
-       ASSERT(m_aLines.GetSize() == 0);
+       ASSERT(m_aLines.size() == 0);
 
        // Unpacking the file here, save the result in a temporary file
        String sFileName(pszFileNameInit);
@@ -391,7 +391,7 @@ int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
 
                // Manually grow line array exponentially
                UINT arraysize = 500;
-               m_aLines.SetSize(arraysize);
+               m_aLines.resize(arraysize);
                
                // preveol must be initialized for empty files
                preveol = _T("\n");
@@ -415,7 +415,7 @@ int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
                                        arraysize *= 2;
                                else
                                        arraysize += 100 * 1024;
-                               m_aLines.SetSize(arraysize);
+                               m_aLines.resize(arraysize);
                        }
 
                        sline += eol; // TODO: opportunity for optimization, as CString append is terrible
@@ -458,7 +458,7 @@ int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
 #endif // _DEBUG
 
                // fix array size (due to our manual exponential growth
-               m_aLines.SetSize(lineno);
+               m_aLines.resize(lineno);
        
                
                //Try to determine current CRLF mode (most frequent)
@@ -471,7 +471,7 @@ int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
                
                //  At least one empty line must present
                // (view does not work for empty buffers)
-               ASSERT(m_aLines.GetSize() > 0);
+               ASSERT(m_aLines.size() > 0);
                
                m_bInit = TRUE;
                m_bModified = FALSE;
@@ -592,7 +592,7 @@ int CDiffTextBuffer::SaveToFile (LPCTSTR pszFileName,
        // line loop : get each real line and write it in the file
        CString sLine;
        CString sEol = GetStringEol(nCrlfStyle);
-       int nLineCount = m_aLines.GetSize();
+       int nLineCount = m_aLines.size();
        for (int line = 0; line < nLineCount; ++line)
        {
                if (GetLineFlags(line) & LF_GHOST)
index 86505cb..9b0cd5e 100644 (file)
@@ -86,7 +86,7 @@ BOOL CGhostTextBuffer::InternalInsertGhostLine (CCrystalTextView * pSource,
        ASSERT (m_bInit);             //  Text buffer not yet initialized.
        //  You must call InitNew() or LoadFromFile() first!
 
-       ASSERT (nLine >= 0 && nLine <= m_aLines.GetSize ());
+       ASSERT (nLine >= 0 && nLine <= m_aLines.size ());
        if (m_bReadOnly)
                return FALSE;
 
@@ -120,7 +120,7 @@ BOOL CGhostTextBuffer::InternalDeleteGhostLine (CCrystalTextView * pSource,
 {
        ASSERT (m_bInit);             //  Text buffer not yet initialized.
        //  You must call InitNew() or LoadFromFile() first!
-       ASSERT (nLine >= 0 && nLine <= m_aLines.GetSize ());
+       ASSERT (nLine >= 0 && nLine <= m_aLines.size ());
 
        if (m_bReadOnly)
                return FALSE;
@@ -138,7 +138,10 @@ BOOL CGhostTextBuffer::InternalDeleteGhostLine (CCrystalTextView * pSource,
                ASSERT (GetLineFlags(i) & LF_GHOST);
                m_aLines[i].Clear();
        }
-       m_aLines.RemoveAt (nLine, nCount);
+
+       vector<LineInfo>::iterator iterBegin = m_aLines.begin() + nLine;
+       vector<LineInfo>::iterator iterEnd = iterBegin + nCount;
+       m_aLines.erase(iterBegin, iterEnd);
 
        if (pSource != NULL)
        {
@@ -178,7 +181,7 @@ void CGhostTextBuffer::GetTextWithoutEmptys(int nStartLine, int nStartChar,
                  int nEndLine, int nEndChar, 
                  CString &text, CRLFSTYLE nCrlfStyle /* CRLF_STYLE_AUTOMATIC */)
 {
-       int lines = (int) m_aLines.GetSize();
+       int lines = (int) m_aLines.size();
        ASSERT(nStartLine >= 0 && nStartLine < lines);
        ASSERT(nStartChar >= 0 && nStartChar <= GetLineLength(nStartLine));
        ASSERT(nEndLine >= 0 && nEndLine < lines);
@@ -278,7 +281,7 @@ Undo (CCrystalTextView * pSource, CPoint & ptCursorPos)
 
                if (ur.m_ptStartPos_nGhost > 0)
                        // if we need a ghost line at position apparent_ptStartPos.y
-                       if (apparent_ptStartPos.y >= m_aLines.GetSize() || (GetLineFlags(apparent_ptStartPos.y) & LF_GHOST) == 0)
+                       if (apparent_ptStartPos.y >= m_aLines.size() || (GetLineFlags(apparent_ptStartPos.y) & LF_GHOST) == 0)
                        {
                                // if we don't find it, we insert it 
                                InsertGhostLine (pSource, apparent_ptStartPos.y);
@@ -289,7 +292,7 @@ Undo (CCrystalTextView * pSource, CPoint & ptCursorPos)
                // EndPos defined only for UNDO_INSERT (when we delete)
                if (ur.m_dwFlags & UNDO_INSERT && ur.m_ptEndPos_nGhost > 0)
                        // if we need a ghost line at position apparent_ptStartPos.y
-                       if (apparent_ptEndPos.y >= m_aLines.GetSize() || (GetLineFlags(apparent_ptEndPos.y) & LF_GHOST) == 0)
+                       if (apparent_ptEndPos.y >= m_aLines.size() || (GetLineFlags(apparent_ptEndPos.y) & LF_GHOST) == 0)
                        {
                                // if we don't find it, we insert it
                                InsertGhostLine (pSource, apparent_ptEndPos.y);
@@ -309,9 +312,9 @@ Undo (CCrystalTextView * pSource, CPoint & ptCursorPos)
                        // flags are going to be deleted so we store them now
                        int bLastLineGhost = ((GetLineFlags(apparent_ptEndPos.y) & LF_GHOST) != 0);
 
-                       if ((apparent_ptStartPos.y < m_aLines.GetSize ()) &&
+                       if ((apparent_ptStartPos.y < m_aLines.size ()) &&
                                        (apparent_ptStartPos.x <= m_aLines[apparent_ptStartPos.y].Length()) &&
-                                       (apparent_ptEndPos.y < m_aLines.GetSize ()) &&
+                                       (apparent_ptEndPos.y < m_aLines.size ()) &&
                                        (apparent_ptEndPos.x <= m_aLines[apparent_ptEndPos.y].Length()))
                        {
                                GetTextWithoutEmptys (apparent_ptStartPos.y, apparent_ptStartPos.x, apparent_ptEndPos.y, apparent_ptEndPos.x, text);
@@ -454,7 +457,7 @@ Redo (CCrystalTextView * pSource, CPoint & ptCursorPos)
 
                if (ur.m_redo_ptStartPos_nGhost > 0) 
                        // we need a ghost line at position apparent_ptStartPos.y
-                       if (apparent_ptStartPos.y >= m_aLines.GetSize() || (GetLineFlags(apparent_ptStartPos.y) & LF_GHOST) == 0)
+                       if (apparent_ptStartPos.y >= m_aLines.size() || (GetLineFlags(apparent_ptStartPos.y) & LF_GHOST) == 0)
                        {
                                // if we don't find it, we insert it 
                                InsertGhostLine (pSource, apparent_ptStartPos.y);
@@ -465,7 +468,7 @@ Redo (CCrystalTextView * pSource, CPoint & ptCursorPos)
                // EndPos defined only for UNDO_DELETE (when we delete)
                if ((ur.m_dwFlags & UNDO_INSERT) == 0 && ur.m_redo_ptEndPos_nGhost > 0)
                        // we need a ghost line at position apparent_ptStartPos.y
-                       if (apparent_ptEndPos.y >= m_aLines.GetSize() || (GetLineFlags(apparent_ptEndPos.y) & LF_GHOST) == 0)
+                       if (apparent_ptEndPos.y >= m_aLines.size() || (GetLineFlags(apparent_ptEndPos.y) & LF_GHOST) == 0)
                        {
                                // if we don't find it, we insert it
                                InsertGhostLine (pSource, apparent_ptEndPos.y);
@@ -865,7 +868,7 @@ void CGhostTextBuffer::RemoveAllGhostLines()
        }
 
        // Discard unused entries in one shot
-       m_aLines.SetSize(newnl);
+       m_aLines.resize(newnl);
        RecomputeRealityMapping();
 }
 
index e6f2c5e..31c0800 100644 (file)
@@ -1635,8 +1635,8 @@ void CMergeDoc::PrimeTextBuffers()
        UINT lcount1new = lcount1 + RightExtras;
 // this ASSERT may be false because of empty last line (see function's note)
 //     ASSERT(lcount0new == lcount1new);
-       m_ptBuf[0]->m_aLines.SetSize(lcount0new);
-       m_ptBuf[1]->m_aLines.SetSize(lcount1new);
+       m_ptBuf[0]->m_aLines.resize(lcount0new);
+       m_ptBuf[1]->m_aLines.resize(lcount1new);
 
        // walk the diff list backward, move existing lines to proper place,
        // add ghost lines, and set flags
index 4d8c3df..0a546af 100644 (file)
@@ -198,10 +198,11 @@ void CCrystalTextBuffer::InsertLine (LPCTSTR pszLine, int nLength /*= -1*/ ,
 
   // nPosition not defined ? Insert at end of array
   if (nPosition == -1)
-    nPosition = (int) m_aLines.GetSize();
+    nPosition = (int) m_aLines.size();
 
   // insert all lines in one pass
-  m_aLines.InsertAt (nPosition, li, nCount);
+  vector<LineInfo>::iterator iter = m_aLines.begin() + nPosition;
+  m_aLines.insert(iter, nCount, li);
 
   // duplicate the text data for lines after the first one
   for (int ic = 1; ic < nCount; ic++) 
@@ -212,7 +213,7 @@ void CCrystalTextBuffer::InsertLine (LPCTSTR pszLine, int nLength /*= -1*/ ,
 #ifdef _DEBUG
   // Warning : this function is also used during rescan
   // and this trace will appear even after the initial load
-  int nLines = (int) m_aLines.GetSize ();
+  int nLines = (int) m_aLines.size();
   if (nLines / 5000 != (nLines-nCount) / 5000)
     TRACE1 ("%d lines loaded!\n", nLines);
 #endif
@@ -284,12 +285,13 @@ void CCrystalTextBuffer::
 FreeAll ()
 {
   //  Free text
-  const int nCount = (int) m_aLines.GetSize ();
-  for (int I = 0; I < nCount; I++)
+  vector<LineInfo>::iterator iter = m_aLines.begin();
+  while (iter != m_aLines.end())
     {
-      m_aLines[I].Clear();
+      (*iter).Clear();
+      iter++;
     }
-  m_aLines.RemoveAll ();
+  m_aLines.clear();
 
   // Undo buffer will be cleared by its destructor
 
@@ -303,7 +305,7 @@ BOOL CCrystalTextBuffer::
 InitNew (CRLFSTYLE nCrlfStyle /*= CRLF_STYLE_DOS*/ )
 {
   ASSERT (!m_bInit);
-  ASSERT (m_aLines.GetSize () == 0);
+  ASSERT (m_aLines.size() == 0);
   ASSERT (nCrlfStyle >= 0 && nCrlfStyle <= 2);
   InsertLine (_T (""), 0);
   m_bInit = TRUE;
@@ -355,7 +357,7 @@ BOOL CCrystalTextBuffer::
 LoadFromFile (LPCTSTR pszFileName, int nCrlfStyle /*= CRLF_STYLE_AUTOMATIC*/ )
 {
   ASSERT (!m_bInit);
-  ASSERT (m_aLines.GetSize () == 0);
+  ASSERT (m_aLines.size() == 0);
 
   HANDLE hFile = NULL;
   int nCurrentMax = 256;
@@ -422,7 +424,7 @@ LoadFromFile (LPCTSTR pszFileName, int nCrlfStyle /*= CRLF_STYLE_AUTOMATIC*/ )
       ASSERT (nCrlfStyle >= 0 && nCrlfStyle <= 2);
       m_nCRLFMode = nCrlfStyle;
 
-      m_aLines.SetSize (0, 4096);
+      m_aLines.setsize(4096);
 
       DWORD dwBufPtr = 0;
       while (dwBufPtr < dwCurSize)
@@ -474,7 +476,7 @@ LoadFromFile (LPCTSTR pszFileName, int nCrlfStyle /*= CRLF_STYLE_AUTOMATIC*/ )
       pcLineBuf[nCurrentLength] = 0;
       InsertLine (pcLineBuf);
 
-      ASSERT (m_aLines.GetSize () > 0);   //  At least one empty line must present
+      ASSERT (m_aLines.size() > 0);   //  At least one empty line must present
 
       m_bInit = TRUE;
       m_bReadOnly = (dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0;
@@ -545,7 +547,7 @@ BOOL CCrystalTextBuffer::SaveToFile(LPCTSTR pszFileName,
           LPCTSTR pszCRLF = crlfs[nCrlfStyle];
           int nCRLFLength = _tcslen (pszCRLF);
 
-          int nLineCount = m_aLines.GetSize ();
+          int nLineCount = m_aLines.size();
           for (int nLine = 0; nLine < nLineCount; nLine++)
             {
               int nLength = m_aLines[nLine].m_nLength;
@@ -654,7 +656,7 @@ applyEOLMode()
        LPCTSTR lpEOLtoApply = GetDefaultEol();
        BOOL bChanged = FALSE;
        int i;
-       for (i = 0 ; i < m_aLines.GetSize () ; i++)
+       for (i = 0 ; i < m_aLines.size () ; i++)
        {
                // the last real line has no EOL
                if (!m_aLines[i].HasEol())
@@ -674,7 +676,7 @@ GetLineCount () const
   ASSERT (m_bInit);             //  Text buffer not yet initialized.
   //  You must call InitNew() or LoadFromFile() first!
 
-  return (int) m_aLines.GetSize ();
+  return (int) m_aLines.size ();
 }
 
 // number of characters in line (excluding any trailing eol characters)
@@ -768,7 +770,7 @@ FlagToIndex (DWORD dwFlag)
 int CCrystalTextBuffer::
 FindLineWithFlag (DWORD dwFlag)
 {
-  int nSize = (int) m_aLines.GetSize ();
+  int nSize = (int) m_aLines.size ();
   for (int L = 0; L < nSize; L++)
     {
       if ((m_aLines[L].m_dwFlags & dwFlag) != 0)
@@ -870,9 +872,9 @@ GetText (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString & t
   ASSERT (m_bInit);             //  Text buffer not yet initialized.
   //  You must call InitNew() or LoadFromFile() first!
 
-  ASSERT (nStartLine >= 0 && nStartLine < m_aLines.GetSize ());
+  ASSERT (nStartLine >= 0 && nStartLine < m_aLines.size ());
   ASSERT (nStartChar >= 0 && nStartChar <= m_aLines[nStartLine].Length());
-  ASSERT (nEndLine >= 0 && nEndLine < m_aLines.GetSize ());
+  ASSERT (nEndLine >= 0 && nEndLine < m_aLines.size ());
   ASSERT (nEndChar >= 0 && nEndChar <= m_aLines[nEndLine].Length());
   ASSERT (nStartLine < nEndLine || nStartLine == nEndLine && nStartChar <= nEndChar);
   // some edit functions (copy...) should do nothing when there is no selection.
@@ -996,9 +998,9 @@ InternalDeleteText (CCrystalTextView * pSource, int nStartLine, int nStartChar,
   ASSERT (m_bInit);             //  Text buffer not yet initialized.
   //  You must call InitNew() or LoadFromFile() first!
 
-  ASSERT (nStartLine >= 0 && nStartLine < m_aLines.GetSize ());
+  ASSERT (nStartLine >= 0 && nStartLine < m_aLines.size ());
   ASSERT (nStartChar >= 0 && nStartChar <= m_aLines[nStartLine].Length());
-  ASSERT (nEndLine >= 0 && nEndLine < m_aLines.GetSize ());
+  ASSERT (nEndLine >= 0 && nEndLine < m_aLines.size ());
   ASSERT (nEndChar >= 0 && nEndChar <= m_aLines[nEndLine].Length());
   ASSERT (nStartLine < nEndLine || nStartLine == nEndLine && nStartChar <= nEndChar);
   // some edit functions (delete...) should do nothing when there is no selection.
@@ -1030,7 +1032,9 @@ InternalDeleteText (CCrystalTextView * pSource, int nStartLine, int nStartChar,
       int nDelCount = nEndLine - nStartLine;
       for (int L = nStartLine + 1; L <= nEndLine; L++)
         m_aLines[L].Clear();
-      m_aLines.RemoveAt (nStartLine + 1, nDelCount);
+      vector<LineInfo>::iterator iterBegin = m_aLines.begin() + nStartLine + 1;
+      vector<LineInfo>::iterator iterEnd = iterBegin + nDelCount;
+      m_aLines.erase(iterBegin, iterEnd);
 
       //  nEndLine is no more valid
       m_aLines[nStartLine].DeleteEnd(nStartChar);
@@ -1095,7 +1099,7 @@ InternalInsertText (CCrystalTextView * pSource, int nLine, int nPos,
   ASSERT (m_bInit);             //  Text buffer not yet initialized.
   //  You must call InitNew() or LoadFromFile() first!
 
-  ASSERT (nLine >= 0 && nLine < m_aLines.GetSize ());
+  ASSERT (nLine >= 0 && nLine < m_aLines.size ());
   ASSERT (nPos >= 0 && nPos <= m_aLines[nLine].Length());
   if (m_bReadOnly)
     return FALSE;
@@ -1366,9 +1370,9 @@ Undo (CCrystalTextView * pSource, CPoint & ptCursorPos)
           // we need to put the cursor before the deleted section
           CString text;
 
-          if ((apparent_ptStartPos.y < m_aLines.GetSize ()) &&
+          if ((apparent_ptStartPos.y < m_aLines.size()) &&
               (apparent_ptStartPos.x <= m_aLines[apparent_ptStartPos.y].Length()) &&
-              (apparent_ptEndPos.y < m_aLines.GetSize ()) &&
+              (apparent_ptEndPos.y < m_aLines.size()) &&
               (apparent_ptEndPos.x <= m_aLines[apparent_ptEndPos.y].Length()))
             {
               GetTextWithoutEmptys (apparent_ptStartPos.y, apparent_ptStartPos.x, apparent_ptEndPos.y, apparent_ptEndPos.x, text);
@@ -1774,7 +1778,7 @@ FindNextBookmarkLine (int nCurrentLine)
   if ((dwFlags & LF_BOOKMARKS) != 0)
     nCurrentLine++;
 
-  int nSize = (int) m_aLines.GetSize ();
+  int nSize = (int) m_aLines.size ();
   for (;;)
     {
       while (nCurrentLine < nSize)
@@ -1803,7 +1807,7 @@ FindPrevBookmarkLine (int nCurrentLine)
   if ((dwFlags & LF_BOOKMARKS) != 0)
     nCurrentLine--;
 
-  int nSize = (int) m_aLines.GetSize ();
+  int nSize = (int) m_aLines.size ();
   for (;;)
     {
       while (nCurrentLine >= 0)
@@ -1877,7 +1881,9 @@ void CCrystalTextBuffer::DeleteLine(int line, int nCount /*=1*/)
 {
   for (int ic = 0; ic < nCount; ic++)
     m_aLines[line + ic].Clear();
-  m_aLines.RemoveAt(line, nCount);
+  vector<LineInfo>::iterator iterBegin = m_aLines.begin() + line;
+  vector<LineInfo>::iterator iterEnd = iterBegin + nCount;
+  m_aLines.erase(iterBegin, iterEnd);
 }
 
 int CCrystalTextBuffer::GetTabSize()
index 6d2ca74..80ec8a7 100644 (file)
@@ -151,7 +151,7 @@ public :
       };
 
     //  Lines of text
-    CArray < LineInfo, LineInfo & >m_aLines;
+    std::vector<LineInfo> m_aLines; /**< Text lines. */
 
     //  Undo
     std::vector<UndoRecord> m_aUndoBuf; /**< Undo records. */