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"
34 // -------------------------------------------------------------
39 enum ETag
{ TAG_EMPTY
, TAG_USED
, TAG_DELETED
};
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
; }
53 { return m_Tag
== TAG_DELETED
|| m_Tag
== TAG_EMPTY
; }
56 { return m_Tag
== TAG_EMPTY
; }
59 { return m_Tag
== TAG_USED
; }
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
67 void* GetObject() const
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 // -------------------------------------------------------------
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
)
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
106 for (ULONG i=0; i<GetSize(); i++)
108 void *pObject = GetObjectAt(i);
111 OnDeleteObject(pObject());
116 // Speicher für HashItems freigeben
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
140 for (i
=0,n
=strlen( Key
); i
<n
; i
++)
142 h
= (h
<<4) + (unsigned long)(unsigned short)Key
[i
];
155 unsigned long HashTable::DHash(const char* Key
, unsigned long lOldHash
) const
157 unsigned long lHash
= lOldHash
;
160 for (i
=0,n
=strlen( Key
); i
<n
; i
++)
163 lHash
+= (unsigned long)(unsigned short)Key
[i
];
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;
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
193 if (FindPos(Key
) != NULL
)
196 unsigned long lPos
= Hash(Key
);
197 HashItem
*pItem
= &m_pData
[lPos
];
201 if (pItem
->IsEmpty())
203 pItem
->SetObject(Key
, pObject
);
211 lPos
= DHash(Key
,lPos
);
212 pItem
= &m_pData
[lPos
];
214 if (pItem
->IsEmpty())
216 pItem
->SetObject(Key
, pObject
);
227 pItem
= &m_pData
[lPos
];
229 while(!pItem
->IsEmpty());
231 pItem
->SetObject(Key
, pObject
);
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
245 unsigned long lPos
= Hash(Key
);
246 HashItem
*pItem
= &m_pData
[lPos
];
249 && !(strcmp( pItem
->GetKey(), Key
)))
256 if (pItem
->IsDeleted() || pItem
->IsUsed())
258 lPos
= DHash(Key
,lPos
);
259 pItem
= &m_pData
[lPos
];
262 && (!strcmp( pItem
->GetKey(), Key
)))
269 if (pItem
->IsDeleted() || pItem
->IsUsed())
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
;
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.
304 HashItem
*pItem
= FindPos(Key
);
307 && ( !strcmp( pItem
->GetKey(), Key
)))
308 return pItem
->GetObject();
313 void* HashTable::Delete( const char * Key
)
314 // Löscht Objekt, das unter Key abgespeichert ist und gibt Verweis
316 // Gibt NULL zurück, wenn Key nicht vorhanden ist.
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
);
325 && ( !strcmp( pItem
->GetKey(), Key
)))
327 void* pObject
= pItem
->GetObject();
330 OnDeleteObject(pObject
);
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
364 // Zustand "Tabelle voll" wird in Insert abgefangen
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
];
382 Insert(pItem
->GetKey(),pItem
->GetObject());
388 // Iterator ---------------------------------------------------------
391 HashTableIterator::HashTableIterator(HashTable
const& aTable
)
397 void* HashTableIterator::GetFirst()
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())
415 return FindValidObject(true /* forward */);
418 void* HashTableIterator::GetPrev()
424 return FindValidObject(false /* backward */);
427 void* HashTableIterator::FindValidObject(bool bForward
)
428 // Sucht nach einem vorhandenen Objekt ab der aktuellen
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
438 // m_lAt ist die gefundene Position
440 void *pObject
= m_aTable
.GetObjectAt(m_lAt
);
445 while (pObject
== NULL
446 && (bForward
? ((m_lAt
+1) < m_aTable
.GetSize())
454 pObject
= m_aTable
.GetObjectAt(m_lAt
);