From ec58e6f1838270abf9b591b687f54115a95c7d93 Mon Sep 17 00:00:00 2001 From: psychon Date: Wed, 20 Aug 2008 08:13:23 +0000 Subject: [PATCH] Some cleanup and optimizations to CTable CTable now caches the width of each column instead of recalculating it each time it is needed. The code for this was there before, but it was not used. Now the widths are calculated when the table is filled, not when the width is needed the first time. This caching leads to a huge speedup (3-4 times faster) if there are many rows in the table. This also cleans up the interface by making it possible to mark GetColumnWidth and GetLine as const. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1160 726aef4b-f618-498e-8847-2d620e286838 --- Utils.cpp | 48 +++++++++++++++++------------------------------- Utils.h | 10 +++++----- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/Utils.cpp b/Utils.cpp index e01f39dc..903f6ff5 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -396,15 +396,6 @@ void CUtils::PrintStatus(bool bSuccess, const CString& sMessage) { fflush(stdout); } -CTable::CTable() {} -CTable::~CTable() { - for (unsigned int a = 0; a < size(); a++) { - delete (*this)[a]; - } - - clear(); -} - bool CTable::AddColumn(const CString& sName) { for (unsigned int a = 0; a < m_vsHeaders.size(); a++) { if (m_vsHeaders[a].CaseCmp(sName) == 0) { @@ -413,11 +404,13 @@ bool CTable::AddColumn(const CString& sName) { } m_vsHeaders.push_back(sName); + m_msuWidths[sName] = sName.size(); + return true; } unsigned int CTable::AddRow() { - push_back(new map); + push_back(map()); return size() -1; } @@ -430,11 +423,15 @@ bool CTable::SetCell(const CString& sColumn, const CString& sValue, unsigned int uRowIdx = size() -1; } - (*(*this)[uRowIdx])[sColumn] = sValue; + (*this)[uRowIdx][sColumn] = sValue; + + if (m_msuWidths[sColumn] < sValue.size()) + m_msuWidths[sColumn] = sValue.size(); + return true; } -bool CTable::GetLine(unsigned int uIdx, CString& sLine) { +bool CTable::GetLine(unsigned int uIdx, CString& sLine) const { stringstream ssRet; if (!size()) { @@ -442,7 +439,6 @@ bool CTable::GetLine(unsigned int uIdx, CString& sLine) { } if (uIdx == 1) { - m_msuWidths.clear(); // Clear out the width cache ssRet.fill(' '); ssRet << "| "; @@ -470,13 +466,13 @@ bool CTable::GetLine(unsigned int uIdx, CString& sLine) { uIdx -= 3; if (uIdx < size()) { - map* pRow = (*this)[uIdx]; + const map& mRow = (*this)[uIdx]; ssRet.fill(' '); ssRet << "| "; for (unsigned int c = 0; c < m_vsHeaders.size(); c++) { ssRet.width(GetColumnWidth(c)); - ssRet << std::left << (*pRow)[m_vsHeaders[c]]; + ssRet << std::left << mRow.at(m_vsHeaders[c]); ssRet << ((c == m_vsHeaders.size() -1) ? " |" : " | "); } @@ -515,29 +511,19 @@ bool CTable::Output(std::ostream oOut) { } */ -unsigned int CTable::GetColumnWidth(unsigned int uIdx) { +unsigned int CTable::GetColumnWidth(unsigned int uIdx) const { if (uIdx >= m_vsHeaders.size()) { return 0; } const CString& sColName = m_vsHeaders[uIdx]; - unsigned int uRet = sColName.size(); - map::iterator it = m_msuWidths.find(sColName); + map::const_iterator it = m_msuWidths.find(sColName); - if (it != m_msuWidths.end()) { - return it->second; + if (it == m_msuWidths.end()) { + // AddColumn() and SetCell() should make sure that we get a value :/ + return 0; } - - for (unsigned int a = 0; a < size(); a++) { - map* pRow = (*this)[a]; - unsigned int uTmp = (*pRow)[m_vsHeaders[uIdx]].size(); - - if (uTmp > uRet) { - uRet = uTmp; - } - } - - return uRet; + return it->second; } diff --git a/Utils.h b/Utils.h index 030355d1..fb087c4d 100644 --- a/Utils.h +++ b/Utils.h @@ -198,17 +198,17 @@ protected: }; -class CTable : public vector* > { +class CTable : public vector > { public: - CTable(); - virtual ~CTable(); + CTable() {} + virtual ~CTable() {} bool AddColumn(const CString& sName); unsigned int AddRow(); bool SetCell(const CString& sColumn, const CString& sValue, unsigned int uRowIdx = ~0); - bool GetLine(unsigned int uIdx, CString& sLine); + bool GetLine(unsigned int uIdx, CString& sLine) const; - unsigned int GetColumnWidth(unsigned int uIdx); + unsigned int GetColumnWidth(unsigned int uIdx) const; private: protected: vector m_vsHeaders;