diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2014-08-05 16:30:14 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2014-08-05 16:30:31 +0200 |
commit | a47b51b4159729472b8ef55d1fa3e1103947fad1 (patch) | |
tree | 60803f2b7d3c4d79b16d502aa4dcd8f138e9f23e | |
parent | b34ddf0700a0d3ea51e7aca5e1b28851150f6f8c (diff) |
Avoid fn ptr casts in SwNodes::ForEeach
...by moving ForEach from BigPtrArray down to its sole user SwNodes.
Change-Id: I6d326d3e1ac9dc6ac820e600973af420785d5a50
-rw-r--r-- | sw/inc/bparr.hxx | 9 | ||||
-rw-r--r-- | sw/inc/ndarr.hxx | 8 | ||||
-rw-r--r-- | sw/qa/core/Test-BigPtrArray.cxx | 135 | ||||
-rw-r--r-- | sw/source/core/bastyp/bparr.cxx | 38 | ||||
-rw-r--r-- | sw/source/core/docnode/nodes.cxx | 34 |
5 files changed, 36 insertions, 188 deletions
diff --git a/sw/inc/bparr.hxx b/sw/inc/bparr.hxx index 541be142ceec..3c5ea878067e 100644 --- a/sw/inc/bparr.hxx +++ b/sw/inc/bparr.hxx @@ -43,8 +43,6 @@ protected: }; typedef BigPtrEntry* ElementPtr; -typedef bool (*FnForEach)( const ElementPtr&, void* pArgs ); - // 1000 entries per Block = a bit less then 4K #define MAXENTRY 1000 @@ -63,6 +61,7 @@ struct BlockInfo { // block info: class SW_DLLPUBLIC BigPtrArray { +protected: BlockInfo** ppInf; // block info sal_uLong nSize; ///< number of elements sal_uInt16 nMaxBlock; ///< current max. number of blocks @@ -75,7 +74,6 @@ class SW_DLLPUBLIC BigPtrArray void BlockDel( sal_uInt16 ); ///< some blocks were deleted void UpdIndex( sal_uInt16 ); ///< recalculate indices -protected: // fill all blocks // the short parameter specifies in percent, how full the blocks should be // made @@ -93,11 +91,6 @@ public: void Replace( sal_uLong pos, const ElementPtr& r); ElementPtr operator[]( sal_uLong ) const; - void ForEach( FnForEach fn, void* pArgs = NULL ) - { - ForEach( 0, nSize, fn, pArgs ); - } - void ForEach( sal_uLong nStart, sal_uLong nEnd, FnForEach fn, void* pArgs = NULL ); }; inline sal_uLong BigPtrEntry::GetPos() const diff --git a/sw/inc/ndarr.hxx b/sw/inc/ndarr.hxx index 26fee7e73fd1..02292deb472f 100644 --- a/sw/inc/ndarr.hxx +++ b/sw/inc/ndarr.hxx @@ -146,13 +146,9 @@ public: sal_uLong Count() const { return BigPtrArray::Count(); } void ForEach( FnForEach_SwNodes fnForEach, void* pArgs = 0 ) { - BigPtrArray::ForEach( 0, BigPtrArray::Count(), - (FnForEach) fnForEach, pArgs ); - } - void ForEach( sal_uLong nStt, sal_uLong nEnd, FnForEach_SwNodes fnForEach, void* pArgs = 0 ) - { - BigPtrArray::ForEach( nStt, nEnd, (FnForEach) fnForEach, pArgs ); + ForEach( 0, BigPtrArray::Count(), fnForEach, pArgs ); } + void ForEach( sal_uLong nStt, sal_uLong nEnd, FnForEach_SwNodes fnForEach, void* pArgs = 0 ); void ForEach( const SwNodeIndex& rStart, const SwNodeIndex& rEnd, FnForEach_SwNodes fnForEach, void* pArgs = 0 ); diff --git a/sw/qa/core/Test-BigPtrArray.cxx b/sw/qa/core/Test-BigPtrArray.cxx index 18749d8164a7..bc1d824c9b40 100644 --- a/sw/qa/core/Test-BigPtrArray.cxx +++ b/sw/qa/core/Test-BigPtrArray.cxx @@ -30,6 +30,7 @@ #include <stdlib.h> #include <bparr.hxx> +#include <ndarr.hxx> using namespace std; @@ -53,11 +54,6 @@ namespace /* private */ return count_; } - void setCount(sal_uLong newCount) - { - count_ = newCount; - } - sal_uLong Position() const { return GetPos(); @@ -67,13 +63,6 @@ namespace /* private */ sal_uLong count_; }; - bool AddToCount(const ElementPtr& rElem, void* pArgs) - { - BigPtrEntryMock* const pbem = static_cast<BigPtrEntryMock* const>(rElem); - pbem->setCount(pbem->getCount() + *((sal_uLong*)pArgs)); - return true; - } - void dumpBigPtrArray(const BigPtrArray& bparr) { (void)bparr; @@ -591,124 +580,6 @@ public: releaseBigPtrArrayContent(bparr); } - void test_for_each() - { - printMethodName("test_for_each\n"); - - BigPtrArray bparr; - - fillBigPtrArray(bparr, NUM_ENTRIES); - dumpBigPtrArray(bparr); - - sal_uLong addCount = 1; - bparr.ForEach(AddToCount, &addCount); - - for (sal_uLong i = 0; i < NUM_ENTRIES; i++) - { - CPPUNIT_ASSERT_MESSAGE - ( - "test_for_each failed", - static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i+1) - ); - } - - releaseBigPtrArrayContent(bparr); - dumpBigPtrArray(bparr); - } - - void test_for_some1() - { - printMethodName("test_for_some1\n"); - - BigPtrArray bparr; - - fillBigPtrArray(bparr, NUM_ENTRIES); - dumpBigPtrArray(bparr); - - sal_uLong addCount = 1; - bparr.ForEach(0, NUM_ENTRIES / 2, AddToCount, &addCount); - - sal_uLong i = 0; - for (/* */; i < NUM_ENTRIES / 2; i++) - { - CPPUNIT_ASSERT_MESSAGE - ( - "test_for_some1 failed", - static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i+1) - ); - } - - for (/* */; i < NUM_ENTRIES; i++) - { - CPPUNIT_ASSERT_MESSAGE - ( - "test_for_some1 failed", - static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i) - ); - } - - releaseBigPtrArrayContent(bparr); - dumpBigPtrArray(bparr); - } - - void test_for_some2() - { - printMethodName("test_for_some2\n"); - - BigPtrArray bparr; - - fillBigPtrArray(bparr, NUM_ENTRIES); - dumpBigPtrArray(bparr); - - sal_uLong addCount = 1; - bparr.ForEach(NUM_ENTRIES / 2, NUM_ENTRIES, AddToCount, &addCount); - - sal_uLong i = 0; - for (/* */; i < NUM_ENTRIES / 2; i++) - { - CPPUNIT_ASSERT_MESSAGE - ( - "test_for_some2 failed", - static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i) - ); - } - - for (/* */; i < NUM_ENTRIES; i++) - { - CPPUNIT_ASSERT_MESSAGE - ( - "test_for_some2 failed", - static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == (i+1) - ); - } - - releaseBigPtrArrayContent(bparr); - dumpBigPtrArray(bparr); - } - - void test_for_some3() - { - printMethodName("test_for_some3\n"); - - BigPtrArray bparr; - - fillBigPtrArray(bparr, NUM_ENTRIES); - dumpBigPtrArray(bparr); - - sal_uLong addCount = 1; - bparr.ForEach(0, 0, AddToCount, &addCount); - - for (sal_uLong i = 0; i < NUM_ENTRIES; i++) - { - CPPUNIT_ASSERT_MESSAGE - ( - "test_for_some3 failed", - static_cast<BigPtrEntryMock*>(bparr[i])->getCount() == i - ); - } - releaseBigPtrArrayContent(bparr); - } - CPPUNIT_TEST_SUITE(BigPtrArrayUnittest); CPPUNIT_TEST(test_ctor); CPPUNIT_TEST(test_insert_entries_at_front); @@ -724,10 +595,6 @@ public: CPPUNIT_TEST(test_move_elements_from_higher_to_lower_pos); CPPUNIT_TEST(test_move_to_same_position); CPPUNIT_TEST(test_replace_elements); - CPPUNIT_TEST(test_for_each); - CPPUNIT_TEST(test_for_some1); - CPPUNIT_TEST(test_for_some2); - CPPUNIT_TEST(test_for_some3); CPPUNIT_TEST_SUITE_END(); }; diff --git a/sw/source/core/bastyp/bparr.cxx b/sw/source/core/bastyp/bparr.cxx index 935a35b366a7..53fb2f525e46 100644 --- a/sw/source/core/bastyp/bparr.cxx +++ b/sw/source/core/bastyp/bparr.cxx @@ -81,44 +81,6 @@ void BigPtrArray::Move( sal_uLong from, sal_uLong to ) } } -/** Apply function to every element. - - @param nStart First element to start with - @param nEnd Last element (exclusive!) - @param fn Function - @param pArgs Additional arguments for <fn> -*/ -void BigPtrArray::ForEach( sal_uLong nStart, sal_uLong nEnd, - FnForEach fn, void* pArgs ) -{ - if( nEnd > nSize ) - nEnd = nSize; - - if( nStart < nEnd ) - { - sal_uInt16 cur = Index2Block( nStart ); - BlockInfo** pp = ppInf + cur; - BlockInfo* p = *pp; - sal_uInt16 nElem = sal_uInt16( nStart - p->nStart ); - ElementPtr* pElem = p->pData + nElem; - nElem = p->nElem - nElem; - for(;;) - { - if( !(*fn)( *pElem++, pArgs ) || ++nStart >= nEnd ) - break; - - // next element - if( !--nElem ) - { - // new block - p = *++pp; - pElem = p->pData; - nElem = p->nElem; - } - } - } -} - ElementPtr BigPtrArray::operator[]( sal_uLong idx ) const { assert(idx < nSize); // operator[]: Index out of bounds diff --git a/sw/source/core/docnode/nodes.cxx b/sw/source/core/docnode/nodes.cxx index 0479332d86a0..169c78fc9fb6 100644 --- a/sw/source/core/docnode/nodes.cxx +++ b/sw/source/core/docnode/nodes.cxx @@ -2176,11 +2176,41 @@ SwNode* SwNodes::FindPrvNxtFrmNode( SwNodeIndex& rFrmIdx, return pFrmNd; } +void SwNodes::ForEach( sal_uLong nStart, sal_uLong nEnd, + FnForEach_SwNodes fn, void* pArgs ) +{ + if( nEnd > nSize ) + nEnd = nSize; + + if( nStart < nEnd ) + { + sal_uInt16 cur = Index2Block( nStart ); + BlockInfo** pp = ppInf + cur; + BlockInfo* p = *pp; + sal_uInt16 nElem = sal_uInt16( nStart - p->nStart ); + ElementPtr* pElem = p->pData + nElem; + nElem = p->nElem - nElem; + for(;;) + { + if( !(*fn)( static_cast<SwNode *>(*pElem++), pArgs ) || ++nStart >= nEnd ) + break; + + // next element + if( !--nElem ) + { + // new block + p = *++pp; + pElem = p->pData; + nElem = p->nElem; + } + } + } +} + void SwNodes::ForEach( const SwNodeIndex& rStart, const SwNodeIndex& rEnd, FnForEach_SwNodes fnForEach, void* pArgs ) { - BigPtrArray::ForEach( rStart.GetIndex(), rEnd.GetIndex(), - (FnForEach) fnForEach, pArgs ); + ForEach( rStart.GetIndex(), rEnd.GetIndex(), fnForEach, pArgs ); } namespace { |