-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
matsuuram
committed
Dec 8, 2023
1 parent
ed48433
commit 6a85307
Showing
6 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package bitbuffer | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"unsafe" | ||
|
||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
//////////////////////////////////////////////// | ||
// すげぇ単純にGeneric化してみる | ||
// | ||
// しかし、これに果たして意味があるのか? | ||
|
||
// func NewPeek[T constraints.Unsigned](b *BitBuffer, length uint16) T { | ||
// var t interface{} = *new(T) | ||
// switch t.(type) { | ||
// case uint8: | ||
// return T(b.PeekUint8(length)) | ||
// case uint16: | ||
// return T(b.PeekUint8(length)) | ||
// case uint32: | ||
// return T(b.PeekUint8(length)) | ||
// case uint64: | ||
// return T(b.PeekUint8(length)) | ||
// } | ||
// } | ||
|
||
const BYTE_SIZE = 8 | ||
|
||
func Peek[T constraints.Unsigned](b *BitBuffer, length uint16) (T, error) { | ||
retVal, err := b.NewPeek(length) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var t interface{} = *new(T) | ||
s := uint16(unsafe.Sizeof(t) * BYTE_SIZE) | ||
if length > s { | ||
return 0, fmt.Errorf("length(%d) is out of range(0-%d)", length, s) | ||
} | ||
|
||
return T(retVal), nil | ||
} | ||
|
||
func (b *BitBuffer) NewPeek(length uint16) (uint64, error) { | ||
if length > 64 { | ||
return 0, fmt.Errorf("length(%d) is out of range(0-64)", length) | ||
} | ||
bufSize := uint16(len(b.buf) * BYTE_SIZE) | ||
if (b.pos + length) > bufSize { | ||
return 0, fmt.Errorf("length(%d) is out of buf size(%d)", length, bufSize) | ||
} | ||
firstByteIndex := b.pos / BYTE_SIZE | ||
firstBytePos := b.pos % BYTE_SIZE | ||
lastByteIndex := (b.pos + length) / BYTE_SIZE | ||
// fmt.Printf("(b.pos + length): %d\n", (b.pos + length)) | ||
lastBytePos := (b.pos + length) % BYTE_SIZE | ||
|
||
var retVal uint64 | ||
var pos uint16 = 0 | ||
var digit uint16 = length | ||
// byte単位のループ | ||
for i := firstByteIndex; i <= lastByteIndex; i++ { | ||
// fmt.Printf("index roop: b.pos=%d, index=%d, firstByteIndex=%d, firstBytePos=%d, lastByteIndex=%d, lastBytePos=%d\n", | ||
// b.pos, i, firstByteIndex, firstBytePos, lastByteIndex, lastBytePos) | ||
pos = 0 | ||
if i == firstByteIndex { | ||
pos = firstBytePos | ||
} | ||
|
||
// byteの中のbit単位のループ | ||
for j := pos; j < BYTE_SIZE; j++ { | ||
// fmt.Printf("isBitSet: %v, i=%d, j=%d, pos=%d, buf=%#08b\n", isBitSet(b.buf[i], BYTE_SIZE-1-j), i, j, pos, b.buf[i]) | ||
if isBitSet(b.buf[i], BYTE_SIZE-1-j) { | ||
// fmt.Printf("retVal add: %d\n", uint64(math.Pow(2, float64(digit-1)))) | ||
retVal += uint64(math.Pow(2, float64(digit-1))) | ||
} | ||
digit-- | ||
pos++ | ||
if i == lastByteIndex && pos > lastBytePos { | ||
break | ||
} | ||
} | ||
} | ||
b.pos += length | ||
|
||
return retVal, nil | ||
} | ||
|
||
// 任意の桁のbitが立っているかを返す | ||
func isBitSet(b byte, p uint16) bool { | ||
return ((b >> p) & 1) == 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
func genericTest() { | ||
|
||
// var hoge uint16 = 1 | ||
// valueType(hoge) | ||
// typeParameterType[uint32](1) | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
// 今までのGoの書き方 | ||
|
||
func containsInt(needle int, array []int) bool { | ||
for _, v := range array { | ||
if v == needle { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func containsInt64(needle int64, array []int64) bool { | ||
for _, v := range array { | ||
if v == needle { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
// go.1.18で導入されたGenericを使うと・・・ | ||
|
||
func genericContains[T comparable](needle T, array []T) bool { | ||
for _, v := range array { | ||
if v == needle { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
// 午前中に調査していたこと | ||
// 関数内での型判定は出来るのか? | ||
|
||
// 引数の値から調べる | ||
// goはコンパイル時に呼び出しに応じてTの型が決定される | ||
func valueType[T constraints.Unsigned](input T) T { | ||
switch any(input).(type) { | ||
case uint8: | ||
fmt.Println("type: uint8") | ||
case uint16: | ||
fmt.Println("type: uint16") | ||
case uint32: | ||
fmt.Println("type: uint32") | ||
case uint64: | ||
fmt.Println("type: uint64") | ||
default: | ||
fmt.Println("type: other") | ||
} | ||
return 1 | ||
} | ||
|
||
// 値ではなく型引数の指定情報を取得できるのか? | ||
func typeParameterType[T constraints.Unsigned](input uint) T { | ||
var t interface{} = *new(T) | ||
switch t.(type) { | ||
case uint8: | ||
fmt.Println("type parameter: uint8") | ||
var retVal uint8 | ||
return T(retVal) | ||
case uint16: | ||
fmt.Println("type parameter: uint16") | ||
var retVal uint16 | ||
return T(retVal) | ||
case uint32: | ||
fmt.Println("type parameter: uint32") | ||
var retVal uint32 | ||
return T(retVal) | ||
case uint64: | ||
fmt.Println("type parameter: uint64") | ||
var retVal uint64 | ||
return T(retVal) | ||
default: | ||
fmt.Println("type parameter: other") | ||
} | ||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/small-teton/mpeg-ts-analyzer/bitbuffer" | ||
) | ||
|
||
func newBitBufferTest() { | ||
buf := []byte{0b10101010, 0b10101010, 0b10101010, 0b10101011, 0b10101010, 0b10101010, 0b10101010, 0b10101010} | ||
bb := new(bitbuffer.BitBuffer) | ||
bb.Set(buf) | ||
|
||
f, err := bitbuffer.Peek[uint16](bb, 14) | ||
if err != nil { | ||
fmt.Printf("err1: %v\n", err) | ||
} | ||
fmt.Printf("======first peek 14byte: %d, %#16b\n", f, f) | ||
s, err := bitbuffer.Peek[uint32](bb, 21) | ||
if err != nil { | ||
fmt.Printf("err2: %v\n", err) | ||
} | ||
fmt.Printf("======second peek 21byte: %d, %#32b\n", s, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters