Skip to content

Commit

Permalink
lsa: define CoordinateLsa operator==
Browse files Browse the repository at this point in the history
getCorRadius, setCorRadius, getCorTheta, setCorTheta methods are
renamed, in accordance with ndn-cxx code style recommendations.

refs #4094

Change-Id: Iaf3ebf3b895d6dc351b2f754e4a9c65713bdca3a
  • Loading branch information
yoursunny committed Jan 9, 2024
1 parent f467467 commit b573484
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/adjacent.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
Expand All @@ -21,6 +21,7 @@
#include "adjacent.hpp"
#include "logger.hpp"
#include "tlv-nlsr.hpp"
#include "utility/numeric.hpp"

namespace nlsr {

Expand Down Expand Up @@ -158,10 +159,9 @@ Adjacent::wireDecode(const ndn::Block& wire)
bool
Adjacent::operator==(const Adjacent& adjacent) const
{
return (m_name == adjacent.getName()) &&
(m_faceUri == adjacent.getFaceUri()) &&
(std::abs(m_linkCost - adjacent.getLinkCost()) <
std::numeric_limits<double>::epsilon());
return m_name == adjacent.getName() &&
m_faceUri == adjacent.getFaceUri() &&
util::diffInEpsilon(m_linkCost, adjacent.getLinkCost());
}

bool
Expand Down
26 changes: 4 additions & 22 deletions src/lsa/coordinate-lsa.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -38,24 +38,6 @@ CoordinateLsa::CoordinateLsa(const ndn::Block& block)
wireDecode(block);
}

bool
CoordinateLsa::isEqualContent(const CoordinateLsa& clsa) const
{
if (clsa.getCorTheta().size() != m_hyperbolicAngles.size()) {
return false;
}

std::vector<double> m_angles2 = clsa.getCorTheta();
for (unsigned int i = 0; i < clsa.getCorTheta().size(); i++) {
if (std::abs(m_hyperbolicAngles[i] - m_angles2[i]) > std::numeric_limits<double>::epsilon()) {
return false;
}
}

return (std::abs(m_hyperbolicRadius - clsa.getCorRadius()) <
std::numeric_limits<double>::epsilon());
}

template<ndn::encoding::Tag TAG>
size_t
CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Expand Down Expand Up @@ -155,10 +137,10 @@ std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
CoordinateLsa::update(const std::shared_ptr<Lsa>& lsa)
{
auto clsa = std::static_pointer_cast<CoordinateLsa>(lsa);
if (!isEqualContent(*clsa)) {
m_hyperbolicRadius = clsa->getCorRadius();
if (*this != *clsa) {
m_hyperbolicRadius = clsa->getRadius();
m_hyperbolicAngles.clear();
for (const auto& angle : clsa->getCorTheta()) {
for (const auto& angle : clsa->getTheta()) {
m_hyperbolicAngles.push_back(angle);
}
return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Expand Down
36 changes: 25 additions & 11 deletions src/lsa/coordinate-lsa.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand All @@ -23,6 +23,9 @@
#define NLSR_LSA_COORDINATE_LSA_HPP

#include "lsa.hpp"
#include "utility/numeric.hpp"

#include <boost/operators.hpp>

namespace nlsr {

Expand All @@ -33,7 +36,7 @@ namespace nlsr {
HyperbolicRadius
HyperbolicAngle+
*/
class CoordinateLsa : public Lsa
class CoordinateLsa : public Lsa, private boost::equality_comparable<CoordinateLsa>
{
public:
CoordinateLsa() = default;
Expand All @@ -57,34 +60,31 @@ class CoordinateLsa : public Lsa
}

double
getCorRadius() const
getRadius() const
{
return m_hyperbolicRadius;
}

void
setCorRadius(double cr)
setRadius(double cr)
{
m_wire.reset();
m_hyperbolicRadius = cr;
}

const std::vector<double>
getCorTheta() const
const std::vector<double>&
getTheta() const
{
return m_hyperbolicAngles;
}

void
setCorTheta(std::vector<double> ct)
setTheta(std::vector<double> ct)
{
m_wire.reset();
m_hyperbolicAngles = ct;
m_hyperbolicAngles = std::move(ct);
}

bool
isEqualContent(const CoordinateLsa& clsa) const;

template<ndn::encoding::Tag TAG>
size_t
wireEncode(ndn::EncodingImpl<TAG>& block) const;
Expand All @@ -101,6 +101,20 @@ class CoordinateLsa : public Lsa
std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
update(const std::shared_ptr<Lsa>& lsa) override;

private: // non-member operators
// NOTE: the following "hidden friend" operators are available via
// argument-dependent lookup only and must be defined inline.
// boost::equality_comparable provides != operator.

friend bool
operator==(const CoordinateLsa& lhs, const CoordinateLsa& rhs)
{
return util::diffInEpsilon(lhs.m_hyperbolicRadius, rhs.m_hyperbolicRadius) &&
std::equal(lhs.m_hyperbolicAngles.begin(), lhs.m_hyperbolicAngles.end(),
rhs.m_hyperbolicAngles.begin(), rhs.m_hyperbolicAngles.end(),
util::diffInEpsilon);
}

private:
double m_hyperbolicRadius = 0.0;
std::vector<double> m_hyperbolicAngles;
Expand Down
10 changes: 5 additions & 5 deletions src/route/routing-table-calculator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -496,11 +496,11 @@ HyperbolicRoutingCalculator::getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, nd
return UNKNOWN_DISTANCE;
}

std::vector<double> srcTheta = srcLsa->getCorTheta();
std::vector<double> destTheta = destLsa->getCorTheta();
std::vector<double> srcTheta = srcLsa->getTheta();
std::vector<double> destTheta = destLsa->getTheta();

double srcRadius = srcLsa->getCorRadius();
double destRadius = destLsa->getCorRadius();
double srcRadius = srcLsa->getRadius();
double destRadius = destLsa->getRadius();

double diffTheta = calculateAngularDistance(srcTheta, destTheta);

Expand Down
43 changes: 43 additions & 0 deletions src/utility/numeric.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
* This file is part of NLSR (Named-data Link State Routing).
* See AUTHORS.md for complete list of NLSR authors and contributors.
*
* NLSR is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef NLSR_NUMERIC_HPP
#define NLSR_NUMERIC_HPP

#include "common.hpp"

namespace nlsr::util {

/**
* @brief Determine whether the difference between two numbers are within epsilon.
* @param lhs first number.
* @param rhs second number.
* @returns true if their difference is within epsilon.
*/
inline bool
diffInEpsilon(double lhs, double rhs)
{
return std::abs(lhs - rhs) < std::numeric_limits<double>::epsilon();
}

} // namespace nlsr::util

#endif // NLSR_NUMERIC_HPP
12 changes: 6 additions & 6 deletions tests/lsa/test-coordinate-lsa.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -94,22 +94,22 @@ BOOST_AUTO_TEST_CASE(Basic)
CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles1);
CoordinateLsa clsa2("router1", 12, testTimePoint, 2.5, angles2);

BOOST_CHECK_CLOSE(clsa1.getCorRadius(), 2.5, 0.0001);
BOOST_CHECK(clsa1.getCorTheta() == angles1);
BOOST_CHECK_CLOSE(clsa1.getRadius(), 2.5, 0.0001);
BOOST_TEST(clsa1.getTheta() == angles1, boost::test_tools::per_element());

BOOST_CHECK(clsa1.isEqualContent(clsa2));
BOOST_CHECK_EQUAL(clsa1, clsa2);

BOOST_CHECK_EQUAL(clsa1.wireEncode(), clsa2.wireEncode());

auto wire = clsa1.wireEncode();
BOOST_TEST(wire == COORDINATE_LSA1, boost::test_tools::per_element());

std::vector<double> angles3{40.0};
clsa1.setCorTheta(angles3);
clsa1.setTheta(angles3);
wire = clsa1.wireEncode();
BOOST_TEST(wire == COORDINATE_LSA_DIFF_ANGLE, boost::test_tools::per_element());

clsa1.setCorRadius(2.3);
clsa1.setRadius(2.3);
wire = clsa1.wireEncode();
BOOST_TEST(wire == COORDINATE_LSA_DIFF_RADIUS, boost::test_tools::per_element());

Expand Down
2 changes: 1 addition & 1 deletion tests/test-statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
auto coorLsa = lsdb.findLsa<CoordinateLsa>(conf.getRouterPrefix());

seqNo = coorLsa->getSeqNo();
coorLsa->setCorTheta({20.0, 30.0});
coorLsa->setTheta({20.0, 30.0});
lsdb.installLsa(coorLsa);

// Receive Adjacency LSA Interest
Expand Down

0 comments on commit b573484

Please sign in to comment.