1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Matt Woodrow <mwoodrow@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef GFX_DIRECTEDGRAPH_H
39 : #define GFX_DIRECTEDGRAPH_H
40 :
41 : #include "gfxTypes.h"
42 : #include "nsTArray.h"
43 :
44 : namespace mozilla {
45 : namespace layers {
46 :
47 : template <typename T>
48 0 : class DirectedGraph {
49 : public:
50 :
51 0 : class Edge {
52 : public:
53 0 : Edge(T aFrom, T aTo) : mFrom(aFrom), mTo(aTo) {}
54 :
55 0 : bool operator==(const Edge& aOther) const
56 : {
57 0 : return mFrom == aOther.mFrom && mTo == aOther.mTo;
58 : }
59 :
60 : T mFrom;
61 : T mTo;
62 : };
63 :
64 : class RemoveEdgesToComparator
65 : {
66 : public:
67 0 : bool Equals(const Edge& a, T const& b) const { return a.mTo == b; }
68 : };
69 :
70 : /**
71 : * Add a new edge to the graph.
72 : */
73 0 : void AddEdge(Edge aEdge)
74 : {
75 0 : NS_ASSERTION(!mEdges.Contains(aEdge), "Adding a duplicate edge!");
76 0 : mEdges.AppendElement(aEdge);
77 0 : }
78 :
79 0 : void AddEdge(T aFrom, T aTo)
80 : {
81 0 : AddEdge(Edge(aFrom, aTo));
82 0 : }
83 :
84 : /**
85 : * Get the list of edges.
86 : */
87 0 : const nsTArray<Edge>& GetEdgeList() const
88 : {
89 0 : return mEdges;
90 : }
91 :
92 : /**
93 : * Remove the given edge from the graph.
94 : */
95 0 : void RemoveEdge(Edge aEdge)
96 : {
97 0 : mEdges.RemoveElement(aEdge);
98 0 : }
99 :
100 : /**
101 : * Remove all edges going into aNode.
102 : */
103 0 : void RemoveEdgesTo(T aNode)
104 : {
105 : RemoveEdgesToComparator c;
106 0 : while (mEdges.RemoveElement(aNode, c)) {}
107 0 : }
108 :
109 : /**
110 : * Get the number of edges going into aNode.
111 : */
112 0 : unsigned int NumEdgesTo(T aNode)
113 : {
114 0 : unsigned int count = 0;
115 0 : for (unsigned int i = 0; i < mEdges.Length(); i++) {
116 0 : if (mEdges.ElementAt(i).mTo == aNode) {
117 0 : count++;
118 : }
119 : }
120 0 : return count;
121 : }
122 :
123 : /**
124 : * Get the list of all edges going from aNode
125 : */
126 0 : void GetEdgesFrom(T aNode, nsTArray<Edge>& aResult)
127 : {
128 0 : for (unsigned int i = 0; i < mEdges.Length(); i++) {
129 0 : if (mEdges.ElementAt(i).mFrom == aNode) {
130 0 : aResult.AppendElement(mEdges.ElementAt(i));
131 : }
132 : }
133 0 : }
134 :
135 : /**
136 : * Get the total number of edges.
137 : */
138 0 : unsigned int GetEdgeCount() { return mEdges.Length(); }
139 :
140 : private:
141 :
142 : nsTArray<Edge> mEdges;
143 : };
144 :
145 : }
146 : }
147 :
148 : #endif // GFX_DIRECTEDGRAPH_H
|