00001 #ifndef LUX_API_SORTER_H
00002 #define LUX_API_SORTER_H
00003
00004 #include "lux/lux.h"
00005 #include "condition.h"
00006 #include <vector>
00007 #include <functional>
00008 #include <algorithm>
00009 #include <iostream>
00010
00011 namespace Lux {
00012
00013 template <class T>
00014 struct sort_by_score_desc : public std::binary_function<T, T, bool>
00015 {
00016 bool operator()(T &t1, T &t2) {
00017 return t1.score > t2.score;
00018 }
00019 };
00020 template <class T>
00021 struct sort_by_score_asc : public std::binary_function<T, T, bool>
00022 {
00023 bool operator()(T &t1, T &t2) {
00024 return t1.score < t2.score;
00025 }
00026 };
00027 template <class T>
00028 struct sort_by_attr_int_desc : public std::binary_function<T, T, bool>
00029 {
00030 bool operator()(T &t1, T &t2) {
00031 return *(int *) t1.attr > *(int *) t2.attr;
00032 }
00033 };
00034 template <class T>
00035 struct sort_by_attr_int_asc : public std::binary_function<T, T, bool>
00036 {
00037 bool operator()(T &t1, T &t2) {
00038 return *(int *) t1.attr < *(int *) t2.attr;
00039 }
00040 };
00041 template <class T>
00042 struct sort_by_attr_str_desc : public std::binary_function<T, T, bool>
00043 {
00044 bool operator()(T &t1, T &t2) {
00045 return strcmp((char *) t1.attr, (char *) t2.attr) > 0;
00046 }
00047 };
00048 template <class T>
00049 struct sort_by_attr_str_asc : public std::binary_function<T, T, bool>
00050 {
00051 bool operator()(T &t1, T &t2) {
00052 return strcmp((char *) t1.attr, (char *) t2.attr) < 0;
00053 }
00054 };
00055
00059 template <class T>
00060 class Sorter {
00061
00062 public:
00068 Sorter(SortCondition sort, uint32_t num_of_sorted)
00069 : sort_(sort), num_of_sorted_(num_of_sorted)
00070 {}
00074 ~Sorter(void) {}
00079 void sort(std::vector<T> &rs)
00080 {
00084 switch (sort_.attr_type) {
00085 case SORT_SCORE:
00086 if (sort_.order_type == DESC) {
00087 partial_sort(rs.begin(),
00088 rs.begin() + num_of_sorted_,
00089 rs.end(),
00090 sort_by_score_desc<T>());
00091 } else {
00092 partial_sort(rs.begin(),
00093 rs.begin() + num_of_sorted_,
00094 rs.end(),
00095 sort_by_score_asc<T>());
00096 }
00097 break;
00098 case SORT_ATTR_INT:
00099 if (sort_.order_type == DESC) {
00100 partial_sort(rs.begin(),
00101 rs.begin() + num_of_sorted_,
00102 rs.end(),
00103 sort_by_attr_int_desc<T>());
00104 } else {
00105 partial_sort(rs.begin(),
00106 rs.begin() + num_of_sorted_,
00107 rs.end(),
00108 sort_by_attr_int_asc<T>());
00109 }
00110 break;
00111 case SORT_ATTR_STR:
00112 if (sort_.order_type == DESC) {
00113 partial_sort(rs.begin(),
00114 rs.begin() + num_of_sorted_,
00115 rs.end(),
00116 sort_by_attr_str_desc<T>());
00117 } else {
00118 partial_sort(rs.begin(),
00119 rs.begin() + num_of_sorted_,
00120 rs.end(),
00121 sort_by_attr_str_asc<T>());
00122 }
00123 break;
00124 default:
00125 partial_sort(rs.begin(),
00126 rs.begin() + num_of_sorted_,
00127 rs.end(),
00128 sort_by_score_desc<T>());
00129 }
00130 }
00131
00132 private:
00133 SortCondition sort_;
00134 uint32_t num_of_sorted_;
00135 };
00136
00137 }
00138
00139 #endif