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
This commit is contained in:
Sergiu Ivanov 2021-01-01 21:46:58 +01:00
parent 15681c23ae
commit cec1ff51b6
1 changed files with 35 additions and 11 deletions

View File

@ -20,34 +20,58 @@
(module graph-wrapper racket (module graph-wrapper racket
(require (prefix-in g: graph)) (require (prefix-in g: graph))
(provide graph? (provide graph? has-vertex? has-edge?
directed-graph directed-graph
has-vertex?
has-edge?
graphviz) graphviz)
;; Wrap the opaque graph structure coming from the generic
;; graph library.
(struct graph (g)) (struct graph (g))
(define (directed-graph es [ws #f])
(graph (g:directed-graph es ws)))
(define (has-vertex? g v) (define (has-vertex? g v)
(g:has-vertex? (graph-g g) v)) (g:has-vertex? (graph-g g) v))
(define (has-edge? g u v) (define (has-edge? g u v)
(g:has-edge? (graph-g 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]) (define (graphviz g #:output [output #f] #:colors [colors #f])
(g:graphviz (graph-g g) #:output output #:colors colors))) (g:graphviz (graph-g g) #:output output #:colors colors)))
(require/typed 'graph-wrapper
(require/typed/provide 'graph-wrapper
[#:opaque Graph graph?] [#:opaque Graph graph?]
[directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)]
[has-vertex? (-> Graph Any Boolean)] [has-vertex? (-> Graph Any Boolean)]
[has-edge? (-> Graph Any 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) [graphviz (->* (Graph)
(#:output Output-Port (#:output Output-Port
#:colors (HashTable Any Natural)) #:colors (HashTable Any Natural))
String)]) String)])
(define g (directed-graph '((a b) (b c))))
(has-edge? g 'a 'c) (module+ test
(has-vertex? g 'a) ;; The goal of the tests is to check that all of the provided
(graphviz g) ;; 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)))