StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StlUtilities.h
1 //StlUtilities.h
2 //M.L. Miller (Yale Software)
3 //07/01
4 
5 #ifndef StlUtilities_HH
6 #define StlUtilities_HH
7 
8 #include "Stiostream.h"
9 
10 #include <algorithm>
11 using std::for_each;
12 using std::find_if;
13 using std::sort;
14 
15 #include <string>
16 using std::string;
17 
18 #include "StiCompositeTreeNode.h"
19 
20 template <class T>
22 {
23  void operator()(const StiCompositeTreeNode<T>* node) {
24  cout <<node->getName()<<endl;
25  }
26 };
27 
28 template <class T>
30 {
31 public:
32  SameNodeName(const string& name) : mname(name) {};
33  bool operator()(const StiCompositeTreeNode<T>* rhs) {
34  return mname == rhs->getName();
35  }
36 
37 private:
38  SameNodeName(); //Not implemented
39  string mname;
40 };
41 
42 template <class T>
44 {
45  void operator()(const StiCompositeTreeNode<T>* node) {
46  T* data = node->getData();
47  //cout <<node->getName()<<endl;
48  if (data) {cout <<" "<<(*data)<<endl;}
49  }
50 };
51 
52 template <class T>
54 {
55  bool operator() (const StiCompositeTreeNode<T>* lhs, const StiCompositeTreeNode<T>* rhs) {
56  return ( lhs->getOrderKey().key < rhs->getOrderKey().key );
57  }
58 
59 };
60 
61 template <class T>
63 {
64  bool operator() (const StiCompositeTreeNode<T>* rhs) {
65  return (morderKey.key == rhs->getOrderKey().key );
66  }
67  StiOrderKey morderKey;
68 };
69 
70 template <class T>
71 struct SameName
72 {
73  bool operator() (const StiCompositeTreeNode<T>* rhs) {
74  return (mname == rhs->getName());
75  }
76  string mname;
77 };
78 
79 template <class T>
80 class IndexNode
81 {
82 public:
83  IndexNode(unsigned int i) : counter(i) {};
84 
85  void operator() (StiCompositeTreeNode<T>* node) {
86  //Order key is not meant to be altered by the average user,
87  //so we have to copy and re-set.
88 
89  StiOrderKey newKey = node->getOrderKey();
90  newKey.index= counter++;
91  node->setOrderKey(newKey);
92  }
93 
94 private:
95  IndexNode();
96  unsigned int counter;
97 };
98 
99 template <class T>
101 {
102  void operator() (StiCompositeTreeNode<T>* node) {
103  if (node->getChildCount()>0) {
104  //swap this to a one-line call, once it works:
105  //IndexNode<data_t> indexer(0);
106  for_each(node->begin(), node->end(), IndexNode<T>(0) );
107  for_each(node->begin(), node->end(), IndexDaughters() );
108  }
109  }
110 };
111 
112 template <class T>
114 {
115  void operator() (StiCompositeTreeNode<T>* node) {
116  if ( node->getChildCount()>0 )
117  {
118  sort(node->begin(), node->end(), NodeLessThan<T>());
119  for_each(node->begin(), node->end(), SortDaughters() );
120  }
121  }
122 };
123 
124 template <class T>
126 {
127  void operator() (StiCompositeTreeNode<T>* node) {
128  //cout <<"Parent: "<<node->getName()<<endl;
129  cout <<"Name: "<<node->getName()<<"\tOrderKey: "<<node->getOrderKey()<<endl;
130  if (node->getChildCount()>0) {
131  cout <<" Daughters"<<endl;
132  for_each(node->begin(), node->end(), RecursiveStreamNode<T>());
133  }
134  }
135 };
136 
137 template <class T>
139 {
141  inline bool operator() (const TNode* lhs, TNode* rhs) {
142  return lhs->getData()->getName() < rhs->getData()->getName();
143  }
144 };
145 
146 template <class T>
148 {
149 public:
150  typedef vector<StiCompositeTreeNode<T>*> tvector_t;
151 
152  LeafFinder(tvector_t& v) : vec(v) {};
153 
154  void operator()(StiCompositeTreeNode<T>* node) {
155  if (node->getChildCount()>0) {
156  (*this) = for_each(node->begin(), node->end(), *this);
157  }
158  else {
159  vec.push_back(node );
160  }
161  }
162 
163  void operator=(const LeafFinder& rhs) {copyToThis(rhs);}
164 
165  tvector_t& vec;
166 
167 private:
168  void copyToThis(const LeafFinder& rhs) {vec = rhs.vec;}
169 
170  LeafFinder(); //Not implemented
171 };
172 
173 template <class T>
174 inline T gFindClosestOrderKey(T begin, T end, const StiOrderKey& findThis)
175 {
176  T where = end;
177  double min = 1.e100;
178  for (T it=begin; it!=end; ++it) {
179  double val = fabs(findThis.key-(*it)->getOrderKey().key);
180  if (val<min) {
181  min=val;
182  where = it;
183  }
184  }
185  return where;
186 }
187 
188 template <class T>
189 struct SameData
190 {
191  SameData(T* data) : thedata(data) {};
192  const T* thedata;
193  bool operator()(const StiCompositeTreeNode<T>* rhs) {
194  return (thedata == rhs->getData());
195  }
196 };
197 
198 //Stream a pointer
199 template <class T>
201 {
202  void operator()(const T* val) {
203  cout <<*val<<endl;
204  }
205 };
206 
207 #endif
const string & getName() const
Return the name of the node.
T * getData() const
Return a (non-const!) pointer to the data hung on this node.
unsigned int getChildCount() const
Return the number of children that belong to this node.
const StiOrderKey & getOrderKey() const
Return a reference to the the orderkey of this node.
vec_type::iterator begin()
Provide random access iterator to the beginning of the vector of children.
vec_type::iterator end()
Provide random access iterator to the end of the vector of children.
void setOrderKey(const StiOrderKey &)
Set the order-key for the node.