1 /* bag.c -- ARMulator support code: ARM6 Instruction Emulator.
2 Copyright (C) 1994 Advanced RISC Machines Ltd.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>. */
17 /********************************************************************/
19 /* Offers a data structure for storing and getting pairs of number. */
20 /* The numbers are stored together, put one can be looked up by */
21 /* quoting the other. If a new pair is entered and one of the */
22 /* numbers is a repeat of a previous pair, then the previos pair */
24 /********************************************************************/
29 #define HASH_TABLE_SIZE 256
30 #define hash(x) (((x)&0xff)^(((x)>>8)&0xff)^(((x)>>16)&0xff)^(((x)>>24)&0xff))
32 typedef struct hashentry
34 struct hashentry
*next
;
40 Hashentry
*lookupbyfirst
[HASH_TABLE_SIZE
];
41 Hashentry
*lookupbysecond
[HASH_TABLE_SIZE
];
44 addtolist (Hashentry
** add
, long first
, long second
)
47 add
= &((*add
)->next
);
48 /* Malloc will never fail? :o( */
49 (*add
) = (Hashentry
*) malloc (sizeof (Hashentry
));
50 (*add
)->next
= (Hashentry
*) 0;
51 (*add
)->first
= first
;
52 (*add
)->second
= second
;
56 killwholelist (Hashentry
* p
)
69 removefromlist (Hashentry
** p
, long first
)
75 if ((*p
)->first
== first
)
87 BAG_putpair (long first
, long second
)
91 if (BAG_getfirst (&junk
, second
) != NO_SUCH_PAIR
)
92 BAG_killpair_bysecond (second
);
93 addtolist (&lookupbyfirst
[hash (first
)], first
, second
);
94 addtolist (&lookupbysecond
[hash (second
)], first
, second
);
98 BAG_getfirst (long *first
, long second
)
102 look
= lookupbysecond
[hash (second
)];
104 if (look
->second
== second
)
106 *first
= look
->first
;
113 BAG_getsecond (long first
, long *second
)
117 look
= lookupbyfirst
[hash (first
)];
120 if (look
->first
== first
)
122 *second
= look
->second
;
131 BAG_killpair_byfirst (long first
)
135 if (BAG_getsecond (first
, &second
) == NO_SUCH_PAIR
)
137 removefromlist (&lookupbyfirst
[hash (first
)], first
);
138 removefromlist (&lookupbysecond
[hash (second
)], first
);
143 BAG_killpair_bysecond (long second
)
147 if (BAG_getfirst (&first
, second
) == NO_SUCH_PAIR
)
149 removefromlist (&lookupbyfirst
[hash (first
)], first
);
150 removefromlist (&lookupbysecond
[hash (second
)], first
);
159 for (i
= 0; i
< 256; i
++)
161 killwholelist (lookupbyfirst
[i
]);
162 killwholelist (lookupbysecond
[i
]);
163 lookupbyfirst
[i
] = lookupbysecond
[i
] = (Hashentry
*) 0;
This page took 0.035363 seconds and 4 git commands to generate.