merge the formfield patch from ooo-build
[ooovba.git] / tools / workben / hashtbl.hxx
blobef3c98aa3a0497fa8d1ed1ecc5eb76e65cab25cb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hashtbl.hxx,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
31 #ifndef _HASHTBL_HXX
32 #define _HASHTBL_HXX
34 #include <tlgen.hxx>
36 // ADT hash table
37 //
38 // Invariante:
39 // 1. m_lElem < m_lSize
40 // 2. die Elemente in m_Array wurden double-hashed erzeugt
42 class HashItem;
44 class HashTable
46 ULONG m_lSize;
47 ULONG m_lElem;
48 HashItem *m_pData;
49 double m_dMaxLoadFactor;
50 double m_dGrowFactor;
51 BOOL m_bOwner;
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;
58 void SmartGrow();
59 double CalcLoadFactor() const;
61 // Statistik
62 #ifdef DBG_UTIL
63 private:
64 struct
66 ULONG m_lSingleHash;
67 ULONG m_lDoubleHash;
68 ULONG m_lProbe;
70 m_aStatistic;
71 #endif
73 protected:
74 friend class HashTableIterator;
76 virtual void OnDeleteObject(void* pObject);
78 void* GetObjectAt(ULONG lPos) const;
80 // Default-Werte
81 public:
82 static double m_defMaxLoadFactor;
83 static double m_defDefGrowFactor;
85 public:
86 HashTable
88 ULONG lSize,
89 BOOL bOwner,
90 double dMaxLoadFactor = HashTable::m_defMaxLoadFactor /* 0.8 */,
91 double dGrowFactor = HashTable::m_defDefGrowFactor /* 2.0 */
94 ~HashTable();
96 BOOL IsFull() const;
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
110 ULONG m_lAt;
111 HashTable const& m_aTable;
113 void* FindValidObject(BOOL bForward);
115 protected:
116 void* GetFirst(); // Interation _ohne_ Sortierung
117 void* GetNext();
118 void* GetLast();
119 void* GetPrev();
121 public:
122 HashTableIterator(HashTable const&);
125 // typsichere Makros ---------------------------------------------------
127 #define DECLARE_HASHTABLE_INTERN(ClassName,Owner,KeyType,ObjType) \
128 class ClassName : public HashTable \
130 public: \
131 ClassName \
133 ULONG lSize, \
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 \
158 protected: \
159 virtual void OnDeleteObject(void* pObject); \
160 public: \
161 ClassName \
163 ULONG lSize, \
164 double dMaxLoadFactor = HashTable::m_defMaxLoadFactor, \
165 double dGrowFactor = HashTable::m_defDefGrowFactor \
167 : ClassName##2(lSize,dMaxLoadFactor,dGrowFactor) {} \
168 ~ClassName(); \
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 \
190 public: \
191 ClassName(HashTable const& aTable) \
192 : HashTableIterator(aTable) {} \
194 ObjType GetFirst() \
195 { return (ObjType)HashTableIterator::GetFirst(); } \
196 ObjType GetNext() \
197 { return (ObjType)HashTableIterator::GetNext(); } \
198 ObjType GetLast() \
199 { return (ObjType)HashTableIterator::GetLast(); } \
200 ObjType GetPrev() \
201 { return (ObjType)HashTableIterator::GetPrev(); } \
205 #endif // _HASHTBL_HXX