xref: /aoo42x/main/idl/inc/hash.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _HASH_HXX
29 #define _HASH_HXX
30 
31 
32 
33 #include <tools/ref.hxx>
34 #include <tools/string.hxx>
35 
36 /****************** H a s h - T a b l e **********************************/
37 class SvHashTable
38 {
39     sal_uInt32       nMax;                 // size of hash-tabel
40     sal_uInt32       nFill;                // elements in hash-tabel
41     sal_uInt32       lAsk;                 // Anzahl der Anfragen
42     sal_uInt32       lTry;                 // Anzahl der Versuche
43 protected:
44     sal_Bool        Test_Insert( const void *, sal_Bool bInsert, sal_uInt32 * pInsertPos );
45 
46                             // compare element with entry
47     virtual StringCompare   Compare( const void * , sal_uInt32 ) const = 0;
48                             // get hash value from subclass
49     virtual sal_uInt32          HashFunc( const void * ) const = 0;
50 public:
51                 SvHashTable( sal_uInt32 nMaxEntries );
52                 virtual ~SvHashTable();
53 
54     sal_uInt32      GetMax() const { return nMax; }
55 
56     virtual sal_Bool            IsEntry( sal_uInt32 ) const = 0;
57 };
58 
59 /************** S t r i n g H a s h T a b l e E n t r y ******************/
60 class SvStringHashTable;
61 class SvStringHashEntry : public SvRefBase
62 {
63 friend class SvStringHashTable;
64     ByteString  aName;
65     sal_uInt32  nHashId;
66     sal_uLong   nValue;
67     sal_Bool    bHasId;
68 public:
69                     SvStringHashEntry() : bHasId( sal_False ) {;}
70                     SvStringHashEntry( const ByteString & rName, sal_uInt32 nIdx )
71                         : aName( rName )
72                         , nHashId( nIdx )
73                         , nValue( 0 )
74                         , bHasId( sal_True ) {}
75                     ~SvStringHashEntry();
76 
77     const ByteString &  GetName() const { return aName; }
78     sal_Bool            HasId() const { return bHasId; }
79     sal_uInt32          GetId() const { return nHashId; }
80 
81     void            SetValue( sal_uLong n ) { nValue = n; }
82     sal_uLong           GetValue() const { return nValue; }
83 
84     sal_Bool            operator == ( const SvStringHashEntry & rRef )
85                     { return nHashId == rRef.nHashId; }
86     sal_Bool            operator != ( const SvStringHashEntry & rRef )
87                     { return ! operator == ( rRef ); }
88     SvStringHashEntry & operator = ( const SvStringHashEntry & rRef )
89 		{ SvRefBase::operator=( rRef );
90 		  aName   = rRef.aName;
91 		  nHashId = rRef.nHashId;
92 	      nValue  = rRef.nValue;
93 		  bHasId  = rRef.bHasId;
94 		  return *this;
95 		}
96 };
97 
98 SV_DECL_IMPL_REF(SvStringHashEntry)
99 
100 /****************** S t r i n g H a s h T a b l e ************************/
101 DECLARE_LIST(SvStringHashList,SvStringHashEntry *)
102 
103 class SvStringHashTable : public SvHashTable
104 {
105     SvStringHashEntry * pEntries;
106 protected:
107     virtual sal_uInt32          HashFunc( const void * pElement ) const;
108     virtual StringCompare   Compare( const void * pElement, sal_uInt32 nIndex ) const;
109 public:
110             SvStringHashTable( sal_uInt32 nMaxEntries );   // max size of hash-tabel
111             virtual ~SvStringHashTable();
112 
113     ByteString          GetNearString( const ByteString & rName ) const;
114     virtual sal_Bool    IsEntry( sal_uInt32 nIndex ) const;
115 
116     sal_Bool    Insert( const ByteString & rStr, sal_uInt32 * pHash ); // insert string
117     sal_Bool    Test( const ByteString & rStr, sal_uInt32 * pHash ) const; // test of insert string
118     SvStringHashEntry * Get ( sal_uInt32 nIndex ) const; // return pointer to string
119     SvStringHashEntry & operator []( sal_uInt32 nPos ) const
120             { return pEntries[ nPos ]; }
121 
122     void    FillHashList( SvStringHashList * rList ) const;
123 };
124 
125 #endif // _RSCHASH_HXX
126