aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine/int_hashmap.cpp
blob: 487a87aa62e2bae499f232ae60bf290b9f3e8fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
 int_hashmap. Copyright (C) 2001 Christoph Reichenbach


 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/

#include "sci/engine/int_hashmap.h"


#define HASH_MAX DCS_INT_HASH_MAX
#define COMP(x, y) ((x)-(y))
#define HASH(x) (x & 0xff)

int_hash_map_t *
new_int_hash_map(void) {
	int_hash_map_t *map = (int_hash_map_t*)calloc(1, sizeof(int_hash_map_t));

	return map;
}


static void
print_int_nodes(int_hash_map_node_t *node) {
	while (node) {
		fprintf(stderr, "%p  ", (void *)node);
		node = node->next;
	}
}

void
print_int_hash_map(int_hash_map_t *map) {
	int bucket;
	fprintf(stderr, "int map %p: base value=%d\n", (void *)map,
	        map->base_value);
	for (bucket = 0; bucket <= HASH_MAX; bucket++) {
		fprintf(stderr, "bucket %d: ", bucket);
		print_int_nodes(map->nodes[bucket]);
		fprintf(stderr, "\n");
	}
	fprintf(stderr, "holes: ");
	print_int_nodes(map->holes);
	fprintf(stderr, "\n");
}

void
apply_to_int_hash_map(int_hash_map_t *map, void *param, void (*note)(void *param, int name, int value)) {
	int i;
	for (i = 0; i < HASH_MAX; i++) {
		int_hash_map_node_t *node = map->nodes[i];
		while (node) {
			note(param, node->name, node->value);
			node = node->next;
		}
	}
}


static void
free_int_hash_map_node_t_recursive(int_hash_map_node_t *node) {
	if (node) {
		free_int_hash_map_node_t_recursive(node->next);
		free(node);
	}
}


void
free_int_hash_map(int_hash_map_t *map) {
	int i;

	for (i = 0; i <= HASH_MAX; i++)
		free_int_hash_map_node_t_recursive(map->nodes[i]);

	free_int_hash_map_node_t_recursive(map->holes);

	map->base_value = -42000; /* Trigger problems for people who
			     ** forget to loose the reference  */
	free(map);
}

int
int_hash_map_check_value(int_hash_map_t *map, int value,
                         char add, char *was_added) {
	int_hash_map_node_t **node = &(map->nodes[HASH(value)]);

	while (*node && COMP(value, (*node)->name))
		node = &((*node)->next);

	if (was_added)
		*was_added = 0;

	if (*node) {
		return (*node)->value;
	}
	/* Not found */

	if (!add)
		return -1;

	if (was_added)
		*was_added = 1;

	if (map->holes) { /* Re-use old node */
		(*node) = map->holes;
		map->holes = (*node)->next;
		(*node)->next = NULL;
		(*node)->name = value;
	} else {
		*node = (int_hash_map_node_t*)malloc(sizeof(int_hash_map_node_t));
		(*node)->name = value;
		(*node)->value = map->base_value++;
		(*node)->next = NULL;
	}

	return (*node)->value;
}


int
int_hash_map_remove_value(int_hash_map_t *map, int value) {
	int_hash_map_node_t **node = &(map->nodes[HASH(value)]);

	while (*node && COMP(value, (*node)->name))
		node = &((*node)->next);

	if (*node) {
		int_hash_map_node_t *oldnode = *node;
		*node = (*node)->next;

		oldnode->next = map->holes; /* Old node is now a 'hole' */
		map->holes = oldnode;
		return oldnode->value;
	} else return -1; /* Not found */
}