stdlib_hash_64bit_pengy.fypp 源文件

PENGY_HASH 是对 Alberto Fajardo 的 pengyhash 算法的 Fortran 2008 和有符号二进制补码算术的翻译,版权所有 2020。Alberto Fajardo 的原始 C 代码 pengyhash.c 可在以下 URL 获取:https://github.com/tinypeng/pengyhash/blob/master/pengyhash.c,根据 BSD 2 条款许可证:https://github.com/tinypeng/pengyhash/blob/master/LICENSE

BSD 2 条款许可证如下所示

BSD 2 条款许可证

pengyhash 版权所有 (c) 2020 Alberto Fajardo 保留所有权利。

在满足以下条件的情况下,允许以源代码和二进制形式重新分发和使用本软件,无论是否修改。

  1. 源代码的重新分发必须保留上述版权声明、此条件列表和以下免责声明。

  2. 二进制形式的重新分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、此条件列表和以下免责声明。

本软件由版权持有人和贡献者“按原样”提供,任何明示或暗示的担保,包括但不限于适销性和特定用途适用性的暗示担保,均予以免除。在任何情况下,版权持有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润损失;或业务中断)负责,无论其责任基础如何,无论是在合同、严格责任还是侵权行为(包括疏忽或其他原因)中,即使已被告知可能发生此类损害。




源代码

!!------------------------------------------------------------------------------
!! `PENGY_HASH` is a translation to Fortran 2008 and signed two's complement
!! arithmetic of the `pengyhash` algorithm of Alberto Fajardo, copyright 2020.
!! Alberto Fajardo's original C code, `pengyhash.c`, is available at the URL:
!! https://github.com/tinypeng/pengyhash/blob/master/pengyhash.c
!! under the BSD 2-Clause License:
!! https://github.com/tinypeng/pengyhash/blob/master/LICENSE
!!
!! The BSD 2-Clause license is as follows:
!!
!! BSD 2-Clause License
!!
!! pengyhash
!! Copyright (c) 2020 Alberto Fajardo
!! All rights reserved.
!!
!! Redistribution and use in source and binary forms, with or without
!! modification, are permitted provided that the following conditions are met:
!!
!! 1. Redistributions of source code must retain the above copyright notice,
!!    this list of conditions and the following disclaimer.
!!
!! 2. Redistributions in binary form must reproduce the above copyright notice,
!!    this list of conditions and the following disclaimer in the documentation
!!    and/or other materials provided with the distribution.
!!
!! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
!! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
!! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
!! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
!! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
!! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
!! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
!! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
!! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
!! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
!! POSSIBILITY OF SUCH DAMAGE.
!!------------------------------------------------------------------------------

#! Integer kinds to be considered during templating
#:set INT_KINDS = ["int16", "int32", "int64"]

submodule(stdlib_hash_64bit) stdlib_hash_64bit_pengy

    implicit none

contains

    pure module function int8_pengy_hash( key, seed ) result(hash_code)
        integer(int64) :: hash_code
        integer(int8), intent(in) :: key(0:)
        integer(int32), intent(in) :: seed

        integer(int64) :: b(0:3)
        integer(int64) :: i
        integer(int64) :: index
        integer(int64) :: len
        integer(int64) :: s(0:3)
        integer(int64) :: seed2
        integer(int8)  :: dummy(0:31)

        b(0:3) = 0_int64
        len = size( key, kind=int64 )
        s(0:3) = [ 0_int64, 0_int64, 0_int64, len ]

        index = 0_int64
        do while ( len >= 32 )
            b(0:3) = transfer( key( index:index+31 ), 0_int64, 4 )

            s(0) = s(0) + s(1) + b(3)
            s(1) = s(0) + ishftc( s(1), 14 )
            s(2) = s(2) + s(3) + b(2)
            s(3) = s(2) + ishftc( s(3), 23 )
            s(0) = s(0) + s(3) + b(1)
            s(3) = ieor( s(0), ishftc( s(3), 16 ) )
            s(2) = s(2) + s(1) + b(0)
            s(1) = ieor( s(2), ishftc( s(1), 40 ) )

            len = len - 32
            index = index + 32
        end do

        dummy(0:31) = transfer( b, 0_int8, 32 )
        dummy(0:len-1) = key(index:index+len-1)
        b(0:3) = transfer( dummy, 0_int64, 4)
        if ( little_endian ) then
            seed2 = transfer( [ seed, 0_int32 ], 0_int64 )
        else
            seed2 = transfer( [ 0_int32, seed ], 0_int64 )
        end if

        do i = 0, 5
            s(0) = s(0) + s(1) + b(3)
            s(1) = s(0) + ishftc( s(1), 14 ) + seed2
            s(2) = s(2) + s(3) + b(2)
            s(3) = s(2) + ishftc( s(3), 23 )
            s(0) = s(0) + s(3) + b(1)
            s(3) = ieor( s(0), ishftc( s(3), 16 ) )
            s(2) = s(2) + s(1) + b(0)
            s(1) = ieor( s(2), ishftc( s(1), 40 ) )
        end do

        hash_code = s(0) + s(1) + s(2) + s(3)

    end function int8_pengy_hash

#:for k1 in INT_KINDS
    pure module function ${k1}$_pengy_hash( key, seed ) result(hash_code)
!! PENGY_HASH hash function for rank 1 array keys of kind ${k1}$
        integer(${k1}$), intent(in) :: key(:)
        integer(int32), intent(in)  :: seed
        integer(int64)              :: hash_code

        hash_code = int8_pengy_hash( transfer( key, 0_int8, &
                     bytes_${k1}$*size(key, kind=int64) ), seed)

    end function ${k1}$_pengy_hash

#:endfor

    elemental module function character_pengy_hash( key, seed ) &
        result(hash_code)
!! PENGY_HASH hash function for default character keys
        character(*), intent(in)   :: key
        integer(int32), intent(in) :: seed
        integer(int64)             :: hash_code

        hash_code = int8_pengy_hash( transfer( key, 0_int8, &
                     bytes_char*len(key, kind=int64) ), seed)

    end function character_pengy_hash

    module subroutine new_pengy_hash_seed( seed )
! Random SEED generator for PENGY_HASH
        integer(int32), intent(inout) :: seed
        real(dp) :: sample
        integer(int32) :: old_seed

        old_seed = seed
        find_seed: do
            call random_number( sample )
            seed = int( floor( sample * 2_int64**32, int64 ) - 2_int64**31, &
                int32 )
            if ( seed /= old_seed ) return
        end do find_seed

    end subroutine new_pengy_hash_seed

end submodule stdlib_hash_64bit_pengy