Bug 849918 - Initial support for PannerNode's 3D positional audio (equalpower panning...
[gecko.git] / gfx / layers / DirectedGraph.h
blob8c6d9cc1d8f9fa05edb15d6340cf3d20d868dd19
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
9 #include "gfxTypes.h"
10 #include "nsTArray.h"
12 namespace mozilla {
13 namespace layers {
15 template <typename T>
16 class DirectedGraph {
17 public:
19 class Edge {
20 public:
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;
28 T mFrom;
29 T mTo;
32 class RemoveEdgesToComparator
34 public:
35 bool Equals(const Edge& a, T const& b) const { return a.mTo == b; }
38 /**
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));
52 /**
53 * Get the list of edges.
55 const nsTArray<Edge>& GetEdgeList() const
57 return mEdges;
60 /**
61 * Remove the given edge from the graph.
63 void RemoveEdge(Edge aEdge)
65 mEdges.RemoveElement(aEdge);
68 /**
69 * Remove all edges going into aNode.
71 void RemoveEdgesTo(T aNode)
73 RemoveEdgesToComparator c;
74 while (mEdges.RemoveElement(aNode, c)) {}
77 /**
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) {
85 count++;
88 return count;
91 /**
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(); }
108 private:
110 nsTArray<Edge> mEdges;
116 #endif // GFX_DIRECTEDGRAPH_H