Skip to content
GitLab
Menu
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
a237b7ce
Commit
a237b7ce
authored
Nov 01, 2009
by
Mark Olesen
Browse files
HashTbl - iterator/const_iterator implemented in terms of an iteratorBase
parent
3a0c427c
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C
View file @
a237b7ce
...
...
@@ -65,9 +65,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl(const label size)
HashTblName
(),
nElmts_
(
0
),
tableSize_
(
canonicalSize
(
size
)),
table_
(
NULL
),
endIter_
(),
endConstIter_
()
table_
(
NULL
)
{
if
(
tableSize_
)
{
...
...
@@ -87,9 +85,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl(const HashTbl<T, Key, Hash>& ht)
HashTblName
(),
nElmts_
(
0
),
tableSize_
(
ht
.
tableSize_
),
table_
(
NULL
),
endIter_
(),
endConstIter_
()
table_
(
NULL
)
{
if
(
tableSize_
)
{
...
...
@@ -116,9 +112,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl
HashTblName
(),
nElmts_
(
0
),
tableSize_
(
0
),
table_
(
NULL
),
endIter_
(),
endConstIter_
()
table_
(
NULL
)
{
transfer
(
ht
());
}
...
...
@@ -344,10 +338,10 @@ bool Foam::HashTbl<T, Key, Hash>::set
template
<
class
T
,
class
Key
,
class
Hash
>
bool
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
erase
()
{
// note: e
lmt
Ptr_ is NULL for end(), so this catches that too
if
(
e
lmt
Ptr_
)
// note: e
ntry
Ptr_ is NULL for end(), so this catches that too
if
(
e
ntry
Ptr_
)
{
// Search element before e
lmt
Ptr_
// Search element before e
ntry
Ptr_
hashedEntry
*
prev
=
0
;
for
...
...
@@ -357,7 +351,7 @@ bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
ep
=
ep
->
next_
)
{
if
(
ep
==
e
lmt
Ptr_
)
if
(
ep
==
e
ntry
Ptr_
)
{
break
;
}
...
...
@@ -366,19 +360,20 @@ bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
if
(
prev
)
{
// has an element before e
lmt
Ptr - reposition to there
prev
->
next_
=
e
lmt
Ptr_
->
next_
;
delete
e
lmt
Ptr_
;
e
lmt
Ptr_
=
prev
;
// has an element before e
ntry
Ptr - reposition to there
prev
->
next_
=
e
ntry
Ptr_
->
next_
;
delete
e
ntry
Ptr_
;
e
ntry
Ptr_
=
prev
;
}
else
{
// e
lmt
Ptr was first element on SLList
hashTable_
->
table_
[
hashIndex_
]
=
e
lmt
Ptr_
->
next_
;
delete
e
lmt
Ptr_
;
// e
ntry
Ptr was first element on SLList
hashTable_
->
table_
[
hashIndex_
]
=
e
ntry
Ptr_
->
next_
;
delete
e
ntry
Ptr_
;
// assign any non-NULL value so it doesn't look like end()/cend()
elmtPtr_
=
reinterpret_cast
<
hashedEntry
*>
(
hashTable_
);
// 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.
...
...
@@ -411,92 +406,15 @@ bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
template
<
class
T
,
class
Key
,
class
Hash
>
bool
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
erase
(
const
iterator
&
cit
)
{
// note: elmtPtr_ is NULL for end(), so this catches that too
if
(
cit
.
elmtPtr_
)
{
// Search element before elmtPtr_
hashedEntry
*
prev
=
0
;
for
(
hashedEntry
*
ep
=
table_
[
cit
.
hashIndex_
];
ep
;
ep
=
ep
->
next_
)
{
if
(
ep
==
cit
.
elmtPtr_
)
{
break
;
}
prev
=
ep
;
}
// adjust iterator after erase
iterator
&
iter
=
const_cast
<
iterator
&>
(
cit
);
if
(
prev
)
{
// has an element before elmtPtr - reposition to there
prev
->
next_
=
iter
.
elmtPtr_
->
next_
;
delete
iter
.
elmtPtr_
;
iter
.
elmtPtr_
=
prev
;
}
else
{
// elmtPtr was first element on SLList
table_
[
iter
.
hashIndex_
]
=
iter
.
elmtPtr_
->
next_
;
delete
iter
.
elmtPtr_
;
// assign an non-NULL value so it doesn't look like end()/cend()
iter
.
elmtPtr_
=
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.
//
// For the current position 'X', mark it as '-(X+1)', which is
// written as '-X-1' to avoid overflow.
// The extra '-1' is needed to avoid ambiguity for position '0'.
// To retrieve the previous position 'X-1' we would later
// use '-(-X-1) - 2'
iter
.
hashIndex_
=
-
iter
.
hashIndex_
-
1
;
}
nElmts_
--
;
# ifdef FULLDEBUG
if
(
debug
)
{
Info
<<
"HashTbl<T, Key, Hash>::erase(const iterator&) : "
<<
"hashedEntry "
<<
iter
.
elmtPtr_
->
key_
<<
" removed.
\n
"
;
}
# endif
return
true
;
}
else
{
# ifdef FULLDEBUG
if
(
debug
)
{
Info
<<
"HashTbl<T, Key, Hash>::erase(const iterator&) : "
<<
"cannot remove hashedEntry from hash table
\n
"
;
}
# endif
return
false
;
}
// adjust iterator after erase
return
const_cast
<
iterator
&>
(
cit
).
erase
();
}
template
<
class
T
,
class
Key
,
class
Hash
>
bool
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
erase
(
const
Key
&
key
)
{
// we could also simply do: erase(find(key));
iterator
fnd
=
find
(
key
);
if
(
fnd
!=
end
())
{
return
erase
(
fnd
);
}
else
{
return
false
;
}
return
erase
(
find
(
key
));
}
...
...
@@ -560,22 +478,22 @@ void Foam::HashTbl<T, Key, Hash>::resize(const label sz)
return
;
}
HashTbl
<
T
,
Key
,
Hash
>*
new
Table
=
new
HashTbl
<
T
,
Key
,
Hash
>
(
newSize
);
HashTbl
<
T
,
Key
,
Hash
>*
tmp
Table
=
new
HashTbl
<
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
;
}
...
...
@@ -687,7 +605,7 @@ bool Foam::HashTbl<T, Key, Hash>::operator==
const
HashTbl
<
T
,
Key
,
Hash
>&
rhs
)
const
{
// sizes (
ie,
number of keys) must match
// sizes (number of keys) must match
if
(
size
()
!=
rhs
.
size
())
{
return
false
;
...
...
src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H
View file @
a237b7ce
...
...
@@ -88,6 +88,7 @@ class HashTbl
{
// Private data type for table entries
//- Structure to hold a hashed entry with SLList for collisions
struct
hashedEntry
{
//- The lookup key
...
...
@@ -99,18 +100,15 @@ class HashTbl
//- 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
&
);
};
...
...
@@ -222,7 +220,7 @@ public:
inline
bool
set
(
const
Key
&
,
const
T
&
newElmt
);
//- Erase a hashedEntry specified by given iterator
// This invalidates the iterator until the operator++
// This invalidates the iterator until the
next
operator++
bool
erase
(
const
iterator
&
);
//- Erase a hashedEntry specified by the given key
...
...
@@ -235,7 +233,7 @@ public:
//- Remove entries given by the given keys from this HashTbl
// Return the number of elements removed.
// The parameter HashTbl needs the same type of key, but the
// type of values held and the hashing function
is
arbitrary.
// type of values held and the hashing function
are
arbitrary.
template
<
class
AnyType
,
class
AnyHash
>
label
erase
(
const
HashTbl
<
AnyType
,
Key
,
AnyHash
>&
);
...
...
@@ -306,24 +304,23 @@ public:
//- The iterator base for HashTbl
// Note: data and functions are protected, to allow reuse by iterator
// and prevent most external usage.
// iterator and const_iterator have the same size, allowing
// us to reinterpret_cast between them (if desired)
class
iteratorBase
{
friend
class
HashTbl
;
public:
// Protected Data
// Private Data
//- Pointer to the HashTbl for which this is an iterator
// This also lets us use the default bitwise copy/assignment
HashTbl
<
T
,
Key
,
Hash
>*
hashTable_
;
//- Current element
hashedEntry
*
e
lmt
Ptr_
;
hashedEntry
*
e
ntry
Ptr_
;
//- Current hash index
label
hashIndex_
;
protected:
// Protected Member Functions
...
...
@@ -333,22 +330,32 @@ public:
inline
iteratorBase
();
//- Construct from hash table, moving to its 'begin' position
inline
iteratorBase
inline
explicit
iteratorBase
(
const
HashTbl
<
T
,
Key
,
Hash
>*
curHashTbl
);
//- Construct from hash table, element and hash index
inline
iteratorBase
inline
explicit
iteratorBase
(
const
HashTbl
<
T
,
Key
,
Hash
>*
curHashTbl
,
const
hashedEntry
*
elmt
,
const
label
hashIndex
);
inline
void
incr
();
//- Increment to the next position
inline
void
increment
();
//- Erase the HashTbl element at the current position
bool
erase
();
//- Return non-const access to referenced object
inline
T
&
object
();
//- Return const access to referenced object
inline
const
T
&
cobject
()
const
;
public:
// Member operators
...
...
@@ -358,9 +365,6 @@ public:
//- Return the Key corresponding to the iterator
inline
const
Key
&
key
()
const
;
//- Return referenced object
inline
const
T
&
object
()
const
;
//- Compare hashedEntry element pointers
inline
bool
operator
==
(
const
iteratorBase
&
)
const
;
inline
bool
operator
!=
(
const
iteratorBase
&
)
const
;
...
...
@@ -369,26 +373,21 @@ public:
//- An STL-conforming iterator
class
iterator
:
public
iteratorBase
{
friend
class
HashTbl
;
friend
class
const_iterator
;
// Private data
//- Pointer to the HashTbl for which this is an iterator
// This also lets us use the default bitwise copy/assignment
HashTbl
<
T
,
Key
,
Hash
>*
hashTable_
;
//- Current element
hashedEntry
*
elmtPtr_
;
//- Current hash index
label
hashIndex_
;
// Private Member Functions
//- Construct from hash table, moving to its 'begin' position
inline
explicit
iterator
(
HashTbl
<
T
,
Key
,
Hash
>*
curHashTbl
);
//- Construct from hash table, element and hash index
inline
iterator
inline
explicit
iterator
(
HashTbl
<
T
,
Key
,
Hash
>*
curHashTbl
,
hashedEntry
*
elmt
,
...
...
@@ -404,18 +403,10 @@ public:
// Member operators
// Access
//- Return the Key corresponding to the iterator
inline
const
Key
&
key
()
const
;
//- Compare hashedEntry element pointers
inline
bool
operator
==
(
const
iterator
&
)
const
;
inline
bool
operator
!=
(
const
iterator
&
)
const
;
//- Conversion to a const_iterator
inline
operator
const_iterator
()
const
;
//- Compare hashedEntry element pointers
inline
bool
operator
==
(
const
const_iterator
&
)
const
;
inline
bool
operator
!=
(
const
const_iterator
&
)
const
;
// Access
//- Return referenced hash value
inline
T
&
operator
*
();
...
...
@@ -429,9 +420,6 @@ public:
inline
iterator
operator
++
(
int
);
};
//- iterator set to the begining of the HashTbl
inline
iteratorBase
beginBase
();
//- iterator set to the begining of the HashTbl
inline
iterator
begin
();
...
...
@@ -443,26 +431,21 @@ public:
//- An STL-conforming const_iterator
class
const_iterator
:
public
iteratorBase
{
friend
class
HashTbl
;
friend
class
iterator
;
// Private data
//- Pointer to the HashTbl for which this is an iterator
// This also lets us use the default bitwise copy/assignment
const
HashTbl
<
T
,
Key
,
Hash
>*
hashTable_
;
//- Current element
const
hashedEntry
*
elmtPtr_
;
//- Current hash index
label
hashIndex_
;
// Private Member Functions
//- Construct from hash table, moving to its 'begin' position
inline
explicit
const_iterator
(
const
HashTbl
<
T
,
Key
,
Hash
>*
curHashTbl
);
//- Construct from hash table, element and hash index
inline
const_iterator
inline
explicit
const_iterator
(
const
HashTbl
<
T
,
Key
,
Hash
>*
curHashTbl
,
const
hashedEntry
*
elmt
,
...
...
@@ -476,25 +459,10 @@ public:
//- Construct null (end iterator)
inline
const_iterator
();
//- Construct from the non-const iterator
inline
const_iterator
(
const
iterator
&
);
// Member operators
// Access
//- Return the Key corresponding to the iterator
inline
const
Key
&
key
()
const
;
//- Compare hashedEntry element pointers
inline
bool
operator
==
(
const
const_iterator
&
)
const
;
inline
bool
operator
!=
(
const
const_iterator
&
)
const
;
//- Compare hashedEntry element pointers
inline
bool
operator
==
(
const
iterator
&
)
const
;
inline
bool
operator
!=
(
const
iterator
&
)
const
;
//- Return referenced hash value
inline
const
T
&
operator
*
()
const
;
inline
const
T
&
operator
()()
const
;
...
...
src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H
View file @
a237b7ce
...
...
@@ -33,12 +33,12 @@ inline Foam::HashTbl<T, Key, Hash>::hashedEntry::hashedEntry
(
const
Key
&
key
,
hashedEntry
*
next
,
const
T
&
newEntry
const
T
&
obj
)
:
key_
(
key
),
next_
(
next
),
obj_
(
newEntry
)
obj_
(
obj
)
{}
...
...
@@ -159,13 +159,13 @@ inline T& Foam::HashTbl<T, Key, Hash>::operator()(const Key& key)
}
// * * * * * * * * * * * * * * *
* STL
iterator * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * iterator
base *
* * * * * * * * * * * * * * //
template
<
class
T
,
class
Key
,
class
Hash
>
inline
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
iteratorBase
()
:
hashTable_
(
0
),
e
lmt
Ptr_
(
0
),
e
ntry
Ptr_
(
0
),
hashIndex_
(
0
)
{}
...
...
@@ -177,7 +177,7 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
)
:
hashTable_
(
const_cast
<
HashTbl
<
T
,
Key
,
Hash
>*>
(
hashTbl
)),
e
lmt
Ptr_
(
0
),
e
ntry
Ptr_
(
0
),
hashIndex_
(
0
)
{
if
(
hashTable_
->
nElmts_
&&
hashTable_
->
table_
)
...
...
@@ -185,7 +185,7 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
// find first non-NULL table entry
while
(
!
(
e
lmt
Ptr_
=
hashTable_
->
table_
[
hashIndex_
])
!
(
e
ntry
Ptr_
=
hashTable_
->
table_
[
hashIndex_
])
&&
++
hashIndex_
<
hashTable_
->
tableSize_
)
{}
...
...
@@ -193,7 +193,7 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
if
(
hashIndex_
>=
hashTable_
->
tableSize_
)
{
// make into an end iterator
e
lmt
Ptr_
=
0
;
e
ntry
Ptr_
=
0
;
hashIndex_
=
0
;
}
else
...
...
@@ -213,22 +213,14 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
)
:
hashTable_
(
const_cast
<
HashTbl
<
T
,
Key
,
Hash
>*>
(
hashTbl
)),
e
lmt
Ptr_
(
const_cast
<
hashedEntry
*>
(
elmt
)),
e
ntry
Ptr_
(
const_cast
<
hashedEntry
*>
(
elmt
)),
hashIndex_
(
hashIndex
)
{}
template
<
class
T
,
class
Key
,
class
Hash
>
inline
typename
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
beginBase
()
{
return
iteratorBase
(
this
);
}
template
<
class
T
,
class
Key
,
class
Hash
>
inline
void
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
incr
()
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
incr
ement
()
{
// A negative index is a special value from erase
if
(
hashIndex_
<
0
)
...
...
@@ -237,10 +229,10 @@ Foam::HashTbl<T, Key, Hash>::iteratorBase::incr()
// but we wish to continue at 'X-1'
hashIndex_
=
-
hashIndex_
-
2
;
}
else
if
(
e
lmt
Ptr_
&&
e
lmt
Ptr_
->
next_
)
else
if
(
e
ntry
Ptr_
&&
e
ntry
Ptr_
->
next_
)
{
//
Do we have additional
element
s
on the SLList
?
e
lmt
Ptr_
=
e
lmt
Ptr_
->
next_
;
//
Move to next
element on the SLList
e
ntry
Ptr_
=
e
ntry
Ptr_
->
next_
;
return
;
}
...
...
@@ -248,14 +240,14 @@ Foam::HashTbl<T, Key, Hash>::iteratorBase::incr()
while
(
++
hashIndex_
<
hashTable_
->
tableSize_
&&
!
(
e
lmt
Ptr_
=
hashTable_
->
table_
[
hashIndex_
])
&&
!
(
e
ntry
Ptr_
=
hashTable_
->
table_
[
hashIndex_
])
)
{}
if
(
hashIndex_
>=
hashTable_
->
tableSize_
)
{
// make into an end iterator
e
lmt
Ptr_
=
0
;
e
ntry
Ptr_
=
0
;
hashIndex_
=
0
;
}
}
...
...
@@ -265,15 +257,23 @@ template<class T, class Key, class Hash>
inline
const
Key
&
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
key
()
const
{
return
elmtPtr_
->
key_
;
return
entryPtr_
->
key_
;
}
template
<
class
T
,
class
Key
,
class
Hash
>
inline
T
&
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
object
()
{
return
entryPtr_
->
obj_
;
}
template
<
class
T
,
class
Key
,
class
Hash
>
inline
const
T
&
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
object
()
const
Foam
::
HashTbl
<
T
,
Key
,
Hash
>::
iteratorBase
::
c
object
()
const
{
return
e
lmt
Ptr_
->
obj_
;
return
e
ntry
Ptr_
->
obj_
;
}
...
...
@@ -283,7 +283,7 @@ inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator==
const
iteratorBase
&
iter
)
const
{
return
e
lmt
Ptr_
==
iter
.
e
lmt
Ptr_
;
return
e
ntry
Ptr_
==
iter
.
e
ntry
Ptr_
;
}
...
...
@@ -293,70 +293,49 @@ inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator!=