Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Add more linked_list functionallity and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristopherson committed May 26, 2021
1 parent 6f75e5e commit 1ce16a4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/collections.f90
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ module collections
!! @param[in] fcn A pointer to the routine used to compare items.
!! @return Returns true if @p item is found; else, false.
procedure, public :: contains => ll_contains
!> @brief Moves to the first occurrence of the item that matches the
!! specified criteria. If no match is found the move does not happen.
!!
!! @par Syntax
!! @code{.f90}
!! logical move_to(class(linked_list) this, class(*) item, procedure(items_equal) fcn)
!! @endcode
!!
!! @param[in,out] this The linked_list object.
!! @param[in] item The item to search for.
!! @param[in] fcn A pointer to the routine used to compare items.
!! @return Returns true if the item was found and the move was
!! successful; else, false if the item wasn't found and the move did
!! not occur.
procedure, public :: move_to => ll_move_to_matching
end type

! ******************************************************************************
Expand Down Expand Up @@ -562,6 +577,13 @@ module function ll_contains(this, item, fcn) result(rst)
procedure(items_equal), pointer, intent(in) :: fcn
logical :: rst
end function

module function ll_move_to_matching(this, item, fcn) result(rst)
class(linked_list), intent(inout) :: this
class(*), intent(in) :: item
procedure(items_equal), pointer, intent(in) :: fcn
logical :: rst
end function
end interface

! ------------------------------------------------------------------------------
Expand Down
22 changes: 21 additions & 1 deletion src/collections_linked_list.f90
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,27 @@ module function ll_contains(this, item, fcn) result(rst)
end function

! ------------------------------------------------------------------------------
! move to matching
module function ll_move_to_matching(this, item, fcn) result(rst)
! Arguments
class(linked_list), intent(inout) :: this
class(*), intent(in) :: item
procedure(items_equal), pointer, intent(in) :: fcn
logical :: rst

! Local Variables
logical :: check

! Process
check = this%move_to_first()
rst = .false.
do while (check)
if (fcn(item, this%get())) then
rst = .true.
exit
end if
check = this%move_to_next()
end do
end function

! ------------------------------------------------------------------------------
end submodule
13 changes: 13 additions & 0 deletions tests/test_fcore_list.f90
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,19 @@ function test_linked_list_1() result(rst)
print '(A)', "TEST_LINKED_LIST_1 (Test 4): Could not find a " // &
"value known to exist in the collection."
end if

! Check the move-to routine
check = x%move_to(list_size / 4, fcn)
ptr => x%get()
select type (ptr)
type is (integer(int32))
if (ptr /= list_size / 4) then
rst = .false.
print '(AI0AI0A)', &
"TEST_LINKED_LIST_1 (Test 5); Expected: ", list_size / 4, &
", but found: ", ptr, "."
end if
end select
end function

! ------------------------------------------------------------------------------
Expand Down

0 comments on commit 1ce16a4

Please sign in to comment.