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 : #ifndef _HASH_HXX
21 : #define _HASH_HXX
22 :
23 : #include <rtl/ustring.hxx>
24 : #include <tools/ref.hxx>
25 : #include <tools/solar.h>
26 : #include <vector>
27 :
28 : class SvHashTable
29 : {
30 : sal_uInt32 nMax; // size of hash-tabel
31 : sal_uInt32 nFill; // elements in hash-tabel
32 : sal_uInt32 lAsk; // number of requests
33 : sal_uInt32 lTry; // number of tries
34 : protected:
35 : sal_Bool Test_Insert( const OString&, sal_Bool bInsert, sal_uInt32 * pInsertPos );
36 :
37 : // compare element with entry
38 : virtual bool equals( const OString& , sal_uInt32 ) const = 0;
39 : // get hash value from subclass
40 : virtual sal_uInt32 HashFunc( const OString& ) const = 0;
41 : public:
42 : SvHashTable( sal_uInt32 nMaxEntries );
43 : virtual ~SvHashTable();
44 :
45 0 : sal_uInt32 GetMax() const { return nMax; }
46 :
47 : virtual sal_Bool IsEntry( sal_uInt32 ) const = 0;
48 : };
49 :
50 : class SvStringHashTable;
51 : class SvStringHashEntry : public SvRefBase
52 : {
53 : friend class SvStringHashTable;
54 : OString aName;
55 : sal_uInt32 nHashId;
56 : sal_uLong nValue;
57 : sal_Bool bHasId;
58 : public:
59 0 : SvStringHashEntry()
60 : : nHashId(0)
61 : , nValue(0)
62 0 : , bHasId(false)
63 : {
64 0 : }
65 0 : SvStringHashEntry( const OString& rName, sal_uInt32 nIdx )
66 : : aName(rName)
67 : , nHashId(nIdx)
68 : , nValue(0)
69 0 : , bHasId(true)
70 : {
71 0 : }
72 : virtual ~SvStringHashEntry();
73 :
74 0 : const OString& GetName() const { return aName; }
75 0 : sal_Bool HasId() const { return bHasId; }
76 : sal_uInt32 GetId() const { return nHashId; }
77 :
78 0 : void SetValue( sal_uLong n ) { nValue = n; }
79 0 : sal_uLong GetValue() const { return nValue; }
80 :
81 : sal_Bool operator == ( const SvStringHashEntry & rRef )
82 : { return nHashId == rRef.nHashId; }
83 : sal_Bool operator != ( const SvStringHashEntry & rRef )
84 : { return ! operator == ( rRef ); }
85 0 : SvStringHashEntry & operator = ( const SvStringHashEntry & rRef )
86 0 : { SvRefBase::operator=( rRef );
87 0 : aName = rRef.aName;
88 0 : nHashId = rRef.nHashId;
89 0 : nValue = rRef.nValue;
90 0 : bHasId = rRef.bHasId;
91 0 : return *this;
92 : }
93 : };
94 :
95 : typedef tools::SvRef<SvStringHashEntry> SvStringHashEntryRef;
96 :
97 : typedef ::std::vector< SvStringHashEntry* > SvStringHashList;
98 :
99 : class SvStringHashTable : public SvHashTable
100 : {
101 : SvStringHashEntry* pEntries;
102 : protected:
103 : virtual sal_uInt32 HashFunc( const OString& rElement ) const SAL_OVERRIDE;
104 : virtual bool equals( const OString &rElement, sal_uInt32 nIndex ) const SAL_OVERRIDE;
105 : public:
106 : SvStringHashTable( sal_uInt32 nMaxEntries ); // max size of hash-tabel
107 : virtual ~SvStringHashTable();
108 :
109 : OString GetNearString( const OString& rName ) const;
110 : virtual sal_Bool IsEntry( sal_uInt32 nIndex ) const SAL_OVERRIDE;
111 :
112 : sal_Bool Insert( const OString& rStr, sal_uInt32 * pHash ); // insert string
113 : sal_Bool Test( const OString& rStr, sal_uInt32 * pHash ) const; // test of insert string
114 : SvStringHashEntry * Get ( sal_uInt32 nIndex ) const; // return pointer to string
115 : SvStringHashEntry & operator []( sal_uInt32 nPos ) const
116 : { return pEntries[ nPos ]; }
117 :
118 : void FillHashList( SvStringHashList * rList ) const;
119 : };
120 :
121 : #endif // _RSCHASH_HXX
122 :
123 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|