From: Otto Moerbeek Date: Tue, 16 Jan 2024 11:34:06 +0000 (+0100) Subject: Tidy sortlist.?? X-Git-Tag: rec-5.1.0-alpha1~9^2~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bf38f18ef41d6565b959aa594a4d88818e40bb3b;p=thirdparty%2Fpdns.git Tidy sortlist.?? --- diff --git a/pdns/sortlist.cc b/pdns/sortlist.cc index 11d309bc0b..2b9ab56114 100644 --- a/pdns/sortlist.cc +++ b/pdns/sortlist.cc @@ -8,67 +8,66 @@ void SortList::clear() int SortList::getMaxOrder(const Netmask& formask) const { - int order=0; - - auto place = d_sortlist.lookup(formask); - if(place && place->first == formask) { - for(const auto& o : place->second.d_orders) - order = std::max(order, o.second); + int order = 0; + + const auto* place = d_sortlist.lookup(formask); + if (place != nullptr && place->first == formask) { + for (const auto& node_order : place->second.d_orders) { + order = std::max(order, node_order.second); + } } - + return order; } -void SortList::addEntry(const Netmask& formask, const Netmask& valmask, int order) +void SortList::addEntry(const Netmask& covers, const Netmask& answermask, int order) { - if(order < 0) { - order=getMaxOrder(formask); + if (order < 0) { + order = getMaxOrder(covers); ++order; } // cout<<"Adding for netmask "< SortList::getOrderCmp(const ComboAddress& who) const { - if(!d_sortlist.match(who)) { - return std::unique_ptr(); + if (!d_sortlist.match(who)) { + return {}; } - auto fnd = d_sortlist.lookup(who); + const auto* fnd = d_sortlist.lookup(who); // cerr<<"Returning sort order for "<second.d_orders.size()<<" entries"<(fnd->second); } // call this with **stable_sort** -bool SortListOrderCmp::operator()(const DNSRecord& ar, const DNSRecord& br) const +bool SortListOrderCmp::operator()(const DNSRecord& lhs, const DNSRecord& rhs) const { - bool aAddr = (ar.d_type == QType::A || ar.d_type == QType::AAAA); - bool bAddr = (br.d_type == QType::A || br.d_type == QType::AAAA); + bool aAddr = (lhs.d_type == QType::A || lhs.d_type == QType::AAAA); + bool bAddr = (rhs.d_type == QType::A || rhs.d_type == QType::AAAA); // anything address related is always 'larger', rest is equal - - if(aAddr && !bAddr) + if (aAddr && !bAddr) { return false; - else if(!aAddr && bAddr) + } + if (!aAddr && bAddr) { return true; - else if(!aAddr && !bAddr) + } + if (!aAddr && !bAddr) { return false; - + } + + int aOrder = std::numeric_limits::max(); + int bOrder = aOrder; - int aOrder=std::numeric_limits::max(); - int bOrder=aOrder; + ComboAddress laddr = getAddr(lhs); + ComboAddress raddr = getAddr(rhs); - ComboAddress a=getAddr(ar), b=getAddr(br); - - if(d_slo.d_orders.match(a)) - aOrder = d_slo.d_orders.lookup(a)->second; - else { - // cout<<"Could not find anything for "<second; } - if(d_slo.d_orders.match(b)) - bOrder = d_slo.d_orders.lookup(b)->second; - else { - // cout<<"Could not find anything for "<second; } return aOrder < bOrder; } diff --git a/pdns/sortlist.hh b/pdns/sortlist.hh index cd043295ac..f260db0a6a 100644 --- a/pdns/sortlist.hh +++ b/pdns/sortlist.hh @@ -20,6 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once + #include "iputils.hh" #include "dnsrecords.hh" @@ -28,21 +29,26 @@ struct SortListOrder NetmaskTree d_orders; }; - struct SortListOrderCmp { - SortListOrderCmp(const SortListOrder& slo) : d_slo(slo) {} - bool operator()(const DNSRecord& a, const DNSRecord& b) const; - const SortListOrder d_slo; + SortListOrderCmp(SortListOrder slo) : + d_slo(std::move(slo)) {} + bool operator()(const DNSRecord& lhs, const DNSRecord& rhs) const; + SortListOrder d_slo; }; -class SortList { +class SortList +{ public: void clear(); - void addEntry(const Netmask& covers, const Netmask& answermask, int order=-1); - int getMaxOrder(const Netmask& formask) const; - std::unique_ptr getOrderCmp(const ComboAddress& who) const; + void addEntry(const Netmask& covers, const Netmask& answermask, int order = -1); + [[nodiscard]] int getMaxOrder(const Netmask& formask) const; + [[nodiscard]] std::unique_ptr getOrderCmp(const ComboAddress& who) const; + [[nodiscard]] const NetmaskTree& getTree() const + { + return d_sortlist; + } + private: - NetmaskTree d_sortlist; };