Skip to content

Commit 8d4dd1b

Browse files
author
Damian Rouson
committed
refac(vertex): make edges component array private
1 parent 6f71fac commit 8d4dd1b

File tree

4 files changed

+69
-58
lines changed

4 files changed

+69
-58
lines changed

src/dag_s.f90

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ pure module function topological_sort(dag) result(order)
2828
integer, allocatable :: order(:), searched(:)
2929
integer v
3030

31-
call assert(all([(allocated(dag%vertices(v)%edges_), v=1, size(dag%vertices))]), &
32-
"dag_s topological_sort: (all([(allocated(dag%vertices(v)%edges_), v=1, size(dag%vertices))])")
31+
call assert(all(dag%vertices(:)%edges_allocated()), "dag_s topological_sort: all(dag%vertices(:)%edges_allocated())")
3332

3433
block
3534
integer, allocatable :: discovered(:)
@@ -165,7 +164,7 @@ end function topological_sort
165164
integer v
166165

167166
do v = 1, size(self%vertices)
168-
if (any(self%vertices(v)%edges_ == vertex_num)) dependencies = [dependencies, v]
167+
if (any(self%vertices(v)%edges() == vertex_num)) dependencies = [dependencies, v]
169168
end do
170169
end block
171170

@@ -220,6 +219,9 @@ function generate_digraph(self,rankdir,dpi) result(str)
220219
character(len=*),parameter :: tab = ' '
221220
character(len=*),parameter :: newline = new_line(' ')
222221

222+
223+
call assert(all(self%vertices(:)%edges_allocated()), "generate_digraph: self%edges_allocated()")
224+
223225
str = 'digraph G {'//newline//newline
224226
if (present(rankdir)) &
225227
str = str//tab//'rankdir='//rankdir//newline//newline
@@ -236,16 +238,16 @@ function generate_digraph(self,rankdir,dpi) result(str)
236238

237239
! define the dependencies:
238240
do i=1,size(self%vertices)
239-
if (allocated(self%vertices(i)%edges_)) then
240-
n_edges = size(self%vertices(i)%edges_)
241-
str = str//tab//integer_to_string(i)//merge(' -> ',' ',n_edges/=0)
242-
do j=1,n_edges
243-
! comma-separated list:
244-
str = str//integer_to_string(self%vertices(i)%edges_(j))
245-
if (n_edges>1 .and. j<n_edges) str = str//','
246-
end do
247-
str = str//';'//newline
248-
end if
241+
n_edges = size(self%vertices(i)%edges())
242+
str = str//tab//integer_to_string(i)//merge(' -> ',' ',n_edges/=0)
243+
do j=1,n_edges
244+
! comma-separated list:
245+
associate(edges => self%vertices(i)%edges())
246+
str = str//integer_to_string(edges(j))
247+
if (n_edges>1 .and. j<n_edges) str = str//','
248+
end associate
249+
end do
250+
str = str//';'//newline
249251
end do
250252

251253
str = str//newline//'}'

src/vertex_m.f90

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ module vertex_m
1313

1414
type vertex_t
1515
!! Encapsulate a node in a graph comprised of vertices connected by dependencies (edges)
16-
private
17-
integer, public, allocatable :: edges_(:)
18-
! integer, allocatable :: edges(:) - should be private, but a bug in GCC needs it to be public. When fixed, this will be private again
19-
type(varying_string) :: label_
20-
character(len=:), allocatable :: attributes_
16+
private
17+
integer, allocatable :: edges_(:)
18+
type(varying_string) :: label_
19+
character(len=:), allocatable :: attributes_
2120
contains
22-
procedure :: to_json
23-
procedure :: edges
24-
procedure :: label
25-
procedure :: attributes
21+
procedure :: to_json
22+
procedure :: edges
23+
procedure :: label
24+
procedure :: attributes
25+
procedure :: edges_allocated
2626
end type
2727

2828
interface vertex_t
@@ -53,41 +53,47 @@ pure module function construct_from_components(edges, label, attributes) result(
5353
end interface
5454

5555
interface
56+
57+
elemental module function edges_allocated(self) result(edges_array_allocated)
58+
implicit none
59+
class(vertex_t), intent(in) :: self
60+
logical edges_array_allocated
61+
end function
62+
63+
module function from_json_object(json_object) result(vertex)
64+
!! construct a vertexa_t object from a jsonff JSON object
65+
implicit none
66+
type(json_object_t), intent(in) :: json_object
67+
type(vertex_t) :: vertex
68+
end function
69+
70+
impure elemental module function to_json(self) result(json_object)
71+
!! Result is a JSON representation of self
72+
implicit none
73+
class(vertex_t), intent(in) :: self
74+
type(json_object_t) :: json_object
75+
end function
5676

57-
module function from_json_object(json_object) result(vertex)
58-
!! construct a vertexa_t object from a jsonff JSON object
59-
implicit none
60-
type(json_object_t), intent(in) :: json_object
61-
type(vertex_t) :: vertex
62-
end function
63-
64-
impure elemental module function to_json(self) result(json_object)
65-
!! Result is a JSON representation of self
66-
implicit none
67-
class(vertex_t), intent(in) :: self
68-
type(json_object_t) :: json_object
69-
end function
70-
71-
pure module function edges(self) result(my_edges)
72-
!! Result is the array element numbers of the vertices on which this vertex depends
73-
implicit none
74-
class(vertex_t), intent(in) :: self
75-
integer :: my_edges(size(self%edges_))
76-
end function
77-
78-
pure module function label(self) result(my_label)
79-
!! Vertexd label getter
80-
implicit none
81-
class(vertex_t), intent(in) :: self
82-
character(len=len(self%label_)) my_label
83-
end function
84-
85-
pure module function attributes(self) result(my_attributes)
86-
!! Vertex attributes getter
87-
implicit none
88-
class(vertex_t), intent(in) :: self
89-
character(len=len(self%attributes_)) my_attributes
90-
end function
77+
pure module function edges(self) result(my_edges)
78+
!! Result is the array element numbers of the vertices on which this vertex depends
79+
implicit none
80+
class(vertex_t), intent(in) :: self
81+
integer :: my_edges(size(self%edges_))
82+
end function
83+
84+
pure module function label(self) result(my_label)
85+
!! Vertexd label getter
86+
implicit none
87+
class(vertex_t), intent(in) :: self
88+
character(len=len(self%label_)) my_label
89+
end function
90+
91+
pure module function attributes(self) result(my_attributes)
92+
!! Vertex attributes getter
93+
implicit none
94+
class(vertex_t), intent(in) :: self
95+
character(len=len(self%attributes_)) my_attributes
96+
end function
9197

9298
end interface
9399

src/vertex_s.f90

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313

1414
contains
1515

16+
module procedure edges_allocated
17+
edges_array_allocated = allocated(self%edges_)
18+
end procedure
19+
1620
module procedure to_json
1721
integer i
1822
type(json_string_t) :: edges_key, label_key, label_value
1923
type(json_array_t) :: edges_value
2024
type(error_list_t) :: errors
2125
type(fallible_json_string_t) :: maybe_key, maybe_value
2226

23-
2427
maybe_key = fallible_json_string_t("edges")
2528
errors = maybe_key%errors()
2629
call assert(.not. errors%has_any(), "vertex%to_json (edges key): .not. errors%has_any()", char(errors%to_string()))

test/dag_test.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ function test_dag_construction() result(tests)
1818

1919
tests = describe("dag's module dependency graph", &
2020
[ it("can be constructed, output to .dot file, and converted to a PDF", construct_dag_and_write_pdf) &
21-
, it("can be constructed and converted to a JSON object", construct_dag_and_json_object) &
2221
, it("is topologically sorted when constructed from components", component_constructor_sorts) &
2322
, it("is topologically sorted when constructed from a JSON object", json_constructor_sorts) &
23+
, it("can be constructed and converted to a JSON object", construct_dag_and_json_object) &
2424
])
2525

2626
end function

0 commit comments

Comments
 (0)