function buffer_trial_ht_entries(block, source_ht, source_ht_size) result(nsend)
integer, intent(in) :: block
type(trial_hashtable), intent(inout) :: source_ht(:)
integer, intent(in) :: source_ht_size
integer :: nsend
integer :: clashes, j, k, det_block, det(nel)
integer(n_int) :: source_state(0:NConEntry)
! get all entries from source_ht that belong to block and move them
! to con_send_buf, deleting them from source_ht in the process
nsend = 0
do j = 1, source_ht_size
clashes = source_ht(j)%nclash
if (clashes > 0) then
k = 0
do
k = k + 1
call decode_bit_det(det, source_ht(j)%states(:, k))
det_block = get_det_block(nel, det, 0)
if (det_block == block) then
call extract_trial_ht_entry(j, k, source_state, source_ht)
nsend = nsend + 1
con_send_buf(:, nsend) = source_state
clashes = clashes - 1
k = k - 1
end if
if (k == clashes) exit
end do
end if
end do
end function buffer_trial_ht_entries