aboutsummaryrefslogtreecommitdiffstats
path: root/map.c
blob: cd8272fc869ad708def0146a589a87f6d76556be (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#ifndef GMAP_C
#define GMAP_C GMAP_C
#include <map.h>



/*
 *	ID and residue and all of delta is assigned to NULL
 * */
void Map_Init(Map *tree)
{
	tree->is_final=0;
	for(int i=0;i<256;++i)tree->delta[i] = NULL;
	tree->ID = NULL;
} 

void Map_Scour(Map *tree,void *str,size_t size,size_t *where,Map **final_node)
{
	for(
		 *where=0,*final_node=tree;
		*where<size && final_node[0]->delta[((unsigned char*)str)[ where[0] ]]!=NULL;
		++where[0]
	)
	{
		(*final_node) = (*final_node)->delta[((unsigned char*)str)[*where]];
	}
}



/*
 * tree must not be null	
 * */
void Map_Push(Map *tree,void *str,size_t size,void *id)
{
	size_t temp;
	Map_Scour(tree,str,size,&temp,&tree);	

	if(temp == size)
	{
		assert(tree->ID==NULL);
		tree->ID=id;
		tree->is_final=1;
		return;
	}
	for(temp;temp<size;++temp)
	{
		tree=tree->delta[((unsigned char*)str)[temp]]=calloc(1,sizeof(Map));
		/*
		Map_Init(
			tree=tree->delta[((unsigned char*)str)[temp]]=malloc(sizeof(Map))
			);
			*/
	}

	tree->ID=id;
	tree->is_final=1;

}


/*
 * 	scours the tree and returns the id of the node that recognises the str
 * 	returns NULL if the string is not recognised
 * */
void* Map_Check(Map *tree, void *str,size_t size)
{
	size_t temp;
	Map_Scour(tree,str,size,&temp,&tree);

	if(temp<size)
	{
		return NULL;	
	}else
	{
		return tree->ID; //this has been set to be the last reached node
	}
}

void Map_Remove(Map *tree, void *str,size_t size)
{
	Stack stk;
	Stack_Init(&stk);
	size_t where;
	char what_to_null=((char*)str)[0];

	Stack_Push(&stk,tree);

	for(where=0;where<size-1 && tree->delta[((unsigned char*)str)[where]]!=NULL;++where)
	{
		tree = tree->delta[((unsigned char*)str)[where]];
		if(tree->is_final==1)
		{
			while(stk.size>0)Stack_Pop(&stk);
			what_to_null=((char*)str)[where+1];
		}
		Stack_Push(&stk,tree);
	}
	if(tree->delta[((unsigned char*)str)[where]] == NULL)return;
	free(tree->delta[((unsigned char*)str)[where]]);
	while(stk.size>1)free(Stack_Pop(&stk));
	tree=(Map*)Stack_Pop(&stk);
	tree->delta[(unsigned char)what_to_null]=NULL;
	
}
/*This function especially requires that the map has no loops*/
void Map_Map(Map *tree,void (*map)(void*))
{
	if(tree->is_final==1)map(tree->ID);
	for(int i=0;i<256;++i)
	{
		if(tree->delta[i]!=NULL)
		{
			Map_Map(tree->delta[i],map);
		}
	}
}

/*first argument of map is the node id , the second is pass_data*/
void Map_Map_Extended(Map *tree,void (*map)(void*,void*),void* pass_data)
{
	if(tree->is_final==1)map(tree->ID,pass_data);
	for(int i=0;i<256;++i)
	{
		if(tree->delta[i]!=NULL)
		{
			Map_Map_Extended(tree->delta[i],map,pass_data);
		}
	}
}


/*this does not destroy(free) any memory pointed to by a node in the Map. This does not free() the root (Map *tree) */
/*This function especially does not require that the map has no loop ( for example after grepification )*/
void Map_Destroy(Map *tree)
{
	Stack path;
	Stack nodes;
	Map *current_node;
	unsigned int i;
	 

	Stack_Init(&path);
	Stack_Init(&nodes);

	Stack_Push(&path,tree);
	Stack_Push(&nodes,tree);

	/*
	   using DFS we fill up the nodes stack with all the used
	   (accessible) nodes.
	 */
	while(path.size>0)
	{
		current_node=Stack_Pop(&path);
		current_node->ID=&(current_node->ID);/*mark the node*/
		for(i=0;i<256;++i)
		{
			if(current_node->delta[i]!=NULL && current_node->delta[i]->ID != &(current_node->delta[i]->ID) )
			{
				Stack_Push(&path,current_node->delta[i]);


				/*we mark the unmarked child of the current_node*/
				current_node->delta[i]->ID=&(current_node->delta[i]->ID);
				/*every node in nodes continues to be marked*/
				Stack_Push(&nodes,current_node->delta[i]);
			}
		}
		
	}
	/*
	   There should not be any duplicates in here
	 */
	while(nodes.size>1)
	{
		current_node=Stack_Pop(&nodes);
		/*Again the things that ID points to is not freed ( this structure is used to map the structure of data )
		  deletion of it is up to you.
		 */
		free(current_node);
	}
	/*this is where the root is at- we don't delete it , but we must free the last stack node*/
	Stack_Pop(&nodes);


}

/*requres that cpy has no loops*/
Map* Map_Copy(Map *cpy)
{
	short i;
	Map *ret;

	if(cpy==NULL)
	{
		return NULL;
	}

	ret=malloc(sizeof(Map));
	ret->is_final=cpy->is_final;
	ret->ID=cpy->ID;

	for(i=0;i<256;++i)
	{
		ret->delta[i]=Map_Copy(cpy->delta[i]);
	}
	return ret;
}

struct Map* Map_Check_And_Get(Map *tree, void *str,size_t size)
{
	size_t temp;
	Map_Scour(tree,str,size,&temp,&tree);

	if(temp<size)
	{
		return NULL;	
	}else
	{
		return tree;
	}
}

struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id)
{
	size_t temp;
	Map_Scour(tree,str,size,&temp,&tree);	

	if(temp == size)
	{
		if(tree->ID!=NULL)tree->ID=id;
		tree->is_final=1;
		return tree;
	}

	for(temp;temp<size;++temp)
	{
		Map_Init(
			tree=
			tree->delta[((unsigned char*)str)[temp]]=
			malloc(sizeof(Map))
			);
	}

	tree->ID=id;
	tree->is_final=1;
	return tree;
}

void* Map_Check_And_Push(struct Map *tree,void *str,size_t size,void *id)
{
	size_t temp;
	Map_Scour(tree,str,size,&temp,&tree);	

	if(temp == size)
	{
		if(!tree->is_final)
		{
			tree->ID=id;
			tree->is_final=1;
		}
		return tree->ID;
	}
	for(temp;temp<size;++temp)
	{
		tree=tree->delta[((unsigned char*)str)[temp]]=calloc(1,sizeof(Map));
		/*
		Map_Init(
			tree=tree->delta[((unsigned char*)str)[temp]]=malloc(sizeof(Map))
			);
			*/
	}

	tree->ID=id;
	tree->is_final=1;
	return NULL;
}
/*requires that the map has no loops. does not free the root node*/
/*TODO*/
void Map_Delete_Map(struct Map *tree)
{

}
#endif //#ifndef GMAP