Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <tools/unqidx.hxx>
21 :
22 783 : sal_uIntPtr UniqueIndexImpl::Insert( void* p )
23 : {
24 : // NULL-Pointer not allowed
25 783 : if ( !p )
26 0 : return UNIQUEINDEX_ENTRY_NOTFOUND;
27 :
28 : // Expend array if full
29 783 : sal_uIntPtr nTmp = maMap.size();
30 783 : if( nTmp == nCount )
31 783 : nTmp++;
32 :
33 : // Avoid overflow of UniqIndex upon deletion
34 783 : nUniqIndex = nUniqIndex % nTmp;
35 :
36 : // Search next empty index
37 1566 : while ( maMap.find( nUniqIndex ) != maMap.end() )
38 0 : nUniqIndex = (nUniqIndex+1) % nTmp;
39 :
40 : // Insert object to array
41 783 : maMap[ nUniqIndex ] = p;
42 :
43 783 : nCount++;
44 783 : nUniqIndex++;
45 783 : return ( nUniqIndex + nStartIndex - 1 );
46 : }
47 :
48 0 : void UniqueIndexImpl::Insert( sal_uIntPtr nIndex, void* p )
49 : {
50 : // NULL-Pointer not allowed
51 0 : if ( !p )
52 0 : return;
53 :
54 0 : sal_uIntPtr nContIndex = nIndex - nStartIndex;
55 :
56 0 : bool bFound = maMap.find( nContIndex ) != maMap.end();
57 :
58 : // Insert object to array
59 0 : maMap[ nContIndex ] = p;
60 :
61 0 : if( !bFound )
62 0 : nCount++;
63 : }
64 :
65 783 : void* UniqueIndexImpl::Remove( sal_uIntPtr nIndex )
66 : {
67 : // Check for valid index
68 1566 : if ( (nIndex >= nStartIndex) &&
69 783 : (nIndex < (size() + nStartIndex)) )
70 : {
71 : // insert index as empty entry, and reduce indexcount,
72 : // if this entry was used
73 783 : std::map<sal_uInt32, void*>::iterator it = maMap.find( nIndex - nStartIndex );
74 783 : if( it != maMap.end() )
75 : {
76 783 : void* p = it->second;
77 783 : maMap.erase( it );
78 783 : nCount--;
79 783 : return p;
80 : }
81 : }
82 0 : return NULL;
83 : }
84 :
85 3472 : void* UniqueIndexImpl::Get( sal_uIntPtr nIndex ) const
86 : {
87 : // check for valid index
88 6944 : if ( (nIndex >= nStartIndex) &&
89 3472 : (nIndex < (size() + nStartIndex)) )
90 : {
91 3472 : std::map<sal_uInt32, void*>::const_iterator it = maMap.find( nIndex - nStartIndex );
92 3472 : if( it != maMap.end() )
93 3472 : return it->second;
94 : }
95 0 : return NULL;
96 : }
97 :
98 2041 : sal_uIntPtr UniqueIndexImpl::FirstIndex() const
99 : {
100 2041 : if ( maMap.empty() )
101 419 : return UNIQUEINDEX_ENTRY_NOTFOUND;
102 :
103 1622 : return maMap.begin()->first;
104 : }
105 :
106 1202 : sal_uIntPtr UniqueIndexImpl::LastIndex() const
107 : {
108 1202 : if ( maMap.empty() )
109 419 : return UNIQUEINDEX_ENTRY_NOTFOUND;
110 :
111 783 : return maMap.rbegin()->first;
112 : }
113 :
114 1988 : sal_uIntPtr UniqueIndexImpl::NextIndex(sal_uIntPtr aIndex) const
115 : {
116 1988 : std::map<sal_uInt32, void*>::const_iterator it = maMap.find( aIndex );
117 1988 : if ( it == maMap.end() )
118 0 : return UNIQUEINDEX_ENTRY_NOTFOUND;
119 1988 : ++it;
120 1988 : if ( it == maMap.end() )
121 1258 : return UNIQUEINDEX_ENTRY_NOTFOUND;
122 730 : return it->first;
123 : }
124 :
125 895 : sal_uIntPtr UniqueIndexImpl::GetIndexOf(void* p) const
126 : {
127 1260 : for( std::map<sal_uInt32, void*>::const_iterator it = maMap.begin(); it != maMap.end(); ++it )
128 1260 : if( it->second == p )
129 895 : return it->first;
130 0 : return UNIQUEINDEX_ENTRY_NOTFOUND;
131 : }
132 :
133 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|