Macaulay2 Engine
Loading...
Searching...
No Matches
exptable.c
Go to the documentation of this file.
1#include "exptable.h"
2#include "table.h"
3/* Implementation of a hashtable [exponent vectors, of fixed length] -->
4 * unsigned long int. */
5/* The implementation uses table.{h,c}, which was written by David R. Hanson */
6
7#include "interface/m2-mem.h"
8
9/*these next lines added by MES, July 2002, to use our gc routines..*/
10#define NEW(p) ((p) = (void *) getmem((long)sizeof *(p)))
11#define FREE(ptr) ((void)(freemem((ptr)), (ptr) = 0))
12
14{
15 int nvars;
16 Table_T *table;
17};
18
19static int table_nvars; /* Set this before calling 'hash' */
20
21unsigned int exp_hash(const void *x)
22/* exp has type 'exponent' */
23{
24 int i;
25 const int *xx = x;
26 unsigned int result = 0;
27 for (i = 0; i < table_nvars; i++) result += (xx[i] << 3);
28 return result;
29}
30
31int exp_cmp(const void *x, const void *y)
32/* x, y are both of type 'exponent' */
33{
34 int i;
35 const int *xx = x;
36 const int *yy = y;
37 for (i = 0; i < table_nvars; i++)
38 {
39 int cmp = xx[i] - yy[i];
40 if (cmp != 0) return cmp;
41 }
42 return 0;
43}
44
46{
48 NEW(result);
49 result->nvars = nvars;
50 result->table = Table_new(hint, exp_cmp, exp_hash);
51 return result;
52}
53
56{
57 Table_free(&((*E)->table));
58 FREE(*E);
59 *E = 0;
60}
61
62long exponent_table_put(exponent_table *E, const exponent expon, long value)
63/* Puts the element 'expon => value' into the table, and if an element is
64 already there, its value is returned, otherwise 0 is returned.
65 Thus, it is a good idea to not insert zero values into the table */
66{
67 table_nvars = E->nvars;
68 return (long)Table_put(E->table, expon, (void *)value);
69}
70
72/* Returns the value associated to 'expon', returning zero, if 'expon'
73 is not in the table */
74{
75 table_nvars = E->nvars;
76 return (long)Table_get(E->table, expon);
77}
78
80{
81 return Table_toArray(E->table, NULL);
82}
83
84/*
85// Local Variables:
86// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
87// indent-tabs-mode: nil
88// End:
89*/
#define FREE(ptr)
Definition exptable.c:11
static int table_nvars
Definition exptable.c:19
void exponent_table_free(exponent_table **E)
Definition exptable.c:55
const void ** exponent_table_to_array(exponent_table *E)
Definition exptable.c:79
exponent_table * exponent_table_new(int hint, int nvars)
Definition exptable.c:45
#define NEW(p)
Definition exptable.c:10
unsigned int exp_hash(const void *x)
Definition exptable.c:21
int exp_cmp(const void *x, const void *y)
Definition exptable.c:31
long exponent_table_put(exponent_table *E, const exponent expon, long value)
Definition exptable.c:62
int exponent_table_length(exponent_table *E)
Definition exptable.c:54
long exponent_table_get(exponent_table *E, const exponent expon)
Definition exptable.c:71
int * exponent
Definition exptable.h:34
Hash table specialisation for (exponent vector, unsigned long) pairs.
VALGRIND_MAKE_MEM_DEFINED & result(result)
Engine-wide GC allocator surface (getmem / getmem_atomic) and debug-allocation trap.
volatile int x
Table_T * table
Definition exptable.c:16
int Table_length(T *table)
Definition table.c:108
void Table_free(T **table)
Definition table.c:170
const void ** Table_toArray(T *table, void *end)
Definition table.c:154
void * Table_get(T *table, const void *key)
Definition table.c:72
void * Table_put(T *table, const void *key, void *value)
Definition table.c:83
T * Table_new(int hint, int cmp(const void *x, const void *y), unsigned hash(const void *key))
Definition table.c:50