OpenASIP  2.0
CostDBEntry.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file CostDBEntry.cc
26  *
27  * Implementation of CostDBEntry class.
28  *
29  * @author Tommi Rantanen 2003 (tommi.rantanen-no.spam-tut.fi)
30  * @author Jari Mäntyneva 2005 (jari.mantyneva-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include "CostDBEntry.hh"
35 #include "Application.hh"
36 #include "CostDBEntryStatsRF.hh"
37 #include "CostDBEntryStatsFU.hh"
38 
39 /**
40  * Constructor.
41  *
42  * @param key An entry key.
43  */
45  entryKey_(key) {
46 }
47 
48 /**
49  * Constructor.
50  *
51  * Combines two entries into one using weighter as a weighting factor
52  * on interpolating the statistics. Also replacing weighter field in
53  * entry key.
54  *
55  * @param entry1 First entry.
56  * @param entry2 Second entry.
57  * @param weighter A field.
58  */
60  const CostDBEntry& entry1,
61  const CostDBEntry& entry2,
62  const EntryKeyField& weighter) {
63 
64  EntryKeyField field1 = entry1.keyFieldOfType(*weighter.type());
65  EntryKeyField field2 = entry2.keyFieldOfType(*weighter.type());
66  double coefficient = weighter.coefficient(field1, field2);
67 
68  entryKey_ = entry1.entryKey_->copy();
69  entryKey_->replaceField(new EntryKeyField(weighter));
70  for (StatsTable::const_iterator i = entry1.entryStats_.begin();
71  i != entry1.entryStats_.end(); i++) {
72  for (StatsTable::const_iterator j = entry2.entryStats_.begin();
73  j != entry2.entryStats_.end(); j++) {
74  // newStats cannot be base class type if stats are for FU or RF
75  if (dynamic_cast<CostDBEntryStatsRF*>(*i) &&
76  dynamic_cast<CostDBEntryStatsRF*>(*j)) {
77  CostDBEntryStatsRF* newStats = new CostDBEntryStatsRF(
78  *dynamic_cast<CostDBEntryStatsRF*>(*i),
79  *dynamic_cast<CostDBEntryStatsRF*>(*j), coefficient);
80  addStatistics(newStats);
81  } else if (dynamic_cast<CostDBEntryStatsFU*>(*i) &&
82  dynamic_cast<CostDBEntryStatsFU*>(*j)) {
83  CostDBEntryStatsFU* newStats = new CostDBEntryStatsFU(
84  *dynamic_cast<CostDBEntryStatsFU*>(*i),
85  *dynamic_cast<CostDBEntryStatsFU*>(*j), coefficient);
86  addStatistics(newStats);
87  } else { // if the entry is Bus or Socket
88  CostDBEntryStats* newStats =
89  new CostDBEntryStats(*(*i), *(*j), coefficient);
90  addStatistics(newStats);
91  }
92  }
93  }
94 }
95 
96 /**
97  * Destructor.
98  */
100  assert(entryKey_ != NULL);
101  delete entryKey_;
102  entryKey_ = NULL;
103 
104  for (StatsTable::iterator i = entryStats_.begin();
105  i != entryStats_.end(); i++) {
106  assert(*i != NULL);
107  delete *i;
108  }
109 }
110 
111 /**
112  * Copies the entry.
113  *
114  * Client is responsible of deallocating the memory reserved for the
115  * returned object.
116  *
117  * @return A copy of the entry.
118  */
121  CostDBEntry* newEntry = new CostDBEntry(entryKey_->copy());
122 
123  for (StatsTable::const_iterator i = entryStats_.begin();
124  i != entryStats_.end(); i++) {
125  newEntry->addStatistics((*i)->copy());
126  }
127 
128  return newEntry;
129 }
130 
131 /**
132  * Returns type of the entry.
133  *
134  * @return Type.
135  */
136 const EntryKeyProperty*
138  return entryKey_->type();
139 }
140 
141 /**
142  * Returns demanded field of the entry.
143  *
144  * @param type Type of the field.
145  * @return Demanded entry field.
146  */
149  return entryKey_->keyFieldOfType(type);
150 }
151 
152 /**
153  * Returns demanded field of the entry.
154  *
155  * @param type Type of the field.
156  * @return Demanded entry field.
157  */
159 CostDBEntry::keyFieldOfType(std::string type) const {
160  return entryKey_->keyFieldOfType(type);
161 }
162 
163 /**
164  * Returns the number of fields in an entry.
165  *
166  * @return The number of fields in an entry.
167  */
168 int
170  return entryKey_->fieldCount();
171 }
172 
173 /**
174  * Returns the field found on the given index.
175  *
176  * The index must be between 0 and the number of entry key - 1.
177  *
178  * @param index Index.
179  * @return The field found on the given index.
180  */
181 const EntryKeyField&
182 CostDBEntry::field(int index) const {
183  return entryKey_->field(index);
184 }
185 
186 /**
187  * Replaces certain field of the entry.
188  *
189  * @param newField A field.
190  */
191 void
193  entryKey_->replaceField(newField);
194 }
195 
196 /**
197  * Checks whether two entries have equal key.
198  *
199  * @param entry Database entry to compare with.
200  * @return True if two entries have equal key, false otherwise.
201  */
202 bool
204  return entryKey_->isEqual(*entry.entryKey_);
205 }
206 
207 /**
208  * Add new statistics into this entry.
209  *
210  * @param newStats Statistics to be added.
211  */
212 void
214  entryStats_.push_back(newStats);
215 }
216 
217 
218 /**
219  * Returns the number of statistics in the entry.
220  *
221  * @return Number of statistics in the entry.
222  */
223 int
225  return entryStats_.size();
226 }
227 
228 /**
229  * Returns the statistics according to given index.
230  *
231  * @param index The index of the statistics.
232  * @return The statistics according to given index.
233  * @throw OutOfRange Index is out of bounds.
234  */
235 const CostDBEntryStats&
236 CostDBEntry::statistics(int index) const {
237  if (index < 0 || index >= statisticsCount()) {
238  throw OutOfRange(__FILE__, __LINE__, "CostDBEntry::statistics");
239  }
240  return *entryStats_[index];
241 }
CostDBEntry::replaceField
void replaceField(EntryKeyField *newField)
Definition: CostDBEntry.cc:192
CostDBEntry::statisticsCount
int statisticsCount() const
Definition: CostDBEntry.cc:224
EntryKeyFieldProperty
Definition: EntryKeyFieldProperty.hh:47
CostDBEntryKey::type
const EntryKeyProperty * type() const
CostDBEntry::fieldCount
int fieldCount() const
Definition: CostDBEntry.cc:169
OutOfRange
Definition: Exception.hh:320
CostDBEntryKey::fieldCount
int fieldCount() const
CostDBEntry::addStatistics
void addStatistics(CostDBEntryStats *newStats)
Definition: CostDBEntry.cc:213
EntryKeyField
Definition: EntryKeyField.hh:45
CostDBEntryStatsRF
Definition: CostDBEntryStatsRF.hh:45
CostDBEntry::copy
CostDBEntry * copy() const
Definition: CostDBEntry.cc:120
CostDBEntry::entryStats_
StatsTable entryStats_
Statistics of the entry.
Definition: CostDBEntry.hh:82
assert
#define assert(condition)
Definition: Application.hh:86
CostDBEntry
Definition: CostDBEntry.hh:52
CostDBEntryStatsFU
Definition: CostDBEntryStatsFU.hh:44
CostDBEntryKey::replaceField
void replaceField(EntryKeyField *newField)
Definition: CostDBEntryKey.cc:105
Application.hh
EntryKeyField::coefficient
double coefficient(const EntryKeyField &field1, const EntryKeyField &field2) const
Definition: EntryKeyField.cc:130
EntryKeyField::type
const EntryKeyFieldProperty * type() const
CostDBEntryKey::isEqual
bool isEqual(const CostDBEntryKey &entryKey) const
Definition: CostDBEntryKey.cc:152
CostDBEntry::type
const EntryKeyProperty * type() const
Definition: CostDBEntry.cc:137
CostDBEntryKey::field
const EntryKeyField & field(int index) const
Definition: CostDBEntryKey.cc:181
CostDBEntry::entryKey_
CostDBEntryKey * entryKey_
Key of the entry.
Definition: CostDBEntry.hh:80
CostDBEntry.hh
EntryKeyProperty
Definition: EntryKeyProperty.hh:57
CostDBEntry::statistics
const CostDBEntryStats & statistics(int index) const
Definition: CostDBEntry.cc:236
CostDBEntryStatsRF.hh
CostDBEntry::~CostDBEntry
virtual ~CostDBEntry()
Definition: CostDBEntry.cc:99
CostDBEntryKey::copy
CostDBEntryKey * copy() const
Definition: CostDBEntryKey.cc:69
CostDBEntryKey::keyFieldOfType
EntryKeyField keyFieldOfType(const EntryKeyFieldProperty &fieldType) const
Definition: CostDBEntryKey.cc:123
CostDBEntryStatsFU.hh
CostDBEntryKey
Definition: CostDBEntryKey.hh:52
CostDBEntry::keyFieldOfType
EntryKeyField keyFieldOfType(const EntryKeyFieldProperty &type) const
Definition: CostDBEntry.cc:148
CostDBEntry::field
const EntryKeyField & field(int index) const
Definition: CostDBEntry.cc:182
CostDBEntryStats
Definition: CostDBEntryStats.hh:46
CostDBEntry::CostDBEntry
CostDBEntry(CostDBEntryKey *key)
Definition: CostDBEntry.cc:44
CostDBEntry::isEqualKey
bool isEqualKey(const CostDBEntry &entry) const
Definition: CostDBEntry.cc:203