summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/model/Collection.lua
blob: abc454b7dbbea8fe2c7f890aa96f2eed5a69073b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
-- Copyright 2012 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

-- @release $Header$


---
-- A collection is essentially a subgraph of a graph, that is, a
-- ``collection'' of some nodes and some edges of the graph. The name
-- ``collection'' was chosen over ``subgraph'' since the latter are
-- often thought of as parts of a graph that are rendered in a special
-- way (such as being surrounded by a rectangle), while collections
-- are used to model such diverse things as hyperedges, sets of
-- vertices that should be on the same level in a layered algorithm,
-- or, indeed, subgraphs that are rendered in a special way.
--
-- Collections are grouped into ``kinds''. All collections of a given
-- kind can be accessed by algorithms through an array whose elements
-- are the collections. On the display layer, for each kind a separate
-- key is available to indicate that a node or an edge belongs to a
-- collection.
--
-- Collections serve two purposes: First, they can be seen as ``hints''
-- to graph drawing algorithms that certain nodes and/or edges ``belong
-- together''. For instance, collections of kind |same layer| are used
-- by the Sugiyama algorithm to group together nodes that should appear
-- at the same height of the output. Second, since collections are also
-- passed back to the display layer in a postprocessing step, they can be
-- used to render complicated concepts such as hyperedges (which are
-- just collections of nodes, after all) or subgraphs.
--
-- @field kind The ``kind'' of the collection.
--
-- @field vertices A lookup table of vertices (that is, both an array
-- with the vertices in the order in which they appear as well as a
-- table such that |vertices[vertex] == true| whenever |vertex| is
-- present in the table.
--
-- @field edges A lookup table of edges (not arcs!).
--
-- @field options An options table. This is the table of options that
-- was in force when the collection was created.
--
-- @field child_collections An array of all collections that are
-- direct children of this collection (that is,
-- they were defined while the current collection was the most
-- recently defined collection on the options stack). However, you
-- should use the methods |children|, |descendants|, and so to access
-- this field.
--
-- @field parent_collection The parent collection of the current
-- collection. This field may be |nil| in case a collection has no parent.
--
-- @field event An |Event| object that was create for this
-- collection. Its |kind| will be |"collection"| while its |parameter|
-- will be the collection kind.

local Collection = {}
Collection.__index = Collection


-- Namespace

require("pgf.gd.model").Collection = Collection


-- Imports
local Storage      = require "pgf.gd.lib.Storage"



---
-- Creates a new collection. You should not call this function
-- directly, it is called by the interface classes.
--
-- @param t A table of initial values. The field |t.kind| must be a
-- nonempty string.
--
-- @return The new collection
--
function Collection.new(t)
  assert (type(t.kind) == "string" and t.kind ~= "", "collection kind not set")

  return setmetatable(
    {
      vertices               = t.vertices or {},
      edges                  = t.edges or {},
      options                = t.options or {},
      generated_options      = t.generated_options or {},
      kind                   = t.kind,
      event                  = t.event,
      child_collections      = t.child_collections or {},
    }, Collection)
end




--
-- An internal function for registering a collection as child of
-- another collection. The collection |self| will be made a child
-- collection of |parent|.
--
-- @param parent A collection.

function Collection:registerAsChildOf(parent)
  self.parent = parent
  if parent then
    assert (getmetatable(parent) == Collection, "parent must be a collection")
    parent.child_collections[#parent.child_collections+1] = self
  end
end



---
-- A collection can have any number of \emph{child collections}, which
-- are collections nested inside the collection. You can access the
-- array of these children through this method. You may not modify
-- the array returned by this function.
--
-- @return The array of children of |self|.
--
function Collection:children()
  return self.child_collections
end


---
-- This method works like the |children| method. However, the tree of
-- collections is, conceptually, contracted by considering only these
-- collections that have the |kind| given as parameter. For instance,
-- if |self| has a child collection of a kind different from |kind|,
-- but this child collection has, in turn, a child collection of kind
-- |kind|, this latter child collection will be included in the array
-- -- but not any of its child collections.
--
-- @param kind The collection kind to which the tree of collections
-- should be restricted.
--
-- @return The array of children of |self| in this contracted tree.
--
function Collection:childrenOfKind(kind)
  local function rec (c, a)
    for _,d in ipairs(c.child_collections) do
      if d.kind == kind then
        a[#a + 1] = d
      else
        rec (d, a)
      end
    end
    return a
  end
  return rec(self, {})
end


---
-- The descendants of a collection are its children, plus their
-- children, plus their children, and so on.
--
-- @return An array of all descendants of |self|. It will be in
-- preorder.

function Collection:descendants()
  local function rec (c, a)
    for _,d in ipairs(c.child_collections) do
      a[#a + 1] = d
      rec (d, a)
    end
    return a
  end
  return rec(self, {})
end



---
-- The descendants of a collection of the given |kind|.
--
-- @param kind A collection kind.
--
-- @return An array of all descendants of |self| of the given |kind|.

function Collection:descendantsOfKind(kind)
  local function rec (c, a)
    for _,d in ipairs(c.child_collections) do
      if d.kind == kind then
        a[#a + 1] = d
      end
      rec (d, a)
    end
    return a
  end
  return rec(self, {})
end



-- Done

return Collection