Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Development
openfoam
Commits
c7c9a6e0
Commit
c7c9a6e0
authored
Nov 04, 2009
by
Mark Olesen
Browse files
Activate the reworked HashTable
- previous draft version was HashTbl - accidentally still had canonicalSize in templated code
parent
ebe39c4e
Changes
11
Hide whitespace changes
Inline
Side-by-side
src/OpenFOAM/Make/files
View file @
c7c9a6e0
...
...
@@ -61,8 +61,7 @@ $(sha1)/SHA1Digest.C
primitives/random/Random.C
containers/HashTables/HashTbl/HashTblCore.C
containers/HashTables/HashTable/HashTableName.C
containers/HashTables/HashTable/HashTableCore.C
containers/HashTables/StaticHashTable/StaticHashTableCore.C
containers/Lists/SortableList/ParSortableListName.C
containers/Lists/PackedList/PackedListName.C
...
...
src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
View file @
c7c9a6e0
...
...
@@ -30,44 +30,15 @@ License
#include
"HashTable.H"
#include
"List.H"
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
template
<
class
T
,
class
Key
,
class
Hash
>
Foam
::
label
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
canonicalSize
(
const
label
size
)
{
if
(
size
<
1
)
{
return
0
;
}
// enforce power of two
unsigned
int
goodSize
=
size
;
if
(
goodSize
&
(
goodSize
-
1
))
{
// brute-force is fast enough
goodSize
=
1
;
while
(
goodSize
<
unsigned
(
size
))
{
goodSize
<<=
1
;
}
}
return
goodSize
;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template
<
class
T
,
class
Key
,
class
Hash
>
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
HashTable
(
const
label
size
)
:
HashTable
Nam
e
(),
HashTable
Cor
e
(),
nElmts_
(
0
),
tableSize_
(
canonicalSize
(
size
)),
table_
(
NULL
),
endIter_
(
*
this
,
NULL
,
0
),
endConstIter_
(
*
this
,
NULL
,
0
)
tableSize_
(
HashTableCore
::
canonicalSize
(
size
)),
table_
(
NULL
)
{
if
(
tableSize_
)
{
...
...
@@ -84,12 +55,10 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size)
template
<
class
T
,
class
Key
,
class
Hash
>
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
HashTable
(
const
HashTable
<
T
,
Key
,
Hash
>&
ht
)
:
HashTable
Nam
e
(),
HashTable
Cor
e
(),
nElmts_
(
0
),
tableSize_
(
ht
.
tableSize_
),
table_
(
NULL
),
endIter_
(
*
this
,
NULL
,
0
),
endConstIter_
(
*
this
,
NULL
,
0
)
table_
(
NULL
)
{
if
(
tableSize_
)
{
...
...
@@ -113,12 +82,10 @@ Foam::HashTable<T, Key, Hash>::HashTable
const
Xfer
<
HashTable
<
T
,
Key
,
Hash
>
>&
ht
)
:
HashTable
Nam
e
(),
HashTable
Cor
e
(),
nElmts_
(
0
),
tableSize_
(
0
),
table_
(
NULL
),
endIter_
(
*
this
,
NULL
,
0
),
endConstIter_
(
*
this
,
NULL
,
0
)
table_
(
NULL
)
{
transfer
(
ht
());
}
...
...
@@ -182,7 +149,7 @@ Foam::HashTable<T, Key, Hash>::find
{
if
(
key
==
ep
->
key_
)
{
return
iterator
(
*
this
,
ep
,
hashIdx
);
return
iterator
(
this
,
ep
,
hashIdx
);
}
}
}
...
...
@@ -195,7 +162,7 @@ Foam::HashTable<T, Key, Hash>::find
}
# endif
return
end
();
return
iterator
();
}
...
...
@@ -214,7 +181,7 @@ Foam::HashTable<T, Key, Hash>::find
{
if
(
key
==
ep
->
key_
)
{
return
const_iterator
(
*
this
,
ep
,
hashIdx
);
return
const_iterator
(
this
,
ep
,
hashIdx
);
}
}
}
...
...
@@ -227,32 +194,32 @@ Foam::HashTable<T, Key, Hash>::find
}
# endif
return
c
end
();
return
c
onst_iterator
();
}
template
<
class
T
,
class
Key
,
class
Hash
>
Foam
::
List
<
Key
>
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
toc
()
const
{
List
<
Key
>
tofc
(
nElmts_
);
label
i
=
0
;
List
<
Key
>
keys
(
nElmts_
);
label
keyI
=
0
;
for
(
const_iterator
iter
=
cbegin
();
iter
!=
cend
();
++
iter
)
{
tofc
[
i
++
]
=
iter
.
key
();
keys
[
keyI
++
]
=
iter
.
key
();
}
return
tofc
;
return
keys
;
}
template
<
class
T
,
class
Key
,
class
Hash
>
Foam
::
List
<
Key
>
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
sortedToc
()
const
{
List
<
Key
>
sortedL
i
st
=
this
->
toc
();
sort
(
sortedL
i
st
);
List
<
Key
>
sortedLst
=
this
->
toc
();
sort
(
sortedLst
);
return
sortedL
i
st
;
return
sortedLst
;
}
...
...
@@ -290,7 +257,7 @@ bool Foam::HashTable<T, Key, Hash>::set
table_
[
hashIdx
]
=
new
hashedEntry
(
key
,
table_
[
hashIdx
],
newEntry
);
nElmts_
++
;
if
(
double
(
nElmts_
)
/
tableSize_
>
0
.
8
)
if
(
double
(
nElmts_
)
/
tableSize_
>
0
.
8
&&
tableSize_
<
maxTableSize
)
{
# ifdef FULLDEBUG
if
(
debug
)
...
...
@@ -342,18 +309,22 @@ bool Foam::HashTable<T, Key, Hash>::set
template
<
class
T
,
class
Key
,
class
Hash
>
bool
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
era
se
(
const
iterator
&
cit
)
bool
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
it
era
torBase
::
erase
(
)
{
if
(
cit
.
elmtPtr_
)
// note: endIter_ also has 0 elmtPtr_
// note: entryPtr_ is NULL for end(), so this catches that too
if
(
entryPtr_
)
{
iterator
&
it
=
const_cast
<
iterator
&>
(
cit
);
// Search element before elmtPtr_
// Search element before entryPtr_
hashedEntry
*
prev
=
0
;
for
(
hashedEntry
*
ep
=
table_
[
it
.
hashIndex_
];
ep
;
ep
=
ep
->
next_
)
for
(
hashedEntry
*
ep
=
hashTable_
->
table_
[
hashIndex_
];
ep
;
ep
=
ep
->
next_
)
{
if
(
ep
==
it
.
elmt
Ptr_
)
if
(
ep
==
entry
Ptr_
)
{
break
;
}
...
...
@@ -362,98 +333,76 @@ bool Foam::HashTable<T, Key, Hash>::erase(const iterator& cit)
if
(
prev
)
{
//
Have
element before e
lmtPtr
prev
->
next_
=
it
.
elmt
Ptr_
->
next_
;
delete
it
.
elmt
Ptr_
;
it
.
elmt
Ptr_
=
prev
;
//
has an
element before e
ntryPtr - reposition to there
prev
->
next_
=
entry
Ptr_
->
next_
;
delete
entry
Ptr_
;
entry
Ptr_
=
prev
;
}
else
{
// elmtPtr is first element on SLList
table_
[
it
.
hashIndex_
]
=
it
.
elmtPtr_
->
next_
;
delete
it
.
elmtPtr_
;
// Search back for previous non-zero table entry
while
(
--
it
.
hashIndex_
>=
0
&&
!
table_
[
it
.
hashIndex_
])
{}
if
(
it
.
hashIndex_
>=
0
)
{
// In table entry search for last element
it
.
elmtPtr_
=
table_
[
it
.
hashIndex_
];
while
(
it
.
elmtPtr_
&&
it
.
elmtPtr_
->
next_
)
{
it
.
elmtPtr_
=
it
.
elmtPtr_
->
next_
;
}
}
else
{
// No previous found. Mark with special value which is
// - not end()/cend()
// - handled by operator++
it
.
elmtPtr_
=
reinterpret_cast
<
hashedEntry
*>
(
this
);
it
.
hashIndex_
=
-
1
;
}
// entryPtr was first element on SLList
hashTable_
->
table_
[
hashIndex_
]
=
entryPtr_
->
next_
;
delete
entryPtr_
;
// assign any non-NULL pointer value so it doesn't look
// like end()/cend()
entryPtr_
=
reinterpret_cast
<
hashedEntry
*>
(
this
);
// Mark with special hashIndex value to signal it has been rewound.
// The next increment will bring it back to the present location.
//
// From the current position 'curPos', we wish to continue at
// prevPos='curPos-1', which we mark as markPos='-curPos-1'.
// The negative lets us notice it is special, the extra '-1'
// is needed to avoid ambiguity for position '0'.
// To retrieve prevPos, we would later use '-(markPos+1) - 1'
hashIndex_
=
-
hashIndex_
-
1
;
}
nElmts_
--
;
# ifdef FULLDEBUG
if
(
debug
)
{
Info
<<
"HashTable<T, Key, Hash>::erase(iterator&) : "
<<
"hashedEntry "
<<
it
.
elmtPtr_
->
key_
<<
" removed.
\n
"
;
}
# endif
hashTable_
->
nElmts_
--
;
return
true
;
}
else
{
# ifdef FULLDEBUG
if
(
debug
)
{
Info
<<
"HashTable<T, Key, Hash>::erase(iterator&) : "
<<
"cannot remove hashedEntry from hash table
\n
"
;
}
# endif
return
false
;
}
}
// NOTE:
// We use (const iterator&) here, but manipulate its contents anyhow.
// The parameter should be (iterator&), but then the compiler doesn't find
// it correctly and tries to call as (iterator) instead.
//
template
<
class
T
,
class
Key
,
class
Hash
>
bool
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
erase
(
const
Key
&
key
)
bool
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
erase
(
const
iterator
&
iter
)
{
iterator
fnd
=
find
(
key
);
// adjust iterator after erase
return
const_cast
<
iterator
&>
(
iter
).
erase
();
}
if
(
fnd
!=
end
())
{
return
erase
(
fnd
);
}
else
{
return
false
;
}
template
<
class
T
,
class
Key
,
class
Hash
>
bool
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
erase
(
const
Key
&
key
)
{
return
erase
(
find
(
key
));
}
template
<
class
T
,
class
Key
,
class
Hash
>
Foam
::
label
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
erase
(
const
UList
<
Key
>&
keys
)
{
const
label
nTotal
=
nElmts_
;
label
count
=
0
;
// Remove listed keys from this table
if
(
this
->
size
()
)
// Remove listed keys from this table
- terminates early if possible
for
(
label
keyI
=
0
;
count
<
nTotal
&&
keyI
<
keys
.
size
();
++
keyI
)
{
forAll
(
keys
,
keyI
)
if
(
erase
(
keys
[
keyI
])
)
{
if
(
erase
(
keys
[
keyI
]))
{
count
++
;
}
count
++
;
}
}
...
...
@@ -462,24 +411,21 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase(const UList<Key>& keys)
template
<
class
T
,
class
Key
,
class
Hash
>
template
<
class
AnyType
>
template
<
class
AnyType
,
class
AnyHash
>
Foam
::
label
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
erase
(
const
HashTable
<
AnyType
,
Key
,
Hash
>&
rhs
const
HashTable
<
AnyType
,
Key
,
Any
Hash
>&
rhs
)
{
label
count
=
0
;
// Remove rhs elements from this table
if
(
this
->
size
())
// Remove rhs keys from this table - terminates early if possible
// Could optimize depending on which hash is smaller ...
for
(
iterator
iter
=
begin
();
iter
!=
end
();
++
iter
)
{
// NOTE: could further optimize depending on which hash is smaller
for
(
iterator
iter
=
begin
();
iter
!=
end
();
++
iter
)
if
(
rhs
.
found
(
iter
.
key
())
&&
erase
(
iter
))
{
if
(
rhs
.
found
(
iter
.
key
())
&&
erase
(
iter
))
{
count
++
;
}
count
++
;
}
}
...
...
@@ -490,7 +436,7 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase
template
<
class
T
,
class
Key
,
class
Hash
>
void
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
resize
(
const
label
sz
)
{
label
newSize
=
canonicalSize
(
sz
);
label
newSize
=
HashTableCore
::
canonicalSize
(
sz
);
if
(
newSize
==
tableSize_
)
{
...
...
@@ -505,22 +451,22 @@ void Foam::HashTable<T, Key, Hash>::resize(const label sz)
return
;
}
HashTable
<
T
,
Key
,
Hash
>*
new
Table
=
new
HashTable
<
T
,
Key
,
Hash
>
(
newSize
);
HashTable
<
T
,
Key
,
Hash
>*
tmp
Table
=
new
HashTable
<
T
,
Key
,
Hash
>
(
newSize
);
for
(
const_iterator
iter
=
cbegin
();
iter
!=
cend
();
++
iter
)
{
new
Table
->
insert
(
iter
.
key
(),
*
iter
);
tmp
Table
->
insert
(
iter
.
key
(),
*
iter
);
}
label
old
Table
Size
=
tableSize_
;
tableSize_
=
new
Table
->
tableSize_
;
new
Table
->
tableSize_
=
old
Table
Size
;
label
oldSize
=
tableSize_
;
tableSize_
=
tmp
Table
->
tableSize_
;
tmp
Table
->
tableSize_
=
oldSize
;
hashedEntry
**
oldTable
=
table_
;
table_
=
new
Table
->
table_
;
new
Table
->
table_
=
oldTable
;
table_
=
tmp
Table
->
table_
;
tmp
Table
->
table_
=
oldTable
;
delete
new
Table
;
delete
tmp
Table
;
}
...
...
@@ -556,6 +502,19 @@ void Foam::HashTable<T, Key, Hash>::clearStorage()
}
template
<
class
T
,
class
Key
,
class
Hash
>
void
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
shrink
()
{
const
label
newSize
=
HashTableCore
::
canonicalSize
(
nElmts_
);
if
(
newSize
<
tableSize_
)
{
// avoid having the table disappear on us
resize
(
newSize
?
newSize
:
2
);
}
}
template
<
class
T
,
class
Key
,
class
Hash
>
void
Foam
::
HashTable
<
T
,
Key
,
Hash
>::
transfer
(
HashTable
<
T
,
Key
,
Hash
>&
ht
)
{
...
...
@@ -619,18 +578,12 @@ bool Foam::HashTable<T, Key, Hash>::operator==
const
HashTable
<
T
,
Key
,
Hash
>&
rhs
)
const
{
//
Are all my elements in rhs?
for
(
const_iterator
iter
=
cbegin
();
iter
!=
cend
();
++
iter
)
//
sizes (number of keys) must match
if
(
size
()
!=
rhs
.
size
()
)
{
const_iterator
fnd
=
rhs
.
find
(
iter
.
key
());
if
(
fnd
==
rhs
.
cend
()
||
fnd
()
!=
iter
())
{
return
false
;
}
return
false
;
}
// Are all rhs elements in me?
for
(
const_iterator
iter
=
rhs
.
cbegin
();
iter
!=
rhs
.
cend
();
++
iter
)
{
const_iterator
fnd
=
find
(
iter
.
key
());
...
...
@@ -640,6 +593,7 @@ bool Foam::HashTable<T, Key, Hash>::operator==
return
false
;
}
}
return
true
;
}
...
...
src/OpenFOAM/containers/HashTables/HashTable/HashTable.H
View file @
c7c9a6e0
...
...
@@ -71,23 +71,60 @@ Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
/*---------------------------------------------------------------------------*\
Class HashTable
Nam
e Declaration
Class HashTable
Cor
e Declaration
\*---------------------------------------------------------------------------*/
TemplateName
(
HashTable
);
//- Template-invariant bits for HashTable
struct
HashTableCore
{
//- Return a canonical (power-of-two) size
static
label
canonicalSize
(
const
label
);
//- Maximum allowable table size
static
const
label
maxTableSize
;
//- Construct null
HashTableCore
()
{}
//- Define template name and debug
ClassName
(
"HashTable"
);
//- A zero-sized end iterator
struct
iteratorEnd
{
//- Construct null
iteratorEnd
()
{}
};
//- iteratorEnd set to beyond the end of any HashTable
inline
static
iteratorEnd
cend
()
{
return
iteratorEnd
();
}
//- iteratorEnd set to beyond the end of any HashTable
inline
static
iteratorEnd
end
()
{
return
iteratorEnd
();
}
};
/*---------------------------------------------------------------------------*\
Class HashTable Declaration
Class HashTable Declaration
\*---------------------------------------------------------------------------*/
template
<
class
T
,
class
Key
=
word
,
class
Hash
=
string
::
hash
>
class
HashTable
:
public
HashTable
Nam
e
public
HashTable
Cor
e
{
// Private data type for table entries
//- Structure to hold a hashed entry with SLList for collisions
struct
hashedEntry
{
//- The lookup key
...
...
@@ -99,18 +136,15 @@ class HashTable
//- The data object
T
obj_
;
//- Constructors
//- Construct from key, next pointer and object
inline
hashedEntry
(
const
Key
&
,
hashedEntry
*
next
,
const
T
&
);
//- Construct given key, next pointer and object
inline
hashedEntry
(
const
Key
&
,
hashedEntry
*
next
,
const
T
&
newEntry
);
private:
//- Disallow default bitwise copy construct
hashedEntry
(
const
hashedEntry
&
);
//- Dis
s
allow
construction as copy
hashedEntry
(
const
hashedEntry
&
);
//- Disallow
default bitwise assignment
void
operator
=
(
const
hashedEntry
&
);
};
...
...
@@ -119,7 +153,7 @@ class HashTable
//- The current number of elements in table
label
nElmts_
;
//- Number of primary entries allocated in table
(not necessarily used)
//- Number of primary entries allocated in table
label
tableSize_
;
//- The table of primary entries
...
...
@@ -140,17 +174,23 @@ class HashTable
public:
// Forward declaration of iterators
class
iteratorBase
;
class
iterator
;
class
const_iterator
;
//- Declare friendship with the HashPtrTable class
template
<
class
T2
,
class
Key2
,
class
Hash2
>
friend
class
HashPtrTable
;
//- Declare friendship with the iteratorBase
friend
class
iteratorBase
;
// Forward declaration of STL iterators
class
iterator
;
//- Declare friendship with the iterator
friend
class
iterator
;
class
const_iterator
;
//- Declare friendship with the
const_iterator
friend
class
const_iterator
;
...
...
@@ -178,7 +218,10 @@ public:
// Access
//- Return number of elements in table.
//- The size of the underlying table
inline
label
capacity
()
const
;
//- Return number of elements in table
inline
label
size
()
const
;
//- Return true if the hash table is empty
...
...
@@ -212,10 +255,11 @@ public:
//- Assign a new hashedEntry, overwriting existing entries
inline
bool
set
(
const
Key
&
,
const
T
&
newElmt
);
//- Erase an hashedEntry specified by given iterator
//- Erase a hashedEntry specified by given iterator
// This invalidates the iterator until the next operator++
bool
erase
(
const
iterator
&
);
//- Erase a
n
hashedEntry specified by given key
if in table
//- Erase a hashedEntry specified by
the
given key
bool
erase
(
const
Key
&
);
//- Remove entries given by the listed keys from this HashTable
...
...
@@ -224,10 +268,10 @@ public:
//- Remove entries given by the given keys from this HashTable
// Return the number of elements removed.