2 * UCW Library -- Universal Sorter: Two-Way Merge Module
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 static void P(twoway_merge)(struct sort_context *ctx UNUSED, struct sort_bucket **ins, struct sort_bucket **outs)
12 struct fastbuf *fin1, *fin2, *fout1, *fout2, *ftmp;
13 P(key) kbuf1, kbuf2, kbuf3, kbuf4;
14 P(key) *kin1 = &kbuf1, *kprev1 = &kbuf2, *kin2 = &kbuf3, *kprev2 = &kbuf4;
15 P(key) *kout = NULL, *ktmp;
16 int next1, next2, run1, run2;
20 fin1 = sbuck_read(ins[0]);
21 next1 = P(read_key)(fin1, kin1);
22 if (sbuck_have(ins[1]))
24 fin2 = sbuck_read(ins[1]);
25 next2 = P(read_key)(fin2, kin2);
34 run1 = next1, run2 = next2;
35 while (next1 || next2)
42 comp = P(compare)(kin1, kin2);
43 ktmp = (comp <= 0) ? kin1 : kin2;
44 if (!kout || !(P(compare)(kout, ktmp) LESS 0))
46 SWAP(fout1, fout2, ftmp);
50 fout1 = sbuck_write(outs[0]);
52 fout1 = sbuck_write(outs[1]);
58 #ifdef SORT_ASSERT_UNIQUE
63 P(copy_data)(kin1, fin1, fout1);
64 SWAP(kin1, kprev1, ktmp);
65 next1 = P(read_key)(fin1, kin1);
66 run1 = next1 && (P(compare)(kprev1, kin1) LESS 0);
72 P(key) *mkeys[] = { kin1, kin2 };
73 struct fastbuf *mfb[] = { fin1, fin2 };
74 P(copy_merged)(mkeys, mfb, 2, fout1);
75 SWAP(kin1, kprev1, ktmp);
76 next1 = P(read_key)(fin1, kin1);
77 run1 = next1 && (P(compare)(kprev1, kin1) LESS 0);
78 SWAP(kin2, kprev2, ktmp);
79 next2 = P(read_key)(fin2, kin2);
80 run2 = next2 && (P(compare)(kprev2, kin2) LESS 0);
86 P(copy_data)(kin2, fin2, fout1);
87 SWAP(kin2, kprev2, ktmp);
88 next2 = P(read_key)(fin2, kin2);
89 run2 = next2 && (P(compare)(kprev2, kin2) LESS 0);
99 if (fout2 && fout2 != fout1)
100 outs[1]->runs += run_count / 2;
102 outs[0]->runs += (run_count+1) / 2;