forked from vteromero/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composition.go
63 lines (50 loc) · 1.79 KB
/
composition.go
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
/*
* Copyright (c) 2013 Zhen, LLC. http://zhen.io. All rights reserved.
* Use of this source code is governed by the Apache 2.0 license.
*
*/
package composition
import (
"errors"
"github.com/Akron/encoding"
"github.com/Akron/encoding/cursor"
)
type Composition struct {
f1 encoding.Integer
f2 encoding.Integer
}
var _ encoding.Integer = (*Composition)(nil)
func New(f1 encoding.Integer, f2 encoding.Integer) encoding.Integer {
return &Composition{
f1: f1,
f2: f2,
}
}
func (this *Composition) Compress(in []int32, inpos *cursor.Cursor, inlength int, out []int32, outpos *cursor.Cursor) error {
if inlength == 0 {
return errors.New("composition/Compress: inlength = 0. No work done.")
}
init := inpos.Get()
this.f1.Compress(in, inpos, inlength, out, outpos)
if outpos.Get() == 0 {
out[0] = 0
outpos.Increment()
}
//log.Printf("composition/Compress: f1 inpos = %d, outpos = %d, inlength = %d\n", inpos.Get(), outpos.Get(), inlength)
inlength -= inpos.Get() - init
this.f2.Compress(in, inpos, inlength, out, outpos)
//log.Printf("composition/Compress: f2 inpos = %d, outpos = %d, inlength = %d\n", inpos.Get(), outpos.Get(), inlength)
return nil
}
func (this *Composition) Uncompress(in []int32, inpos *cursor.Cursor, inlength int, out []int32, outpos *cursor.Cursor) error {
if inlength == 0 {
return errors.New("composition/Uncompress: inlength = 0. No work done.")
}
init := inpos.Get()
this.f1.Uncompress(in, inpos, inlength, out, outpos)
//log.Printf("composition/Uncompress: f1 inpos = %d, outpos = %d, inlength = %d\n", inpos.Get(), outpos.Get(), inlength)
inlength -= inpos.Get() - init
this.f2.Uncompress(in, inpos, inlength, out, outpos)
//log.Printf("composition/Uncompress: f2 inpos = %d, outpos = %d, inlength = %d\n", inpos.Get(), outpos.Get(), inlength)
return nil
}