Keep order of nodes when wxHashMap is resized; this ensures
that wxHashTable has the same behaviour when wxUSE_STL=1 as when wxUSE_STL=0. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@23052 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -70,6 +70,7 @@ protected:
|
|||||||
static void CopyHashTable( _wxHashTable_NodeBase** srcTable,
|
static void CopyHashTable( _wxHashTable_NodeBase** srcTable,
|
||||||
size_t srcBuckets, _wxHashTableBase2* dst,
|
size_t srcBuckets, _wxHashTableBase2* dst,
|
||||||
_wxHashTable_NodeBase** dstTable,
|
_wxHashTable_NodeBase** dstTable,
|
||||||
|
size_t dstBuckets,
|
||||||
BucketFromNode func, ProcessNode proc );
|
BucketFromNode func, ProcessNode proc );
|
||||||
|
|
||||||
static void** AllocTable( size_t sz )
|
static void** AllocTable( size_t sz )
|
||||||
@@ -357,7 +358,7 @@ protected: \
|
|||||||
m_tableBuckets = newSize; \
|
m_tableBuckets = newSize; \
|
||||||
\
|
\
|
||||||
CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
|
CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
|
||||||
this, (_wxHashTable_NodeBase**)m_table, \
|
this, (_wxHashTable_NodeBase**)m_table, newSize, \
|
||||||
(BucketFromNode)GetBucketForNode,\
|
(BucketFromNode)GetBucketForNode,\
|
||||||
(ProcessNode)&DummyProcessNode ); \
|
(ProcessNode)&DummyProcessNode ); \
|
||||||
free(srcTable); \
|
free(srcTable); \
|
||||||
@@ -369,7 +370,7 @@ protected: \
|
|||||||
ResizeTable( ht.size() ); \
|
ResizeTable( ht.size() ); \
|
||||||
CopyHashTable( (_wxHashTable_NodeBase**)ht.m_table, ht.m_tableBuckets,\
|
CopyHashTable( (_wxHashTable_NodeBase**)ht.m_table, ht.m_tableBuckets,\
|
||||||
(_wxHashTableBase2*)this, \
|
(_wxHashTableBase2*)this, \
|
||||||
(_wxHashTable_NodeBase**)m_table, \
|
(_wxHashTable_NodeBase**)m_table, m_tableBuckets, \
|
||||||
(BucketFromNode)GetBucketForNode, \
|
(BucketFromNode)GetBucketForNode, \
|
||||||
(ProcessNode)CopyNode ); \
|
(ProcessNode)CopyNode ); \
|
||||||
} \
|
} \
|
||||||
|
@@ -1151,7 +1151,7 @@ static void TestHash()
|
|||||||
wxPuts(_T("*** Testing wxHashTable ***\n"));
|
wxPuts(_T("*** Testing wxHashTable ***\n"));
|
||||||
|
|
||||||
{
|
{
|
||||||
wxHashTable hash(wxKEY_INTEGER), hash2(wxKEY_STRING);
|
wxHashTable hash(wxKEY_INTEGER, 10), hash2(wxKEY_STRING);
|
||||||
wxObject o;
|
wxObject o;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@@ -128,9 +128,20 @@ void _wxHashTableBase2::CopyHashTable( _wxHashTable_NodeBase** srcTable,
|
|||||||
size_t srcBuckets,
|
size_t srcBuckets,
|
||||||
_wxHashTableBase2* dst,
|
_wxHashTableBase2* dst,
|
||||||
_wxHashTable_NodeBase** dstTable,
|
_wxHashTable_NodeBase** dstTable,
|
||||||
|
size_t dstBuckets,
|
||||||
BucketFromNode func, ProcessNode proc )
|
BucketFromNode func, ProcessNode proc )
|
||||||
{
|
{
|
||||||
for( size_t i = 0; i < srcBuckets; ++i )
|
// for compatibility with wxHashTable (to avoid reimplementig it
|
||||||
|
// from scratch), we need to preserve the order of nodes in a
|
||||||
|
// source bucket when copying the table, hence, to avoid
|
||||||
|
// allocating an auxiliary table we use a circular list for each
|
||||||
|
// bucket, and we keep the *tail* of each list in dstTable[i], to
|
||||||
|
// be able to append nodes in O(1) time. Wen we're done copying,
|
||||||
|
// we adjust dstTable[i] to point at the head of the list and we
|
||||||
|
// break the circular list into a linear one.
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for( i = 0; i < srcBuckets; ++i )
|
||||||
{
|
{
|
||||||
_wxHashTable_NodeBase* nextnode;
|
_wxHashTable_NodeBase* nextnode;
|
||||||
|
|
||||||
@@ -140,8 +151,24 @@ void _wxHashTableBase2::CopyHashTable( _wxHashTable_NodeBase** srcTable,
|
|||||||
|
|
||||||
nextnode = node->m_nxt;
|
nextnode = node->m_nxt;
|
||||||
_wxHashTable_NodeBase* newnode = proc( node );
|
_wxHashTable_NodeBase* newnode = proc( node );
|
||||||
newnode->m_nxt = dstTable[bucket];
|
if( dstTable[bucket] )
|
||||||
dstTable[bucket] = newnode;
|
{
|
||||||
|
newnode->m_nxt = dstTable[bucket]->m_nxt; // head of the list
|
||||||
|
dstTable[bucket]->m_nxt = newnode;
|
||||||
|
dstTable[bucket] = newnode;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
dstTable[bucket] = newnode->m_nxt = newnode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for( i = 0; i < dstBuckets; ++i )
|
||||||
|
{
|
||||||
|
if( dstTable[i] )
|
||||||
|
{
|
||||||
|
_wxHashTable_NodeBase* tmp = dstTable[i];
|
||||||
|
dstTable[i] = dstTable[i]->m_nxt;
|
||||||
|
tmp->m_nxt = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user