Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Diff of /src/include/graph/Node.h [50073b] .. [0513cd] Maximize Restore

  Switch to side-by-side view

--- a/src/include/graph/Node.h
+++ b/src/include/graph/Node.h
@@ -38,10 +38,10 @@
 
 protected:
     std::vector<unsigned int> const &_dim;
-    unsigned int _length;
+    const unsigned int _length;
     const unsigned int _nchain;
     double *_data;
-
+    unsigned int _index;
 public:
     /**
      * Constucts a Node with no parents.
@@ -162,6 +162,26 @@
     void removeChild(StochasticNode *node) const;
     void addChild(DeterministicNode *node) const;
     void removeChild(DeterministicNode *node) const;
+    /**
+     * Sets the index of the node. A Node index should be unique so
+     * that it can be used in sorting operations.  A newly created
+     * Node has an index of zero.
+     *
+     * It is only permitted to set the index if the current index is
+     * zero. If an attempt to reset the index is made, a logic_error
+     * is thrown. This provides a runtime guarantee that the index is
+     * constant.
+     *
+     * @param index Value to be used as the Node's index.
+     *
+     * @see Graph, Model#insert
+     */
+    void setIndex(unsigned int index);
+    /**
+     * Returns the index of a Node. If setIndex has not been called
+     * then the index is zero.
+     */
+    unsigned int index() const;
 };
 
 /**
@@ -170,6 +190,28 @@
  */
 unsigned int countChains(std::vector<Node const *> const &parameters);
 
+/**
+ * Comparison function for nodes using the indices of the two nodes.
+ *
+ * @return true if node1 has a lower index than node2
+ *
+ * A logic_error is thrown if either node has an index of zero, or if
+ * two distinct nodes have the same index.
+ */
+bool lt(Node const *node1, Node const *node2);
+
+/**
+ * @short STL function object for comparing two indexed Nodes
+ */
+struct ltNode
+{
+    bool operator()(Node const *arg1, Node const *arg2) const
+    {
+	return lt(arg1, arg2);
+    }
+};
+
+
 } /* namespace jags */
 
 #endif /* NODE_H_ */