8 #include "io_lib_header.h"
9 #include "util_lib_header.h"
10 #include "define_header.h"
12 HaschT * hcreate ( int n_elements,struct Hasch_data * declare_data(struct Hasch_entry *), struct Hasch_data *free_data(struct Hasch_data *) )
17 n_elements=n_elements*2+1;
19 T=vcalloc ( 1, sizeof (HaschT));
21 T->p=vcalloc (n_elements,sizeof ( Hasch_entry*));
22 for ( a=0; a<n_elements; a++)
24 T->p[a]=allocate_hasch_entry(NULL,DECLARE,declare_data, free_data);
28 HaschT *hdestroy (HaschT *T,struct Hasch_data * declare_data(struct Hasch_entry *), struct Hasch_data *free_data(struct Hasch_data *) )
35 if ( T==NULL)return NULL;
37 for (a=0; a< T->ne; a++)
44 allocate_hasch_entry(pp,FREE, declare_data, free_data);
53 Hasch_entry* hsearch (HaschT *T, int k, int action, struct Hasch_data * declare_data(struct Hasch_entry *), struct Hasch_data *free_data(struct Hasch_data *) )
57 /*action: FIND,ADD, REMOVE*/
63 /* find the key: k->h*/
68 if ( action==ADD || action==FIND)
71 while (p && p->k!=k){p=p->n;}
72 if (action==ADD && !p)
74 p=insert_hasch_entry_in_list (pi, NULL, NULL, declare_data, free_data);
77 else if (action==FIND && !p)p=NULL;
80 else if ( action==REMOVE)
82 allocate_hasch_entry(hsearch ( T, k, FIND, declare_data, free_data), FREE, declare_data, free_data);
89 Hasch_entry * extract_hasch_entry_from_list (Hasch_entry *e, struct Hasch_data * declare_data(struct Hasch_entry *), struct Hasch_data *free_data(struct Hasch_data *) )
93 /*extracts entry e and returns p, or next if is NULL*/
94 Hasch_entry *p=NULL, *n=NULL;
109 Hasch_entry * insert_hasch_entry_in_list (Hasch_entry *p, Hasch_entry *e, Hasch_entry *n, struct Hasch_data * declare_data(struct Hasch_entry *), struct Hasch_data *free_data(struct Hasch_data *) )
113 /*inserts entry e between entry p and entry n and returns e*/
115 if (!e)e=allocate_hasch_entry (NULL,DECLARE, declare_data, free_data);
132 Hasch_entry * allocate_hasch_entry (Hasch_entry *e, int action,struct Hasch_data * declare_data(struct Hasch_entry *), struct Hasch_data *free_data(struct Hasch_data *) )
136 static Hasch_entry *s;
139 if ( !s)s=vcalloc ( 1, sizeof (Hasch_entry));
141 if ( action==DECLARE)
144 e=extract_hasch_entry_from_list (s, declare_data, free_data);
145 if ( e->free_data)(e->free_data)(e->data);
146 e->declare_data=declare_data;
147 e->free_data=free_data;
152 else if ( action==FREE)
154 extract_hasch_entry_from_list (e,declare_data, free_data );
156 if ( e->free_data)e->data=(e->free_data)(e->data);
158 e->declare_data=NULL;
159 s=insert_hasch_entry_in_list (s, e, NULL, declare_data, free_data);
162 else if ( action==FREE_STACK)
167 allocate_hasch_entry (s, FREE, declare_data,free_data);
172 else crash ("Unknown MODE for allocate_hasch_entry\n");
176 /*********************************************************************/
181 /*********************************************************************/
184 int string2key (char *s, Char_node *n)
186 static Char_node *root;
188 if ( !root)root=declare_char_node (DECLARE);
190 if ( n==NULL && s==NULL)
192 declare_char_node (FREE_STACK);
196 return string2key(s, root);
198 else if ( s[0]=='\0')
204 return string2key(s+1, (n->c[(int)s[0]])?(n->c[(int)s[0]]):(n->c[(int)s[0]]=declare_char_node (DECLARE)));
209 Char_node * declare_char_node (int action)
211 static struct Char_node **heap;
212 static int heap_size, free_heap, a;
214 if ( action==DECLARE)
220 heap=vrealloc (heap,(heap_size+free_heap)*sizeof (struct Char_node *));
221 for ( a=heap_size; a<heap_size+free_heap; a++)
223 (heap[a])=vcalloc ( 1, sizeof ( struct Char_node));
224 (heap[a])->c=vcalloc ( 256, sizeof (Char_node*));
225 (heap[a])->key=key++;
227 heap_size+=free_heap;
229 return heap[heap_size-(free_heap--)];
231 else if ( action==FREE_STACK)
233 for (a=0; a< heap_size; a++)
237 (heap[a])->c=vcalloc ( 256, sizeof (Char_node*));
245 /* old declare_char_node (too hungry)
246 Char_node * declare_char_node (int action)
250 static Char_node *root;
252 if ( action==DECLARE)
254 cn=vcalloc (1, sizeof (Char_node));
256 cn->c=vcalloc (256, sizeof (Char_node *));
262 /******************************COPYRIGHT NOTICE*******************************/
263 /*© Centro de Regulacio Genomica */
265 /*Cedric Notredame */
266 /*Fri Feb 18 08:27:45 CET 2011 - Revision 596. */
267 /*All rights reserved.*/
268 /*This file is part of T-COFFEE.*/
270 /* T-COFFEE is free software; you can redistribute it and/or modify*/
271 /* it under the terms of the GNU General Public License as published by*/
272 /* the Free Software Foundation; either version 2 of the License, or*/
273 /* (at your option) any later version.*/
275 /* T-COFFEE is distributed in the hope that it will be useful,*/
276 /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
277 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
278 /* GNU General Public License for more details.*/
280 /* You should have received a copy of the GNU General Public License*/
281 /* along with Foobar; if not, write to the Free Software*/
282 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
283 /*............................................... |*/
284 /* If you need some more information*/
285 /* cedric.notredame@europe.com*/
286 /*............................................... |*/
290 /******************************COPYRIGHT NOTICE*******************************/