Name: In Name::Component, added operator<, etc. for comparison. Refs #1085.
diff --git a/CHANGELOG b/CHANGELOG
index 0251934..66f3e5f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -8,6 +8,7 @@
* Fix bug in getForwardingEntryFlags: Need to check all flags, not the first flag using "else if".
Changes
+* http://redmine.named-data.net/issues/1085: In Name::Component, added comparison operators.
* MetaInfo: Added setFinalBlockID for Name::Component, remove unused setFinalBlockID which take uint8_t*, etc.
* Fix clang compiler warnings: Include headers, parentheses and cast explicitly.
* Moved class ExcludeEntry to inner class Exclude::Entry.
diff --git a/include/ndn-cpp/name.hpp b/include/ndn-cpp/name.hpp
index 8c348fe..9a1b39e 100644
--- a/include/ndn-cpp/name.hpp
+++ b/include/ndn-cpp/name.hpp
@@ -167,7 +167,81 @@
*/
static Component
fromNumberWithMarker(uint64_t number, uint8_t marker);
+
+ /**
+ * Check if this is the same component as other.
+ * @param other The other Component to compare with.
+ * @return true if the components are equal, otherwise false.
+ */
+ bool
+ equals(const Component& other) const
+ {
+ return *value_ == *other.value_;
+ }
+
+ /**
+ * Check if this is the same component as other.
+ * @param other The other Component to compare with.
+ * @return true if the components are equal, otherwise false.
+ */
+ bool
+ operator == (const Component& other) const { return equals(other); }
+
+ /**
+ * Check if this is not the same component as other.
+ * @param other The other Component to compare with.
+ * @return true if the components are not equal, otherwise false.
+ */
+ bool
+ operator != (const Component& other) const { return !equals(other); }
+
+ /**
+ * Compare this to the other Component using NDN canonical ordering.
+ * @param other The other Component to compare with.
+ * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
+ * 1 if *this comes after other in the canonical ordering.
+ *
+ * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
+ */
+ int
+ compare(const Component& other) const;
+ /**
+ * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
+ * @param other The other Component to compare with.
+ *
+ * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
+ */
+ bool
+ operator <= (const Component& other) const { return compare(other) <= 0; }
+
+ /**
+ * Return true if this is less than the other Component in the NDN canonical ordering.
+ * @param other The other Component to compare with.
+ *
+ * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
+ */
+ bool
+ operator < (const Component& other) const { return compare(other) < 0; }
+
+ /**
+ * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
+ * @param other The other Component to compare with.
+ *
+ * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
+ */
+ bool
+ operator >= (const Component& other) const { return compare(other) >= 0; }
+
+ /**
+ * Return true if this is greater than the other Component in the NDN canonical ordering.
+ * @param other The other Component to compare with.
+ *
+ * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
+ */
+ bool
+ operator > (const Component& other) const { return compare(other) > 0; }
+
private:
Blob value_;
};
diff --git a/src/name.cpp b/src/name.cpp
index 3d4519a..5012ca2 100644
--- a/src/name.cpp
+++ b/src/name.cpp
@@ -2,6 +2,7 @@
/**
* Copyright (C) 2013 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
* See COPYING for copyright and distribution information.
*/
@@ -10,6 +11,7 @@
#include <string.h>
#include <ndn-cpp/name.hpp>
#include "c/name.h"
+#include "c/util/ndn_memory.h"
using namespace std;
using namespace ndn::ptr_lib;
@@ -174,6 +176,19 @@
return ndn_NameComponent_toNumber(&componentStruct);
}
+int
+Name::Component::compare(const Name::Component& other) const
+{
+ // Imitate ndn_Exclude_compareComponents.
+ if (value_.size() < other.value_.size())
+ return -1;
+ if (value_.size() > other.value_.size())
+ return 1;
+
+ // The components are equal length. Just do a byte compare.
+ return ndn_memcmp((uint8_t*)value_.buf(), (uint8_t*)other.value_.buf(), value_.size());
+}
+
void
Name::set(const char *uri_cstr)
{