Skip to content

Commit

Permalink
Merge "Use a template to compare STL vectors; make sure that Specs ha…
Browse files Browse the repository at this point in the history
…ve comparisson functions."
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed May 18, 2015
2 parents ad70a19 + 92e907f commit 3cdcf63
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 17 deletions.
31 changes: 22 additions & 9 deletions src/base/util.h
Expand Up @@ -74,15 +74,28 @@ static ModuleInitializer TOKENPASTE2(init_, __LINE__)(Func);
} while(0);

// Compare sorted vectors of pointers.
#define KEY_COMPARE_VECTOR_PTRS(T, x, y) \
do { \
KEY_COMPARE((x).size(), (y).size()); \
std::vector<T *>::const_iterator __ix, __iy; \
for (__ix = (x).begin(), __iy = (y).begin(); \
__ix < x.end(); ++__ix, ++__iy) { \
KEY_COMPARE(**__ix, **__iy); \
} \
} while (0)
template <class InputIterator, class CompareOp>
int STLSortedCompare(InputIterator first1, InputIterator last1,
InputIterator first2, InputIterator last2,
CompareOp op) {
InputIterator iter1 = first1;
InputIterator iter2 = first2;
while (iter1 != last1 && iter2 != last2) {
int result = op(*iter1, *iter2);
if (result != 0) {
return result;
}
++iter1;
++iter2;
}
if (iter1 != last1) {
return 1;
}
if (iter2 != last2) {
return -1;
}
return 0;
}

template <typename Container>
void STLDeleteValues(Container *container) {
Expand Down
75 changes: 67 additions & 8 deletions src/bgp/bgp_attr.cc
Expand Up @@ -343,10 +343,24 @@ void EdgeDiscoverySpec::Edge::SetLabels(
labels.push_back(last_label);
}

struct EdgeDiscoverySpecEdgeCompare {
int operator()(const EdgeDiscoverySpec::Edge *lhs,
const EdgeDiscoverySpec::Edge *rhs) const {
KEY_COMPARE(lhs->address, rhs->address);
KEY_COMPARE(lhs->labels, rhs->labels);
return 0;
}
};

int EdgeDiscoverySpec::CompareTo(const BgpAttribute &rhs_attr) const {
int ret = BgpAttribute::CompareTo(rhs_attr);
if (ret != 0) return ret;
return 0;
const EdgeDiscoverySpec &rhs =
static_cast<const EdgeDiscoverySpec &>(rhs_attr);
ret = STLSortedCompare(edge_list.begin(), edge_list.end(),
rhs.edge_list.begin(), rhs.edge_list.end(),
EdgeDiscoverySpecEdgeCompare());
return ret;
}

void EdgeDiscoverySpec::ToCanonical(BgpAttr *attr) {
Expand Down Expand Up @@ -412,9 +426,19 @@ EdgeDiscovery::~EdgeDiscovery() {
STLDeleteValues(&edge_list);
}

struct EdgeDiscoveryEdgeCompare {
int operator()(const EdgeDiscovery::Edge *lhs,
const EdgeDiscovery::Edge *rhs) const {
KEY_COMPARE(*lhs, *rhs);
return 0;
}
};

int EdgeDiscovery::CompareTo(const EdgeDiscovery &rhs) const {
KEY_COMPARE_VECTOR_PTRS(Edge, edge_list, rhs.edge_list);
return 0;
int result = STLSortedCompare(edge_list.begin(), edge_list.end(),
rhs.edge_list.begin(), rhs.edge_list.end(),
EdgeDiscoveryEdgeCompare());
return result;
}

void EdgeDiscovery::Remove() {
Expand Down Expand Up @@ -467,10 +491,26 @@ void EdgeForwardingSpec::Edge::SetOutboundIp4Address(Ip4Address addr) {
outbound_address.begin());
}

struct EdgeForwardingSpecEdgeCompare {
int operator()(const EdgeForwardingSpec::Edge *lhs,
const EdgeForwardingSpec::Edge *rhs) const {
KEY_COMPARE(lhs->inbound_address, rhs->inbound_address);
KEY_COMPARE(lhs->outbound_address, rhs->outbound_address);
KEY_COMPARE(lhs->inbound_label, rhs->inbound_label);
KEY_COMPARE(lhs->outbound_label, rhs->outbound_label);
return 0;
}
};

int EdgeForwardingSpec::CompareTo(const BgpAttribute &rhs_attr) const {
int ret = BgpAttribute::CompareTo(rhs_attr);
if (ret != 0) return ret;
return 0;
const EdgeForwardingSpec &rhs =
static_cast<const EdgeForwardingSpec &>(rhs_attr);
ret = STLSortedCompare(edge_list.begin(), edge_list.end(),
rhs.edge_list.begin(), rhs.edge_list.end(),
EdgeForwardingSpecEdgeCompare());
return ret;
}

void EdgeForwardingSpec::ToCanonical(BgpAttr *attr) {
Expand Down Expand Up @@ -539,9 +579,19 @@ EdgeForwarding::~EdgeForwarding() {
STLDeleteValues(&edge_list);
}

struct EdgeForwardingEdgeCompare {
int operator()(const EdgeForwarding::Edge *lhs,
const EdgeForwarding::Edge *rhs) const {
KEY_COMPARE(*lhs, *rhs);
return 0;
}
};

int EdgeForwarding::CompareTo(const EdgeForwarding &rhs) const {
KEY_COMPARE_VECTOR_PTRS(Edge, edge_list, rhs.edge_list);
return 0;
int result = STLSortedCompare(edge_list.begin(), edge_list.end(),
rhs.edge_list.begin(), rhs.edge_list.end(),
EdgeForwardingEdgeCompare());
return result;
}

void EdgeForwarding::Remove() {
Expand Down Expand Up @@ -598,10 +648,19 @@ BgpOList::~BgpOList() {
STLDeleteValues(&elements);
}

struct BgpOListElementCompare {
int operator()(const BgpOListElem *lhs, const BgpOListElem *rhs) const {
KEY_COMPARE(*lhs, *rhs);
return 0;
}
};

int BgpOList::CompareTo(const BgpOList &rhs) const {
KEY_COMPARE(olist().subcode, rhs.olist().subcode);
KEY_COMPARE_VECTOR_PTRS(BgpOListElem, elements, rhs.elements);
return 0;
int result = STLSortedCompare(elements.begin(), elements.end(),
rhs.elements.begin(), rhs.elements.end(),
BgpOListElementCompare());
return result;
}

void BgpOList::Remove() {
Expand Down

0 comments on commit 3cdcf63

Please sign in to comment.