xref: /aoo42x/main/idl/inc/hash.hxx (revision 67e470da)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _HASH_HXX
25 #define _HASH_HXX
26 
27 
28 
29 #include <tools/ref.hxx>
30 #include <tools/string.hxx>
31 
32 /****************** H a s h - T a b l e **********************************/
33 class SvHashTable
34 {
35     sal_uInt32       nMax;                 // size of hash-tabel
36     sal_uInt32       nFill;                // elements in hash-tabel
37     sal_uInt32       lAsk;                 // Anzahl der Anfragen
38     sal_uInt32       lTry;                 // Anzahl der Versuche
39 protected:
40     sal_Bool        Test_Insert( const void *, sal_Bool bInsert, sal_uInt32 * pInsertPos );
41 
42                             // compare element with entry
43     virtual StringCompare   Compare( const void * , sal_uInt32 ) const = 0;
44                             // get hash value from subclass
45     virtual sal_uInt32          HashFunc( const void * ) const = 0;
46 public:
47                 SvHashTable( sal_uInt32 nMaxEntries );
48                 virtual ~SvHashTable();
49 
GetMax() const50     sal_uInt32      GetMax() const { return nMax; }
51 
52     virtual sal_Bool            IsEntry( sal_uInt32 ) const = 0;
53 };
54 
55 /************** S t r i n g H a s h T a b l e E n t r y ******************/
56 class SvStringHashTable;
57 class SvStringHashEntry : public SvRefBase
58 {
59 friend class SvStringHashTable;
60     ByteString  aName;
61     sal_uInt32  nHashId;
62     sal_uLong   nValue;
63     sal_Bool    bHasId;
64 public:
SvStringHashEntry()65                     SvStringHashEntry() : bHasId( sal_False ) {;}
SvStringHashEntry(const ByteString & rName,sal_uInt32 nIdx)66                     SvStringHashEntry( const ByteString & rName, sal_uInt32 nIdx )
67                         : aName( rName )
68                         , nHashId( nIdx )
69                         , nValue( 0 )
70                         , bHasId( sal_True ) {}
71                     ~SvStringHashEntry();
72 
GetName() const73     const ByteString &  GetName() const { return aName; }
HasId() const74     sal_Bool            HasId() const { return bHasId; }
GetId() const75     sal_uInt32          GetId() const { return nHashId; }
76 
SetValue(sal_uLong n)77     void            SetValue( sal_uLong n ) { nValue = n; }
GetValue() const78     sal_uLong           GetValue() const { return nValue; }
79 
operator ==(const SvStringHashEntry & rRef)80     sal_Bool            operator == ( const SvStringHashEntry & rRef )
81                     { return nHashId == rRef.nHashId; }
operator !=(const SvStringHashEntry & rRef)82     sal_Bool            operator != ( const SvStringHashEntry & rRef )
83                     { return ! operator == ( rRef ); }
operator =(const SvStringHashEntry & rRef)84     SvStringHashEntry & operator = ( const SvStringHashEntry & rRef )
85 		{ SvRefBase::operator=( rRef );
86 		  aName   = rRef.aName;
87 		  nHashId = rRef.nHashId;
88 	      nValue  = rRef.nValue;
89 		  bHasId  = rRef.bHasId;
90 		  return *this;
91 		}
92 };
93 
94 SV_DECL_IMPL_REF(SvStringHashEntry)
95 
96 /****************** S t r i n g H a s h T a b l e ************************/
97 DECLARE_LIST(SvStringHashList,SvStringHashEntry *)
98 
99 class SvStringHashTable : public SvHashTable
100 {
101     SvStringHashEntry * pEntries;
102 protected:
103     virtual sal_uInt32          HashFunc( const void * pElement ) const;
104     virtual StringCompare   Compare( const void * pElement, sal_uInt32 nIndex ) const;
105 public:
106             SvStringHashTable( sal_uInt32 nMaxEntries );   // max size of hash-tabel
107             virtual ~SvStringHashTable();
108 
109     ByteString          GetNearString( const ByteString & rName ) const;
110     virtual sal_Bool    IsEntry( sal_uInt32 nIndex ) const;
111 
112     sal_Bool    Insert( const ByteString & rStr, sal_uInt32 * pHash ); // insert string
113     sal_Bool    Test( const ByteString & rStr, sal_uInt32 * pHash ) const; // test of insert string
114     SvStringHashEntry * Get ( sal_uInt32 nIndex ) const; // return pointer to string
operator [](sal_uInt32 nPos) const115     SvStringHashEntry & operator []( sal_uInt32 nPos ) const
116             { return pEntries[ nPos ]; }
117 
118     void    FillHashList( SvStringHashList * rList ) const;
119 };
120 
121 #endif // _RSCHASH_HXX
122