1 #include <compiler/NodeFactory.h>
2 #include <graph/Node.h>
3 #include <util/nainf.h>
4
5 #include <cfloat>
6 #include <cmath>
7
8 using std::vector;
9
10 namespace jags {
11
12 /* Comparison function for arrays of doubles of equal length */
lt(double const * value1,double const * value2,unsigned int length)13 bool lt(double const *value1, double const *value2, unsigned int length)
14 {
15 for (unsigned long i = 0; i < length; ++i) {
16 if (lt(value1[i], value2[i])) {
17 return true;
18 }
19 else if (lt(value2[i], value1[i])) {
20 return false;
21 }
22 }
23 return false;
24 }
25
26 /* Comparison function for STL vectors */
27 /*
28 bool lt(vector<double> const &value1, vector<double> const &value2)
29 {
30 for (unsigned long i = 0; i < value1.size(); ++i) {
31 if (lt(value1[i], value2[i])) {
32 return true;
33 }
34 else if (lt(value2[i], value1[i])) {
35 return false;
36 }
37 }
38 return false;
39 }
40 */
41
42 /* Comparison function for Nodes */
lt(Node const * node1,Node const * node2)43 bool lt(Node const *node1, Node const *node2)
44 {
45 if (node1 == node2) {
46 // A node is always identical to itself
47 return false;
48 }
49
50 bool fix1 = node1->isFixed();
51 bool fix2 = node2->isFixed();
52
53 if (fix1 && fix2) {
54 //Fixed nodes are sorted by dimension, then value
55 if (node1->dim() == node2->dim()) {
56 return lt(node1->value(0), node2->value(0), node1->length());
57 }
58 else {
59 return node1->dim() < node2->dim();
60 }
61 }
62 else if (!fix1 && !fix2) {
63 //Non-fixed nodes are sorted by address. The ordering is
64 //arbitrary, but unique.
65 return (node1 < node2);
66 }
67 else {
68 //Fixed nodes come before non-fixed nodes
69 return fix1 > fix2;
70 }
71 }
72
73 /* Comparison operator for vectors of parameters */
lt(vector<Node const * > const & par1,vector<Node const * > const & par2)74 bool lt(vector<Node const *> const &par1, vector<Node const *> const &par2)
75 {
76 if (par1.size() == par2.size()) {
77 //Equal sized vectors: Sort by ordering of elements
78 for (unsigned int i = 0; i < par1.size(); ++i) {
79 if (lt(par1[i], par2[i])) {
80 return true;
81 }
82 else if (lt(par2[i], par1[i])) {
83 return false;
84 }
85 }
86 return false;
87 }
88 else {
89 return par1.size() < par2.size();
90 }
91 }
92
93 }
94