1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_table.cpp
29 // copyright (c) 1997 Andrew W. Appel.
31 // author: Andrew Appel (appel@cs.princeton.edu)
32 // modified: Ge Wang (gewang@cs.princeton.edu)
33 // Perry R. Cook (prc@cs.princeton.edu)
35 //-----------------------------------------------------------------------------
38 #include "chuck_table.h"
39 #include "chuck_utils.h"
44 typedef struct binder_
*binder
;
45 struct binder_
{void *key
; void *value
; binder next
; void *prevtop
;};
51 TAB_hash_func hash_func
;
55 static binder
Binder(void *key
, void *value
, binder next
, void *prevtop
)
57 binder b
= (binder
)checked_malloc(sizeof(struct binder_
));
58 b
->key
= key
; b
->value
=value
; b
->next
=next
; b
->prevtop
= prevtop
;
63 TAB_table
TAB_empty(void)
65 return TAB_empty2(TABSIZE
);
69 TAB_table
TAB_empty2( unsigned long s
)
71 TAB_table t
= (TAB_table
)checked_malloc(sizeof(struct TAB_table_
));
73 t
->table
= (binder
*)checked_malloc(sizeof(binder
)*s
);
78 for( i
= 0; i
< s
; i
++ )
83 TAB_table
TAB_empty3( TAB_eq_func eq
, TAB_hash_func hash
, unsigned long s
)
85 TAB_table t
= TAB_empty2(s
);
94 void TAB_delete( TAB_table t
)
97 binder p
= NULL
, n
= NULL
;
98 for(i
= 0; i
< t
->size
; i
++ )
116 /* The cast from pointer to integer in the expression
117 * ((unsigned)key) % TABSIZE
118 * may lead to a warning message. However, the code is safe,
119 * and will still operate correctly. This line is just hashing
120 * a pointer value into an integer value, and no matter how the
121 * conversion is done, as long as it is done consistently, a
122 * reasonable and repeatable index into the table will result.
125 void TAB_enter(TAB_table t
, void *key
, void *value
)
128 unsigned long hval
= (unsigned long)key
;
131 hval
= (unsigned long)t
->hash_func(key
);
132 index
= hval
% t
->size
;
133 t
->table
[index
] = Binder(key
, value
, t
->table
[index
], t
->top
);
137 void *TAB_look(TAB_table t
, void *key
)
140 unsigned long hval
= (unsigned long)key
;
144 hval
= (unsigned long)t
->hash_func(key
);
145 index
= hval
% t
->size
;
148 for(b
=t
->table
[index
]; b
; b
=b
->next
)
149 if (b
->key
==key
) return b
->value
;
153 for(b
=t
->table
[index
]; b
; b
=b
->next
)
154 if (t
->eq_func(b
->key
, key
)) return b
->value
;
160 void *TAB_pop(TAB_table t
)
162 void *k
; binder b
; long index
;
167 hval
= (unsigned long)k
;
168 if(t
->hash_func
) hval
= (unsigned long)t
->hash_func(k
);
169 index
= ((unsigned long)hval
) % t
->size
;
172 t
->table
[index
] = b
->next
;
177 void *TAB_topv(TAB_table t
)
179 void *k
; binder b
; long index
;
184 hval
= (unsigned long)k
;
185 if(t
->hash_func
) hval
= (unsigned long)t
->hash_func(k
);
186 index
= hval
% t
->size
;
192 void TAB_dump(TAB_table t
, void (*show
)(void *key
, void *value
))
195 long index
= ((unsigned long)k
) % t
->size
;
196 binder b
= t
->table
[index
];
198 t
->table
[index
]=b
->next
;
200 show(b
->key
,b
->value
);
202 assert(t
->top
== b
->prevtop
&& t
->table
[index
]==b
->next
);
207 long str_eq( void * lhs
, void * rhs
)
209 return !strcmp( (char *)lhs
, (char *)rhs
);
212 long str_hash( void * str
)
214 unsigned long h
=0; char *s
;
215 for(s
=(char *)str
; *s
; s
++)