6 public :: hecmwst_pair_array
14 integer(kind=kint) :: id
15 integer(kind=kint) :: i1, i2
18 type hecmwst_pair_array
19 integer(kind=kint) :: num
20 integer(kind=kint) :: max_num
21 type (hecmwST_pair),
pointer :: pairs(:) => null()
22 end type hecmwst_pair_array
28 type (hecmwst_pair_array),
intent(inout) :: parray
29 integer(kind=kint),
intent(in) :: max_num
31 allocate(parray%pairs(max_num))
32 parray%max_num = max_num
38 type (hecmwst_pair_array),
intent(inout) :: parray
39 if (
associated(parray%pairs))
deallocate(parray%pairs)
46 type (hecmwst_pair_array),
intent(inout) :: parray
47 integer(kind=kint),
intent(in) :: id, i1, i2
48 if (parray%num >= parray%max_num)
then
49 stop
'ERROR: hecmw_pair_array_append: overflow'
51 parray%num = parray%num + 1
52 parray%pairs(parray%num)%id = id
53 parray%pairs(parray%num)%i1 = i1
54 parray%pairs(parray%num)%i2 = i2
59 type (hecmwst_pair_array),
intent(inout) :: parray
60 call pairs_sort(parray%pairs, 1, parray%num)
66 type (hecmwst_pair_array),
intent(inout) :: parray
67 integer(kind=kint),
intent(in) :: i1, i2
68 type (hecmwst_pair) :: p
69 integer(kind=kint) :: id
72 call pairs_find(parray%pairs, 1, parray%num, p, id)
76 function pairs_comp(p1, p2)
78 integer(kind=kint) :: pairs_comp
79 type (hecmwst_pair),
intent(in) :: p1, p2
80 if (p1%i1 < p2%i1)
then
82 else if (p1%i1 > p2%i1)
then
85 if (p1%i2 < p2%i2)
then
87 else if (p1%i2 > p2%i2)
then
93 end function pairs_comp
95 recursive subroutine pairs_sort(pairs, from, to)
97 type (hecmwst_pair),
pointer :: pairs(:)
98 integer(kind=kint),
intent(in) :: from, to
99 integer(kind=kint) :: center, left, right
100 type (hecmwst_pair) :: pivot, tmp
101 if (from >= to)
return
102 center = (from + to) / 2
103 pivot = pairs(center)
107 do while (pairs_comp(pairs(left), pivot) < 0)
110 do while (pairs_comp(pivot, pairs(right)) < 0)
113 if (left >= right)
exit
115 pairs(left) = pairs(right)
120 if (from < left-1)
call pairs_sort(pairs, from, left-1)
121 if (right+1 < to)
call pairs_sort(pairs, right+1, to)
123 end subroutine pairs_sort
125 recursive subroutine pairs_find(pairs, from, to, p, id)
127 type (hecmwst_pair),
pointer :: pairs(:)
128 integer(kind=kint),
intent(in) :: from, to
129 type (hecmwst_pair),
intent(in) :: p
130 integer(kind=kint),
intent(out) :: id
131 integer(kind=kint) :: center, icomp
136 center = (from + to) / 2
137 icomp = pairs_comp(p, pairs(center))
139 call pairs_find(pairs, from, center-1, p, id)
141 else if (icomp > 0)
then
142 call pairs_find(pairs, center+1, to, p, id)
145 id = pairs(center)%id
148 end subroutine pairs_find