Name: Added function breadthFirstLess to use in sort, and function object BreadthFirstLess to use in map.
diff --git a/include/ndn-cpp/name.hpp b/include/ndn-cpp/name.hpp
index 0ece55c..132def5 100644
--- a/include/ndn-cpp/name.hpp
+++ b/include/ndn-cpp/name.hpp
@@ -617,6 +617,30 @@
bool
operator != (const Name &name) const { return !equals(name); }
+ /**
+ * Compare two names for "less than" using breadth first. If the first components of each name are not equal,
+ * this returns true if the first comes before the second using the NDN canonical ordering for name components.
+ * If they are equal, this compares the second components of each name, etc. If both names are the same up to
+ * the size of the shorter name, this returns true if the first name is shorter than the second. For example, if you
+ * use breadthFirstLess in std::sort, it gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names
+ * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb
+ * according to NDN canonical ordering since it is shorter. Note: We don't define this directly as the Name
+ * less than operation because there are other valid ways to sort names.
+ * @param name1 The first name to compare.
+ * @param name2 The second name to compare.
+ * @return True if the first name is less than the second using breadth first comparison.
+ */
+ static bool
+ breadthFirstLess(const Name& name1, const Name& name2);
+
+ /**
+ * Name::BreadthFirstLess is a function object which calls breadthFirstLess, for use as the "less" operator in map, etc.
+ * For example, you can use Name as the key type in a map as follows: map<Name, int, Name::BreadthFirstLess>.
+ */
+ struct BreadthFirstLess {
+ bool operator() (const Name& name1, const Name& name2) const { return breadthFirstLess(name1, name2); }
+ };
+
//
// Iterator interface to name components.
//
diff --git a/src/name.cpp b/src/name.cpp
index 5012ca2..68005e6 100644
--- a/src/name.cpp
+++ b/src/name.cpp
@@ -422,4 +422,21 @@
return result.str();
}
+bool
+Name::breadthFirstLess(const Name& name1, const Name& name2)
+{
+ for (size_t i = 0; i < name1.size() && i < name2.size(); ++i) {
+ if (name1[i] == name2[i])
+ // The components at this index are equal, so check the next components.
+ continue;
+
+ // Otherwise, the result is based on the components at this index.
+ return name1[i] < name2[i];
+ }
+
+ // The components up to min(name1.size(), name2.size()) are equal, so sort on the shorter name.
+ return name1.size() < name2.size();
+}
+
+
}