Kaydet (Commit) a47b51b4 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Avoid fn ptr casts in SwNodes::ForEeach

...by moving ForEach from BigPtrArray down to its sole user SwNodes.

Change-Id: I6d326d3e1ac9dc6ac820e600973af420785d5a50
üst b34ddf07
......@@ -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
......
......@@ -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 );
......
......@@ -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();
};
......
......@@ -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
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment