Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hash.h
Go to the documentation of this file.
1 /*
2  * hash.h - hash function interface
3  *
4  * Copyright (C) 2005, 2007 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id$
22  *
23  */
24 
25 #ifndef __HASH_H__
26 #define __HASH_H__
27 
28 namespace qucs {
29 
30 /* Useful defines. */
31 #define HASH_SHRINK 4
32 #define HASH_EXPAND 8
33 #define HASH_MIN_SIZE 4
34 #define HASH_SHRINK_LIMIT (buckets >> 2)
35 #define HASH_EXPAND_LIMIT ((buckets >> 1) + (buckets >> 2))
36 #define HASH_LOCATION(code) ((code) & (buckets - 1))
37 
38 template <class type_t> class hashentry;
39 template <class type_t> class hashbucket;
40 template <class type_t> class hash;
41 template <class type_t> class hashiterator;
42 
43 /* This is the basic structure of a hash entry consisting of its key,
44  the actual value stored in the hash table and the hash code of the
45  key. */
46 template <class type_t>
47 class hashentry
48 {
49  friend class hashiterator<type_t>;
50  friend class hashbucket<type_t>;
51  friend class hash<type_t>;
52 
53  public:
54  hashentry () { // Constructor.
55  code = 0; key = NULL; value = NULL;
56  }
57  ~hashentry () { // Destructor.
58  if (key) free (key);
59  }
60 
61  private:
62  int code; // Hash code.
63  char * key; // Hash key.
64  type_t * value; // Hash value.
65 };
66 
67 /* The hash table consists of different hash buckets. This contains
68  the bucket's size and the entry array. */
69 template <class type_t>
70 class hashbucket
71 {
72  friend class hashiterator<type_t>;
73  friend class hash<type_t>;
74 
75  public:
76  hashbucket () { // Constructor.
77  capacity = size = 0;
78  entry = NULL;
79  }
80  ~hashbucket () { // Destructor.
81  if (entry) {
82  for (int n = 0; n < size; n++) delete entry[n];
83  free (entry);
84  }
85  }
86 
87  public:
88  // Adds an entry to the bucket.
89  void add (hashentry<type_t> * e) {
90  if (capacity == 0) {
93  malloc (sizeof (hashentry<type_t> *) * capacity);
94  }
95  else if (size >= capacity) {
96  capacity *= 2;
98  realloc (entry, sizeof (hashentry<type_t> *) * capacity);
99  }
100  entry[size++] = e;
101  }
102  // Removes an entry from the bucket.
103  void del (int idx) {
104  size--;
105  if (idx != size) entry[idx] = entry[size];
106  }
107 
108  private:
109  int capacity; // The capacity of the bucket.
110  int size; // The current size.
111  hashentry<type_t> ** entry; // Hash entry table.
112 };
113 
114 
115 /* This structure keeps information of a specific hash table. */
116 template <class type_t>
117 class hash
118 {
119  friend class hashiterator<type_t>;
120 
121  public:
122  hash (int size = HASH_MIN_SIZE);
123  ~hash ();
124 
125  int count (void);
126  void clear (void);
127  void rehash (int);
128  type_t * put (char *, type_t *);
129  type_t * get (char *);
130  type_t * del (char *);
131 
132  private:
133  int buckets;
134  int fill;
135  int keys;
136  int (* equals) (char *, char *);
137  int (* code) (char *);
138  unsigned (* keylen) (char *);
140 };
141 
142 /* Definition of the hash table iterator. */
143 template <class type_t>
144 class hashiterator
145 {
146  public:
147  hashiterator ();
149  ~hashiterator ();
150 
151  int count (void);
152  char * toFirst (void);
153  char * toLast (void);
154  char * operator++ (void);
155  char * operator-- (void);
156  char * operator * (void) { return current (); }
157  char * current (void);
158  char * currentKey (void);
159  type_t * currentVal (void);
160  char * first (void);
161  char * last (void);
162 
163  private:
168  int _bucket;
169  int _entry;
170 };
171 
172 } /* namespace qucs */
173 
174 #include "hash.cpp"
175 
176 #endif /* __HASH_H__ */
hashentry< type_t > * _last
Definition: hash.h:166
type_t * currentVal(void)
Definition: hash.cpp:398
void rehash(int)
Definition: hash.cpp:130
size
Definition: parse_vcd.y:203
type_t * put(char *, type_t *)
Definition: hash.cpp:192
int(* equals)(char *, char *)
Definition: hash.h:136
char * last(void)
Definition: hash.cpp:410
char * first(void)
Definition: hash.cpp:404
hashentry< type_t > * _first
Definition: hash.h:165
char * operator--(void)
Definition: hash.cpp:364
n
Definition: parse_citi.y:147
int(* code)(char *)
Definition: hash.h:137
void clear(void)
Definition: hash.cpp:106
char * operator++(void)
Definition: hash.cpp:342
~hash()
Definition: hash.cpp:96
char * key
Definition: hash.h:63
char * operator*(void)
Definition: hash.h:156
char * toLast(void)
Definition: hash.cpp:326
#define HASH_MIN_SIZE
Definition: hash.h:33
hashentry< type_t > ** entry
Definition: hash.h:111
free($1)
unsigned(* keylen)(char *)
Definition: hash.h:138
int count(void)
Definition: hash.cpp:122
char * current(void)
Definition: hash.cpp:386
~hashentry()
Definition: hash.h:57
type_t * del(char *)
Definition: hash.cpp:237
hash< type_t > * _hash
Definition: hash.h:164
type_t * value
Definition: hash.h:64
int code
Definition: hash.h:62
List int
Definition: parse_citi.y:183
char * currentKey(void)
Definition: hash.cpp:392
char * toFirst(void)
Definition: hash.cpp:310
hash(int size=HASH_MIN_SIZE)
Definition: hash.cpp:74
void add(hashentry< type_t > *e)
Definition: hash.h:89
hashentry< type_t > * _current
Definition: hash.h:167
void del(int idx)
Definition: hash.h:103
int fill
Definition: hash.h:134
int keys
Definition: hash.h:135
hashbucket< type_t > ** table
Definition: hash.h:139
int capacity
Definition: hash.h:109
int buckets
Definition: hash.h:133
int count(void)
Definition: hash.cpp:304