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(size_t reg_size, CMP_FUNC cmp)
47 MERGEPOOL* mp = malloc(sizeof(MERGEPOOL));
48 if (!mp) return 0; /* ERROR: no hay más memoria */
52 mp->reg_size = reg_size;
57 void mergepool_delete(MERGEPOOL* mp)
62 for (i = 0; i < mp->size; i++) mergefile_delete(mp->pool[i]);
67 MERGEFILE* mergepool_add_file(MERGEPOOL* mp)
70 MERGEFILE* new_mf = mergefile_new(mp->reg_size);
72 assert(mp->mode == OUTPUT);
73 if (!new_mf) return 0;
74 if (!(pool = realloc(mp->pool, sizeof(MERGEFILE*) * (mp->size+1)))) {
75 mergefile_delete(new_mf);
79 mp->pool[mp->size] = new_mf;
80 return mp->pool[mp->size++];
83 int mergepool_append_data(MERGEPOOL* mp, void* data)
88 assert(mp->pool[mp->size-1]);
89 assert(mp->mode == OUTPUT);
90 return mergefile_push(mp->pool[mp->size-1], data);
93 void* mergepool_pop_min(MERGEPOOL* mp)
97 int assigned = -1; /* no asignado */
101 /* si todavía está en modo OUTPUT, lo pasó a INPUT */
102 if (mp->mode == OUTPUT) {
103 mergepool_switch_to_input(mp);
105 /* busco el mínimo */
106 for (i = 0; i < mp->size; i++) {
107 if (mergefile_has_more(mp->pool[i]) &&
108 ((assigned == -1) || LT(mp, mergefile_peek(mp->pool[i]), min))){
109 assigned = i; /* se asigno el valor del archivo i */
110 min = mergefile_peek(mp->pool[i]);
113 /* si fue asignado */
114 if (assigned != -1) {
115 mergefile_pop(mp->pool[assigned]); /* lo saco del archivo */
116 return min; /* OK, se obtuvo un mínimo */
118 return 0; /* si no fue asignado es porque no hay más archivos con datos */
121 int mergepool_switch_to_input(MERGEPOOL* mp)
126 for (i = 0; i < mp->size; i++) {
127 /* lo paso a modo INPUT y si hubo error salgo */
128 if (!mergefile_switch_to_input(mp->pool[i])) return 0; /* ERROR */