Freeciv-3.2: /fast/freeciv/nightly/S3_2/common/idex.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
idex.c
Go to the documentation of this file.
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.
7
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***********************************************************************/
13
14/***********************************************************************
15 idex = ident index: a lookup table for quick mapping of unit and city
16 id values to unit and city pointers.
17
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.
23
24 Note id values should probably be unsigned int: here leave as plain int
25 so can use pointers to pcity->id etc.
26***********************************************************************/
27
28#ifdef HAVE_CONFIG_H
29#include <fc_config.h>
30#endif
31
32/* utility */
33#include "log.h"
34
35/* common */
36#include "city.h"
37#include "unit.h"
38#include "world_object.h"
39
40#include "idex.h"
41
42/**********************************************************************/
45 void idex_init(struct world *iworld)
46{
47 iworld->cities = city_hash_new();
48 iworld->units = unit_hash_new();
49}
50
51/**********************************************************************/
54 void idex_free(struct world *iworld)
55{
56 city_hash_destroy(iworld->cities);
57 iworld->cities = NULL;
58
59 unit_hash_destroy(iworld->units);
60 iworld->units = NULL;
61}
62
63/**********************************************************************/
67 void idex_register_city(struct world *iworld, struct city *pcity)
68{
69 struct city *old;
70
71 city_hash_replace_full(iworld->cities, pcity->id, pcity, NULL, &old);
72 fc_assert_ret_msg(NULL == old,
73 "IDEX: city collision: new %d %p %s, old %d %p %s",
74 pcity->id, (void *) pcity, city_name_get(pcity),
75 old->id, (void *) old, city_name_get(old));
76}
77
78/**********************************************************************/
82 void idex_register_unit(struct world *iworld, struct unit *punit)
83{
84 struct unit *old;
85
86 unit_hash_replace_full(iworld->units, punit->id, punit, NULL, &old);
87 fc_assert_ret_msg(NULL == old,
88 "IDEX: unit collision: new %d %p %s, old %d %p %s",
89 punit->id, (void *) punit, unit_rule_name(punit),
90 old->id, (void *) old, unit_rule_name(old));
91}
92
93/**********************************************************************/
97 void idex_unregister_city(struct world *iworld, struct city *pcity)
98{
99 struct city *old;
100
101 city_hash_remove_full(iworld->cities, pcity->id, NULL, &old);
102 fc_assert_ret_msg(NULL != old,
103 "IDEX: city unreg missing: %d %p %s",
104 pcity->id, (void *) pcity, city_name_get(pcity));
105 fc_assert_ret_msg(old == pcity, "IDEX: city unreg mismatch: "
106 "unreg %d %p %s, old %d %p %s",
107 pcity->id, (void *) pcity, city_name_get(pcity),
108 old->id, (void *) old, city_name_get(old));
109}
110
111/**********************************************************************/
115 void idex_unregister_unit(struct world *iworld, struct unit *punit)
116{
117 struct unit *old;
118
119 unit_hash_remove_full(iworld->units, punit->id, NULL, &old);
120 fc_assert_ret_msg(NULL != old,
121 "IDEX: unit unreg missing: %d %p %s",
122 punit->id, (void *) punit, unit_rule_name(punit));
123 fc_assert_ret_msg(old == punit, "IDEX: unit unreg mismatch: "
124 "unreg %d %p %s, old %d %p %s",
125 punit->id, (void *) punit, unit_rule_name(punit),
126 old->id, (void*) old, unit_rule_name(old));
127}
128
129/**********************************************************************/
133 struct city *idex_lookup_city(struct world *iworld, int id)
134{
135 struct city *pcity;
136
137 city_hash_lookup(iworld->cities, id, &pcity);
138
139 return pcity;
140}
141
142/**********************************************************************/
146 struct unit *idex_lookup_unit(struct world *iworld, int id)
147{
148 struct unit *punit;
149
150 unit_hash_lookup(iworld->units, id, &punit);
151
152 return punit;
153}
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
city.h
incite_cost
char * incite_cost
Definition comments.c:74
punit
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
idex_free
void idex_free(struct world *iworld)
Definition idex.c:54
idex_register_unit
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:82
idex_lookup_city
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:133
idex_unregister_city
void idex_unregister_city(struct world *iworld, struct city *pcity)
Definition idex.c:97
idex_init
void idex_init(struct world *iworld)
Definition idex.c:45
idex_lookup_unit
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:146
idex_unregister_unit
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Definition idex.c:115
idex_register_city
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:67
idex.h
log.h
fc_assert_ret_msg
#define fc_assert_ret_msg(condition, message,...)
Definition log.h:205
city
Definition city.h:320
city::id
int id
Definition city.h:326
unit
Definition unit.h:138
unit::id
int id
Definition unit.h:145
world
Definition world_object.h:37
unit.h
unit_rule_name
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
world_object.h