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