Compare commits

...

2 Commits

Author SHA1 Message Date
Sergiu Ivanov cec1ff51b6 graph.rkt: Tidy up and structure in sections.
The sections are the same as in the documentation of the generic graph
library: https://docs.racket-lang.org/graph/index.html
2021-01-01 21:46:58 +01:00
Sergiu Ivanov 15681c23ae Add has-vertex?. 2021-01-01 21:31:14 +01:00
1 changed files with 38 additions and 9 deletions

View File

@ -20,29 +20,58 @@
(module graph-wrapper racket
(require (prefix-in g: graph))
(provide graph?
(provide graph? has-vertex? has-edge?
directed-graph
has-edge?
graphviz)
;; Wrap the opaque graph structure coming from the generic
;; graph library.
(struct graph (g))
(define (directed-graph es [ws #f])
(graph (g:directed-graph es ws)))
(define (has-vertex? g v)
(g:has-vertex? (graph-g g) v))
(define (has-edge? g u v)
(g:has-edge? (graph-g g) u v))
;; 2 Graph constructors
;; 2.2 Weighted graphs
(define (directed-graph es [ws #f])
(graph (g:directed-graph es ws)))
;; 10 Graphviz
(define (graphviz g #:output [output #f] #:colors [colors #f])
(g:graphviz (graph-g g) #:output output #:colors colors)))
(require/typed 'graph-wrapper
(require/typed/provide 'graph-wrapper
[#:opaque Graph graph?]
[directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)]
[has-vertex? (-> Graph Any Boolean)]
[has-edge? (-> Graph Any Any Boolean)]
;; 2 Graph constructors
;; 2.2 Weighted graphs
[directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)]
;; 10 Graphviz
[graphviz (->* (Graph)
(#:output Output-Port
#:colors (HashTable Any Natural))
String)])
(define g (directed-graph '((a b) (b c))))
(has-edge? g 'a 'c)
(graphviz g)
(module+ test
;; The goal of the tests is to check that all of the provided
;; functions can be invoked without errors. The tests do not check
;; whether the results make sense.
(require typed/rackunit)
(test-case "1 Generic Graph Interface"
(define g (directed-graph '((a b) (b c))))
(check-true (has-edge? g 'a 'c))
(check-true (has-vertex? g 'a)))
(test-case "10 Graphviz"
(define g (directed-graph '((a b) (b c))))
(graphviz g)))