00001 #ifndef VIS_G4_CORE_VIS_G4_PATH_H
00002 # define VIS_G4_CORE_VIS_G4_PATH_H
00003
00004
00005
00006 # include "VisGeant4/VisG4Core/interface/config.h"
00007 # include <classlib/utils/DebugAids.h>
00008 # include <vector>
00009 # include <utility>
00010 # include <iosfwd>
00011
00012
00013
00014
00015
00016 class VisG4Path;
00017 class logstream;
00018
00019
00020
00021
00022 std::ostream &operator<< (std::ostream &out, const VisG4Path &path);
00023 logstream &operator<< (logstream &out, const VisG4Path &path);
00024
00025
00026
00028 class VIS_G4_CORE_API VisG4Path
00029 {
00030 class Node
00031 {
00032 public:
00033 Node (void);
00034 Node (Node *parent, short daughter, short replica);
00035 ~Node (void);
00036
00037 void ref (void);
00038 void unref (void);
00039
00040 short daughter (void) const;
00041 short replica (void) const;
00042
00043 Node * parent (void) const;
00044 Node * get (short daughter, short replica);
00045 static Node * root (void);
00046
00047 private:
00048 Node *m_parent;
00049 Node *m_sibling;
00050 Node *m_children;
00051 unsigned m_refs;
00052 short m_daughter;
00053 short m_replica;
00054
00055 static Node *s_root;
00056 };
00057
00058 public:
00059 VisG4Path (void);
00060 VisG4Path (short daughter, short replica);
00061 VisG4Path (const VisG4Path &prefix, short daughter, short replica);
00062 VisG4Path (const VisG4Path &x);
00063 VisG4Path &operator= (const VisG4Path &x);
00064 ~VisG4Path (void);
00065
00066 VisG4Path prefix (void) const;
00067 bool empty (void) const;
00068 short daughter (void) const;
00069 short replica (void) const;
00070
00071 bool operator== (const VisG4Path &path) const;
00072 bool operator!= (const VisG4Path &path) const;
00073 bool operator< (const VisG4Path &path) const;
00074 bool operator<= (const VisG4Path &path) const;
00075 bool operator> (const VisG4Path &path) const;
00076 bool operator>= (const VisG4Path &path) const;
00077
00078 private:
00079 VisG4Path (Node *node);
00080
00081 Node *m_node;
00082 };
00083
00084
00085
00086
00087 inline
00088 VisG4Path::Node::Node (void)
00089 : m_parent (this),
00090 m_sibling (0),
00091 m_children (0),
00092 m_refs (1),
00093 m_daughter (-1),
00094 m_replica (-1)
00095 {}
00096
00097 inline
00098 VisG4Path::Node::Node (Node *parent, short daughter, short replica)
00099 : m_parent (parent),
00100 m_sibling (parent->m_children),
00101 m_children (0),
00102 m_refs (0),
00103 m_daughter (daughter),
00104 m_replica (replica)
00105 { m_parent->m_refs++; m_parent->m_children = this; }
00106
00107 inline
00108 VisG4Path::Node::~Node (void)
00109 {
00110 if (m_parent->m_children == this)
00111 m_parent->m_children = m_sibling;
00112 else
00113 {
00114 Node *prev = m_parent->m_children;
00115 while (prev->m_sibling != this)
00116 prev = prev->m_sibling;
00117
00118 prev->m_sibling = m_sibling;
00119 }
00120
00121 if (--m_parent->m_refs == 0)
00122 delete m_parent;
00123 }
00124
00125 inline void
00126 VisG4Path::Node::ref (void)
00127 { m_refs++; }
00128
00129 inline void
00130 VisG4Path::Node::unref (void)
00131 {
00132 if (--m_refs == 0)
00133 delete this;
00134 }
00135
00136 inline short
00137 VisG4Path::Node::daughter (void) const
00138 { return m_daughter; }
00139
00140 inline short
00141 VisG4Path::Node::replica (void) const
00142 { return m_replica; }
00143
00144 inline VisG4Path::Node *
00145 VisG4Path::Node::parent (void) const
00146 { return m_parent; }
00147
00148 inline VisG4Path::Node *
00149 VisG4Path::Node::get (short daughter, short replica)
00150 {
00151 Node *n = m_children;
00152 while (n && ! (n->m_daughter == daughter && n->m_replica == replica))
00153 n = n->m_sibling;
00154
00155 if (! n)
00156 n = new Node (this, daughter, replica);
00157
00158 return n;
00159 }
00160
00161 inline VisG4Path::Node *
00162 VisG4Path::Node::root (void)
00163 { return s_root; }
00164
00167 inline
00168 VisG4Path::VisG4Path (void)
00169 : m_node (Node::root ())
00170 { m_node->ref (); }
00171
00173 inline
00174 VisG4Path::VisG4Path (short daughter, short replica)
00175 : m_node (Node::root ()->get (daughter, replica))
00176 { m_node->ref (); }
00177
00179 inline
00180 VisG4Path::VisG4Path (const VisG4Path &prefix, short daughter, short replica)
00181 : m_node (prefix.m_node->get (daughter, replica))
00182 { m_node->ref (); }
00183
00185 inline
00186 VisG4Path::VisG4Path (Node *node)
00187 : m_node (node)
00188 { m_node->ref (); }
00189
00191 inline
00192 VisG4Path::VisG4Path (const VisG4Path &x)
00193 : m_node (x.m_node)
00194 { m_node->ref (); }
00195
00197 inline VisG4Path &
00198 VisG4Path::operator= (const VisG4Path &x)
00199 { x.m_node->ref (); m_node->unref (); m_node = x.m_node; return *this; }
00200
00202 inline
00203 VisG4Path::~VisG4Path (void)
00204 { m_node->unref (); }
00205
00208 inline VisG4Path
00209 VisG4Path::prefix (void) const
00210 { return VisG4Path (m_node->parent ()); }
00211
00213 inline bool
00214 VisG4Path::empty (void) const
00215 { return m_node == Node::root (); }
00216
00218 inline short
00219 VisG4Path::daughter (void) const
00220 { return m_node->daughter (); }
00221
00223 inline short
00224 VisG4Path::replica (void) const
00225 { return m_node->replica (); }
00226
00228 inline bool
00229 VisG4Path::operator== (const VisG4Path &path) const
00230 { return m_node == path.m_node; }
00231
00233 inline bool
00234 VisG4Path::operator!= (const VisG4Path &path) const
00235 { return m_node != path.m_node; }
00236
00241 inline bool
00242 VisG4Path::operator< (const VisG4Path &path) const
00243 { return m_node < path.m_node; }
00244
00249 inline bool
00250 VisG4Path::operator<= (const VisG4Path &path) const
00251 { return m_node <= path.m_node; }
00252
00257 inline bool
00258 VisG4Path::operator> (const VisG4Path &path) const
00259 { return m_node > path.m_node; }
00260
00265 inline bool
00266 VisG4Path::operator>= (const VisG4Path &path) const
00267 { return m_node >= path.m_node; }
00268
00269 #endif // VIS_G4_CORE_VIS_G4_PATH_H