Skip to content

Commit

Permalink
refactor: strhelper: use bitword/bw8; add deprecated mark
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed May 27, 2019
1 parent 888fc78 commit bd18e80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 72 deletions.
76 changes: 17 additions & 59 deletions strhelper/strhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ import (
"math/bits"
"reflect"
"strings"
)

var (
// mask for 1, 2, 4, 8 bit word
wordMask = []byte{
// 1, 2, 3, 4, 5, 6, 7, 8
0, 1, 3, 0, 15, 0, 0, 0, 255,
}
"github.com/openacid/low/bitword"
)

// ToBitWords split a string into a slice of byte.
Expand All @@ -23,72 +17,36 @@ var (
//
// Significant bits in a char is place at left.
// Thus the result byte slice keeps order with the original string.
//
// Deprecated: It will be removed since 1.0.0 .
// Use "github.com/openacid/low/bitword"
func ToBitWords(s string, n int) []byte {
if wordMask[n] == 0 {
panic("n must be one of 1, 2, 4, 8")
}

mask := wordMask[n]

// number of words per char
m := 8 / n
lenSrc := len(s)
words := make([]byte, lenSrc*m)

for i := 0; i < lenSrc; i++ {
b := s[i]

for j := 0; j < m; j++ {
words[i*m+j] = (b >> uint(8-n*j-n)) & mask
}
}
return words
return bitword.BitWord[n].FromStr(s)
}

// SliceToBitWords converts a `[]string` to a n-bit word `[][]byte`.
//
// Deprecated: It will be removed since 1.0.0 .
// Use "github.com/openacid/low/bitword"
func SliceToBitWords(strs []string, n int) [][]byte {
rst := make([][]byte, len(strs))
for i, s := range strs {
rst[i] = ToBitWords(s, n)
}
return rst
return bitword.BitWord[n].FromStrs(strs)
}

// FromBitWords is the reverse of ToBitWords.
// It composes a string of which each byte is formed from 8/n words from bs.
//
// Deprecated: It will be removed since 1.0.0 .
// Use "github.com/openacid/low/bitword"
func FromBitWords(bs []byte, n int) string {
if wordMask[n] == 0 {
panic("n must be one of 1, 2, 4, 8")
}

// number of words per char
m := 8 / n
sz := (len(bs) + m - 1) / m
strbs := make([]byte, sz)

var b byte
for i := 0; i < len(strbs); i++ {
b = 0
for j := 0; j < m; j++ {
if i*m+j < len(bs) {
b = (b << uint(n)) + bs[i*m+j]
} else {
b = b << uint(n)
}
}
strbs[i] = b
}

return string(strbs)
return bitword.BitWord[n].ToStr(bs)
}

// SliceFromBitWords converts a `[][]byte` back to a `[]string`.
//
// Deprecated: It will be removed since 1.0.0 .
// Use "github.com/openacid/low/bitword"
func SliceFromBitWords(bytesslice [][]byte, n int) []string {
rst := make([]string, len(bytesslice))
for i, s := range bytesslice {
rst[i] = FromBitWords(s, n)
}
return rst
return bitword.BitWord[n].ToStrs(bytesslice)
}

// ToBin converts integer or slice of integer to binary format string.
Expand Down
3 changes: 2 additions & 1 deletion strhelper/strhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/openacid/low/bitword"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -139,7 +140,7 @@ func TestSliceToAndFromBitWords(t *testing.T) {
}

for i, c := range cases {
rst := SliceToBitWords(c.input, c.n)
rst := bitword.BitWord[c.n].FromStrs(c.input)
if !reflect.DeepEqual(c.want, rst) {
t.Fatalf("%d-th: input: %v; want: %v; actual: %v",
i+1, c.input, c.want, rst)
Expand Down
8 changes: 6 additions & 2 deletions trie/slimtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ import (
"math/bits"

"github.com/openacid/errors"
"github.com/openacid/low/bitword"
"github.com/openacid/slim/array"
"github.com/openacid/slim/encode"
"github.com/openacid/slim/serialize"
"github.com/openacid/slim/strhelper"
)

var (
bw4 = bitword.BitWord[4]
)

const (
Expand Down Expand Up @@ -100,7 +104,7 @@ func NewSlimTrie(e encode.Encoder, keys []string, values interface{}) (*SlimTrie
// values must be a slice of data-type of fixed size or compatible with
// SlimTrie.Leaves.Encoder.
func (st *SlimTrie) load(keys []string, values interface{}) (err error) {
ks := strhelper.SliceToBitWords(keys, 4)
ks := bw4.FromStrs(keys)
return st.load4bitWords(ks, values)
}

Expand Down
19 changes: 9 additions & 10 deletions trie/slimtrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/openacid/errors"
"github.com/openacid/slim/array"
"github.com/openacid/slim/encode"
"github.com/openacid/slim/strhelper"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -38,7 +37,7 @@ func from8bit(x ...byte) string {

// from8bit create string from 4bit words
func from4bit(x ...byte) string {
return strhelper.FromBitWords(x, 4)
return bw4.ToStr(x)
}

var (
Expand Down Expand Up @@ -76,7 +75,7 @@ var (

func unsquashedIntSlimTrie(t *testing.T, keys []string, values interface{}) *SlimTrie {

ks := strhelper.SliceToBitWords(keys, 4)
ks := bw4.FromStrs(keys)

trie, err := NewTrie(ks, values, false)
if err != nil {
Expand Down Expand Up @@ -270,7 +269,7 @@ func TestUnsquashedSearch(t *testing.T) {

for _, c := range cases {

keys := strhelper.SliceToBitWords(c.keys, 4)
keys := bw4.FromStrs(c.keys)

// Unsquashed Trie

Expand All @@ -280,13 +279,13 @@ func TestUnsquashedSearch(t *testing.T) {
}

for _, ex := range c.searches {
lt, eq, gt := trie.Search(strhelper.ToBitWords(ex.key, 4))
lt, eq, gt := trie.Search(bw4.FromStr(ex.key))
rst := searchRst{lt, eq, gt}

if !reflect.DeepEqual(ex.want, rst) {
fmt.Println(trie)
fmt.Println("search:", strhelper.ToBitWords(ex.key, 4))
t.Fatal("key: ", strhelper.ToBitWords(ex.key, 4), "expected value: ", ex.want, "rst: ", rst)
fmt.Println("search:", bw4.FromStr(ex.key))
t.Fatal("key: ", bw4.FromStr(ex.key), "expected value: ", ex.want, "rst: ", rst)
}
}
}
Expand Down Expand Up @@ -366,7 +365,7 @@ func TestSquashedTrieSearch(t *testing.T) {

for _, c := range cases {

keys := strhelper.SliceToBitWords(c.keys, 4)
keys := bw4.FromStrs(c.keys)

// Squashed Trie

Expand All @@ -376,7 +375,7 @@ func TestSquashedTrieSearch(t *testing.T) {
}

for _, ex := range c.searches {
lt, eq, gt := trie.Search(strhelper.ToBitWords(ex.key, 4))
lt, eq, gt := trie.Search(bw4.FromStr(ex.key))
rst := searchRst{lt, eq, gt}

if !reflect.DeepEqual(ex.want, rst) {
Expand All @@ -388,7 +387,7 @@ func TestSquashedTrieSearch(t *testing.T) {

trie.Squash()
for _, ex := range c.searches {
lt, eq, gt := trie.Search(strhelper.ToBitWords(ex.key, 4))
lt, eq, gt := trie.Search(bw4.FromStr(ex.key))
rst := searchRst{lt, eq, gt}

if !reflect.DeepEqual(ex.want, rst) {
Expand Down

0 comments on commit bd18e80

Please sign in to comment.