From bd18e80dae73d09c330d64bb70b3e5850d288442 Mon Sep 17 00:00:00 2001 From: drdr xp Date: Mon, 27 May 2019 20:54:52 +0800 Subject: [PATCH] refactor: strhelper: use bitword/bw8; add deprecated mark --- strhelper/strhelper.go | 76 +++++++++---------------------------- strhelper/strhelper_test.go | 3 +- trie/slimtrie.go | 8 +++- trie/slimtrie_test.go | 19 +++++----- 4 files changed, 34 insertions(+), 72 deletions(-) diff --git a/strhelper/strhelper.go b/strhelper/strhelper.go index 8e30d121..f1ab3cb5 100644 --- a/strhelper/strhelper.go +++ b/strhelper/strhelper.go @@ -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. @@ -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. diff --git a/strhelper/strhelper_test.go b/strhelper/strhelper_test.go index 5b911bdd..97a5d85a 100644 --- a/strhelper/strhelper_test.go +++ b/strhelper/strhelper_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/openacid/low/bitword" "github.com/stretchr/testify/require" ) @@ -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) diff --git a/trie/slimtrie.go b/trie/slimtrie.go index 72b51524..4ea78b59 100644 --- a/trie/slimtrie.go +++ b/trie/slimtrie.go @@ -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 ( @@ -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) } diff --git a/trie/slimtrie_test.go b/trie/slimtrie_test.go index 6db0155e..7bc9fa4c 100644 --- a/trie/slimtrie_test.go +++ b/trie/slimtrie_test.go @@ -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" ) @@ -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 ( @@ -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 { @@ -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 @@ -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) } } } @@ -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 @@ -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) { @@ -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) {