File: tree_map.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (182 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2012 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tree_map.h"

#include "tree_set.h"
#include "warning_error.h"
#include "memory_management.h"

struct _MLV_TreeMap {
	MLV_TreeSet* set;
};
typedef struct {
	MLV_Key* key;
	void* data;
	void (* data_destroying_function )( void* data );
} PairKeyData;

PairKeyData* create_pair_key_data(
	MLV_Key* key,
	void* data,
	void (* data_destroying_function )( void* data )
){
	PairKeyData* result = MLV_MALLOC( 1, PairKeyData );
	result->key = key;
	result->data = data;
	result->data_destroying_function = data_destroying_function;
	return result;
}

void free_pair_key_data( PairKeyData* pair ){
	if( pair ){
		MLV_free_key( pair->key );
		if( pair->data_destroying_function ){
			pair->data_destroying_function( pair->data );
		}
	}
	MLV_FREE( pair, PairKeyData );
}

void free_pair_key_data_without_key( PairKeyData* pair ){
	if( pair ){
		if( pair->data_destroying_function ){
			pair->data_destroying_function( pair->data );
		}
	}
	MLV_FREE( pair, PairKeyData );
}


int compare_pairs_key_data( PairKeyData* pair1, PairKeyData* pair2 ){
	return MLV_compare_keys( pair1->key, pair2->key );
}

MLV_TreeMap* MLV_create_tree_map(){
	MLV_TreeMap* result = MLV_MALLOC( 1, MLV_TreeMap );
	result->set = NULL;
	return result;
}

void MLV_add_data_in_tree_map(
	MLV_Key* key, void* data,
	void (* data_destroying_function)( void* data ),
	MLV_TreeMap* tree_map
){
	tree_map->set = MLV_add_data_in_tree_set(
	 	create_pair_key_data(
			key, data, data_destroying_function
		),
		( void (*)( void* ) ) free_pair_key_data,
		( int (*)( void*, void* ) ) compare_pairs_key_data,
		tree_map->set
	);
}

void* MLV_get_data_from_tree_map( MLV_Key* key, MLV_TreeMap* tree_map ){
	PairKeyData* key_data = create_pair_key_data( key, NULL, NULL );
	MLV_TreeSet* elem = MLV_find_tree_set( key_data, tree_map->set );
	PairKeyData* founded_key_data = (PairKeyData*) elem->data;
	free_pair_key_data_without_key( key_data );
	return founded_key_data->data;
}

void free_key_of_pair_key_data( PairKeyData* pair ){
	MLV_free_key( pair->key );
}

void wrapper_free_key_of_pair_key_data( void* pair, void* useless_data ){
	free_key_of_pair_key_data( ( PairKeyData* ) pair );
}

void MLV_init_tree_map( MLV_TreeMap* tree_map ){
	// On supprime les clés
	MLV_foreach_data_tree_set(
		wrapper_free_key_of_pair_key_data,
		NULL,
		tree_map->set
	);
	// On réinitialise la table
	MLV_init_tree_set( tree_map->set );
}


typedef struct {
	void* data_user;
	void (* key_data_function )( MLV_Key* key, void* data, void* data_user );
} Wrapper_data_user;

Wrapper_data_user* create_wrapper_data_user(
	void* data_user,
	void (* key_data_function )( MLV_Key* key, void* data, void* data_user )
){
	Wrapper_data_user* result = MLV_MALLOC( 1, Wrapper_data_user );
	result->data_user = data_user;
	result->key_data_function = key_data_function;
	return result;
}

void free_wrapper_data_user( Wrapper_data_user* data ){
	MLV_FREE( data, Wrapper_data_user );
}

void wrapper_key_data_function(
	void* data,
	void* data_user
){
	PairKeyData* pair = ( PairKeyData* ) data;
	Wrapper_data_user* wrapper_data_user = (Wrapper_data_user*) data_user;
	if( wrapper_data_user->key_data_function ){
		wrapper_data_user->key_data_function(
			pair->key, pair->data, wrapper_data_user->data_user
		);
	}
}

void MLV_foreach_key_data_tree_map(
	void (* key_data_function )( MLV_Key* key, void* data, void* data_user ),
	void* data_user,
	MLV_TreeMap* tree_map
){
	
	Wrapper_data_user* wrapper_data = create_wrapper_data_user(
		data_user, key_data_function
	);
	MLV_foreach_data_tree_set(
		wrapper_key_data_function,
		wrapper_data,
		tree_map->set
	);
	free_wrapper_data_user( wrapper_data );
}

void MLV_clear_tree_map( MLV_TreeMap* tree_map ){
	MLV_clear_tree_set( tree_map->set );
}

void MLV_free_tree_map( MLV_TreeMap* tree_map ){
	MLV_clear_tree_map( tree_map );
	MLV_FREE( tree_map, MLV_TreeMap );
}

void MLV_superficial_free_tree_map( MLV_TreeMap* tree_map ){
	MLV_init_tree_map( tree_map );
	MLV_FREE( tree_map, MLV_TreeMap );
}