My Project
partmap.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  * NOTE: very generic partition container
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
19 #ifndef __PARTMAP__
20 #define __PARTMAP__
21 
22 #include <map>
23 
47 template<typename _linetype,typename _valuetype>
48 class partmap {
49 public:
50  typedef std::map<_linetype,_valuetype> maptype;
51  typedef typename maptype::iterator iterator;
52  typedef typename maptype::const_iterator const_iterator;
53 private:
54  maptype database;
55  _valuetype defaultvalue;
56 public:
57  _valuetype &getValue(const _linetype &pnt);
58  const _valuetype &getValue(const _linetype &pnt) const;
59  const _valuetype &bounds(const _linetype &pnt,_linetype &before,_linetype &after,int &valid) const;
60  _valuetype &split(const _linetype &pnt);
61  const _valuetype &defaultValue(void) const { return defaultvalue; }
62  _valuetype &defaultValue(void) { return defaultvalue; }
63  _valuetype & clearRange(const _linetype &pnt1,const _linetype &pnt2);
64  const_iterator begin(void) const { return database.begin(); }
65  const_iterator end(void) const { return database.end(); }
66  iterator begin(void) { return database.begin(); }
67  iterator end(void) { return database.end(); }
68  const_iterator begin(const _linetype &pnt) const { return database.lower_bound(pnt); }
69  iterator begin(const _linetype &pnt) { return database.lower_bound(pnt); }
70  void clear(void) { database.clear(); }
71  bool empty(void) const { return database.empty(); }
72 };
73 
79 template<typename _linetype,typename _valuetype>
81  getValue(const _linetype &pnt)
82 
83  {
84  iterator iter;
85 
86  iter = database.upper_bound(pnt);
87  if (iter == database.begin())
88  return defaultvalue;
89  --iter;
90  return (*iter).second;
91  }
92 
98 template<typename _linetype,typename _valuetype>
99  const _valuetype &partmap<_linetype,_valuetype>::
100  getValue(const _linetype &pnt) const
101 
102  {
103  const_iterator iter;
104 
105  iter = database.upper_bound(pnt);
106  if (iter == database.begin())
107  return defaultvalue;
108  --iter;
109  return (*iter).second;
110  }
111 
115 template<typename _linetype,typename _valuetype>
117  split(const _linetype &pnt)
118 
119  {
120  iterator iter;
121 
122  iter = database.upper_bound(pnt);
123  if (iter != database.begin()) {
124  --iter;
125  if ((*iter).first == pnt) // point matches exactly
126  return (*iter).second; // Return old ref
127  _valuetype &newref( database[pnt] ); // Create new ref at point
128  newref = (*iter).second; // Copy of original partition value
129  return newref;
130  }
131  _valuetype &newref( database[pnt] ); // Create new ref at point
132  newref = defaultvalue; // Copy of defaultvalue
133  return newref;
134  }
135 
142 template<typename _linetype,typename _valuetype>
144  clearRange(const _linetype &pnt1,const _linetype &pnt2)
145  {
146  split(pnt1);
147  split(pnt2);
148  iterator beg = begin(pnt1);
149  iterator end = begin(pnt2);
150 
151  _valuetype &ref( (*beg).second );
152  ++beg;
153  database.erase(beg,end);
154  return ref;
155  }
156 
170 template<typename _linetype,typename _valuetype>
171  const _valuetype &partmap<_linetype,_valuetype>::
172  bounds(const _linetype &pnt,_linetype &before,_linetype &after,int &valid) const
173  {
174  if (database.empty()) {
175  valid = 3;
176  return defaultvalue;
177  }
178  const_iterator iter,enditer;
179 
180  enditer = database.upper_bound(pnt);
181  if (enditer != database.begin()) {
182  iter = enditer;
183  --iter;
184  before = (*iter).first;
185  if (enditer == database.end())
186  valid = 2; // No upperbound
187  else {
188  after = (*enditer).first;
189  valid = 0; // Fully bounded
190  }
191  return (*iter).second;
192  }
193  valid = 1; // No lowerbound
194  after = (*enditer).first;
195  return defaultvalue;
196  }
197 #endif
198 
199 #if 0
200 
201 #include <iostream>
202 using namespace std;
203 
204 int main(int argc,char **argv)
205 
206 {
208 
209  data.defaultValue() = 0;
210  data.split(5) = 5;
211  data.split(2) = 2;
212  data.split(3) = 4;
213  data.split(3) = 3;
214 
215  cout << data.getValue(6) << endl;
216  cout << data.getValue(8) << endl;
217  cout << data.getValue(4) << endl;
218  cout << data.getValue(1) << endl;
219 
221 
222  iter = data.begin(3);
223  while(iter!=data.end()) {
224  cout << (*iter).second << endl;
225  ++iter;
226  }
227 }
228 #endif
229 
std::map< _linetype, _valuetype > maptype
Defining the map from split points to value objects.
Definition: partmap.hh:50
const _valuetype & defaultValue(void) const
Get the default value object.
Definition: partmap.hh:61
_valuetype & split(const _linetype &pnt)
Introduce a new split point.
Definition: partmap.hh:117
void clear(void)
Clear all split points.
Definition: partmap.hh:70
iterator begin(void)
Beginning of split points.
Definition: partmap.hh:66
maptype::iterator iterator
A partmap iterator is an iterator into the map.
Definition: partmap.hh:51
_valuetype & getValue(const _linetype &pnt)
Get the value object at a point.
Definition: partmap.hh:81
_valuetype & defaultValue(void)
Get the default value object.
Definition: partmap.hh:62
maptype::const_iterator const_iterator
A constant iterator.
Definition: partmap.hh:52
_valuetype & clearRange(const _linetype &pnt1, const _linetype &pnt2)
Clear a range of split points.
Definition: partmap.hh:144
const_iterator begin(void) const
Beginning of split points.
Definition: partmap.hh:64
A map from a linear space to value objects.
Definition: partmap.hh:48
const _valuetype & bounds(const _linetype &pnt, _linetype &before, _linetype &after, int &valid) const
Get the value object for a given point and return the range over which the value object applies...
Definition: partmap.hh:172
iterator begin(const _linetype &pnt)
Get first split point after given point.
Definition: partmap.hh:69
const_iterator end(void) const
End of split points.
Definition: partmap.hh:65
const_iterator begin(const _linetype &pnt) const
Get first split point after given point.
Definition: partmap.hh:68
bool empty(void) const
Return true if there are no split points.
Definition: partmap.hh:71
iterator end(void)
End of split points.
Definition: partmap.hh:67