merged tag ooo/OOO330_m14
[LibreOffice.git] / soldep / bootstrp / hashtbl.cxx
blobb6f9b0b7826a4dc1d85a812b0b849896fa55cac1
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 #include <tools/gen.hxx>
29 #include <tools/debug.hxx>
30 #include <soldep/hashtbl.hxx>
32 // -------------------------------------------------------------
33 // class HashItem
35 class HashItem
37 enum ETag { TAG_EMPTY, TAG_USED, TAG_DELETED };
39 void* m_pObject;
40 ETag m_Tag;
41 ByteString m_Key;
43 public:
44 HashItem() { m_Tag = TAG_EMPTY; m_pObject = NULL; }
46 BOOL IsDeleted() const
47 { return m_Tag == TAG_DELETED; }
49 BOOL IsEmpty() const
50 { return m_Tag == TAG_DELETED || m_Tag == TAG_EMPTY; }
52 BOOL IsFree() const
53 { return m_Tag == TAG_EMPTY; }
55 BOOL IsUsed() const
56 { return m_Tag == TAG_USED; }
58 void Delete()
59 { m_Tag = TAG_DELETED; m_Key = ""; m_pObject = NULL; }
61 ByteString const& GetKey() const
62 { return m_Key; }
64 void* GetObject() const
65 { return m_pObject; }
67 void SetObject(ByteString const Key, void *pObject)
68 { m_Tag = TAG_USED; m_Key = Key; m_pObject = pObject; }
71 #define MIN(a,b) (a)<(b)?(a):(b)
72 #define MAX(a,b) (a)>(b)?(a):(b)
74 // -------------------------------------------------------------
75 // class HashTable
78 /*static*/ double HashTable::m_defMaxLoadFactor = 0.8;
79 /*static*/ double HashTable::m_defDefGrowFactor = 2.0;
81 HashTable::HashTable(ULONG lSize, BOOL bOwner, double dMaxLoadFactor, double dGrowFactor)
83 m_lSize = lSize;
84 m_bOwner = bOwner;
85 m_lElem = 0;
86 m_dMaxLoadFactor = MAX(0.5,MIN(1.0,dMaxLoadFactor)); // 0.5 ... 1.0
87 m_dGrowFactor = MAX(1.3,MIN(5.0,dGrowFactor)); // 1.3 ... 5.0
88 m_pData = new HashItem [lSize];
90 // Statistik
91 #ifdef DBG_UTIL
92 m_aStatistic.m_lSingleHash = 0;
93 m_aStatistic.m_lDoubleHash = 0;
94 m_aStatistic.m_lProbe = 0;
95 #endif
98 HashTable::~HashTable()
100 // Wenn die HashTable der Owner der Objecte ist,
101 // müssen die Destruktoren separat gerufen werden.
102 // Dies geschieht über die virtuelle Methode OnDeleteObject()
104 // Problem: Virtuelle Funktionen sind im Destructor nicht virtuell!!
105 // Der Code muß deshalb ins Macro
108 if (m_bOwner)
110 for (ULONG i=0; i<GetSize(); i++)
112 void *pObject = GetObjectAt(i);
114 if (pObject != NULL)
115 OnDeleteObject(pObject());
120 // Speicher für HashItems freigeben
121 delete [] m_pData;
124 void* HashTable::GetObjectAt(ULONG lPos) const
125 // Gibt Objekt zurück, wenn es eines gibt, sonst NULL;
127 DBG_ASSERT(lPos<m_lSize, "HashTable::GetObjectAt()");
129 HashItem *pItem = &m_pData[lPos];
131 return pItem->IsUsed() ? pItem->GetObject() : NULL;
134 void HashTable::OnDeleteObject(void*)
136 DBG_ERROR("HashTable::OnDeleteObject(void*) nicht überladen");
139 ULONG HashTable::Hash(ByteString const& Key) const
142 ULONG lHash = 0;
143 ULONG i,n;
145 for (i=0,n=Key.Len(); i<n; i++)
147 lHash *= 256L;
148 lHash += (ULONG)(USHORT)Key.GetStr()[i];
149 lHash %= m_lSize;
151 return lHash;
154 // Hashfunktion von P.J. Weinberger
155 // aus dem "Drachenbuch" von Aho/Sethi/Ullman
156 ULONG i,n;
157 ULONG h = 0;
158 ULONG g = 0;
160 for (i=0,n=Key.Len(); i<n; i++)
162 h = (h<<4) + (ULONG)(USHORT)Key.GetBuffer()[i];
163 g = h & 0xf0000000;
165 if (g != 0)
167 h = h ^ (g >> 24);
168 h = h ^ g;
172 return h % m_lSize;
175 ULONG HashTable::DHash(ByteString const& Key, ULONG lOldHash) const
177 ULONG lHash = lOldHash;
178 ULONG i,n;
180 for (i=0,n=Key.Len(); i<n; i++)
182 lHash *= 256L;
183 lHash += (ULONG)(USHORT)Key.GetBuffer()[i];
184 lHash %= m_lSize;
186 return lHash;
188 /* return
190 lHash
191 + (char)Key.GetStr()[0] * 256
192 + (char)Key.GetStr()[Key.Len()-1]
195 % m_lSize;
199 ULONG HashTable::Probe(ULONG lPos) const
200 // gibt den Folgewert von lPos zurück
202 lPos++; if (lPos==m_lSize) lPos=0;
203 return lPos;
206 BOOL HashTable::IsFull() const
208 return m_lElem>=m_lSize;
211 BOOL HashTable::Insert(ByteString const& Key, void* pObject)
212 // pre: Key ist nicht im Dictionary enthalten, sonst return FALSE
213 // Dictionary ist nicht voll, sonst return FALSE
214 // post: pObject ist unter Key im Dictionary; m_nElem wurde erhöht
216 SmartGrow();
218 if (IsFull())
220 DBG_ERROR("HashTable::Insert() is full");
221 return FALSE;
224 if (FindPos(Key) != NULL )
225 return FALSE;
227 ULONG lPos = Hash(Key);
228 HashItem *pItem = &m_pData[lPos];
230 // first hashing
232 if (pItem->IsEmpty())
234 pItem->SetObject(Key, pObject);
235 m_lElem++;
237 #ifdef DBG_UTIL
238 m_aStatistic.m_lSingleHash++;
239 #endif
241 return TRUE;
244 // double hashing
246 lPos = DHash(Key,lPos);
247 pItem = &m_pData[lPos];
249 if (pItem->IsEmpty())
251 pItem->SetObject(Key, pObject);
252 m_lElem++;
254 #ifdef DBG_UTIL
255 m_aStatistic.m_lDoubleHash++;
256 #endif
258 return TRUE;
261 // linear probing
265 #ifdef DBG_UTIL
266 m_aStatistic.m_lProbe++;
267 #endif
269 lPos = Probe(lPos);
270 pItem = &m_pData[lPos];
272 while(!pItem->IsEmpty());
274 pItem->SetObject(Key, pObject);
275 m_lElem++;
276 return TRUE;
279 HashItem* HashTable::FindPos(ByteString const& Key) const
280 // sucht den Key; gibt Refrenz auf den Eintrag (gefunden)
281 // oder NULL (nicht gefunden) zurück
283 // pre: -
284 // post: -
286 // first hashing
288 ULONG lPos = Hash(Key);
289 HashItem *pItem = &m_pData[lPos];
291 if (pItem->IsUsed()
292 && pItem->GetKey() == Key)
294 return pItem;
297 // double hashing
299 if (pItem->IsDeleted() || pItem->IsUsed())
301 lPos = DHash(Key,lPos);
302 pItem = &m_pData[lPos];
304 if (pItem->IsUsed()
305 && pItem->GetKey() == Key)
307 return pItem;
310 // linear probing
312 if (pItem->IsDeleted() || pItem->IsUsed())
314 ULONG n = 0;
315 BOOL bFound = FALSE;
316 BOOL bEnd = FALSE;
320 n++;
321 lPos = Probe(lPos);
322 pItem = &m_pData[lPos];
324 bFound = pItem->IsUsed()
325 && pItem->GetKey() == Key;
327 bEnd = !(n<m_lSize || pItem->IsFree());
329 while(!bFound && !bEnd);
331 return bFound ? pItem : NULL;
335 // nicht gefunden
337 return NULL;
340 void* HashTable::Find(ByteString const& Key) const
341 // Gibt Verweis des Objektes zurück, das unter Key abgespeichert ist,
342 // oder NULL wenn nicht vorhanden.
344 // pre: -
345 // post: -
347 HashItem *pItem = FindPos(Key);
349 if (pItem != NULL
350 && pItem->GetKey() == Key)
351 return pItem->GetObject();
352 else
353 return NULL;
356 void* HashTable::Delete(ByteString const& Key)
357 // Löscht Objekt, das unter Key abgespeichert ist und gibt Verweis
358 // darauf zurück.
359 // Gibt NULL zurück, wenn Key nicht vorhanden ist.
361 // pre: -
362 // post: Objekt ist nicht mehr enthalten; m_lElem dekrementiert
363 // Wenn die HashTable der Owner ist, wurde das Object gelöscht
365 HashItem *pItem = FindPos(Key);
367 if (pItem != NULL
368 && pItem->GetKey() == Key)
370 void* pObject = pItem->GetObject();
372 if (m_bOwner)
373 OnDeleteObject(pObject);
375 pItem->Delete();
376 m_lElem--;
377 return pObject;
379 else
381 return NULL;
385 double HashTable::CalcLoadFactor() const
386 // prozentuale Belegung der Hashtabelle berechnen
388 return double(m_lElem) / double(m_lSize);
391 void HashTable::SmartGrow()
392 // Achtung: da die Objekte umkopiert werden, darf die OnDeleteObject-Methode
393 // nicht gerufen werden
395 double dLoadFactor = CalcLoadFactor();
397 if (dLoadFactor <= m_dMaxLoadFactor)
398 return; // nothing to grow
400 ULONG lOldSize = m_lSize; // alte Daten sichern
401 HashItem* pOldData = m_pData;
403 m_lSize = ULONG (m_dGrowFactor * m_lSize); // neue Größe
404 m_pData = new HashItem[m_lSize]; // neue Daten holen
406 // kein Speicher:
407 // Zustand "Tabelle voll" wird in Insert abgefangen
409 if (m_pData == NULL)
411 m_lSize = lOldSize;
412 m_pData = pOldData;
413 return;
416 m_lElem = 0; // noch keine neuen Daten
418 // Umkopieren der Daten
420 for (ULONG i=0; i<lOldSize; i++)
422 HashItem *pItem = &pOldData[i];
424 if (pItem->IsUsed())
425 Insert(pItem->GetKey(),pItem->GetObject());
428 delete [] pOldData;
431 // Iterator ---------------------------------------------------------
434 HashTableIterator::HashTableIterator(HashTable const& aTable)
435 : m_aTable(aTable)
437 m_lAt = 0;
440 void* HashTableIterator::GetFirst()
442 m_lAt = 0;
443 return FindValidObject(TRUE /* forward */);
446 void* HashTableIterator::GetLast()
448 m_lAt = m_aTable.GetSize() -1;
449 return FindValidObject(FALSE /* backward */);
452 void* HashTableIterator::GetNext()
454 if (m_lAt+1 >= m_aTable.GetSize())
455 return NULL;
457 m_lAt++;
458 return FindValidObject(TRUE /* forward */);
461 void* HashTableIterator::GetPrev()
463 if (m_lAt <= 0)
464 return NULL;
466 m_lAt--;
467 return FindValidObject(FALSE /* backward */);
470 void* HashTableIterator::FindValidObject(BOOL bForward)
471 // Sucht nach einem vorhandenen Objekt ab der aktuellen
472 // Position.
474 // pre: ab inkl. m_lAt soll die Suche beginnen
475 // post: if not found then
476 // if bForward == TRUE then
477 // m_lAt == m_aTable.GetSize() -1
478 // else
479 // m_lAt == 0
480 // else
481 // m_lAt ist die gefundene Position
483 void *pObject = m_aTable.GetObjectAt(m_lAt);
485 if (pObject != NULL)
486 return pObject;
488 while (pObject == NULL
489 && (bForward ? ((m_lAt+1) < m_aTable.GetSize())
490 : m_lAt > 0))
492 if (bForward)
493 m_lAt++;
494 else
495 m_lAt--;
497 pObject = m_aTable.GetObjectAt(m_lAt);
500 #ifdef DBG_UTIL
502 if (pObject == NULL)
504 DBG_ASSERT(bForward ? m_lAt == m_aTable.GetSize() -1 : m_lAt == 0,
505 "HashTableIterator::FindValidObject()");
508 #endif
510 return pObject;