Branch libreoffice-5-0-4
[LibreOffice.git] / tools / source / memtools / unqidx.cxx
blob726c96c07b031a2a2b71556e7cf9c9be92d1170e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/unqidx.hxx>
22 sal_uIntPtr UniqueIndexImpl::Insert( void* p )
24 // NULL-Pointer not allowed
25 if ( !p )
26 return UNIQUEINDEX_ENTRY_NOTFOUND;
28 // Expend array if full
29 sal_uIntPtr nTmp = maMap.size();
30 if( nTmp == nCount )
31 nTmp++;
33 // Avoid overflow of UniqIndex upon deletion
34 nUniqIndex = nUniqIndex % nTmp;
36 // Search next empty index
37 while ( maMap.find( nUniqIndex ) != maMap.end() )
38 nUniqIndex = (nUniqIndex+1) % nTmp;
40 // Insert object to array
41 maMap[ nUniqIndex ] = p;
43 nCount++;
44 nUniqIndex++;
45 return ( nUniqIndex + nStartIndex - 1 );
48 void UniqueIndexImpl::Insert( sal_uIntPtr nIndex, void* p )
50 // NULL-Pointer not allowed
51 if ( !p )
52 return;
54 sal_uIntPtr nContIndex = nIndex - nStartIndex;
56 bool bFound = maMap.find( nContIndex ) != maMap.end();
58 // Insert object to array
59 maMap[ nContIndex ] = p;
61 if( !bFound )
62 nCount++;
65 void* UniqueIndexImpl::Remove( sal_uIntPtr nIndex )
67 // Check for valid index
68 if ( (nIndex >= nStartIndex) &&
69 (nIndex < (size() + nStartIndex)) )
71 // insert index as empty entry, and reduce indexcount,
72 // if this entry was used
73 std::map<sal_uInt32, void*>::iterator it = maMap.find( nIndex - nStartIndex );
74 if( it != maMap.end() )
76 void* p = it->second;
77 maMap.erase( it );
78 nCount--;
79 return p;
82 return NULL;
85 void* UniqueIndexImpl::Get( sal_uIntPtr nIndex ) const
87 // check for valid index
88 if ( (nIndex >= nStartIndex) &&
89 (nIndex < (size() + nStartIndex)) )
91 std::map<sal_uInt32, void*>::const_iterator it = maMap.find( nIndex - nStartIndex );
92 if( it != maMap.end() )
93 return it->second;
95 return NULL;
98 sal_uIntPtr UniqueIndexImpl::FirstIndex() const
100 if ( maMap.empty() )
101 return UNIQUEINDEX_ENTRY_NOTFOUND;
103 return maMap.begin()->first;
106 sal_uIntPtr UniqueIndexImpl::LastIndex() const
108 if ( maMap.empty() )
109 return UNIQUEINDEX_ENTRY_NOTFOUND;
111 return maMap.rbegin()->first;
114 sal_uIntPtr UniqueIndexImpl::NextIndex(sal_uIntPtr aIndex) const
116 std::map<sal_uInt32, void*>::const_iterator it = maMap.find( aIndex );
117 if ( it == maMap.end() )
118 return UNIQUEINDEX_ENTRY_NOTFOUND;
119 ++it;
120 if ( it == maMap.end() )
121 return UNIQUEINDEX_ENTRY_NOTFOUND;
122 return it->first;
125 sal_uIntPtr UniqueIndexImpl::GetIndexOf(void* p) const
127 for( std::map<sal_uInt32, void*>::const_iterator it = maMap.begin(); it != maMap.end(); ++it )
128 if( it->second == p )
129 return it->first;
130 return UNIQUEINDEX_ENTRY_NOTFOUND;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */