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 1205 : sal_uIntPtr UniqueIndexImpl::Insert( void* p )
23 : {
24 : // NULL-Pointer not allowed
25 1205 : if ( !p )
26 0 : return UNIQUEINDEX_ENTRY_NOTFOUND;
27 :
28 : // Expend array if full
29 1205 : sal_uIntPtr nTmp = maMap.size();
30 1205 : if( nTmp == nCount )
31 1205 : nTmp++;
32 :
33 : // Avoid overflow of UniqIndex upon deletion
34 1205 : nUniqIndex = nUniqIndex % nTmp;
35 :
36 : // Search next empty index
37 2410 : while ( maMap.find( nUniqIndex ) != maMap.end() )
38 0 : nUniqIndex = (nUniqIndex+1) % nTmp;
39 :
40 : // Insert object to array
41 1205 : maMap[ nUniqIndex ] = p;
42 :
43 1205 : nCount++;
44 1205 : nUniqIndex++;
45 1205 : 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 779 : void* UniqueIndexImpl::Remove( sal_uIntPtr nIndex )
66 : {
67 : // Check for valid index
68 1558 : if ( (nIndex >= nStartIndex) &&
69 779 : (nIndex < (size() + nStartIndex)) )
70 : {
71 : // insert index as empty entry, and reduce indexcount,
72 : // if this entry was used
73 779 : std::map<sal_uInt32, void*>::iterator it = maMap.find( nIndex - nStartIndex );
74 779 : if( it != maMap.end() )
75 : {
76 779 : void* p = it->second;
77 779 : maMap.erase( it );
78 779 : nCount--;
79 779 : return p;
80 : }
81 : }
82 0 : return NULL;
83 : }
84 :
85 3400 : void* UniqueIndexImpl::Get( sal_uIntPtr nIndex ) const
86 : {
87 : // check for valid index
88 6800 : if ( (nIndex >= nStartIndex) &&
89 3400 : (nIndex < (size() + nStartIndex)) )
90 : {
91 3400 : std::map<sal_uInt32, void*>::const_iterator it = maMap.find( nIndex - nStartIndex );
92 3400 : if( it != maMap.end() )
93 3400 : return it->second;
94 : }
95 0 : return NULL;
96 : }
97 :
98 2031 : sal_uIntPtr UniqueIndexImpl::FirstIndex() const
99 : {
100 2031 : if ( maMap.empty() )
101 417 : return UNIQUEINDEX_ENTRY_NOTFOUND;
102 :
103 1614 : return maMap.begin()->first;
104 : }
105 :
106 1196 : sal_uIntPtr UniqueIndexImpl::LastIndex() const
107 : {
108 1196 : if ( maMap.empty() )
109 417 : return UNIQUEINDEX_ENTRY_NOTFOUND;
110 :
111 779 : return maMap.rbegin()->first;
112 : }
113 :
114 1978 : sal_uIntPtr UniqueIndexImpl::NextIndex(sal_uIntPtr aIndex) const
115 : {
116 1978 : std::map<sal_uInt32, void*>::const_iterator it = maMap.find( aIndex );
117 1978 : if ( it == maMap.end() )
118 0 : return UNIQUEINDEX_ENTRY_NOTFOUND;
119 1978 : ++it;
120 1978 : if ( it == maMap.end() )
121 1252 : return UNIQUEINDEX_ENTRY_NOTFOUND;
122 726 : return it->first;
123 : }
124 :
125 891 : sal_uIntPtr UniqueIndexImpl::GetIndexOf(void* p) const
126 : {
127 1254 : for( std::map<sal_uInt32, void*>::const_iterator it = maMap.begin(); it != maMap.end(); ++it )
128 1254 : if( it->second == p )
129 891 : return it->first;
130 0 : return UNIQUEINDEX_ENTRY_NOTFOUND;
131 : }
132 :
133 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|