1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hashtbl.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
39 // 1. m_lElem < m_lSize
40 // 2. die Elemente in m_Array wurden double-hashed erzeugt
49 double m_dMaxLoadFactor
;
53 ULONG
Hash(String
const& Key
) const;
54 ULONG
DHash(String
const& Key
, ULONG lHash
) const;
55 ULONG
Probe(ULONG lPos
) const;
57 HashItem
* FindPos(String
const& Key
) const;
59 double CalcLoadFactor() const;
74 friend class HashTableIterator
;
76 virtual void OnDeleteObject(void* pObject
);
78 void* GetObjectAt(ULONG lPos
) const;
82 static double m_defMaxLoadFactor
;
83 static double m_defDefGrowFactor
;
90 double dMaxLoadFactor
= HashTable::m_defMaxLoadFactor
/* 0.8 */,
91 double dGrowFactor
= HashTable::m_defDefGrowFactor
/* 2.0 */
97 ULONG
GetSize() const { return m_lSize
; }
99 void* Find (String
const& Key
) const;
100 BOOL
Insert (String
const& Key
, void* pObject
);
101 void* Delete (String
const& Key
);
104 // ADT hash table iterator
106 // Invariante: 0 <= m_lAt < m_aTable.GetCount()
108 class HashTableIterator
111 HashTable
const& m_aTable
;
113 void* FindValidObject(BOOL bForward
);
116 void* GetFirst(); // Interation _ohne_ Sortierung
122 HashTableIterator(HashTable
const&);
125 // typsichere Makros ---------------------------------------------------
127 #define DECLARE_HASHTABLE_INTERN(ClassName,Owner,KeyType,ObjType) \
128 class ClassName : public HashTable \
134 double dMaxLoadFactor = HashTable::m_defMaxLoadFactor, \
135 double dGrowFactor = HashTable::m_defDefGrowFactor \
137 : HashTable(lSize,Owner,dMaxLoadFactor,dGrowFactor) {} \
139 ObjType Find (KeyType const& Key) const \
140 { return (ObjType) HashTable::Find(String(Key)); } \
142 BOOL Insert (KeyType const& Key, ObjType Object) \
143 { return HashTable::Insert(String(Key), (void*) Object); } \
145 ObjType Delete (KeyType const&Key) \
146 { return (ObjType) HashTable::Delete (String(Key)); } \
149 // HashTable OHNE Owner-Verhalten
150 #define DECLARE_HASHTABLE(ClassName,KeyType,ObjType) \
151 DECLARE_HASHTABLE_INTERN(ClassName,FALSE,KeyType,ObjType)
153 // HashTable MIT Owner-Verhalten
154 #define DECLARE_HASHTABLE_OWNER(ClassName,KeyType,ObjType) \
155 DECLARE_HASHTABLE_INTERN(ClassName##2,TRUE,KeyType,ObjType) \
156 class ClassName : public ClassName##2 \
159 virtual void OnDeleteObject(void* pObject); \
164 double dMaxLoadFactor = HashTable::m_defMaxLoadFactor, \
165 double dGrowFactor = HashTable::m_defDefGrowFactor \
167 : ClassName##2(lSize,dMaxLoadFactor,dGrowFactor) {} \
171 #define IMPLEMENT_HASHTABLE_OWNER(ClassName,KeyType,ObjType) \
172 void ClassName::OnDeleteObject(void* pObject) \
173 { delete (ObjType) pObject; } \
175 ClassName::~ClassName() \
177 for (ULONG i=0; i<GetSize(); i++) \
179 void *pObject = GetObjectAt(i); \
180 if (pObject != NULL) \
181 OnDeleteObject(pObject); \
185 // Iterator-Makros --------------------------------------------------
187 #define DECLARE_HASHTABLE_ITERATOR(ClassName,ObjType) \
188 class ClassName : public HashTableIterator \
191 ClassName(HashTable const& aTable) \
192 : HashTableIterator(aTable) {} \
195 { return (ObjType)HashTableIterator::GetFirst(); } \
197 { return (ObjType)HashTableIterator::GetNext(); } \
199 { return (ObjType)HashTableIterator::GetLast(); } \
201 { return (ObjType)HashTableIterator::GetPrev(); } \
205 #endif // _HASHTBL_HXX