42 #ifndef _DWMIPV4ROUTES_HH_ 43 #define _DWMIPV4ROUTES_HH_ 50 #include <unordered_map> 67 struct OurIpv4AddressHash
117 template <
typename _valueT>
125 OurIpv4AddressHash> _RepSubType;
126 typedef typename _RepSubType::const_iterator const_iterator;
134 for (uint8_t i = 0; i < 33; ++i) {
135 _hashMaps[i].max_load_factor(.08);
144 for (uint8_t i = 0; i < 33; ++i)
145 _hashMaps[i].clear();
155 for (uint8_t i = 0; i < 33; ++i) {
156 if (! _hashMaps[i].empty()) {
172 typename _RepSubType::iterator iter =
174 if (iter == _hashMaps[prefix.
MaskLength()].end()) {
196 typename _RepSubType::iterator iter =
198 if (iter != _hashMaps[prefix.
MaskLength()].end()) {
213 if (! _hashMaps[prefix.
MaskLength()].empty()) {
214 typename _RepSubType::const_iterator iter =
216 if (iter != _hashMaps[prefix.
MaskLength()].end()) {
217 match = iter->second;
230 std::pair<Ipv4Prefix,_valueT> & match)
const 236 typename _RepSubType::const_iterator iter;
237 for (int8_t i = 32; i >= 0; --i) {
238 if (_hashMaps[i].empty())
241 iter = _hashMaps[i].find(lp.
Network());
242 if (iter != _hashMaps[i].end()) {
244 match.second = iter->second;
262 std::pair<Ipv4Prefix, const _valueT *> & match)
const 266 typename _RepSubType::const_iterator iter;
267 for (int8_t i = 32; i >= 0; --i) {
268 if (_hashMaps[i].empty())
271 iter = _hashMaps[i].find(lp.
Network());
272 if (iter != _hashMaps[i].end()) {
274 match.second = &(iter->second);
287 bool Find(
const Ipv4Address & ipAddr,
288 std::vector<std::pair<Ipv4Prefix,_valueT> > & matches)
const 290 if (! matches.empty())
293 typename _RepSubType::const_iterator iter;
295 for (int8_t i = 32; i >= 0; --i) {
296 if (_hashMaps[i].empty())
299 iter = _hashMaps[i].find(prefix.
Network());
300 if (iter != _hashMaps[i].end()) {
301 std::pair<Ipv4Prefix,_valueT> match(prefix, iter->second);
302 matches.push_back(match);
305 return(! matches.empty());
311 void MaxLoadFactor(
float loadFactor)
313 for (int8_t i = 32; i >= 0; --i) {
314 _hashMaps[i].max_load_factor(loadFactor);
326 for (int8_t i = 32; i >= 0; --i) {
327 if (_hashMaps[i] != r._hashMaps[i])
340 return(! (*
this == r));
349 for (uint8_t i = 0; i < 33; ++i)
350 rc += _hashMaps[i].size();
357 void HashSizes(std::vector<std::pair<uint8_t, uint32_t> > & sizes)
const 361 for (uint8_t i = 0; i < 33; ++i) {
362 if (! _hashMaps[i].empty()) {
363 sizes.push_back(std::pair<uint8_t,uint32_t>(i,_hashMaps[i].size()));
380 std::istream &
Read(std::istream & is)
388 std::ostream &
Write(std::ostream & os)
const 465 void SortByKey(std::vector<std::pair<Ipv4Prefix,_valueT>> & target,
466 bool ascending =
true)
const 468 if (! target.empty())
470 if (! this->_hashMaps.empty()) {
471 target.resize(this->Size());
472 typename std::vector<_RepSubType>::const_iterator iter =
473 this->_hashMaps.begin();
475 for (uint8_t hashNum = 0; hashNum < 33; ++hashNum) {
476 if (! this->_hashMaps[hashNum].empty()) {
477 typename _RepSubType::const_iterator hiter =
478 this->_hashMaps[hashNum].begin();
479 for ( ; hiter != iter->end(); ++hiter) {
480 target[pfx].first =
Ipv4Prefix(hiter->first, hashNum);
481 target[pfx].second = hiter->second;
486 if (! target.empty()) {
488 std::sort(target.begin(), target.end(), KeyLess());
491 std::sort(target.begin(), target.end(), KeyGreater());
506 void SortByValue(std::vector<std::pair<Ipv4Prefix,_valueT> > & target)
508 if (! target.empty())
510 if (! this->_hashMaps.empty()) {
511 target.resize(this->Size());
512 typename std::vector<_RepSubType>::const_iterator iter =
513 this->_hashMaps.begin();
515 for (uint8_t hashNum = 0; hashNum < 33; ++hashNum) {
516 if (! this->_hashMaps[hashNum].empty()) {
517 typename _RepSubType::const_iterator hiter =
518 this->_hashMaps[hashNum].begin();
519 for ( ; hiter != iter->end(); ++hiter) {
520 target[pfx].first =
Ipv4Prefix(hiter->first, hashNum);
521 target[pfx].second = hiter->second;
526 if (! target.empty())
527 std::sort(target.begin(), target.end(), ValueGreater());
540 for (int8_t hashNum = 32; hashNum > 1; --hashNum) {
541 typename _RepSubType::const_iterator hiter =
542 _hashMaps[hashNum].begin();
543 for ( ; hiter != _hashMaps[hashNum].end(); ++hiter) {
544 bool foundWider =
false;
545 for (int8_t widerHash = hashNum - 1; widerHash > 0; --widerHash) {
547 if (_hashMaps[widerHash].find(widerPfx.
Network())
548 != _hashMaps[widerHash].end()) {
555 rc += ((uint32_t)1 << (32 - hashNum));
565 bool operator () (
const std::pair<Ipv4Prefix,_valueT> & e1,
566 const std::pair<Ipv4Prefix,_valueT> & e2)
const 568 return(e1.first > e2.first);
575 bool operator () (
const std::pair<Ipv4Prefix,_valueT> & e1,
576 const std::pair<Ipv4Prefix,_valueT> & e2)
const 578 return(e1.first < e2.first);
585 bool operator () (
const std::pair<Ipv4Prefix,_valueT> & e1,
586 const std::pair<Ipv4Prefix,_valueT> & e2)
const 588 return(e1.second > e2.second);
594 std::vector<_RepSubType> _hashMaps;
600 #endif // _DWMIPV4ROUTES_HH_ uint32_t StreamedLength() const
Returns the number of bytes that should be written if one of the Write() members is called...
Definition: DwmIpv4Routes.hh:372
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
bool FindLongest(const Ipv4Address &ipAddr, std::pair< Ipv4Prefix, const _valueT *> &match) const
Finds the longest match for ipAddr.
Definition: DwmIpv4Routes.hh:261
bool Empty() const
Returns true if there are no entries.
Definition: DwmIpv4Routes.hh:152
Dwm::Writable pure virtual class definition.
Dwm::BZ2Readable pure virtual class definition.
ssize_t Write(int fd) const
Writes the routes to a file descriptor.
Definition: DwmIpv4Routes.hh:424
Dwm::GZReadable pure virtual class definition.
std::ostream & Write(std::ostream &os) const
Writes the routes to an ostream. Returns the ostream.
Definition: DwmIpv4Routes.hh:388
Dwm::BZ2IO class definition.
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
Dwm::Readable pure virtual class definition.
void Clear()
Clears all entries.
Definition: DwmIpv4Routes.hh:142
bool FindLongest(const Ipv4Address &ipAddr, std::pair< Ipv4Prefix, _valueT > &match) const
Finds the longest match for ipAddr.
Definition: DwmIpv4Routes.hh:229
Dwm::GZIO class definition.
bool Add(const Ipv4Prefix &prefix, const _valueT &value)
Adds an entry.
Definition: DwmIpv4Routes.hh:168
bool Find(const Ipv4Prefix &prefix, _valueT &match) const
Find the entry for the given prefix.
Definition: DwmIpv4Routes.hh:210
This class encapsulates an IPv4 address and netmask.
Definition: DwmIpv4Prefix.hh:69
uint32_t Size() const
Returns the number of routes.
Definition: DwmIpv4Routes.hh:346
void SortByValue(std::vector< std::pair< Ipv4Prefix, _valueT > > &target)
Sorts the contained pair<Ipv4Prefix,_valueT> values into a vector, in descending order by the value s...
Definition: DwmIpv4Routes.hh:506
static int Write(gzFile gzf, char c)
Writes c to gzf.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmBZ2Readable.hh:57
static int Read(gzFile gzf, char &c)
Reads from gzf.
size_t Read(FILE *f)
Reades the routes from a FILE pointer.
Definition: DwmIpv4Routes.hh:397
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
bool operator!=(const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &a, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &b)
operator != for unordered_map.
Definition: DwmOperators.hh:85
Dwm::BZ2Writable pure virtual class definition.
Dwm::Ipv4Prefix class definition.
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
Definition: DwmBZ2IO.hh:67
ssize_t Read(int fd)
Reads the routes from a file descriptor.
Definition: DwmIpv4Routes.hh:415
uint8_t MaskLength() const
Returns the length of the netmask (number of significant bits).
bool Delete(const Ipv4Prefix &prefix)
Deletes the entry for prefix.
Definition: DwmIpv4Routes.hh:193
int BZRead(BZFILE *bzf)
Reads the routes from a BZFILE pointer.
Definition: DwmIpv4Routes.hh:451
Ipv4Address Network() const
Returns the network portion of the prefix.
Definition: DwmIpv4Prefix.hh:124
uint32_t AddressesCovered() const
Returns the number of addresses covered by the contained prefixes, not including 0/0.
Definition: DwmIpv4Routes.hh:537
static int BZRead(BZFILE *bzf, char &c)
Reads from bzf.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
bool operator==(const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &a, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &b)
operator == for unordered_map.
Definition: DwmOperators.hh:58
Dwm::IO class definition.
std::istream & Read(std::istream &is)
Reads the routes from an istream. Returns the istream.
Definition: DwmIpv4Routes.hh:380
int BZWrite(BZFILE *bzf) const
Writes the routes to a BZFILE pointer.
Definition: DwmIpv4Routes.hh:460
This class defines an interface for classes that can write their contents to an ostream, file descriptor or FILE pointer.
Definition: DwmWritable.hh:58
static int BZWrite(BZFILE *bzf, char c)
Writes c to bzf.
static uint32_t StreamedLength(char c)
Returns the number of bytes that would be written if we called Write() for a char.
Definition: DwmIO.hh:92
bool Find(const Ipv4Address &ipAddr, std::vector< std::pair< Ipv4Prefix, _valueT > > &matches) const
Finds all matches for ipAddr.
Definition: DwmIpv4Routes.hh:287
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmBZ2Writable.hh:57
Dwm::GZWritable pure virtual class definition.
This class defines an interface for classes that can read their contents from an istream, file descriptor or FILE pointer.
Definition: DwmReadable.hh:58
ipv4addr_t Raw() const
Returns an ipv4addr_t representation (32-bit value in network byte order).
Definition: DwmIpv4Address.hh:87
int Write(gzFile gzf) const
Writes the routes to a gzFile.
Definition: DwmIpv4Routes.hh:442
This template class provides an associative container keyed by IPv4 addresses, with longest-match sea...
Definition: DwmIpv4Routes.hh:118
int Read(gzFile gzf)
Reads the routes from a gzFile.
Definition: DwmIpv4Routes.hh:433
size_t Write(FILE *f) const
Writes the routes to a FILE pointer.
Definition: DwmIpv4Routes.hh:406
A configure target for dealing with OS differences.