Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
maps.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // $Id: maps.h,v 1.6 2008/12/06 08:43:24 mtcampbe Exp $
24 
29 /* Author: Xiangmin Jiao */
30 
31 #ifndef __ROCCOM_MAPS_H__
32 #define __ROCCOM_MAPS_H__
33 
34 #include "roccom_basic.h"
35 #include "roccom_exception.h"
36 #include <list>
37 
38 COM_BEGIN_NAME_SPACE
39 
42 template <class Object>
43 class Roccom_map {
44  typedef std::vector<Object> I2O;
45  typedef std::map<std::string, int> N2I;
46 public:
47  typedef Object value_type;
48 
51 
53  int add_object( std::string name, Object t,
54  bool is_const=false) throw(COM_exception);
55 
57  void remove_object( std::string name,
58  bool is_const=false) throw(COM_exception);
59 
61  bool is_immutable(int i) const
62  { return names[i].find(" (const)")!=std::string::npos; }
63 
65  const Object &operator[](int i) const throw(COM_exception) {
66  if ( i>=(int)i2o.size()) throw COM_exception( COM_UNKNOWN_ERROR);
67  return i2o[i];
68  }
69 
70  Object &operator[](int i) throw(COM_exception) {
71  if ( i>=(int)i2o.size()) throw COM_exception( COM_UNKNOWN_ERROR);
72  return i2o[i];
73  }
74 
76  const std::string &name( int i) const { return names[i]; }
77 
78  int size() const { return names.size(); }
79 
80  std::pair<int,Object *> find( const std::string &name, bool is_const=false) {
81  N2I::iterator it=n2i.find( is_const?name+"(const)":name);
82  if ( it == n2i.end())
83  return std::pair<int,Object *>(-1,NULL);
84  else
85  return std::pair<int,Object *>(it->second, &i2o[ it->second]);
86  }
87 
88  std::vector<std::string> get_names() { return names; }
89 protected:
90  I2O i2o;
91  N2I n2i;
92  std::list<int> salvaged;
93  std::vector<std::string> names;
94 };
95 
96 template <class Object>
97 int Roccom_map<Object>::add_object( std::string name, Object t,
98  bool is_const) throw(COM_exception)
99 {
100  if (is_const) name.append( " (const)");
101 
102  N2I::iterator it = n2i.find(name);
103  int i = ( it == n2i.end()) ? -1 : it->second;
104 
105  if ( i>0)
106  { i2o[i] = t; }
107  else if ( salvaged.empty()) {
108  i=i2o.size(); n2i[name] = i;
109  i2o.push_back( t); names.push_back( name);
110  }
111  else {
112  i=salvaged.front(); salvaged.pop_front();
113  n2i[name] = i; i2o[i] = t; names[i] = name;
114  }
115  return i;
116 }
117 
118 template <class Object>
119 void Roccom_map<Object>::remove_object( std::string name,
120  bool is_const) throw(COM_exception) {
121  if (is_const) name.append( " (const)");
122 
123  N2I::iterator it = n2i.find(name);
124  if ( it == n2i.end()) throw COM_exception( COM_UNKNOWN_ERROR);
125  salvaged.push_back( it->second);
126  n2i.erase(it);
127 }
128 
129 class Function;
130 
134 class Function_map : protected Roccom_map<Function*> {
136 public:
138  int add_object( const std::string &n, Function *t) {
139  unsigned int i = Roccom_map<Function*>::add_object( n, t);
140  if ( i+1>verbs.size()) {
141  verbs.resize(i+1, false);
142  wtimes_self.resize(i+1, 0.);
143  wtimes_tree.resize(i+1, 0.);
144  counts.resize(i+1, 0);
145  }
146  return i;
147  }
148 
149  using Base::name;
150  using Base::operator[];
151  using Base::size;
152 
153  std::vector<char> verbs;
154  std::vector<double> wtimes_self;
155  std::vector<double> wtimes_tree;
156  std::vector<int> counts;
157 };
158 
159 COM_END_NAME_SPACE
160 
161 #endif
162 
163 
164 
165 
166 
167 
A map functions.
Definition: maps.h:134
if(dy > dx)
A Function object corresponds to a function member of a window.
Definition: Function.h:49
N2I n2i
Mapping from names to indices.
Definition: maps.h:91
Roccom_map()
Definition: maps.h:49
int add_object(const std::string &n, Function *t)
Insert a function into the table.
Definition: maps.h:138
std::vector< std::string > get_names()
Definition: maps.h:88
std::pair< int, Object * > find(const std::string &name, bool is_const=false)
Definition: maps.h:80
int size() const
Definition: maps.h:78
std::vector< char > verbs
Whether verbose is on.
Definition: maps.h:153
const std::string & name(int i) const
Name of the object.
Definition: maps.h:76
std::map< std::string, int > N2I
Mapping from names to indices.
Definition: maps.h:45
This file contains some definitions of macros and constants for Roccoms.
Encapsulates the states of an exception.
~Roccom_map()
Definition: maps.h:50
I2O i2o
Mapping from index to objects.
Definition: maps.h:90
std::vector< double > wtimes_tree
Accumulator of wall-clock time spent by itself and those functions called by it.
Definition: maps.h:155
Object value_type
Definition: maps.h:47
std::vector< double > wtimes_self
Accumulator of wall-clock time spent by itself excluding functions called by it.
Definition: maps.h:154
void remove_object(std::string name, bool is_const=false)
Remove an object from the table.
Definition: maps.h:119
const Object & operator[](int i) const
Access an object using its handle.
Definition: maps.h:65
std::vector< std::string > names
Name of the objects.
Definition: maps.h:93
std::vector< int > counts
Counts of the number of calls.
Definition: maps.h:156
Roccom_map< Function * > Base
Definition: maps.h:135
blockLoc i
Definition: read.cpp:79
const NT & n
bool is_immutable(int i) const
whether the object mutable
Definition: maps.h:61
Supports mapping from names to handles and vice-versa for a module, window, function, or attribute.
Definition: maps.h:43
int add_object(std::string name, Object t, bool is_const=false)
Insert an object into the table.
Definition: maps.h:97
Object & operator[](int i)
Definition: maps.h:70
std::list< int > salvaged
List of salvaged indices.
Definition: maps.h:92
std::vector< Object > I2O
Mapping from indices to objects.
Definition: maps.h:44