From 29a09707f385c1d143ac93e3fcb01868414bdd10 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 4 Apr 2015 15:47:41 -0400 Subject: [PATCH] Variant: Implemented custom UDA sorting --- src/Context.cpp | 4 +- src/Variant.cpp | 108 +++++++++++++++++++++++++++++------------------- src/Variant.h | 1 + 3 files changed, 70 insertions(+), 43 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index 3cb1a9562..e6a453daf 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -650,7 +650,9 @@ void Context::staticInitialization () std::string name = rc->first.substr (4, rc->first.length () - 7 - 4); std::vector values; split (values, rc->second, ','); - Task::customOrder[name] = values; + + for (auto r = values.rbegin(); r != values.rend (); ++r) + Task::customOrder[name].push_back (*r); } } diff --git a/src/Variant.cpp b/src/Variant.cpp index 1f378b8f7..855718762 100644 --- a/src/Variant.cpp +++ b/src/Variant.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -314,19 +315,25 @@ bool Variant::operator< (const Variant& other) const return left._string < right._string; case type_string: - if (left.source () == "priority" || right.source () == "priority") { - if (left._string != "H" && right._string == "H") return true; - else if (left._string == "L" && right._string == "M") return true; - else if (left._string == "" && right._string != "") return true; - else return false; - } - else - { - if (left.trivial () || right.trivial ()) + if (left._string == right._string) return false; - return left._string < right._string; + auto order = Task::customOrder.find (left.source ()); + if (order != Task::customOrder.end ()) + { + // Guaranteed to be found, because of ColUDA::validate (). + auto posLeft = std::find (order->second.begin (), order->second.end (), left._string); + auto posRight = std::find (order->second.begin (), order->second.end (), right._string); + return posLeft < posRight; + } + else + { + if (left.trivial () || right.trivial ()) + return false; + + return left._string < right._string; + } } case type_date: @@ -459,20 +466,25 @@ bool Variant::operator<= (const Variant& other) const return left._string <= right._string; case type_string: - if (left.source () == "priority" || right.source () == "priority") { - if (left._string == right._string ) return true; - else if ( right._string == "H") return true; - else if (left._string == "L" && right._string == "M") return true; - else if (left._string == "" ) return true; - else return false; - } - else - { - if (left.trivial () || right.trivial ()) + if (left._string == right._string) return false; - return left._string <= right._string; + auto order = Task::customOrder.find (left.source ()); + if (order != Task::customOrder.end ()) + { + // Guaranteed to be found, because of ColUDA::validate (). + auto posLeft = std::find (order->second.begin (), order->second.end (), left._string); + auto posRight = std::find (order->second.begin (), order->second.end (), right._string); + return posLeft <= posRight; + } + else + { + if (left.trivial () || right.trivial ()) + return false; + + return left._string <= right._string; + } } case type_date: @@ -605,20 +617,27 @@ bool Variant::operator> (const Variant& other) const return left._string > right._string; case type_string: - if (left.source () == "priority" || right.source () == "priority") { - if (left._string == "H" && right._string != "H") return true; - else if (left._string == "M" && right._string == "L") return true; - else if (left._string != "" && right._string == "") return true; - else return false; - } - else - { - if (left.trivial () || right.trivial ()) + if (left._string == right._string) return false; - return left._string> right._string; + auto order = Task::customOrder.find (left.source ()); + if (order != Task::customOrder.end ()) + { + // Guaranteed to be found, because of ColUDA::validate (). + auto posLeft = std::find (order->second.begin (), order->second.end (), left._string); + auto posRight = std::find (order->second.begin (), order->second.end (), right._string); + return posLeft > posRight; + } + else + { + if (left.trivial () || right.trivial ()) + return false; + + return left._string > right._string; + } } + case type_date: if (left.trivial () || right.trivial ()) return false; @@ -749,20 +768,25 @@ bool Variant::operator>= (const Variant& other) const return left._string >= right._string; case type_string: - if (left.source () == "priority" || right.source () == "priority") { - if (left._string == right._string ) return true; - else if (left._string == "H" ) return true; - else if (left._string == "M" && right._string == "L") return true; - else if ( right._string == "" ) return true; - else return false; - } - else - { - if (left.trivial () || right.trivial ()) + if (left._string == right._string) return false; - return left._string >= right._string; + auto order = Task::customOrder.find (left.source ()); + if (order != Task::customOrder.end ()) + { + // Guaranteed to be found, because of ColUDA::validate (). + auto posLeft = std::find (order->second.begin (), order->second.end (), left._string); + auto posRight = std::find (order->second.begin (), order->second.end (), right._string); + return posLeft >= posRight; + } + else + { + if (left.trivial () || right.trivial ()) + return false; + + return left._string >= right._string; + } } case type_date: diff --git a/src/Variant.h b/src/Variant.h index c733a0abe..d0f2a4c55 100644 --- a/src/Variant.h +++ b/src/Variant.h @@ -27,6 +27,7 @@ #ifndef INCLUDED_VARIANT #define INCLUDED_VARIANT +#include #include #include #include