diff --git a/h10.go b/h10.go index f94ad92..8740300 100644 --- a/h10.go +++ b/h10.go @@ -30,6 +30,7 @@ func HashBytesH10(data []byte) uint32 { } type H10 struct { + HasherCommon window_mask_ uint buckets_ [1 << 17]uint32 invalid_pos_ uint32 @@ -37,7 +38,7 @@ type H10 struct { } func SelfH10(handle HasherHandle) *H10 { - return handle.extra.(*H10) + return handle.(*H10) } func ForestH10(self *H10) []uint32 { @@ -45,7 +46,6 @@ func ForestH10(self *H10) []uint32 { } func InitializeH10(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H10) var self *H10 = SelfH10(handle) self.window_mask_ = (1 << params.lgwin) - 1 self.invalid_pos_ = uint32(0 - self.window_mask_) diff --git a/h2.go b/h2.go index e5f09d6..da89929 100644 --- a/h2.go +++ b/h2.go @@ -35,15 +35,15 @@ func HashBytesH2(data []byte) uint32 { This is a hash map of fixed size (1 << 16). Starting from the given index, 1 buckets are used to store values of a key. */ type H2 struct { + HasherCommon buckets_ [(1 << 16) + 1]uint32 } func SelfH2(handle HasherHandle) *H2 { - return handle.extra.(*H2) + return handle.(*H2) } func InitializeH2(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H2) } func PrepareH2(handle HasherHandle, one_shot bool, input_size uint, data []byte) { diff --git a/h3.go b/h3.go index 92e3ed1..841e950 100644 --- a/h3.go +++ b/h3.go @@ -31,15 +31,15 @@ func HashBytesH3(data []byte) uint32 { This is a hash map of fixed size (BUCKET_SIZE). Starting from the given index, 2 buckets are used to store values of a key. */ type H3 struct { + HasherCommon buckets_ [(1 << 16) + 2]uint32 } func SelfH3(handle HasherHandle) *H3 { - return handle.extra.(*H3) + return handle.(*H3) } func InitializeH3(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H3) } func PrepareH3(handle HasherHandle, one_shot bool, input_size uint, data []byte) { diff --git a/h35.go b/h35.go index 85672e2..ab96976 100644 --- a/h35.go +++ b/h35.go @@ -32,17 +32,17 @@ func StoreLookaheadH35() uint { } type H35 struct { + HasherCommon ha HasherHandle hb HasherHandle params *BrotliEncoderParams } func SelfH35(handle HasherHandle) *H35 { - return handle.extra.(*H35) + return handle.(*H35) } func InitializeH35(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H35) var self *H35 = SelfH35(handle) self.ha = nil self.hb = nil @@ -59,16 +59,16 @@ func PrepareH35(handle HasherHandle, one_shot bool, input_size uint, data []byte var common_a *HasherCommon var common_b *HasherCommon - self.ha = new(HasherCommon) - common_a = (*HasherCommon)(self.ha) + self.ha = new(H3) + common_a = self.ha.Common() common_a.params = self.params.hasher common_a.is_prepared_ = false common_a.dict_num_lookups = 0 common_a.dict_num_matches = 0 InitializeH3(self.ha, self.params) - self.hb = new(HasherCommon) - common_b = (*HasherCommon)(self.hb) + self.hb = new(HROLLING_FAST) + common_b = self.hb.Common() common_b.params = self.params.hasher common_b.is_prepared_ = false common_b.dict_num_lookups = 0 diff --git a/h4.go b/h4.go index 2a9fded..a2c71f0 100644 --- a/h4.go +++ b/h4.go @@ -31,15 +31,15 @@ func HashBytesH4(data []byte) uint32 { This is a hash map of fixed size (BUCKET_SIZE). Starting from the given index, 4 buckets are used to store values of a key. */ type H4 struct { + HasherCommon buckets_ [(1 << 17) + 4]uint32 } func SelfH4(handle HasherHandle) *H4 { - return handle.extra.(*H4) + return handle.(*H4) } func InitializeH4(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H4) } func PrepareH4(handle HasherHandle, one_shot bool, input_size uint, data []byte) { diff --git a/h40.go b/h40.go index d858a78..b98efe1 100644 --- a/h40.go +++ b/h40.go @@ -40,6 +40,7 @@ type BankH40 struct { } type H40 struct { + HasherCommon addr [1 << 15]uint32 head [1 << 15]uint16 tiny_hash [65536]byte @@ -49,11 +50,10 @@ type H40 struct { } func SelfH40(handle HasherHandle) *H40 { - return handle.extra.(*H40) + return handle.(*H40) } func InitializeH40(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H40) var q uint if params.quality > 6 { q = 7 diff --git a/h41.go b/h41.go index f57740c..0cc7899 100644 --- a/h41.go +++ b/h41.go @@ -40,6 +40,7 @@ type BankH41 struct { } type H41 struct { + HasherCommon addr [1 << 15]uint32 head [1 << 15]uint16 tiny_hash [65536]byte @@ -49,11 +50,10 @@ type H41 struct { } func SelfH41(handle HasherHandle) *H41 { - return handle.extra.(*H41) + return handle.(*H41) } func InitializeH41(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H41) var tmp uint if params.quality > 6 { tmp = 7 diff --git a/h42.go b/h42.go index 54c91a6..a53b3fc 100644 --- a/h42.go +++ b/h42.go @@ -40,6 +40,7 @@ type BankH42 struct { } type H42 struct { + HasherCommon addr [1 << 15]uint32 head [1 << 15]uint16 tiny_hash [65536]byte @@ -49,11 +50,10 @@ type H42 struct { } func SelfH42(handle HasherHandle) *H42 { - return handle.extra.(*H42) + return handle.(*H42) } func InitializeH42(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H42) var tmp uint if params.quality > 6 { tmp = 7 diff --git a/h5.go b/h5.go index 002bd9a..5edb94a 100644 --- a/h5.go +++ b/h5.go @@ -31,6 +31,7 @@ func HashBytesH5(data []byte, shift int) uint32 { } type H5 struct { + HasherCommon bucket_size_ uint block_size_ uint hash_shift_ int @@ -40,7 +41,7 @@ type H5 struct { } func SelfH5(handle HasherHandle) *H5 { - return handle.extra.(*H5) + return handle.(*H5) } func NumH5(self *H5) []uint16 { @@ -52,8 +53,7 @@ func BucketsH5(self *H5) []uint32 { } func InitializeH5(handle HasherHandle, params *BrotliEncoderParams) { - var common *HasherCommon = GetHasherCommon(handle) - handle.extra = new(H5) + var common *HasherCommon = handle.Common() var self *H5 = SelfH5(handle) self.hash_shift_ = 32 - common.params.bucket_bits self.bucket_size_ = uint(1) << uint(common.params.bucket_bits) @@ -88,7 +88,7 @@ func StoreH5(handle HasherHandle, data []byte, mask uint, ix uint) { var num []uint16 = NumH5(self) var key uint32 = HashBytesH5(data[ix&mask:], self.hash_shift_) var minor_ix uint = uint(num[key]) & uint(self.block_mask_) - var offset uint = minor_ix + uint(key<score is updated only if a better match is found. */ func FindLongestMatchH5(handle HasherHandle, dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { - var common *HasherCommon = GetHasherCommon(handle) + var common *HasherCommon = handle.Common() var self *H5 = SelfH5(handle) var num []uint16 = NumH5(self) var buckets []uint32 = BucketsH5(self) diff --git a/h54.go b/h54.go index 201fefe..1340c57 100644 --- a/h54.go +++ b/h54.go @@ -31,15 +31,15 @@ func HashBytesH54(data []byte) uint32 { This is a hash map of fixed size ((1 << 20)). Starting from the given index, 4 buckets are used to store values of a key. */ type H54 struct { + HasherCommon buckets_ [(1 << 20) + 4]uint32 } func SelfH54(handle HasherHandle) *H54 { - return handle.extra.(*H54) + return handle.(*H54) } func InitializeH54(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H54) } func PrepareH54(handle HasherHandle, one_shot bool, input_size uint, data []byte) { diff --git a/h55.go b/h55.go index a8cb331..e949437 100644 --- a/h55.go +++ b/h55.go @@ -30,17 +30,17 @@ func StoreLookaheadH55() uint { } type H55 struct { + HasherCommon ha HasherHandle hb HasherHandle params *BrotliEncoderParams } func SelfH55(handle HasherHandle) *H55 { - return handle.extra.(*H55) + return handle.(*H55) } func InitializeH55(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H55) var self *H55 = SelfH55(handle) self.ha = nil self.hb = nil @@ -57,16 +57,16 @@ func PrepareH55(handle HasherHandle, one_shot bool, input_size uint, data []byte var common_a *HasherCommon var common_b *HasherCommon - self.ha = new(HasherCommon) - common_a = (*HasherCommon)(self.ha) + self.ha = new(H54) + common_a = self.ha.Common() common_a.params = self.params.hasher common_a.is_prepared_ = false common_a.dict_num_lookups = 0 common_a.dict_num_matches = 0 InitializeH54(self.ha, self.params) - self.hb = new(HasherCommon) - common_b = (*HasherCommon)(self.hb) + self.hb = new(HROLLING_FAST) + common_b = self.hb.Common() common_b.params = self.params.hasher common_b.is_prepared_ = false common_b.dict_num_lookups = 0 diff --git a/h6.go b/h6.go index ed76d8f..046b8fc 100644 --- a/h6.go +++ b/h6.go @@ -31,6 +31,7 @@ func HashBytesH6(data []byte, mask uint64, shift int) uint32 { } type H6 struct { + HasherCommon bucket_size_ uint block_size_ uint hash_shift_ int @@ -41,7 +42,7 @@ type H6 struct { } func SelfH6(handle HasherHandle) *H6 { - return handle.extra.(*H6) + return handle.(*H6) } func NumH6(self *H6) []uint16 { @@ -53,8 +54,7 @@ func BucketsH6(self *H6) []uint32 { } func InitializeH6(handle HasherHandle, params *BrotliEncoderParams) { - var common *HasherCommon = GetHasherCommon(handle) - handle.extra = new(H6) + var common *HasherCommon = handle.Common() var self *H6 = SelfH6(handle) self.hash_shift_ = 64 - common.params.bucket_bits self.hash_mask_ = (^(uint64(0))) >> uint(64-8*common.params.hash_len) @@ -90,7 +90,7 @@ func StoreH6(handle HasherHandle, data []byte, mask uint, ix uint) { var num []uint16 = NumH6(self) var key uint32 = HashBytesH6(data[ix&mask:], self.hash_mask_, self.hash_shift_) var minor_ix uint = uint(num[key]) & uint(self.block_mask_) - var offset uint = minor_ix + uint(key<score is updated only if a better match is found. */ func FindLongestMatchH6(handle HasherHandle, dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { - var common *HasherCommon = GetHasherCommon(handle) + var common *HasherCommon = handle.Common() var self *H6 = SelfH6(handle) var num []uint16 = NumH6(self) var buckets []uint32 = BucketsH6(self) diff --git a/h65.go b/h65.go index 2fdf3e4..b40bbb0 100644 --- a/h65.go +++ b/h65.go @@ -30,17 +30,17 @@ func StoreLookaheadH65() uint { } type H65 struct { + HasherCommon ha HasherHandle hb HasherHandle params *BrotliEncoderParams } func SelfH65(handle HasherHandle) *H65 { - return handle.extra.(*H65) + return handle.(*H65) } func InitializeH65(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(H65) var self *H65 = SelfH65(handle) self.ha = nil self.hb = nil @@ -57,16 +57,16 @@ func PrepareH65(handle HasherHandle, one_shot bool, input_size uint, data []byte var common_a *HasherCommon var common_b *HasherCommon - self.ha = new(HasherCommon) - common_a = (*HasherCommon)(self.ha) + self.ha = new(H6) + common_a = self.ha.Common() common_a.params = self.params.hasher common_a.is_prepared_ = false common_a.dict_num_lookups = 0 common_a.dict_num_matches = 0 InitializeH6(self.ha, self.params) - self.hb = new(HasherCommon) - common_b = (*HasherCommon)(self.hb) + self.hb = new(HROLLING) + common_b = self.hb.Common() common_b.params = self.params.hasher common_b.is_prepared_ = false common_b.dict_num_lookups = 0 diff --git a/hash.go b/hash.go index c245f7d..b3b378e 100644 --- a/hash.go +++ b/hash.go @@ -23,13 +23,14 @@ type HasherCommon struct { is_prepared_ bool dict_num_lookups uint dict_num_matches uint - extra interface{} } -type HasherHandle *HasherCommon +func (h *HasherCommon) Common() *HasherCommon { + return h +} -func GetHasherCommon(handle HasherHandle) *HasherCommon { - return (*HasherCommon)(handle) +type HasherHandle interface { + Common() *HasherCommon } type score_t uint @@ -166,7 +167,7 @@ func TestStaticDictionaryItem(dictionary *BrotliEncoderDictionary, item uint, da func SearchInStaticDictionary(dictionary *BrotliEncoderDictionary, handle HasherHandle, data []byte, max_length uint, max_backward uint, max_distance uint, out *HasherSearchResult, shallow bool) { var key uint var i uint - var self *HasherCommon = GetHasherCommon(handle) + var self *HasherCommon = handle.Common() if self.dict_num_matches < self.dict_num_lookups>>7 { return } @@ -238,7 +239,7 @@ func HasherReset(handle HasherHandle) { if handle == nil { return } - GetHasherCommon(handle).is_prepared_ = false + handle.Common().is_prepared_ = false } func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, position uint, input_size uint, is_last bool) { @@ -247,9 +248,37 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, var one_shot bool = (position == 0 && is_last) if *handle == nil { ChooseHasher(params, ¶ms.hasher) - self = new(HasherCommon) + switch params.hasher.type_ { + case 2: + self = new(H2) + case 3: + self = new(H3) + case 4: + self = new(H4) + case 5: + self = new(H5) + case 6: + self = new(H6) + case 40: + self = new(H40) + case 41: + self = new(H41) + case 42: + self = new(H42) + case 54: + self = new(H54) + case 35: + self = new(H35) + case 55: + self = new(H55) + case 65: + self = new(H65) + case 10: + self = new(H10) + } + *handle = self - common = GetHasherCommon(self) + common = self.Common() common.params = params.hasher switch common.params.type_ { case 2: @@ -278,8 +307,6 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, InitializeH65(*handle, params) case 10: InitializeH10(*handle, params) - break - fallthrough default: break @@ -289,7 +316,7 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, } self = *handle - common = GetHasherCommon(self) + common = self.Common() if !common.is_prepared_ { switch common.params.type_ { case 2: @@ -336,7 +363,7 @@ func InitOrStitchToPreviousBlock(handle *HasherHandle, data []byte, mask uint, p var self HasherHandle HasherSetup(handle, params, data, position, input_size, is_last) self = *handle - switch GetHasherCommon(self).params.type_ { + switch self.Common().params.type_ { case 2: StitchToPreviousBlockH2(self, input_size, position, data, mask) case 3: diff --git a/hrolling.go b/hrolling.go index e57b933..60ef27f 100644 --- a/hrolling.go +++ b/hrolling.go @@ -41,6 +41,7 @@ func HashRollingFunctionHROLLING(state uint32, add byte, rem byte, factor uint32 } type HROLLING struct { + HasherCommon state uint32 table []uint32 next_ix uint @@ -50,11 +51,10 @@ type HROLLING struct { } func SelfHROLLING(handle HasherHandle) *HROLLING { - return handle.extra.(*HROLLING) + return handle.(*HROLLING) } func InitializeHROLLING(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(HROLLING) var self *HROLLING = SelfHROLLING(handle) var i uint self.state = 0 diff --git a/hrolling_fast.go b/hrolling_fast.go index a5359cf..ccf6ee0 100644 --- a/hrolling_fast.go +++ b/hrolling_fast.go @@ -39,6 +39,7 @@ func HashRollingFunctionHROLLING_FAST(state uint32, add byte, rem byte, factor u } type HROLLING_FAST struct { + HasherCommon state uint32 table []uint32 next_ix uint @@ -48,11 +49,10 @@ type HROLLING_FAST struct { } func SelfHROLLING_FAST(handle HasherHandle) *HROLLING_FAST { - return handle.extra.(*HROLLING_FAST) + return handle.(*HROLLING_FAST) } func InitializeHROLLING_FAST(handle HasherHandle, params *BrotliEncoderParams) { - handle.extra = new(HROLLING_FAST) var self *HROLLING_FAST = SelfHROLLING_FAST(handle) var i uint self.state = 0