1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef GFX_DIRECTEDGRAPH_H
7 #define GFX_DIRECTEDGRAPH_H
21 Edge(T aFrom
, T aTo
) : mFrom(aFrom
), mTo(aTo
) {}
23 bool operator==(const Edge
& aOther
) const
25 return mFrom
== aOther
.mFrom
&& mTo
== aOther
.mTo
;
32 class RemoveEdgesToComparator
35 bool Equals(const Edge
& a
, T
const& b
) const { return a
.mTo
== b
; }
39 * Add a new edge to the graph.
41 void AddEdge(Edge aEdge
)
43 NS_ASSERTION(!mEdges
.Contains(aEdge
), "Adding a duplicate edge!");
44 mEdges
.AppendElement(aEdge
);
47 void AddEdge(T aFrom
, T aTo
)
49 AddEdge(Edge(aFrom
, aTo
));
53 * Get the list of edges.
55 const nsTArray
<Edge
>& GetEdgeList() const
61 * Remove the given edge from the graph.
63 void RemoveEdge(Edge aEdge
)
65 mEdges
.RemoveElement(aEdge
);
69 * Remove all edges going into aNode.
71 void RemoveEdgesTo(T aNode
)
73 RemoveEdgesToComparator c
;
74 while (mEdges
.RemoveElement(aNode
, c
)) {}
78 * Get the number of edges going into aNode.
80 unsigned int NumEdgesTo(T aNode
)
82 unsigned int count
= 0;
83 for (unsigned int i
= 0; i
< mEdges
.Length(); i
++) {
84 if (mEdges
.ElementAt(i
).mTo
== aNode
) {
92 * Get the list of all edges going from aNode
94 void GetEdgesFrom(T aNode
, nsTArray
<Edge
>& aResult
)
96 for (unsigned int i
= 0; i
< mEdges
.Length(); i
++) {
97 if (mEdges
.ElementAt(i
).mFrom
== aNode
) {
98 aResult
.AppendElement(mEdges
.ElementAt(i
));
104 * Get the total number of edges.
106 unsigned int GetEdgeCount() { return mEdges
.Length(); }
110 nsTArray
<Edge
> mEdges
;
116 #endif // GFX_DIRECTEDGRAPH_H