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