Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/idex.c Source File

Freeciv-3.1

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
39#include "idex.h"
40
41/**********************************************************************/
44 void idex_init(struct world *iworld)
45{
46 iworld->cities = city_hash_new();
47 iworld->units = unit_hash_new();
48}
49
50/**********************************************************************/
53 void idex_free(struct world *iworld)
54{
55 city_hash_destroy(iworld->cities);
56 iworld->cities = NULL;
57
58 unit_hash_destroy(iworld->units);
59 iworld->units = NULL;
60}
61
62/**********************************************************************/
66 void idex_register_city(struct world *iworld, struct city *pcity)
67{
68 struct city *old;
69
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));
75}
76
77/**********************************************************************/
81 void idex_register_unit(struct world *iworld, struct unit *punit)
82{
83 struct unit *old;
84
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));
90}
91
92/**********************************************************************/
96 void idex_unregister_city(struct world *iworld, struct city *pcity)
97{
98 struct city *old;
99
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));
108}
109
110/**********************************************************************/
114 void idex_unregister_unit(struct world *iworld, struct unit *punit)
115{
116 struct unit *old;
117
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));
126}
127
128/**********************************************************************/
132 struct city *idex_lookup_city(struct world *iworld, int id)
133{
134 struct city *pcity;
135
136 city_hash_lookup(iworld->cities, id, &pcity);
137
138 return pcity;
139}
140
141/**********************************************************************/
145 struct unit *idex_lookup_unit(struct world *iworld, int id)
146{
147 struct unit *punit;
148
149 unit_hash_lookup(iworld->units, id, &punit);
150
151 return punit;
152}
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1111
city.h
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:73
idex_free
void idex_free(struct world *iworld)
Definition idex.c:53
idex_register_unit
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:81
idex_lookup_city
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:132
idex_unregister_city
void idex_unregister_city(struct world *iworld, struct city *pcity)
Definition idex.c:96
idex_init
void idex_init(struct world *iworld)
Definition idex.c:44
idex_lookup_unit
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:145
idex_unregister_unit
void idex_unregister_unit(struct world *iworld, struct unit *punit)
Definition idex.c:114
idex_register_city
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:66
idex.h
log.h
fc_assert_ret_msg
#define fc_assert_ret_msg(condition, message,...)
Definition log.h:205
city
Definition city.h:309
city::id
int id
Definition city.h:315
unit
Definition unit.h:138
unit::id
int id
Definition unit.h:145
world
Definition world_object.h:37
world::units
struct unit_hash * units
Definition world_object.h:40
world::cities
struct city_hash * cities
Definition world_object.h:39
unit.h
unit_rule_name
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1639