File: burrows_wheeler.go

package info (click to toggle)
golang-github-pointlander-compress 1.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 324 kB
  • sloc: makefile: 7
file content (174 lines) | stat: -rw-r--r-- 3,298 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package compress

import "sort"

type rotation struct {
        int
        s []uint8
}

type Rotations []rotation

func (r Rotations) Len() int {
        return len(r)
}

func less(a, b rotation) bool {
	la, lb, ia, ib := len(a.s), len(b.s), a.int, b.int
	for {
		if x, y := a.s[ia], b.s[ib]; x != y {
			return x < y
		}
		ia, ib = ia + 1, ib + 1
		if ia == la {
			ia = 0
		}
		if ib == lb {
			ib = 0
		}
		if ia == a.int && ib == b.int {
			break
		}
	}
	return false
}

func (r Rotations) Less(i, j int) bool {
	return less(r[i], r[j])
}

func (r Rotations) Swap(i, j int) {
	r[i], r[j] = r[j], r[i]
}

func merge(left, right, out Rotations) {
	for len(left) > 0 && len(right) > 0 {
		if less(left[0], right[0]) {
			out[0], left = left[0], left[1:]
		} else {
			out[0], right = right[0], right[1:]
		}
		out = out[1:]
	}
	copy(out, left)
	copy(out, right)
}

func psort(in Rotations, s chan<- bool) {
	if len(in) < 1024 {
		sort.Sort(in)
		s <- true
		return
	}

	l, r, split := make(chan bool), make(chan bool), len(in) / 2
	left, right := in[:split], in[split:]
	go psort(left, l)
	go psort(right, r)
	_, _ = <-l, <-r
	out := make(Rotations, len(in))
	merge(left, right, out)
	copy(in, out)
	s <- true
}

func BijectiveBurrowsWheelerCoder(input <-chan []byte) Coder8 {
	output := make(chan []byte)

	go func() {
		var lyndon Lyndon
		var rotations Rotations
		wait := make(chan bool)
		var buffer []uint8

		for block := range input {
			if cap(buffer) < len(block) {
				buffer = make([]uint8, len(block))
			} else {
				buffer = buffer[:len(block)]
			}
			copy(buffer, block)
			lyndon.Factor(buffer)

			/* rotate */
			if length := len(block); cap(rotations) < length {
				rotations = make(Rotations, length)
			} else {
				rotations = rotations[:length]
			}
			r := 0
			for _, word := range lyndon.Words {
				for i, _ := range word {
					rotations[r], r = rotation{i, word}, r + 1
				}
			}

			go psort(rotations, wait)
			<-wait

			/* output the last character of each rotation */
			for i, j := range rotations {
				if j.int == 0 {
					j.int = len(j.s)
				}
				block[i] = j.s[j.int - 1]
			}

			output <- block
		}

		close(output)
	}()

	return Coder8{Alphabit:256, Input:output}
}

func BijectiveBurrowsWheelerDecoder(input <-chan []byte) Coder8 {
	inverse := func(buffer []byte) {
		length := len(buffer)
		input, major, minor := make([]byte, length), [256]int {}, make([]int, length)
		for k, v := range buffer {
			input[k], minor[k], major[v] = v, major[v], major[v] + 1
		}

		sum := 0
		for k, v := range major {
			major[k], sum = sum, sum + v
		}

		j := length - 1
		for k, _ := range input {
			for minor[k] != -1 {
				buffer[j], j, k, minor[k] = input[k], j - 1, major[input[k]] + minor[k], -1
			}
		}
	}

	buffer, i := []byte(nil), 0
	add := func(symbol uint8) bool {
		if len(buffer) == 0 {
			next, ok := <-input
			if !ok {
				return true
			}
			buffer = next
		}

		buffer[i], i = symbol, i + 1
		if i == len(buffer) {
			inverse(buffer)
			next, ok := <-input
			if !ok {
				return true
			}
			buffer, i = next, 0
		}
		return false
	}

	return Coder8{Alphabit:256, Output:add}
}