1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include "addressmap.hh"
#include <algorithm>
AddressMap::AddressMap()
: _items()
{
// pass...
}
AddressMap::AddressMap(const AddressMap &other)
: _items(other._items)
{
// pass...
}
AddressMap &
AddressMap::operator =(const AddressMap &other) {
_items = other._items;
return *this;
}
void
AddressMap::clear() {
_items.clear();
}
bool
AddressMap::add(uint32_t addr, uint32_t len, int idx) {
if (0 > idx)
idx = _items.size();
AddrMapItem item(addr, len, idx);
std::vector<AddrMapItem>::iterator at = std::lower_bound(_items.begin(), _items.end(), item);
if (_items.end() == at)
_items.push_back(item);
else
_items.insert(at, item);
return true;
}
bool
AddressMap::rem(uint32_t idx) {
std::vector<AddrMapItem>::iterator at = _items.begin();
for (; at!=_items.end(); at++) {
if (at->index == idx)
break;
}
if (_items.end() == at)
return false;
_items.erase(at);
return true;
}
bool
AddressMap::contains(uint32_t addr) const {
return 0 <= find(addr);
}
int
AddressMap::find(uint32_t addr) const {
std::vector<AddrMapItem>::const_iterator at = std::lower_bound(_items.begin(), _items.end(), addr);
if (_items.end() == at)
return _items.back().contains(addr) ? _items.back().index : -1;
if (at->contains(addr))
return at->index;
if (_items.begin() == at)
return -1;
--at;
return at->contains(addr) ? at->index : -1;
}
|