From b90f1218ce12303c847fd5bb50ae5ace23705fe1 Mon Sep 17 00:00:00 2001 From: Gregory Date: Mon, 3 Jan 2022 14:38:48 +0200 Subject: [PATCH] rewrite multiway merge --- .../index_priority_queue_test.go | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/sorting/priority_queue/index_priority_queue_test.go b/sorting/priority_queue/index_priority_queue_test.go index 5c1fd24..92a3650 100644 --- a/sorting/priority_queue/index_priority_queue_test.go +++ b/sorting/priority_queue/index_priority_queue_test.go @@ -160,32 +160,36 @@ func TestIndexChange(t *testing.T) { } func TestMultiwayMerge(t *testing.T) { + + multiwayMerge := func(streams ...*strings.Reader) string { + q := NewIPQ(func(t1, t2 rune) bool { return t1 > t2 }, len(streams)) + b := strings.Builder{} + + for i, stream := range streams { + rune, _, _ := stream.ReadRune() + q.Insert(i, rune) + } + + for !q.IsEmpty() { + b.WriteRune(q.Top()) + streamIndex := q.TopIndex() + q.Remove() + rune, _, err := streams[streamIndex].ReadRune() + if err != io.EOF { + q.Insert(streamIndex, rune) + } + } + + return b.String() + } + // ordered "streams" firstStream := strings.NewReader("ABCFGIIZ") secondStream := strings.NewReader("BDHPQQ") thirdStream := strings.NewReader("ABEFJN") - allStreams := []*strings.Reader{firstStream, secondStream, thirdStream} - expected := "AABBBCDEFFGHIIJNPQQZ" - actual := "" - - pq := NewIPQ(func(t1, t2 string) bool { return t1 > t2 }, len(allStreams)) - - for i, stream := range allStreams { - rune, _, _ := stream.ReadRune() - pq.Insert(i, string(rune)) - } - - for !pq.IsEmpty() { - actual += string(pq.Top()) - streamIndex := pq.TopIndex() - pq.Remove() - rune, _, err := allStreams[streamIndex].ReadRune() - if err != io.EOF { - pq.Insert(streamIndex, string(rune)) - } - } + actual := multiwayMerge(firstStream, secondStream, thirdStream) assert.Equal(t, expected, actual) }