Ophidian
 All Classes Namespaces Functions
lookup_table.h
1 /*
2  * Copyright 2016 Ophidian
3 Licensed to the Apache Software Foundation (ASF) under one
4 or more contributor license agreements. See the NOTICE file
5 distributed with this work for additional information
6 regarding copyright ownership. The ASF licenses this file
7 to you under the Apache License, Version 2.0 (the
8 "License"); you may not use this file except in compliance
9 with the License. You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing,
14 software distributed under the License is distributed on an
15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 KIND, either express or implied. See the License for the
17 specific language governing permissions and limitations
18 under the License.
19  */
20 
21 #ifndef SRC_TIMING_LOOKUP_TABLE_H_
22 #define SRC_TIMING_LOOKUP_TABLE_H_
23 
24 #include <boost/units/io.hpp>
25 #include <boost/units/systems/si.hpp>
26 #include <boost/units/systems/si/prefixes.hpp>
27 #include <iostream>
28 
29 namespace ophidian {
30 namespace timing {
31 
32 template<class RowType, class ColumnType, class ValueType>
33 class lookup_table {
34  std::vector<RowType> m_row_values;
35  std::vector<ColumnType> m_column_values;
36  std::vector<std::vector<ValueType> > m_values;
37 public:
38 
39  bool operator==(const lookup_table<RowType, ColumnType, ValueType> & lut) const
40  {
41  return (m_row_values == lut.m_row_values)&&(m_column_values==lut.m_column_values)&&(m_values==lut.m_values);
42  }
43 
44  lookup_table(std::size_t rows=0, std::size_t columns=0) :
45  m_row_values(rows), m_column_values(columns), m_values(rows,
46  std::vector<ValueType>(columns)) {
47 
48  }
49  virtual ~lookup_table() {
50 
51  }
52 
53  std::size_t row_count() const {
54  return m_row_values.size();
55  }
56  void row_value(std::size_t column, RowType value) {
57  m_row_values[column] = value;
58  }
59 
60  RowType row_value(std::size_t column) const {
61  return m_row_values[column];
62  }
63 
64  std::size_t column_count() const {
65  return m_column_values.size();
66  }
67 
68  void column_value(std::size_t row, ColumnType value) {
69  m_column_values[row] = value;
70  }
71 
72  ColumnType column_value(std::size_t row) const {
73  return m_column_values[row];
74  }
75 
76  ValueType at(std::size_t row, std::size_t column) const {
77  return m_values.at(row).at(column);
78  }
79 
80  void at(std::size_t row, std::size_t column, ValueType value) {
81  if(row >= m_values.size() || column >= m_values.front().size())
82  std::cout << "ERROO!!!" << std::endl;
83  m_values.at(row).at(column) = value;
84  }
85 
86  ValueType compute(RowType rv, ColumnType cv) const {
87 
88  if (m_values.size() == 1)
89  if (m_values.front().size() == 1)
90  return m_values.front().front();
91 
92  double wTransition, wLoad;
93  RowType y1, y2;
94 
95  ColumnType x1, x2;
96  ValueType t[2][2];
97  std::size_t row1, row2, column1, column2;
98  wTransition = 0.0f;
99  wLoad = 0.0f;
100 
101  row1 = m_row_values.size() - 2;
102  row2 = m_row_values.size() - 1;
103 
104  y1 = m_row_values[row1];
105  y2 = m_row_values[row2];
106 
107  // loads -- rows
108  for (size_t i = 0; i < m_row_values.size() - 1; i++) {
109  if (rv >= m_row_values[i] && rv <= m_row_values[i + 1]) {
110  row1 = i;
111  row2 = i + 1;
112  y1 = m_row_values[row1];
113  y2 = m_row_values[row2];
114  }
115  }
116 
117  // transitions -- columns
118  if (cv < m_column_values[0]) {
119  column1 = 0;
120  column2 = 1;
121  x1 = m_column_values[column1];
122  x2 = m_column_values[column2];
123  } else if (cv
124  > m_column_values[m_column_values.size() - 1]) {
125  column1 = m_column_values.size() - 2;
126  column2 = m_column_values.size() - 1;
127  x1 = m_column_values[column1];
128  x2 = m_column_values[column2];
129  } else {
130  for (size_t i = 0; i < m_column_values.size() - 1; i++) {
131  if (cv >= m_column_values[i]
132  && cv <= m_column_values[i + 1]) {
133  column1 = i;
134  column2 = i + 1;
135  x1 = m_column_values[column1];
136  x2 = m_column_values[column2];
137  }
138  }
139  }
140 
141  //equation for interpolation (Ref - ISPD Contest: http://www.ispd.cc/contests/12/ISPD_2012_Contest_Details.pdf), slide 17
142  wTransition = (cv - x1) / (x2 - x1);
143  wLoad = (rv - y1) / (y2 - y1);
144 
145  t[0][0] = m_values[row1][column1];
146  t[0][1] = m_values[row1][column2];
147  t[1][0] = m_values[row2][column1];
148  t[1][1] = m_values[row2][column2];
149 
150  return boost::units::quantity<boost::units::si::time>(((1 - wTransition) * (1 - wLoad) * t[0][0])
151  + (wTransition * (1 - wLoad) * t[0][1])
152  + ((1 - wTransition) * wLoad * t[1][0])
153  + (wTransition * wLoad * t[1][1]));
154 
155  }
156 
157 };
158 
159 } /* namespace timing */
160 } /* namespace openeda */
161 
162 #endif /* SRC_TIMING_LOOKUP_TABLE_H_ */
Definition: lookup_table.h:33