[Groonga-commit] groonga/grnxx at 310d3da [master] Fix a bug that NaN causes wrong results in sorting.

Back to archive index

susumu.yata null+****@clear*****
Thu Aug 21 12:02:30 JST 2014


susumu.yata	2014-08-21 12:02:30 +0900 (Thu, 21 Aug 2014)

  New Revision: 310d3da490668937fe5eb26c10622868b8a96e18
  https://github.com/groonga/grnxx/commit/310d3da490668937fe5eb26c10622868b8a96e18

  Message:
    Fix a bug that NaN causes wrong results in sorting.

  Modified files:
    lib/grnxx/sorter.cpp

  Modified: lib/grnxx/sorter.cpp (+17 -3)
===================================================================
--- lib/grnxx/sorter.cpp    2014-08-21 12:02:17 +0900 (2819b0b)
+++ lib/grnxx/sorter.cpp    2014-08-21 12:02:30 +0900 (56e778f)
@@ -139,6 +139,20 @@ struct ReverseIsPrior {
 // ---- QuickSortNode ----
 
 template <typename T>
+struct Equal {
+  bool operator()(T lhs, T rhs) const {
+    return lhs == rhs;
+  }
+};
+
+template <>
+struct Equal<Float> {
+  bool operator()(Float lhs, Float rhs) const {
+    return (lhs == rhs) || (std::isnan(lhs) && std::isnan(rhs));
+  }
+};
+
+template <typename T>
 class QuickSortNode : public TypedNode<typename T::Value> {
  public:
   using Comparer = T;
@@ -216,7 +230,7 @@ bool QuickSortNode<T>::quick_sort(Error *error,
       while (left < right) {
         if (comparer_(pivot, values[left])) {
           break;
-        } else if (pivot == values[left]) {
+        } else if (Equal<Value>()(pivot, values[left])) {
           std::swap(values[left], values[pivot_left]);
           records.swap(left, pivot_left);
           ++pivot_left;
@@ -227,7 +241,7 @@ bool QuickSortNode<T>::quick_sort(Error *error,
         --right;
         if (comparer_(values[right], pivot)) {
           break;
-        } else if (values[right] == pivot) {
+        } else if (Equal<Value>()(values[right], pivot)) {
           --pivot_right;
           std::swap(values[right], values[pivot_right]);
           records.swap(right, pivot_right);
@@ -335,7 +349,7 @@ bool QuickSortNode<T>::insertion_sort(Error *error,
   if (this->next_) {
     Int begin = 0;
     for (Int i = 1; i < records.size(); ++i) {
-      if (values[i] != values[begin]) {
+      if (!Equal<Value>()(values[i], values[begin])) {
         if ((i - begin) >= 2) {
           if (!this->next_->sort(error, records.ref(begin, i - begin),
                                  0, i - begin)) {
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index