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 2 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
18 /********************************************************************/
20 /* Offers a data structure for storing and getting pairs of number. */
21 /* The numbers are stored together, put one can be looked up by */
22 /* quoting the other. If a new pair is entered and one of the */
23 /* numbers is a repeat of a previous pair, then the previos pair */
25 /********************************************************************/
30 #define HASH_TABLE_SIZE 256
31 #define hash(x) (((x)&0xff)^(((x)>>8)&0xff)^(((x)>>16)&0xff)^(((x)>>24)&0xff))
33 typedef struct hashentry
35 struct hashentry
*next
;
41 Hashentry
*lookupbyfirst
[HASH_TABLE_SIZE
];
42 Hashentry
*lookupbysecond
[HASH_TABLE_SIZE
];
45 addtolist (Hashentry
** add
, long first
, long second
)
48 add
= &((*add
)->next
);
49 /* Malloc will never fail? :o( */
50 (*add
) = (Hashentry
*) malloc (sizeof (Hashentry
));
51 (*add
)->next
= (Hashentry
*) 0;
52 (*add
)->first
= first
;
53 (*add
)->second
= second
;
57 killwholelist (Hashentry
* p
)
70 removefromlist (Hashentry
** p
, long first
)
76 if ((*p
)->first
== first
)
88 BAG_putpair (long first
, long second
)
92 if (BAG_getfirst (&junk
, second
) != NO_SUCH_PAIR
)
93 BAG_killpair_bysecond (second
);
94 addtolist (&lookupbyfirst
[hash (first
)], first
, second
);
95 addtolist (&lookupbysecond
[hash (second
)], first
, second
);
99 BAG_getfirst (long *first
, long second
)
103 look
= lookupbysecond
[hash (second
)];
105 if (look
->second
== second
)
107 *first
= look
->first
;
114 BAG_getsecond (long first
, long *second
)
118 look
= lookupbyfirst
[hash (first
)];
121 if (look
->first
== first
)
123 *second
= look
->second
;
132 BAG_killpair_byfirst (long first
)
136 if (BAG_getsecond (first
, &second
) == NO_SUCH_PAIR
)
138 removefromlist (&lookupbyfirst
[hash (first
)], first
);
139 removefromlist (&lookupbysecond
[hash (second
)], first
);
144 BAG_killpair_bysecond (long second
)
148 if (BAG_getfirst (&first
, second
) == NO_SUCH_PAIR
)
150 removefromlist (&lookupbyfirst
[hash (first
)], first
);
151 removefromlist (&lookupbysecond
[hash (second
)], first
);
160 for (i
= 0; i
< 256; i
++)
162 killwholelist (lookupbyfirst
[i
]);
163 killwholelist (lookupbysecond
[i
]);
164 lookupbyfirst
[i
] = lookupbysecond
[i
] = (Hashentry
*) 0;
This page took 0.037078 seconds and 4 git commands to generate.