[6] | 1 | module m_wxml_elstack |
---|
| 2 | |
---|
| 3 | private |
---|
| 4 | |
---|
| 5 | ! |
---|
| 6 | ! Simple stack to keep track of which elements have appeared so far |
---|
| 7 | ! |
---|
| 8 | integer, parameter, private :: STACK_SIZE = 20 |
---|
| 9 | |
---|
| 10 | type, public :: elstack_t |
---|
| 11 | private |
---|
| 12 | integer :: n_items |
---|
| 13 | character(len=100), dimension(STACK_SIZE) :: data |
---|
| 14 | end type elstack_t |
---|
| 15 | |
---|
| 16 | public :: push_elstack, pop_elstack, reset_elstack, print_elstack |
---|
| 17 | public :: get_top_elstack, is_empty, get_elstack_signature |
---|
| 18 | public :: len |
---|
| 19 | |
---|
| 20 | interface len |
---|
| 21 | module procedure number_of_items |
---|
| 22 | end interface |
---|
| 23 | private :: number_of_items |
---|
| 24 | |
---|
| 25 | interface is_empty |
---|
| 26 | module procedure is_empty_elstack |
---|
| 27 | end interface |
---|
| 28 | private :: is_empty_elstack |
---|
| 29 | |
---|
| 30 | CONTAINS |
---|
| 31 | |
---|
| 32 | !----------------------------------------------------------------- |
---|
| 33 | subroutine reset_elstack(elstack) |
---|
| 34 | type(elstack_t), intent(inout) :: elstack |
---|
| 35 | |
---|
| 36 | elstack%n_items = 0 |
---|
| 37 | |
---|
| 38 | end subroutine reset_elstack |
---|
| 39 | |
---|
| 40 | !----------------------------------------------------------------- |
---|
| 41 | function is_empty_elstack(elstack) result(answer) |
---|
| 42 | type(elstack_t), intent(in) :: elstack |
---|
| 43 | logical :: answer |
---|
| 44 | |
---|
| 45 | answer = (elstack%n_items == 0) |
---|
| 46 | end function is_empty_elstack |
---|
| 47 | |
---|
| 48 | !----------------------------------------------------------------- |
---|
| 49 | function number_of_items(elstack) result(n) |
---|
| 50 | type(elstack_t), intent(in) :: elstack |
---|
| 51 | integer :: n |
---|
| 52 | |
---|
| 53 | n = elstack%n_items |
---|
| 54 | end function number_of_items |
---|
| 55 | |
---|
| 56 | !----------------------------------------------------------------- |
---|
| 57 | subroutine push_elstack(item,elstack) |
---|
| 58 | character(len=*), intent(in) :: item |
---|
| 59 | type(elstack_t), intent(inout) :: elstack |
---|
| 60 | |
---|
| 61 | integer :: n |
---|
| 62 | |
---|
| 63 | n = elstack%n_items |
---|
| 64 | if (n == STACK_SIZE) then |
---|
| 65 | stop "*Element stack full" |
---|
| 66 | endif |
---|
| 67 | n = n + 1 |
---|
| 68 | elstack%data(n) = item |
---|
| 69 | elstack%n_items = n |
---|
| 70 | |
---|
| 71 | end subroutine push_elstack |
---|
| 72 | |
---|
| 73 | !----------------------------------------------------------------- |
---|
| 74 | subroutine pop_elstack(elstack,item) |
---|
| 75 | type(elstack_t), intent(inout) :: elstack |
---|
| 76 | character(len=*), intent(out) :: item |
---|
| 77 | |
---|
| 78 | ! |
---|
| 79 | ! We assume the elstack is not empty... (the user has called is_empty first) |
---|
| 80 | ! |
---|
| 81 | integer :: n |
---|
| 82 | |
---|
| 83 | n = elstack%n_items |
---|
| 84 | if (n == 0) then |
---|
| 85 | stop "*********Element stack empty" |
---|
| 86 | endif |
---|
| 87 | item = elstack%data(n) |
---|
| 88 | elstack%n_items = n - 1 |
---|
| 89 | |
---|
| 90 | end subroutine pop_elstack |
---|
| 91 | |
---|
| 92 | !----------------------------------------------------------------- |
---|
| 93 | subroutine get_top_elstack(elstack,item) |
---|
| 94 | ! |
---|
| 95 | ! Get the top element of the stack, *without popping it*. |
---|
| 96 | ! |
---|
| 97 | type(elstack_t), intent(in) :: elstack |
---|
| 98 | character(len=*), intent(out) :: item |
---|
| 99 | |
---|
| 100 | ! |
---|
| 101 | ! We assume the elstack is not empty... (the user has called is_empty first) |
---|
| 102 | ! |
---|
| 103 | integer :: n |
---|
| 104 | |
---|
| 105 | n = elstack%n_items |
---|
| 106 | if (n == 0) then |
---|
| 107 | stop "*********Element stack empty" |
---|
| 108 | endif |
---|
| 109 | item = elstack%data(n) |
---|
| 110 | |
---|
| 111 | end subroutine get_top_elstack |
---|
| 112 | |
---|
| 113 | !----------------------------------------------------------------- |
---|
| 114 | subroutine print_elstack(elstack,unit) |
---|
| 115 | type(elstack_t), intent(in) :: elstack |
---|
| 116 | integer, intent(in) :: unit |
---|
| 117 | integer :: i |
---|
| 118 | |
---|
| 119 | do i = elstack%n_items, 1, -1 |
---|
| 120 | write(unit=unit,fmt=*) trim(elstack%data(i)) |
---|
| 121 | enddo |
---|
| 122 | |
---|
| 123 | end subroutine print_elstack |
---|
| 124 | |
---|
| 125 | !------------------------------------------------------------- |
---|
| 126 | subroutine get_elstack_signature(elstack,string) |
---|
| 127 | type(elstack_t), intent(in) :: elstack |
---|
| 128 | character(len=*), intent(out) :: string |
---|
| 129 | integer :: i, length, j |
---|
| 130 | |
---|
| 131 | string = "" |
---|
| 132 | j = 0 |
---|
| 133 | do i = 1, elstack%n_items |
---|
| 134 | length = len_trim(elstack%data(i)) |
---|
| 135 | string(j+1:j+1) = "/" |
---|
| 136 | j = j+1 |
---|
| 137 | string(j+1:j+length) = trim(elstack%data(i)) |
---|
| 138 | j = j + length |
---|
| 139 | enddo |
---|
| 140 | |
---|
| 141 | end subroutine get_elstack_signature |
---|
| 142 | |
---|
| 143 | end module m_wxml_elstack |
---|