source: cpp/frams/util/hashtable.h @ 215

Last change on this file since 215 was 197, checked in by Maciej Komosinski, 11 years ago

GDK used by developers since 1999, distributed on the web since 2002

  • Property svn:eol-style set to native
File size: 1.8 KB
RevLine 
[121]1// This file is a part of the Framsticks GDK.
[197]2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
[109]3// Refer to http://www.framsticks.com/ for further information.
4
5#ifndef _HASHTABLE_H_
6#define _HASHTABLE_H_
7
8#include "sstring.h"
9
10class HashEntry
11{
12public:
13int hash;
14SString key;
15void *value;
16HashEntry *next;
17
18HashEntry(int h,const SString& k,void *v):hash(h),key(k),value(v),next(0){}
19};
20
21class HashEntryIterator;
22
23class HashTable
24{
25friend class HashEntryIterator;
26HashEntry **tab;
27int size;
28int count;
29int threshold;
30float load;
31long sync;
32
33int hash(const char *s);
34void rehash(int newsize);
35public:
36
37HashTable(int initsize,float lo) {init(initsize,lo);}
38HashTable(int initsize) {init(initsize,0.75);}
39HashTable() {init();}
40~HashTable();
41
42/** always use init() after clear() ! */
43void clear();
44void init(int initsize=11,float lo=0.75);
45
46int getSize() {return count;}
47void* put(const SString& key,void *value);
48void* get(const SString& key, int *reallygot=0);
49void* remove(const SString& key);
50/** can be used inside iteration loop:
51    for(HashEntryIterator it(hashtable);it;) hashtable.remove(it);
52    \note iterator is "iterated" to the next entry when the current one is removed (no "it++"!)
53 */
54void* remove(HashEntryIterator& it);
55
56void debugprint();
57void getstats(float *);
58};
59
60/** for(HashEntryIterator it(hashtable);it;it++)
61      {
62      ... it->value
63      ... it->key
64      }
65 */
66class HashEntryIterator
67{
68void findNext();
69  public:
70const HashTable *ht;
71int hashindex;
72HashEntry *entry;
73int sync;
74  HashEntryIterator(const HashTable&t):ht(&t),hashindex(0),entry(t.tab[0]),sync(ht->sync)
75        {if (!entry) findNext();}
76  HashEntryIterator() {}
77void operator++();
78void operator++(int) {operator++();}
79HashEntry* operator->() {return entry;}
80bool isValid() {return (entry&&(sync==ht->sync))?1:0;}
81};
82
83
84#endif
Note: See TracBrowser for help on using the repository browser.