OOO330
[LibreOffice.git] / soltools / ldump / hashtbl.cxx
blob27d48926f0df71877eed7ca0d4cbf1d397fc6296
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_soltools.hxx"
31 #include "hashtbl.hxx"
32 #include <string.h>
34 // -------------------------------------------------------------
35 // class HashItem
37 class HashItem
39 enum ETag { TAG_EMPTY, TAG_USED, TAG_DELETED };
41 void* m_pObject;
42 ETag m_Tag;
43 char* m_Key;
45 public:
46 HashItem() { m_Tag = TAG_EMPTY; m_Key = NULL; m_pObject = NULL; }
47 ~HashItem() { delete [] m_Key; }
49 bool IsDeleted() const
50 { return m_Tag == TAG_DELETED; }
52 bool IsEmpty() const
53 { return m_Tag == TAG_DELETED || m_Tag == TAG_EMPTY; }
55 bool IsFree() const
56 { return m_Tag == TAG_EMPTY; }
58 bool IsUsed() const
59 { return m_Tag == TAG_USED; }
61 void Delete()
62 { m_Tag = TAG_DELETED; delete [] m_Key; m_Key = new char[ 1 ]; m_Key[ 0 ] = 0; m_pObject = NULL; }
64 const char *GetKey() const
65 { return m_Key; }
67 void* GetObject() const
68 { return m_pObject; }
70 void SetObject(const char * Key, void *pObject)
71 { m_Tag = TAG_USED; delete [] m_Key; m_Key = new char[ strlen( Key ) + 1 ]; strcpy( m_Key, Key ); m_pObject = pObject; }
74 #define MIN(a,b) (a)<(b)?(a):(b)
75 #define MAX(a,b) (a)>(b)?(a):(b)
77 // -------------------------------------------------------------
78 // class HashTable
81 /*static*/ double HashTable::m_defMaxLoadFactor = 0.5;
82 /*static*/ double HashTable::m_defDefGrowFactor = 2.0;
84 HashTable::HashTable(unsigned long lSize, bool bOwner, double dMaxLoadFactor, double dGrowFactor)
86 m_lSize = lSize;
87 m_bOwner = bOwner;
88 m_lElem = 0;
89 m_dMaxLoadFactor = MAX(0.5,MIN(1.0,dMaxLoadFactor)); // 0.5 ... 1.0
90 m_dGrowFactor = MAX(2.0,MIN(5.0,dGrowFactor)); // 1.3 ... 5.0
91 m_pData = new HashItem [lSize];
94 HashTable::~HashTable()
96 // Wenn die HashTable der Owner der Objecte ist,
97 // müssen die Destruktoren separat gerufen werden.
98 // Dies geschieht über die virtuelle Methode OnDeleteObject()
100 // Problem: Virtuelle Funktionen sind im Destructor nicht virtuell!!
101 // Der Code muß deshalb ins Macro
104 if (m_bOwner)
106 for (ULONG i=0; i<GetSize(); i++)
108 void *pObject = GetObjectAt(i);
110 if (pObject != NULL)
111 OnDeleteObject(pObject());
116 // Speicher für HashItems freigeben
117 delete [] m_pData;
120 void* HashTable::GetObjectAt(unsigned long lPos) const
121 // Gibt Objekt zurück, wenn es eines gibt, sonst NULL;
123 HashItem *pItem = &m_pData[lPos];
125 return pItem->IsUsed() ? pItem->GetObject() : NULL;
128 void HashTable::OnDeleteObject(void*)
132 unsigned long HashTable::Hash(const char *Key) const
134 // Hashfunktion von P.J. Weinberger
135 // aus dem "Drachenbuch" von Aho/Sethi/Ullman
136 unsigned long i,n;
137 unsigned long h = 0;
138 unsigned long g = 0;
140 for (i=0,n=strlen( Key ); i<n; i++)
142 h = (h<<4) + (unsigned long)(unsigned short)Key[i];
143 g = h & 0xf0000000;
145 if (g != 0)
147 h = h ^ (g >> 24);
148 h = h ^ g;
152 return h % m_lSize;
155 unsigned long HashTable::DHash(const char* Key, unsigned long lOldHash) const
157 unsigned long lHash = lOldHash;
158 unsigned long i,n;
160 for (i=0,n=strlen( Key ); i<n; i++)
162 lHash *= 256L;
163 lHash += (unsigned long)(unsigned short)Key[i];
164 lHash %= m_lSize;
166 return lHash;
169 unsigned long HashTable::Probe(unsigned long lPos) const
170 // gibt den Folgewert von lPos zurück
172 lPos++; if (lPos==m_lSize) lPos=0;
173 return lPos;
176 bool HashTable::IsFull() const
178 return m_lElem>=m_lSize;
181 bool HashTable::Insert(const char * Key, void* pObject)
182 // pre: Key ist nicht im Dictionary enthalten, sonst return FALSE
183 // Dictionary ist nicht voll, sonst return FALSE
184 // post: pObject ist unter Key im Dictionary; m_nElem wurde erhöht
186 SmartGrow();
188 if (IsFull())
190 return false;
193 if (FindPos(Key) != NULL )
194 return false;
196 unsigned long lPos = Hash(Key);
197 HashItem *pItem = &m_pData[lPos];
199 // first hashing
201 if (pItem->IsEmpty())
203 pItem->SetObject(Key, pObject);
204 m_lElem++;
206 return true;
209 // double hashing
211 lPos = DHash(Key,lPos);
212 pItem = &m_pData[lPos];
214 if (pItem->IsEmpty())
216 pItem->SetObject(Key, pObject);
217 m_lElem++;
219 return true;
222 // linear probing
226 lPos = Probe(lPos);
227 pItem = &m_pData[lPos];
229 while(!pItem->IsEmpty());
231 pItem->SetObject(Key, pObject);
232 m_lElem++;
233 return true;
236 HashItem* HashTable::FindPos(const char * Key) const
237 // sucht den Key; gibt Refrenz auf den Eintrag (gefunden)
238 // oder NULL (nicht gefunden) zurück
240 // pre: -
241 // post: -
243 // first hashing
245 unsigned long lPos = Hash(Key);
246 HashItem *pItem = &m_pData[lPos];
248 if (pItem->IsUsed()
249 && !(strcmp( pItem->GetKey(), Key )))
251 return pItem;
254 // double hashing
256 if (pItem->IsDeleted() || pItem->IsUsed())
258 lPos = DHash(Key,lPos);
259 pItem = &m_pData[lPos];
261 if (pItem->IsUsed()
262 && (!strcmp( pItem->GetKey(), Key)))
264 return pItem;
267 // linear probing
269 if (pItem->IsDeleted() || pItem->IsUsed())
271 unsigned long n = 0;
272 bool bFound = false;
273 bool bEnd = false;
277 n++;
278 lPos = Probe(lPos);
279 pItem = &m_pData[lPos];
281 bFound = pItem->IsUsed()
282 && !( strcmp( pItem->GetKey(), Key ));
284 bEnd = !(n<m_lSize || pItem->IsFree());
286 while(!bFound && !bEnd);
288 return bFound ? pItem : NULL;
292 // nicht gefunden
294 return NULL;
297 void* HashTable::Find(const char *Key) const
298 // Gibt Verweis des Objektes zurück, das unter Key abgespeichert ist,
299 // oder NULL wenn nicht vorhanden.
301 // pre: -
302 // post: -
304 HashItem *pItem = FindPos(Key);
306 if (pItem != NULL
307 && ( !strcmp( pItem->GetKey(), Key )))
308 return pItem->GetObject();
309 else
310 return NULL;
313 void* HashTable::Delete( const char * Key)
314 // Löscht Objekt, das unter Key abgespeichert ist und gibt Verweis
315 // darauf zurück.
316 // Gibt NULL zurück, wenn Key nicht vorhanden ist.
318 // pre: -
319 // post: Objekt ist nicht mehr enthalten; m_lElem dekrementiert
320 // Wenn die HashTable der Owner ist, wurde das Object gelöscht
322 HashItem *pItem = FindPos(Key);
324 if (pItem != NULL
325 && ( !strcmp( pItem->GetKey(), Key )))
327 void* pObject = pItem->GetObject();
329 if (m_bOwner)
330 OnDeleteObject(pObject);
332 pItem->Delete();
333 m_lElem--;
334 return pObject;
336 else
338 return NULL;
342 double HashTable::CalcLoadFactor() const
343 // prozentuale Belegung der Hashtabelle berechnen
345 return double(m_lElem) / double(m_lSize);
348 void HashTable::SmartGrow()
349 // Achtung: da die Objekte umkopiert werden, darf die OnDeleteObject-Methode
350 // nicht gerufen werden
352 double dLoadFactor = CalcLoadFactor();
354 if (dLoadFactor <= m_dMaxLoadFactor)
355 return; // nothing to grow
357 unsigned long lOldSize = m_lSize; // alte Daten sichern
358 HashItem* pOldData = m_pData;
360 m_lSize = (unsigned long) (m_dGrowFactor * m_lSize); // neue Größe
361 m_pData = new HashItem[m_lSize]; // neue Daten holen
363 // kein Speicher:
364 // Zustand "Tabelle voll" wird in Insert abgefangen
366 if (m_pData == NULL)
368 m_lSize = lOldSize;
369 m_pData = pOldData;
370 return;
373 m_lElem = 0; // noch keine neuen Daten
375 // Umkopieren der Daten
377 for (unsigned long i=0; i<lOldSize; i++)
379 HashItem *pItem = &pOldData[i];
381 if (pItem->IsUsed())
382 Insert(pItem->GetKey(),pItem->GetObject());
385 delete [] pOldData;
388 // Iterator ---------------------------------------------------------
391 HashTableIterator::HashTableIterator(HashTable const& aTable)
392 : m_aTable(aTable)
394 m_lAt = 0;
397 void* HashTableIterator::GetFirst()
399 m_lAt = 0;
400 return FindValidObject(true /* forward */);
403 void* HashTableIterator::GetLast()
405 m_lAt = m_aTable.GetSize() -1;
406 return FindValidObject(false /* backward */);
409 void* HashTableIterator::GetNext()
411 if (m_lAt+1 >= m_aTable.GetSize())
412 return NULL;
414 m_lAt++;
415 return FindValidObject(true /* forward */);
418 void* HashTableIterator::GetPrev()
420 if (m_lAt <= 0)
421 return NULL;
423 m_lAt--;
424 return FindValidObject(false /* backward */);
427 void* HashTableIterator::FindValidObject(bool bForward)
428 // Sucht nach einem vorhandenen Objekt ab der aktuellen
429 // Position.
431 // pre: ab inkl. m_lAt soll die Suche beginnen
432 // post: if not found then
433 // if bForward == TRUE then
434 // m_lAt == m_aTable.GetSize() -1
435 // else
436 // m_lAt == 0
437 // else
438 // m_lAt ist die gefundene Position
440 void *pObject = m_aTable.GetObjectAt(m_lAt);
442 if (pObject != NULL)
443 return pObject;
445 while (pObject == NULL
446 && (bForward ? ((m_lAt+1) < m_aTable.GetSize())
447 : m_lAt > 0))
449 if (bForward)
450 m_lAt++;
451 else
452 m_lAt--;
454 pObject = m_aTable.GetObjectAt(m_lAt);
457 return pObject;