LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Memory.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
24 // ThresholdMemory.cc
25 //
26 // Contact: Ray Plante
27 //
29 
31 #include <boost/tokenizer.hpp>
32 #include <ostream>
33 
34 using namespace std;
35 
36 namespace lsst {
37 namespace pex {
38 namespace logging {
39 namespace threshold {
40 
41 /*
42  * create a hierarchical container for threshold data
43  */
44 Family::Family(int defaultThreshold)
45  : _thresh(defaultThreshold), _children()
46 { }
47 
48 Family* Family::ensureDescendant(tokenizer::iterator toptoken,
49  const tokenizer::iterator& end)
50 {
51  if (_children == 0)
52  // we have no children yet, so just make the descendents
53  return makeDescendants(toptoken, end);
54 
55  // look for a child matching the top field in the name
56  ChildMap::iterator out = _children->find(*toptoken);
57 
58  // if name is not found create all descendants
59  if (out == _children->end())
60  return makeDescendants(toptoken, end);
61 
62  // if this was the last token return the found child
63  if (++toptoken == end)
64  return out->second;
65 
66  // look for the the remainder of the name hierarchy
67  return out->second->ensureDescendant(toptoken, end);
68 }
69 
70 const Family* Family::findDescendant(tokenizer::iterator toptoken,
71  const tokenizer::iterator& end,
72  bool orNearest) const
73 {
74  if (_children == 0)
75  return ((orNearest && _thresh != INHERIT) ? this : 0);
76 
77  // look for a child matching the top field in the name
78  ChildMap::const_iterator found = _children->find(*toptoken);
79 
80  // if name is not found, return null
81  if (found == _children->end())
82  return ((orNearest && _thresh != INHERIT) ? this : 0);
83 
84  const Family *out = found->second;
85  if (++toptoken != end) out = out->findDescendant(toptoken, end, orNearest);
86  if (out != 0 && out->_thresh == INHERIT) out = 0;
87 
88  if (out == 0 && orNearest && _thresh != INHERIT) out = this;
89  return out;
90 }
91 
92 Family* Family::makeDescendants(tokenizer::iterator toptoken,
93  const tokenizer::iterator& end)
94 {
95  if (_children == 0) _children = new ChildMap();
96 
97  Family *next = new Family();
98  _children->insert(ChildMap::value_type(*toptoken, next));
99 
100  if (++toptoken == end) return next;
101 
102  return next->makeDescendants(toptoken, end);
103 }
104 
108 void Family::setThresholdFor(tokenizer::iterator toptoken,
109  const tokenizer::iterator& end,
110  int threshold)
111 {
112  Family *nfd = ensureDescendant(toptoken, end);
113  nfd->_thresh = threshold;
114 }
115 
120 void Family::resetThresholdFor(tokenizer::iterator toptoken,
121  const tokenizer::iterator& end)
122 {
123  Family *nfd = const_cast<Family*>(findDescendant(toptoken, end));
124  if (nfd != 0) nfd->_thresh = INHERIT;
125 }
126 
128 
129  if (_children == 0) return;
130 
131  for(ChildMap::iterator it = _children->begin();
132  it != _children->end();
133  ++it)
134  {
135  if (it->second != 0) delete it->second;
136  }
137 
138  delete _children;
139  _children = 0;
140 }
141 
144 }
145 
146 void Family::printDescThresholds(std::ostream& out,
147  const std::string& prefix) const
148 {
149  if (_children == 0) return;
150 
151  int i;
152  ChildMap::const_iterator it;
153  for(it = _children->begin(); it != _children->end(); ++it) {
154  out << prefix << it->first;
155  if (it->second != 0 && it->second->_thresh != INHERIT) {
156  for(i = prefix.length()+it->first.length(); i < 20; ++i)
157  out << ' ';
158  if (it->second->_thresh >= 0 && it->second->_thresh < 10)
159  out << ' ';
160  out << it->second->_thresh;
161  }
162  out << endl;
163 
164  if (it->second != 0)
165  it->second->printDescThresholds(out, prefix+' ');
166  }
167 }
168 
169 /* ******************************************************************* */
170 
171 Memory::Memory(const std::string& delims)
172  : _tree(), _sep(delims.c_str())
173 { }
174 
178 void Memory::printThresholds(std::ostream& out) {
179  out << "(root) ";
180  int top = _tree.getThreshold();
181  if (top < 10 && top >= 0) out << ' ';
182  out << top << endl;
183 
184  _tree.printDescThresholds(out, " ");
185 }
186 
187 
188 
189 }}}} // end lsst::pex::logging::threshold
Memory(const std::string &delims=".")
Definition: Memory.cc:171
void setThresholdFor(tokenizer::iterator toptoken, const tokenizer::iterator &end, int threshold)
Definition: Memory.cc:108
std::map< std::string, Family * > ChildMap
Definition: Memory.h:159
void printDescThresholds(std::ostream &out, const std::string &prefix) const
Definition: Memory.cc:146
Family(int defaultThreshold=INHERIT)
Definition: Memory.cc:44
void printThresholds(std::ostream &out)
Definition: Memory.cc:178
Family * makeDescendants(tokenizer::iterator toptoken, const tokenizer::iterator &end)
Definition: Memory.cc:92
Family * ensureDescendant(tokenizer::iterator toptoken, const tokenizer::iterator &end)
Definition: Memory.cc:48
definition of the Memory class
void resetThresholdFor(tokenizer::iterator toptoken, const tokenizer::iterator &end)
Definition: Memory.cc:120
A hierarchical tree structure for holding mappings of names to threshold values.
Definition: Memory.h:59
const Family * findDescendant(tokenizer::iterator toptoken, const tokenizer::iterator &end, bool orNearest=false) const
Definition: Memory.cc:70