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 .
26 // program-sensitive includes
28 #include <tools/debug.hxx>
30 SvStringHashEntry::~SvStringHashEntry() { };
32 SvHashTable::SvHashTable( sal_uInt32 nMaxEntries
)
34 nMax
= nMaxEntries
; // set max entries
35 nFill
= 0; // no entries
40 SvHashTable::~SvHashTable()
44 bool SvHashTable::Test_Insert( const OString
& rElement
, bool bInsert
,
45 sal_uInt32
* pInsertPos
)
54 nHash
= HashFunc( rElement
);
55 nIndex
= nHash
% nMax
;
57 nLoop
= 0; // divide to range
58 while( (nMax
!= nLoop
) && IsEntry( nIndex
) )
59 { // is place occupied
60 if( equals( rElement
, nIndex
) )
63 *pInsertPos
= nIndex
; // place of Element
68 nIndex
= (sal_uInt16
)(nIndex
+ nHash
+ 7) % nMax
;
73 DBG_ASSERT( nMax
!= nLoop
, "Hash table full" );
77 *pInsertPos
= nIndex
; // return free place
84 SvStringHashTable::SvStringHashTable( sal_uInt32 nMaxEntries
)
85 : SvHashTable( nMaxEntries
)
87 pEntries
= new SvStringHashEntry
[ nMaxEntries
];
89 // set RefCount to one
90 SvStringHashEntry
* pPos
, *pEnd
;
92 pEnd
= pEntries
+ nMaxEntries
;
100 SvStringHashTable::~SvStringHashTable()
103 // set RefCount to one
104 SvStringHashEntry
* pPos
, *pEnd
;
106 pEnd
= pEntries
+ GetMax();
107 while( pPos
!= pEnd
)
109 DBG_ASSERT( pPos
->GetRefCount() == 1, "Reference count != 1" );
117 sal_uInt32
SvStringHashTable::HashFunc( const OString
& rElement
) const
119 sal_uInt32 nHash
= 0; // hash value
120 const char * pStr
= rElement
.getStr();
125 if( isupper( *pStr
) )
126 nHash
^= sal_uInt32(*pStr
- 'A' + 26) << nShift
;
128 nHash
^= sal_uInt32(*pStr
- 'a') << nShift
;
138 OString
SvStringHashTable::GetNearString( const OString
& rName
) const
140 for( sal_uInt32 i
= 0; i
< GetMax(); i
++ )
142 SvStringHashEntry
* pE
= Get( i
);
145 if( pE
->GetName().equalsIgnoreAsciiCase( rName
) && !pE
->GetName().equals( rName
) )
146 return pE
->GetName();
152 bool SvStringHashTable::IsEntry( sal_uInt32 nIndex
) const
154 if( nIndex
>= GetMax() )
156 return pEntries
[ nIndex
].HasId();
159 bool SvStringHashTable::Insert( const OString
& rName
, sal_uInt32
* pIndex
)
163 if( !pIndex
) pIndex
= &nIndex
;
165 if( !SvHashTable::Test_Insert( rName
, true, pIndex
) )
168 if( !IsEntry( *pIndex
) )
169 pEntries
[ *pIndex
] = SvStringHashEntry( rName
, *pIndex
);
173 bool SvStringHashTable::Test( const OString
& rName
, sal_uInt32
* pPos
) const
175 return const_cast<SvStringHashTable
*>(this)->Test_Insert( rName
, false, pPos
);
178 SvStringHashEntry
* SvStringHashTable::Get( sal_uInt32 nIndex
) const
180 if( IsEntry( nIndex
) )
181 return pEntries
+ nIndex
;
185 bool SvStringHashTable::equals( const OString
& rElement
,
186 sal_uInt32 nIndex
) const
188 return rElement
.equals( pEntries
[ nIndex
].GetName() );
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */