1 /* vim: set noexpandtab tabstop=4 shiftwidth=4 wrap:
2 *----------------------------------------------------------------------------
4 *----------------------------------------------------------------------------
5 * This file is part of emufs.
7 * emufs is free software; you can redistribute it and/or modify it under the
8 * terms of the GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option) any later
12 * emufs is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along
18 * with emufs; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
20 *----------------------------------------------------------------------------
21 * Creado: dom may 30 07:05:15 ART 2004
22 * Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 *----------------------------------------------------------------------------
31 * Pool de archivos temporales para hacer un ordenamiento externo.
33 * Implementación de un pool de archivos temporales para hacer un ordenamiento
38 #include "mergepool.h"
43 static int mergepool_switch_to_input(MERGEPOOL* mp);
45 MERGEPOOL* mergepool_new()
47 MERGEPOOL* mp = malloc(sizeof(MERGEPOOL));
48 if (!mp) return 0; /* ERROR: no hay más memoria */
55 void mergepool_delete(MERGEPOOL* mp)
60 for (i = 0; i < mp->size; i++) mergefile_delete(mp->pool[i]);
65 MERGEFILE* mergepool_add_file(MERGEPOOL* mp)
68 MERGEFILE* new_mf = mergefile_new();
70 assert(mp->mode == OUTPUT);
71 if (!new_mf) return 0;
72 if (!(pool = realloc(mp->pool, sizeof(MERGEFILE*) * (mp->size+1)))) {
73 mergefile_delete(new_mf);
77 mp->pool[mp->size] = new_mf;
78 return mp->pool[mp->size++];
81 int mergepool_append_data(MERGEPOOL* mp, int data)
85 assert(mp->pool[mp->size-1]);
86 assert(mp->mode == OUTPUT);
87 return mergefile_push(mp->pool[mp->size-1], data);
90 int mergepool_pop_min(MERGEPOOL* mp, int* min)
97 if (mp->mode == OUTPUT) {
98 mergepool_switch_to_input(mp);
100 for (i = 0; i < mp->size; i++) {
101 if (mergefile_has_more(mp->pool[i])
102 && ((assigned == -1) || mergefile_peek(mp->pool[i]) < *min)) {
104 *min = mergefile_peek(mp->pool[i]);
107 if (assigned != -1) {
108 mergefile_pop(mp->pool[assigned]); /* lo saco del archivo */
109 return 1; /* OK, se obtuvo un mínimo */
111 return 0; /* No hay más */
114 int mergepool_switch_to_input(MERGEPOOL* mp)
119 for (i = 0; i < mp->size; i++) {
120 if (!mergefile_switch_to_input(mp->pool[i])) return 0;