diff options
author | Matteo Casalin <matteo.casalin@yahoo.com> | 2016-03-05 14:59:11 +0100 |
---|---|---|
committer | Noel Grandin <noelgrandin@gmail.com> | 2016-03-06 06:39:26 +0000 |
commit | 078188793b2753bf607bb629464935ccefd28136 (patch) | |
tree | 71370822febbc9d695766e1d5f79a180aa041eaa /tools | |
parent | bc2e74f3c3093819c499921cf62615e9a8d7301c (diff) |
Remove unuseful nStartIndex data member (and fix indexing)
The methods that modify nUinqIndex already maintain class invariants:
* Insert() never decrease its value
* Remove() can replace its value with that of the removed item,
which was no lower than the one specified in constructor call.
Besides, boundary checks against nStartIndex are not really needed
since the various methods rely on map::find.
Finally, FirstIndex/NextIndex/LastIndex/GetIndexOf did not adjust with
nStartIndex the index value retrieved from tha map, thus provifing wrong
values. Since the map now stores the real indexes, consistency is granted.
Change-Id: I5e47cd2672677805304d4c4860826fe272812abf
Reviewed-on: https://gerrit.libreoffice.org/22935
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/source/memtools/unqidx.cxx | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/tools/source/memtools/unqidx.cxx b/tools/source/memtools/unqidx.cxx index 56a9f0aaa143..b53895fdfce2 100644 --- a/tools/source/memtools/unqidx.cxx +++ b/tools/source/memtools/unqidx.cxx @@ -32,42 +32,33 @@ UniqueIndexImpl::Index UniqueIndexImpl::Insert( void* p ) maMap[ nUniqIndex ] = p; - nUniqIndex++; - return ( nUniqIndex + nStartIndex - 1 ); + return nUniqIndex++; } void* UniqueIndexImpl::Remove( Index nIndex ) { - // Check for valid index - if ( nIndex >= nStartIndex ) + std::map<Index, void*>::iterator it = maMap.find( nIndex ); + if ( it != maMap.end() ) { - std::map<Index, void*>::iterator it = maMap.find( nIndex - nStartIndex ); - if( it != maMap.end() ) - { - // Allow to recycle freed indexes, as was done by - // original implementation based on a vector - // This is not really needed when using a map, and - // really unique indexes might be better/safer? - if ( nIndex < nUniqIndex ) - nUniqIndex = nIndex; + // Allow to recycle freed indexes, as was done by + // original implementation based on a vector + // This is not really needed when using a map, and + // really unique indexes might be better/safer? + if ( nIndex < nUniqIndex ) + nUniqIndex = nIndex; - void* p = it->second; - maMap.erase( it ); - return p; - } + void* p = it->second; + maMap.erase( it ); + return p; } return nullptr; } void* UniqueIndexImpl::Get( Index nIndex ) const { - // check for valid index - if ( nIndex >= nStartIndex ) - { - std::map<Index, void*>::const_iterator it = maMap.find( nIndex - nStartIndex ); - if( it != maMap.end() ) - return it->second; - } + std::map<Index, void*>::const_iterator it = maMap.find( nIndex ); + if ( it != maMap.end() ) + return it->second; return nullptr; } |