match Function

function match(ipattern, ints)

returns true if the pattern is valid for the int list.

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(:) :: ipattern
integer, intent(in), dimension(:) :: ints

Return Value logical


Called by

proc~~match~~CalledByGraph proc~match problem_12::match proc~test~2 problem_12::test proc~test~2->proc~match proc~test~2->proc~test~2 proc~go~8 problem_12::go proc~go~8->proc~test~2 program~problem_12 problem_12 program~problem_12->proc~go~8

Source Code

    logical function match(ipattern, ints)
        !! returns true if the pattern is valid for the int list.
        integer,dimension(:),intent(in) :: ipattern
        integer,dimension(:),intent(in) :: ints

        integer :: i, iacc, int_checked
        integer,dimension(1) :: ifirst, iend
        logical :: accumulating

        ! .##..###... -> 2,3

        ! start and end indices (ignoring leading and trailing spaces)
        ifirst = findloc(ipattern,1)
        iend   = findloc(ipattern,1,back=.true.)
        if (ifirst(1)==0 .or. iend(1)==0) then ! all blank
            match = .false.; return
        end if

        ! step through the pattern and stop once we find it invalid
        accumulating = .true.
        iacc = 0
        int_checked = 0 ! the count of ints that have been checked
        match = .true. ! initialize
        do i = ifirst(1), iend(1)
            select case(ipattern(i))
            case(POINT)
                if (accumulating) then
                    int_checked = int_checked + 1 ! check the next one
                    if (int_checked>size(ints)) then
                        ! too many ints
                        match = .false.
                        return
                    else if (ints(int_checked)/=iacc) then
                        ! doesn't match
                        match = .false.
                        return
                    end if
                    accumulating = .false.
                    iacc = 0
                end if
            case(NUMBER)
                if (accumulating) then
                    iacc = iacc + 1
                else
                    ! start of a new number
                    accumulating = .true.
                    iacc = 1
                end if
                if (i==iend(1)) then ! last number
                    int_checked = int_checked + 1 ! check the next one
                    if (int_checked/=size(ints)) then
                        ! not enough ints
                        match = .false.
                        return
                    else if (ints(int_checked)/=iacc) then
                        ! doesn't match
                        match = .false.
                        return
                    end if
                end if
            end select
        end do

    end function match