annotate src/docgen/graphutils/primitives.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/docgen/graphutils/primitives.d@ec8dd7b8bf0c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
1 /**
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
2 * Author: Aziz Köksal & Jari-Matti Mäkelä
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
3 * License: GPL3
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
4 */
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
5 module docgen.graphutils.primitives;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
6
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
7 /**
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
8 * Extendible graph class. Should support separation of concerns now better with mixins.
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
9 * Provides a method for cycle marking.
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
10 */
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
11 class Graph(alias V, alias E, int capacity = 1) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
12 static class Edge {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
13 Vertex outgoing, incoming;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
14 //bool cyclic = true;
462
b7503e02fbe7 Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 453
diff changeset
15
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
16 this(Vertex o, Vertex i) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
17 outgoing = o;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
18 incoming = i;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
19 o.outgoingEdges ~= this;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
20 i.incomingEdges ~= this;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
21 }
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
22
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
23 bool cyclic() {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
24 return outgoing.cyclic && incoming.cyclic;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
25 }
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
26
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
27 mixin E;
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
28 }
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
29
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
30 static class Vertex {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
31 Edge[] incomingEdges;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
32 Edge[] outgoingEdges;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
33 bool cyclic = true;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
34
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
35 Edge addChild(Vertex v) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
36 return new Edge(v, this);
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
37 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
38
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
39 Edge addParent(Vertex v) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
40 return v.addChild(this);
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
41 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
42
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
43 Vertex[] incoming() {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
44 Vertex[] tmp;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
45
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
46 foreach(edge; incomingEdges)
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
47 tmp ~= edge.outgoing;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
48
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
49 return tmp;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
50 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
51
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
52 Vertex[] outgoing() {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
53 Vertex[] tmp;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
54
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
55 foreach(edge; outgoingEdges)
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
56 tmp ~= edge.incoming;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
57
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
58 return tmp;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
59 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
60
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
61 mixin V;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
62 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
63
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
64 Vertex[] vertices;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
65 Edge[] edges;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
66
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
67 this() {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
68 vertices.length = capacity;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
69 vertices.length = 0;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
70 edges.length = capacity;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
71 edges.length = 0;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
72 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
73
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
74 void add(Vertex vertex) { vertices ~= vertex; }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
75
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
76 void add(Edge edge) { edges ~= edge; }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
77
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
78 void connect(Vertex from, Vertex to) { edges ~= from.addParent(to); }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
79
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
80 void connect(int from, int to) { connect(vertices[from], vertices[to]); }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
81
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
82 /**
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
83 * Starts from non-cyclic nodes and propagates two both directions.
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
84 * Bugs: marks non-cyclic imports between two cycles as cyclic. Could be fixed later if it's really needed (slow)
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
85 */
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
86 void markCycles() {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
87 void mark(Vertex v) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
88 v.cyclic = false;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
89 foreach(o; v.outgoing) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
90 if (!o.cyclic) continue;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
91
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
92 // propagate
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
93 bool cyclic = false;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
94 foreach(p; o.incoming) if (p.cyclic) { cyclic = true; break; }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
95 if (!cyclic) mark(o);
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
96 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
97 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
98
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
99 void mark2(Vertex v) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
100 v.cyclic = false;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
101 foreach(o; v.incoming) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
102 if (!o.cyclic) continue;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
103
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
104 // propagate
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
105 bool cyclic = false;
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
106 foreach(p; o.outgoing) if (p.cyclic) { cyclic = true; break; }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
107 if (!cyclic) mark2(o);
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
108 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
109 }
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
110
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
111 foreach(e; vertices)
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
112 if (e.cyclic) {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
113 if (!e.incoming.length) mark(e);
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
114 if (!e.outgoing.length) mark2(e);
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
115 }
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
116 }
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
117 }
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
118
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
119 template Empty() {}
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
120
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
121
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
122 // graph elements used in dep graphs
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
123
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
124
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
125 template DepEdge() {
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
126 bool isPublic; /// Public import.
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
127 bool isStatic; /// Static import.
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
128 }
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
129
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
130 template DepVertex() {
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
131 char[] name;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
132 char[] location;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
133 uint id;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
134
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
135 this(char[] name, char[] location, uint id = 0) {
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
136 this.name = name;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
137 this.location = location;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
138 this.id = id;
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
139 }
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
140 }
395
ac9cd48151b6 Added couple of docgen modules.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
141
729
ec8dd7b8bf0c Updated graph type.
Jari-Matti M?kel? <jmjm@iki.fi>
parents: 462
diff changeset
142 alias Graph!(DepVertex, DepEdge, 100) DepGraph;