Skip to content

Commit b37fb99

Browse files
Merge pull request #281 from jacobwilliams/reverse
added routine to reverse vector or object.
2 parents 3382caa + 795f5ad commit b37fb99

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

src/json_value_module.F90

+49
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ module json_value_module
643643

644644
procedure,public :: remove => json_value_remove !! Remove a [[json_value]] from a linked-list structure.
645645
procedure,public :: replace => json_value_replace !! Replace a [[json_value]] in a linked-list structure.
646+
procedure,public :: reverse => json_value_reverse !! Reverse the order of the children of an array of object.
646647
procedure,public :: check_for_errors => json_check_for_errors !! check for error and get error message
647648
procedure,public :: clear_exceptions => json_clear_exceptions !! clear exceptions
648649
procedure,public :: count => json_count !! count the number of children
@@ -2071,6 +2072,54 @@ subroutine json_value_replace(json,p1,p2,destroy)
20712072
end subroutine json_value_replace
20722073
!*****************************************************************************************
20732074

2075+
!*****************************************************************************************
2076+
!> author: Jacob Williams
2077+
! date: 4/11/2017
2078+
!
2079+
! Reverse the order of the children of an array or object.
2080+
2081+
subroutine json_value_reverse(json,p)
2082+
2083+
implicit none
2084+
2085+
class(json_core),intent(inout) :: json
2086+
type(json_value),pointer :: p
2087+
2088+
type(json_value),pointer :: tmp !! temp variable for traversing the list
2089+
type(json_value),pointer :: current !! temp variable for traversing the list
2090+
integer(IK) :: var_type !! for getting the variable type
2091+
2092+
if (associated(p)) then
2093+
2094+
call json%info(p,var_type=var_type)
2095+
2096+
! can only reverse objects or arrays
2097+
if (var_type==json_object .or. var_type==json_array) then
2098+
2099+
nullify(tmp)
2100+
current => p%children
2101+
p%tail => current
2102+
2103+
! Swap next and previous for all nodes:
2104+
do
2105+
if (.not. associated(current)) exit
2106+
tmp => current%previous
2107+
current%previous => current%next
2108+
current%next => tmp
2109+
current => current%previous
2110+
end do
2111+
2112+
if (associated(tmp)) then
2113+
p%children => tmp%previous
2114+
end if
2115+
2116+
end if
2117+
2118+
end if
2119+
2120+
end subroutine json_value_reverse
2121+
!*****************************************************************************************
2122+
20742123
!*****************************************************************************************
20752124
!> author: Jacob Williams
20762125
! date: 4/26/2016

src/tests/jf_test_28.f90

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
!*****************************************************************************************
2+
!>
3+
! Unit test for [[json_value_reverse]].
4+
!
5+
!@note This uses Fortran 2008 auto LHS assignments.
6+
7+
program jf_test_28
8+
9+
use json_module
10+
use iso_fortran_env
11+
12+
implicit none
13+
14+
type(json_core) :: json
15+
type(json_value),pointer :: p,vec
16+
integer(json_IK),dimension(:),allocatable :: ivec
17+
integer(json_IK),dimension(:),allocatable :: ivec_value,ivec_value_reversed
18+
character(kind=json_CK,len=:),allocatable :: str
19+
integer :: i !! counter
20+
21+
write(error_unit,'(A)') ''
22+
write(error_unit,'(A)') '================================='
23+
write(error_unit,'(A)') ' TEST 28'
24+
write(error_unit,'(A)') '================================='
25+
write(error_unit,'(A)') ''
26+
27+
call json%initialize(compress_vectors=.true.)
28+
29+
do i=1,4
30+
31+
! all the cases:
32+
select case (i)
33+
case(1)
34+
str = json_CK_'{"vec":[1,2,3,4,5]}'
35+
ivec_value = [1,2,3,4,5]
36+
ivec_value_reversed = [5,4,3,2,1]
37+
case(2)
38+
str = json_CK_'{"vec":[1]}'
39+
ivec_value = [1]
40+
ivec_value_reversed = [1]
41+
case(3)
42+
str = json_CK_'{"vec":[1,2]}'
43+
ivec_value = [1,2]
44+
ivec_value_reversed = [2,1]
45+
case(4)
46+
str = json_CK_'{"vec":[]}'
47+
!ivec_value = []
48+
!ivec_value_reversed = []
49+
end select
50+
51+
call json%parse(p,str)
52+
call json%get(p,'vec',vec)
53+
54+
write(output_unit,'(A)') ''
55+
write(output_unit,'(A)') 'Original:'
56+
write(output_unit,'(A)') ''
57+
call json%print(vec,output_unit)
58+
59+
call json%reverse(vec)
60+
61+
write(output_unit,'(A)') ''
62+
write(output_unit,'(A)') 'Reversed:'
63+
write(output_unit,'(A)') ''
64+
call json%print(vec,output_unit)
65+
66+
call json%get(vec,ivec)
67+
call json%destroy(p)
68+
69+
if (json%failed()) then
70+
call json%print_error_message(error_unit)
71+
stop 1
72+
else
73+
74+
if (allocated(ivec)) then
75+
if (i/=4) then
76+
if (all(ivec==ivec_value_reversed)) then
77+
write(output_unit,'(A)') 'reverse test passed'
78+
else
79+
write(output_unit,'(A,*(I3,1X))') 'reverse test failed: ', ivec
80+
stop 1
81+
end if
82+
else
83+
if (size(ivec)==0) then
84+
write(output_unit,'(A)') 'reverse test passed'
85+
else
86+
write(output_unit,'(A,*(I3,1X))') 'reverse test failed: ', ivec
87+
stop 1
88+
end if
89+
end if
90+
else
91+
write(output_unit,'(A)') 'reverse test failed: error getting ivec'
92+
stop 1
93+
end if
94+
95+
end if
96+
97+
end do
98+
99+
end program jf_test_28
100+
!*****************************************************************************************

0 commit comments

Comments
 (0)