MFIX  2016-1
list.f
Go to the documentation of this file.
1 #include "version.inc"
2 
3 module list
4 
5  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
6  ! !
7  ! Type active_t: !
8  ! !
9  ! Purpose: Represents an unsorted list of ids (positive integers). !
10  ! Implemented with an array, padded with zeroes. !
11  ! !
12  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
13 
14  type list_t
15  integer, dimension(:), allocatable :: list
16  integer :: list_len
17  end type list_t
18 
19 contains
20 
21  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
22  ! !
23  ! Subroutine: list_init !
24  ! !
25  ! Purpose: Initialize list (constructor) !
26  ! !
27  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
28 
29  subroutine list_init(this)
30  implicit none
31  type(list_t), intent(inout) :: this
32 
33  allocate(this%list(10))
34  this%list = 0
35  this%list_len = 0
36 
37  end subroutine list_init
38 
39  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
40  ! !
41  ! Subroutine: list_add !
42  ! !
43  ! Purpose: Adds new value to the list !
44  ! !
45  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
46 
47  subroutine list_add(this,value)
48  use resize, only: integer_grow
49  implicit none
50  type(list_t), intent(inout) :: this
51  integer, intent(in) :: value
52  integer :: old_len
53 
54  this%list_len = this%list_len + 1
55  if (size(this%list) < this%list_len) then
56  old_len = size(this%list)
57  call integer_grow(this%list,this%list_len)
58  this%list(old_len+1:size(this%list)) = 0
59  endif
60 
61  if (0.eq.this%list(this%list_len)) then
62  this%list(this%list_len) = value
63  else
64  print *,"LIST SHOULD END IN ZERO"
65  print *,"value = ",value
66  do old_len = 1, size(this%list)
67  print *,"list: ",old_len,this%list(old_len)
68  enddo
69  error_stop __line__
70  endif
71  end subroutine list_add
72 
73  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
74  ! !
75  ! Subroutine: list_pop !
76  ! !
77  ! Purpose: Removes value from the end of the list !
78  ! !
79  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
80 
81  integer function list_pop(this)
82  implicit none
83  type(list_t), intent(inout) :: this
84 
85  if (this%list_len .eq. 0) then
86  list_pop = -1
87  return
88  endif
89 
90  list_pop = this%list(this%list_len)
91  this%list(this%list_len) = 0
92 
93  this%list_len = this%list_len - 1
94 
95  end function list_pop
96 
97  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
98  ! !
99  ! Subroutine: list_del !
100  ! !
101  ! Purpose: Removes value from array !
102  ! !
103  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
104 
105  subroutine list_del(this,value)
106  implicit none
107  type(list_t), intent(inout) :: this
108  integer, intent(in) :: value
109  integer :: ai, aa
110 
111  do ai=1, size(this%list)
112  aa = this%list(ai)
113  if (value.eq.aa) then
114  this%list(ai) = this%list(this%list_len)
115  this%list(this%list_len) = 0
116  this%list_len = this%list_len - 1
117  exit
118  endif
119  enddo
120 
121  end subroutine list_del
122 
123  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
124  ! !
125  ! Function: list_get !
126  ! !
127  ! Purpose: Return element from list_t list !
128  ! !
129  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
130 
131  integer function list_get(this,index)
132  implicit none
133  type(list_t), intent(in) :: this
134  integer :: index
135 
136  list_get = this%list(index)
137 
138  end function list_get
139 
140  !vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
141  ! !
142  ! Function: list_get_length !
143  ! !
144  ! Purpose: Returns the length of list_t !
145  ! !
146  !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
147 
148  integer function list_get_length(this)
149  implicit none
150  type(list_t), intent(in) :: this
151 
152  list_get_length = this%list_len
153 
154  end function list_get_length
155 
156 end module list
integer function list_get(this, index)
Definition: list.f:132
subroutine list_del(this, value)
Definition: list.f:106
integer function list_pop(this)
Definition: list.f:82
Definition: list.f:3
subroutine, public integer_grow(integer_array, new_size)
Definition: resize.f:33
subroutine list_add(this, value)
Definition: list.f:48
integer function list_get_length(this)
Definition: list.f:149
Definition: resize.f:1
subroutine list_init(this)
Definition: list.f:30