1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
26 return UNIQUEINDEX_ENTRY_NOTFOUND
;
28 // Expend array if full
29 sal_uIntPtr nTmp
= maMap
.size();
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
;
45 return ( nUniqIndex
+ nStartIndex
- 1 );
48 void UniqueIndexImpl::Insert( sal_uIntPtr nIndex
, void* p
)
50 // NULL-Pointer not allowed
54 sal_uIntPtr nContIndex
= nIndex
- nStartIndex
;
56 bool bFound
= maMap
.find( nContIndex
) != maMap
.end();
58 // Insert object to array
59 maMap
[ nContIndex
] = p
;
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() )
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() )
98 sal_uIntPtr
UniqueIndexImpl::FirstIndex() const
101 return UNIQUEINDEX_ENTRY_NOTFOUND
;
103 return maMap
.begin()->first
;
106 sal_uIntPtr
UniqueIndexImpl::LastIndex() const
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
;
120 if ( it
== maMap
.end() )
121 return UNIQUEINDEX_ENTRY_NOTFOUND
;
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
)
130 return UNIQUEINDEX_ENTRY_NOTFOUND
;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */