b45d5912dbd03a0601b484ab8a6bdf6ad3960e5b
[fw/sdcc] / support / cpp2 / hashtable.h
1 /* Hash tables.
2    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifndef GCC_HASHTABLE_H
19 #define GCC_HASHTABLE_H
20
21 #if defined(__APPLE__) && defined(__MACH__)
22 #include "libiberty/obstack.h"
23 #else
24 #include "obstack.h"
25 #endif
26
27 /* This is what each hash table entry points to.  It may be embedded
28    deeply within another object.  */
29 typedef struct ht_identifier ht_identifier;
30 struct ht_identifier
31 {
32   unsigned int len;
33   const unsigned char *str;
34 };
35
36 #define HT_LEN(NODE) ((NODE)->len)
37 #define HT_STR(NODE) ((NODE)->str)
38
39 /* We want code outside cpplib, such as the compiler front-ends, to be
40    able to include this header, and to be able to link with
41    cpphashtbl.o without pulling in any other parts of cpplib.  */
42
43 struct cpp_reader;
44 typedef struct ht hash_table;
45 typedef struct ht_identifier *hashnode;
46
47 enum ht_lookup_option {HT_NO_INSERT = 0, HT_ALLOC, HT_ALLOCED};
48
49 /* An identifier hash table for cpplib and the front ends.  */
50 struct ht
51 {
52   /* Identifiers are allocated from here.  */
53   struct obstack stack;
54
55   hashnode *entries;
56   /* Call back.  */
57   hashnode (*alloc_node) PARAMS ((hash_table *));
58
59   unsigned int nslots;          /* Total slots in the entries array.  */
60   unsigned int nelements;       /* Number of live elements.  */
61
62   /* Link to reader, if any.  For the benefit of cpplib.  */
63   struct cpp_reader *pfile;
64
65   /* Table usage statistics.  */
66   unsigned int searches;
67   unsigned int collisions;
68 };
69
70 extern void gcc_obstack_init PARAMS ((struct obstack *));
71 /* Initialise the hashtable with 2 ^ order entries.  */
72 extern hash_table *ht_create PARAMS ((unsigned int order));
73 extern hashnode ht_lookup PARAMS ((hash_table *, const unsigned char *,
74                                    unsigned int, enum ht_lookup_option));
75
76 /* For all nodes in TABLE, make a callback.  The callback takes
77    TABLE->PFILE, the node, and a PTR, and the callback sequence stops
78    if the callback returns zero.  */
79 typedef int (*ht_cb) PARAMS ((struct cpp_reader *, hashnode, const void *));
80 extern void ht_forall PARAMS ((hash_table *, ht_cb, const void *));
81
82 /* Dump allocation statistics to stderr.  */
83 extern void ht_dump_statistics PARAMS ((hash_table *));
84
85 /* Approximate positive square root of a host double.  This is for
86    statistical reports, not code generation.  */
87 extern double approx_sqrt PARAMS ((double));
88
89 #endif /* GCC_HASHTABLE_H */