Mirror reflection library - Lagoon run-time layer 0.5.13
|
00001 00010 #ifndef LAGOON_RANGE_SORT_1011291729_HPP 00011 #define LAGOON_RANGE_SORT_1011291729_HPP 00012 00013 #include <lagoon/lagoon_fwd.hpp> 00014 #include <lagoon/range/container.hpp> 00015 #include <lagoon/range/std_range.hpp> 00016 #include <cassert> 00017 #include <algorithm> 00018 00019 LAGOON_NAMESPACE_BEGIN 00020 00021 namespace aux { 00022 00023 // Helper base class for sorted container_range copying 00024 // the element from the range into a container by using 00025 // the push_back member during the construction 00026 struct rng2cntr_push_back 00027 { 00028 rng2cntr_push_back(void) = default; 00029 00030 template <class Range, class Container> 00031 inline rng2cntr_push_back(Range range, Container& container) 00032 { 00033 while(!range.empty()) 00034 { 00035 container.push_back(range.front()); 00036 range.step_front(); 00037 } 00038 } 00039 }; 00040 00041 // Helper class for sorted container_range sorting 00042 // the elements by using the given strict weak ordering 00043 // functor passed as the first argument 00044 struct vector_sorter 00045 { 00046 template <typename StrictWeakOrdering, typename Vector> 00047 inline vector_sorter(StrictWeakOrdering sort, Vector& vec) 00048 { 00049 std::sort(vec.begin(), vec.end(), sort); 00050 } 00051 }; 00052 00053 // Base class for the sorted range adaptor 00054 template <typename Range> 00055 class sorted : public container_range< 00056 std::vector<shared<typename range_meta_object<Range>::type> >, 00057 rng2cntr_push_back, 00058 vector_sorter 00059 > 00060 { 00061 private: 00062 typedef container_range< 00063 std::vector<shared<typename range_meta_object<Range>::type> >, 00064 rng2cntr_push_back, 00065 vector_sorter 00066 > base_class; 00067 public: 00068 template <typename SortProc> 00069 sorted(const Range& rng, SortProc sort) 00070 : base_class(rng, sort) 00071 { } 00072 }; 00073 00074 } // namespace aux 00075 00076 #ifdef MIRROR_DOCUMENTATION_ONLY 00077 00078 00086 template <class Range, typename SortProc> 00087 UnspecifiedRange sort(Range range, SortProc sort_proc); 00088 #else 00089 template <class Range, typename SortProc> 00090 inline aux::sorted<Range> sort(Range range, SortProc sort_proc) 00091 { 00092 return aux::sorted<Range>(range, sort_proc); 00093 } 00094 00095 // overload for unadapted Lagoon's meta-object ranges 00096 template <class MetaObject, typename SortProc> 00097 inline aux::sorted<range<MetaObject> > 00098 sort(range<MetaObject> mo_range, SortProc sort_proc) 00099 { 00100 return aux::sorted<range<MetaObject> >( 00101 0, // selector telling to use the std range's begin()/end() 00102 make_std_range(mo_range), 00103 sort_proc 00104 ); 00105 } 00106 #endif 00107 00108 LAGOON_NAMESPACE_END 00109 00110 #endif //include guard 00111