webperimental: killstack decides stack protects.
[freeciv.git] / common / idex.c
blobecfe991b4dc7b01fe436ea1c146d6e49e475b8aa
1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 /***********************************************************************
15 idex = ident index: a lookup table for quick mapping of unit and city
16 id values to unit and city pointers.
18 Method: use separate hash tables for each type.
19 Means code duplication for city/unit cases, but simplicity advantages.
20 Don't have to manage memory at all: store pointers to unit and city
21 structs allocated elsewhere, and keys are pointers to id values inside
22 the structs.
24 Note id values should probably be unsigned int: here leave as plain int
25 so can use pointers to pcity->id etc.
26 ***********************************************************************/
28 #ifdef HAVE_CONFIG_H
29 #include <fc_config.h>
30 #endif
32 /* utility */
33 #include "log.h"
35 /* common */
36 #include "city.h"
37 #include "unit.h"
39 #include "idex.h"
41 /**************************************************************************
42 Initialize. Should call this at the start before use.
43 ***************************************************************************/
44 void idex_init(struct world *iworld)
46 iworld->cities = city_hash_new();
47 iworld->units = unit_hash_new();
50 /**************************************************************************
51 Free the hashs.
52 ***************************************************************************/
53 void idex_free(struct world *iworld)
55 city_hash_destroy(iworld->cities);
56 iworld->cities = NULL;
58 unit_hash_destroy(iworld->units);
59 iworld->units = NULL;
62 /**************************************************************************
63 Register a city into idex, with current pcity->id.
64 Call this when pcity created.
65 ***************************************************************************/
66 void idex_register_city(struct world *iworld, struct city *pcity)
68 struct city *old;
70 city_hash_replace_full(iworld->cities, pcity->id, pcity, NULL, &old);
71 fc_assert_ret_msg(NULL == old,
72 "IDEX: city collision: new %d %p %s, old %d %p %s",
73 pcity->id, (void *) pcity, city_name_get(pcity),
74 old->id, (void *) old, city_name_get(old));
77 /**************************************************************************
78 Register a unit into idex, with current punit->id.
79 Call this when punit created.
80 ***************************************************************************/
81 void idex_register_unit(struct world *iworld, struct unit *punit)
83 struct unit *old;
85 unit_hash_replace_full(iworld->units, punit->id, punit, NULL, &old);
86 fc_assert_ret_msg(NULL == old,
87 "IDEX: unit collision: new %d %p %s, old %d %p %s",
88 punit->id, (void *) punit, unit_rule_name(punit),
89 old->id, (void *) old, unit_rule_name(old));
92 /**************************************************************************
93 Remove a city from idex, with current pcity->id.
94 Call this when pcity deleted.
95 ***************************************************************************/
96 void idex_unregister_city(struct world *iworld, struct city *pcity)
98 struct city *old;
100 city_hash_remove_full(iworld->cities, pcity->id, NULL, &old);
101 fc_assert_ret_msg(NULL != old,
102 "IDEX: city unreg missing: %d %p %s",
103 pcity->id, (void *) pcity, city_name_get(pcity));
104 fc_assert_ret_msg(old == pcity, "IDEX: city unreg mismatch: "
105 "unreg %d %p %s, old %d %p %s",
106 pcity->id, (void *) pcity, city_name_get(pcity),
107 old->id, (void *) old, city_name_get(old));
110 /**************************************************************************
111 Remove a unit from idex, with current punit->id.
112 Call this when punit deleted.
113 ***************************************************************************/
114 void idex_unregister_unit(struct world *iworld, struct unit *punit)
116 struct unit *old;
118 unit_hash_remove_full(iworld->units, punit->id, NULL, &old);
119 fc_assert_ret_msg(NULL != old,
120 "IDEX: unit unreg missing: %d %p %s",
121 punit->id, (void *) punit, unit_rule_name(punit));
122 fc_assert_ret_msg(old == punit, "IDEX: unit unreg mismatch: "
123 "unreg %d %p %s, old %d %p %s",
124 punit->id, (void *) punit, unit_rule_name(punit),
125 old->id, (void*) old, unit_rule_name(old));
128 /**************************************************************************
129 Lookup city with given id.
130 Returns NULL if the city is not registered (which is not an error).
131 ***************************************************************************/
132 struct city *idex_lookup_city(struct world *iworld, int id)
134 struct city *pcity;
136 city_hash_lookup(iworld->cities, id, &pcity);
138 return pcity;
141 /**************************************************************************
142 Lookup unit with given id.
143 Returns NULL if the unit is not registered (which is not an error).
144 ***************************************************************************/
145 struct unit *idex_lookup_unit(struct world *iworld, int id)
147 struct unit *punit;
149 unit_hash_lookup(iworld->units, id, &punit);
151 return punit;