Skip to content

Commit 38a3619

Browse files
committed
reconstruct compress package
1 parent cbbc231 commit 38a3619

33 files changed

+46
-16598
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Algorithm (Sedgewick, 4th) implemented in golang.
5454
* Rabin-Karp
5555
* [Regular Expression](strings/regexp)
5656
* Data Compression
57-
* [Huffman](strings/compress/huffman)
58-
* [LZW](strings/compress/lzw)
57+
* [Huffman](strings/compress/huffman.go)
58+
* [LZW](strings/compress/lzw.go)
5959
## [Priority Queues](pq)
6060
* Heap
6161
* Leftist Heap
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

graphs/digraph_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/howz97/algorithm/util"
2525
)
2626

27+
const testDir = "../assets/graphs/"
28+
2729
func TestSCC_IsStronglyConnected(t *testing.T) {
2830
g := NewDigraph(13)
2931
g.AddEdge(0, 1)
@@ -117,7 +119,7 @@ func TestDFS_Graph(t *testing.T) {
117119
}
118120

119121
func TestDFS_Digraph(t *testing.T) {
120-
dg, err := LoadDigraph(".\\testdata\\dfs.yml")
122+
dg, err := LoadDigraph(testDir + "dfs.yml")
121123
if err != nil {
122124
t.Fatal(err)
123125
}
@@ -146,7 +148,7 @@ func checkDFSResults(t *testing.T, g *Digraph, dfsResults [][]int) {
146148
}
147149

148150
func TestRevDFS(t *testing.T) {
149-
g, err := LoadDigraph(".\\testdata\\dfs.yml")
151+
g, err := LoadDigraph(testDir + "dfs.yml")
150152
if err != nil {
151153
t.Fatal(err)
152154
}
@@ -182,7 +184,7 @@ func ExampleDigraph_FindCycle() {
182184
}
183185

184186
func ExampleDigraph_Topological() {
185-
dg, err := LoadDigraph(`.\testdata\no_cycle.yml`)
187+
dg, err := LoadDigraph(testDir + "no_cycle.yml")
186188
if err != nil {
187189
panic(err)
188190
}
@@ -194,7 +196,7 @@ func ExampleDigraph_Topological() {
194196
}
195197

196198
func ExampleDigraph_Bipartite() {
197-
dg, err := LoadDigraph(`.\testdata\no_cycle.yml`)
199+
dg, err := LoadDigraph(testDir + "no_cycle.yml")
198200
if err != nil {
199201
panic(err)
200202
}
@@ -204,7 +206,7 @@ func ExampleDigraph_Bipartite() {
204206
}
205207

206208
func ExampleReachable() {
207-
dg, err := LoadDigraph(`.\testdata\no_cycle.yml`)
209+
dg, err := LoadDigraph(testDir + "no_cycle.yml")
208210
if err != nil {
209211
panic(err)
210212
}
@@ -218,7 +220,7 @@ func ExampleReachable() {
218220
}
219221

220222
func ExampleBFS() {
221-
dg, err := LoadDigraph(`.\testdata\no_cycle.yml`)
223+
dg, err := LoadDigraph(testDir + "no_cycle.yml")
222224
if err != nil {
223225
panic(err)
224226
}
File renamed without changes.

graphs/w_digraph_test.go graphs/wdigraph_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestDijkstra_Simple(t *testing.T) {
24-
g, err := LoadWDigraph(".\\testdata\\w_digraph.yml")
24+
g, err := LoadWDigraph(testDir + "w_digraph.yml")
2525
if err != nil {
2626
t.Fatal(err)
2727
}
@@ -33,7 +33,7 @@ func TestDijkstra_Simple(t *testing.T) {
3333
}
3434

3535
func TestTopological_Simple(t *testing.T) {
36-
g, err := LoadWDigraph(".\\testdata\\no_cycle.yml")
36+
g, err := LoadWDigraph(testDir + "no_cycle.yml")
3737
if err != nil {
3838
t.Fatal(err)
3939
}
@@ -45,7 +45,7 @@ func TestTopological_Simple(t *testing.T) {
4545
}
4646

4747
func TestBellmanFord_Simple(t *testing.T) {
48-
g, err := LoadWDigraph(".\\testdata\\w_digraph.yml")
48+
g, err := LoadWDigraph(testDir + "w_digraph.yml")
4949
if err != nil {
5050
t.Fatal(err)
5151
}
@@ -57,7 +57,7 @@ func TestBellmanFord_Simple(t *testing.T) {
5757
}
5858

5959
func TestNegativeCycle(t *testing.T) {
60-
g, err := LoadWDigraph(".\\testdata\\negative_cycle.yml")
60+
g, err := LoadWDigraph(testDir + "negative_cycle.yml")
6161
if err != nil {
6262
t.Fatal(err)
6363
}
@@ -144,10 +144,10 @@ func RandWDigraph(edgeLimit int, negativeEdge float64) (wd *WDigraph) {
144144
}
145145

146146
func ExampleWDigraph() {
147-
g, _ := LoadWDigraph("testdata/no_cycle.yml")
147+
g, _ := LoadWDigraph(testDir + "no_cycle.yml")
148148
searcher, _ := g.SearcherDijkstra()
149-
// searcher, _ = g.SearcherTopological()
150-
// searcher, _ = g.SearcherBellmanFord()
149+
// searcher, _ := g.SearcherTopological()
150+
// searcher, _ := g.SearcherBellmanFord()
151151
fmt.Println(searcher.GetPath(1, 2).Str(nil))
152152

153153
// Output:

graphs/w_graph.go graphs/wgraph.go

File renamed without changes.

graphs/w_graph_test.go graphs/wgraph_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestMST_Prim(t *testing.T) {
5050
}
5151

5252
func Example() {
53-
g, err := LoadWGraph("testdata/mst.yml")
53+
g, err := LoadWGraph(testDir + "mst.yml")
5454
if err != nil {
5555
panic(err)
5656
}

strings/compress/huffman/bit.go strings/compress/bit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package huffman
15+
package compress
1616

1717
import (
1818
"io"

strings/compress/huffman/huffman.go strings/compress/huffman.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package huffman
15+
package compress
1616

1717
import (
1818
"strconv"
@@ -71,8 +71,8 @@ func newNode(br *bitReader) (n *node) {
7171
return
7272
}
7373

74-
// Compress data using huffman algorithm
75-
func Compress(data []byte) []byte {
74+
// HuffmanCompress data using huffman algorithm
75+
func HuffmanCompress(data []byte) []byte {
7676
bw, table := compile(data)
7777
bw.WriteUint32(uint32(len(data)))
7878
for _, b := range data {
@@ -129,8 +129,8 @@ func genHuffmanTree(data []byte) (huffmanTree *node) {
129129
return pq.Pop()
130130
}
131131

132-
// Decompress data compressed by huffman algorithm
133-
func Decompress(data []byte) ([]byte, error) {
132+
// HuffmanDecompress data compressed by huffman algorithm
133+
func HuffmanDecompress(data []byte) ([]byte, error) {
134134
br := newBitReader(data)
135135
huffmanTree := newNode(br)
136136
size := br.ReadBits(32)

strings/compress/huffman/README.md

-14
This file was deleted.

strings/compress/huffman/huffman_test.go strings/compress/huffman_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package huffman
15+
package compress
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
19+
"os"
2020
"testing"
2121
)
2222

2323
func TestCompress(t *testing.T) {
24-
data, err := ioutil.ReadFile("..\\testdata\\tale.txt")
24+
data, err := os.ReadFile("../../assets/compress/tale.txt")
2525
if err != nil {
2626
t.Fatal(err)
2727
}
28-
compressed := Compress(data)
28+
compressed := HuffmanCompress(data)
2929
t.Logf("compress performance %.4f", float64(len(compressed))/float64(len(data)))
30-
decompressed, err := Decompress(compressed)
30+
decompressed, err := HuffmanDecompress(compressed)
3131
if err != nil {
3232
t.Error(err)
3333
}
@@ -48,7 +48,7 @@ func TestCompress(t *testing.T) {
4848

4949
func TestSimple(t *testing.T) {
5050
data := "zhang how 1997"
51-
de, err := Decompress(Compress([]byte(data)))
51+
de, err := HuffmanDecompress(HuffmanCompress([]byte(data)))
5252
if err != nil {
5353
t.Error(err)
5454
}
@@ -60,9 +60,9 @@ func TestSimple(t *testing.T) {
6060

6161
func Example() {
6262
data := []byte("zhang how, zhang how, zhang how, zhang how,")
63-
compressed := Compress(data)
63+
compressed := HuffmanCompress(data)
6464
fmt.Printf("performance %.4f\n", float64(len(compressed))/float64(len(data)))
65-
de, _ := Decompress(compressed)
65+
de, _ := HuffmanDecompress(compressed)
6666
fmt.Println(string(de))
6767

6868
// Output:

strings/compress/lzw/lzw.go strings/compress/lzw.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package lzw
15+
package compress
1616

1717
import (
1818
"github.com/howz97/algorithm/search"
1919
"github.com/howz97/algorithm/util"
2020
)
2121

22-
// Compress data using LZW algorithm
23-
func Compress(data []byte) (out []byte) {
22+
// LzwCompress data using LZW algorithm
23+
func LzwCompress(data []byte) (out []byte) {
2424
table := search.NewHashMap[util.Str, uint16]()
2525
unused := uint16(0)
2626
for b := 0; b <= 0xFF; b++ {
@@ -51,8 +51,8 @@ func Compress(data []byte) (out []byte) {
5151
return
5252
}
5353

54-
// Decompress data compressed by LZW algorithm
55-
func Decompress(data []byte) (out []byte) {
54+
// LzwDecompress data compressed by LZW algorithm
55+
func LzwDecompress(data []byte) (out []byte) {
5656
table := search.NewHashMap[util.Int, []byte]()
5757
i := uint16(0)
5858
for b := 0; b <= 0xFF; b++ {

strings/compress/lzw/README.md

-13
This file was deleted.

strings/compress/lzw/lzw_test.go strings/compress/lzw_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package lzw
15+
package compress
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
19+
"os"
2020
"testing"
2121
)
2222

23-
func TestCompress(t *testing.T) {
24-
data, err := ioutil.ReadFile("..\\testdata\\tale_1.txt")
23+
func TestCompressLZW(t *testing.T) {
24+
data, err := os.ReadFile("../../assets/compress/tale_1.txt")
2525
if err != nil {
2626
t.Fatal(err)
2727
}
28-
compressed := Compress(data)
28+
compressed := LzwCompress(data)
2929
t.Logf("compress performance %v", float64(len(compressed))/float64(len(data)))
30-
decompressed := Decompress(compressed)
30+
decompressed := LzwDecompress(compressed)
3131

3232
if len(data) != len(decompressed) {
3333
t.Fatalf("raw data length is %v, decompressed length is %v", len(data), len(decompressed))
@@ -43,11 +43,11 @@ func TestCompress(t *testing.T) {
4343
}
4444
}
4545

46-
func Example() {
46+
func ExampleCompressLZW() {
4747
data := []byte("howhowhowhowhowhowhowhowhow")
48-
compressed := Compress(data)
48+
compressed := LzwCompress(data)
4949
fmt.Printf("performance %.4f\n", float64(len(compressed))/float64(len(data)))
50-
fmt.Println(string(Decompress(compressed)))
50+
fmt.Println(string(LzwDecompress(compressed)))
5151

5252
// Output:
5353
// performance 0.8889

0 commit comments

Comments
 (0)