MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
STLStackInt.f90
Go to the documentation of this file.
2  use kindmodule, only: i4b, lgp
4  use simmodule, only: ustop
5  implicit none
6  private
7  public :: stlstackint
8 
9  !> @brief A derived type representing a stack of integers.
10  !!
11  !! This type provides a stack data structure specifically for integers.
12  !! It includes methods for typical stack operations such as push, pop,
13  !< and checking if the stack is empty.
14  type :: stlstackint
15  type(stlvecint), private :: stack !< the internal stack
16  contains
17  procedure, pass(this) :: init !< allocate memory, init size and capacity
18  procedure, pass(this) :: destroy !< deletes the memory
19  procedure, pass(this) :: push !< adds an element at the end of the vector
20  procedure, pass(this) :: pop !< removes the last element
21  procedure, pass(this) :: top !< returns the last element (without removing it)
22  procedure, pass(this) :: size !< returns the size of the stack
23  end type stlstackint
24 
25 contains ! module routines
26 
27  subroutine init(this, capacity)
28  class(stlstackint), intent(inout) :: this
29  integer(I4B), intent(in), optional :: capacity ! the initial capacity, when given
30 
31  ! init the vector
32  if (present(capacity)) then
33  call this%stack%init(capacity)
34  else
35  call this%stack%init()
36  end if
37 
38  end subroutine init
39 
40  subroutine push(this, newValue)
41  class(stlstackint), intent(inout) :: this
42  integer(I4B) :: newValue
43 
44  call this%stack%push_back(newvalue)
45 
46  end subroutine push
47 
48  subroutine pop(this)
49  class(stlstackint), intent(inout) :: this
50 
51  if (this%stack%size == 0) then
52  write (*, *) 'STLStackInt exception: cannot pop an empty stack'
53  call ustop()
54  end if
55  this%stack%size = this%stack%size - 1
56 
57  end subroutine pop
58 
59  function top(this) result(top_value)
60  class(stlstackint), intent(in) :: this
61  integer(I4B) :: top_value
62 
63  if (this%stack%size == 0) then
64  write (*, *) 'STLStackInt exception: cannot get top of an empty stack'
65  call ustop()
66  end if
67  top_value = this%stack%at(this%stack%size)
68 
69  end function top
70 
71  function size(this) result(size_value)
72  class(stlstackint), intent(in) :: this
73  integer(I4B) :: size_value
74 
75  size_value = this%stack%size
76 
77  end function size
78 
79  subroutine destroy(this)
80  class(stlstackint), intent(inout) :: this
81 
82  call this%stack%destroy()
83 
84  end subroutine destroy
85 
86 end module stlstackintmodule
subroutine init()
Definition: GridSorting.f90:24
This module defines variable data types.
Definition: kind.f90:8
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public ustop(stopmess, ioutlocal)
Stop the simulation.
Definition: Sim.f90:312
subroutine destroy(this)
Definition: STLStackInt.f90:80
subroutine pop(this)
Definition: STLStackInt.f90:49
subroutine push(this, newValue)
Definition: STLStackInt.f90:41
integer(i4b) function size(this)
Definition: STLStackInt.f90:72
integer(i4b) function top(this)
Definition: STLStackInt.f90:60
A derived type representing a stack of integers.
Definition: STLStackInt.f90:14