Open
Graph Drawing
Framework

 v.2012.05
 

NodeSet.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00044 #ifdef _MSC_VER
00045 #pragma once
00046 #endif
00047 
00048 #ifndef OGDF_NODE_SET_H
00049 #define OGDF_NODE_SET_H
00050 
00051 
00052 #include <ogdf/basic/NodeArray.h>
00053 #include <ogdf/basic/List.h>
00054 #include <ogdf/basic/SList.h>
00055 
00056 
00057 
00058 namespace ogdf {
00059 
00060 
00061 //---------------------------------------------------------
00062 // NodeSetSimple
00063 // maintains a subset S of the nodes contained in an associated
00064 // graph G (only insertion of elements and clear operation)
00065 //---------------------------------------------------------
00066 class OGDF_EXPORT NodeSetSimple {
00067 public:
00068     // creates a new empty face set associated with combinatorial embedding E
00069     NodeSetSimple(const Graph &G) : m_isContained(G,false) { }
00070 
00071     // destructor
00072     ~NodeSetSimple() { }
00073 
00074     // inserts node v into set S
00075     // running time: O(1)
00076     // Precond.: v is a node in the associated graph
00077     void insert(node v) {
00078         OGDF_ASSERT(v->graphOf() == m_isContained.graphOf());
00079         bool &isContained = m_isContained[v];
00080         if (isContained == false) {
00081             isContained = true;
00082             m_nodes.pushFront(v);
00083         }
00084     }
00085 
00086 
00087     // removes all nodes from set S
00088     // running time: O(|S|)
00089     void clear() {
00090         SListIterator<node> it;
00091         for(it = m_nodes.begin(); it.valid(); ++it) {
00092             m_isContained[*it] = false;
00093         }
00094         m_nodes.clear();
00095     }
00096 
00097 
00098     // returns true iff node v is contained in S
00099     // running time: O(1)
00100     // Precond.: v is a node in the asociated graph
00101     bool isMember(node v) const {
00102         OGDF_ASSERT(v->graphOf() == m_isContained.graphOf());
00103         return m_isContained[v];
00104     }
00105 
00106     // returns the list of nodes contained in S
00107     const SListPure<node> &nodes() const {
00108         return m_nodes;
00109     }
00110 
00111 private:
00112     // m_isContained[v] is true <=> v is contained in S
00113     NodeArray<bool> m_isContained;
00114     // list of nodes contained in S
00115     SListPure<node> m_nodes;
00116 };
00117 
00118 
00119 
00120 //---------------------------------------------------------
00121 // NodeSetPure
00122 // maintains a subset S of the nodes contained in an associated
00123 // graph G (no efficient access to size of S)
00124 //---------------------------------------------------------
00125 class OGDF_EXPORT NodeSetPure {
00126 public:
00127     // creates a new empty node set associated with graph G
00128     NodeSetPure(const Graph &G) : m_it(G,ListIterator<node>()) { }
00129 
00130     // destructor
00131     ~NodeSetPure() { }
00132 
00133     // inserts node v into set S
00134     // running time: O(1)
00135     // Precond.: v is a node in the associated graph
00136     void insert(node v) {
00137         OGDF_ASSERT(v->graphOf() == m_it.graphOf());
00138         ListIterator<node> &itV = m_it[v];
00139         if (!itV.valid())
00140             itV = m_nodes.pushBack(v);
00141     }
00142 
00143     // removes node v from set S
00144     // running time: O(1)
00145     // Precond.: v is a node in the asociated graph
00146     void remove(node v) {
00147         OGDF_ASSERT(v->graphOf() == m_it.graphOf());
00148         ListIterator<node> &itV = m_it[v];
00149         if (itV.valid()) {
00150             m_nodes.del(itV);
00151             itV = ListIterator<node>();
00152         }
00153     }
00154 
00155 
00156     // removes all nodes from set S
00157     // running time: O(|S|)
00158     void clear() {
00159         ListIterator<node> it;
00160         for(it = m_nodes.begin(); it.valid(); ++it) {
00161             m_it[*it] = ListIterator<node>();
00162         }
00163         m_nodes.clear();
00164     }
00165 
00166 
00167     // returns true iff node v is contained in S
00168     // running time: O(1)
00169     // Precond.: v is a node in the asociated graph
00170     bool isMember(node v) const {
00171         OGDF_ASSERT(v->graphOf() == m_it.graphOf());
00172         return m_it[v].valid();
00173     }
00174 
00175     // returns the list of nodes contained in S
00176     const ListPure<node> &nodes() const {
00177         return m_nodes;
00178     }
00179 
00180 private:
00181     // m_it[v] contains list iterator pointing to v if v is contained in S,
00182     // an invalid list iterator otherwise
00183     NodeArray<ListIterator<node> > m_it;
00184     // list of nodes contained in S
00185     ListPure<node> m_nodes;
00186 };
00187 
00188 
00189 
00190 //---------------------------------------------------------
00191 // NodeSet
00192 // maintains a subset S of the nodes contained in an associated
00193 // graph G
00194 //---------------------------------------------------------
00195 class OGDF_EXPORT NodeSet {
00196 public:
00197     // creates a new empty node set associated with graph G
00198     NodeSet(const Graph &G) : m_it(G,ListIterator<node>()) { }
00199 
00200     // destructor
00201     ~NodeSet() { }
00202 
00203     // inserts node v into set S
00204     // running time: O(1)
00205     // Precond.: v is a node in the associated graph
00206     void insert(node v) {
00207         OGDF_ASSERT(v->graphOf() == m_it.graphOf());
00208         ListIterator<node> &itV = m_it[v];
00209         if (!itV.valid())
00210             itV = m_nodes.pushBack(v);
00211     }
00212 
00213     // removes node v from set S
00214     // running time: O(1)
00215     // Precond.: v is a node in the asociated graph
00216     void remove(node v) {
00217         OGDF_ASSERT(v->graphOf() == m_it.graphOf());
00218         ListIterator<node> &itV = m_it[v];
00219         if (itV.valid()) {
00220             m_nodes.del(itV);
00221             itV = ListIterator<node>();
00222         }
00223     }
00224 
00225 
00226     // removes all nodess from set S
00227     // running time: O(|S|)
00228     void clear() {
00229         ListIterator<node> it;
00230         for(it = m_nodes.begin(); it.valid(); ++it) {
00231             m_it[*it] = ListIterator<node>();
00232         }
00233         m_nodes.clear();
00234     }
00235 
00236 
00237     // returns true iff node v is contained in S
00238     // running time: O(1)
00239     // Precond.: v is a node in the asociated graph
00240     bool isMember(node v) const {
00241         OGDF_ASSERT(v->graphOf() == m_it.graphOf());
00242         return m_it[v].valid();
00243     }
00244 
00245     // returns the size of set S
00246     // running time: O(1)
00247     int size() const {
00248         return m_nodes.size();
00249     }
00250 
00251     // returns the list of nodes contained in S
00252     const List<node> &nodes() const {
00253         return m_nodes;
00254     }
00255 
00256 private:
00257     // m_it[v] contains list iterator pointing to v if v is contained in S,
00258     // an invalid list iterator otherwise
00259     NodeArray<ListIterator<node> > m_it;
00260     // list of nodes contained in S
00261     List<node> m_nodes;
00262 };
00263 
00264 
00265 } // end namespace ogdf
00266 
00267 
00268 #endif