subroutine bin_search_trial(ilut, amp, tTrial, tCon)
integer(n_int), intent(in) :: ilut(0:)
HElement_t(dp), intent(out) :: amp(:)
logical, intent(out) :: tTrial, tCon
integer :: pos
amp = 0.0_dp
tTrial = .false.
tCon = .false.
! Search both the trial space and connected space to see if this state exists in either list.
! First the trial space:
pos = binary_search_custom(trial_space(:, min_trial_ind:trial_space_size), ilut, NIfTot + 1, ilut_gt)
if (pos > 0) then
amp = trial_wfs(:, pos + min_trial_ind - 1)
min_trial_ind = min_trial_ind + pos
tTrial = .true.
else
! The state is not in the trial space. Just update min_trial_ind accordingly.
min_trial_ind = min_trial_ind - pos - 1
end if
! If pos > 0 then the state is in the trial space. A state cannot be in both the trial and
! connected space, so, unless pos < 0, don't bother doing the following binary search.
if (pos < 0) then
pos = binary_search_custom(con_space(:, min_conn_ind:con_space_size), ilut, NIfTot + 1, ilut_gt)
if (pos > 0) then
amp = con_space_vecs(:, pos + min_conn_ind - 1)
min_conn_ind = min_conn_ind + pos
tCon = .true.
else
min_conn_ind = min_conn_ind - pos - 1
end if
end if
end subroutine bin_search_trial