00001 #ifndef UTIL_JOINT_SORT__
00002 #define UTIL_JOINT_SORT__
00003
00004
00005
00006
00007
00008 #include "util/proxy_iterator.hh"
00009
00010 #include <algorithm>
00011 #include <functional>
00012 #include <iostream>
00013
00014 namespace util {
00015
00016 namespace detail {
00017
00018 template <class KeyIter, class ValueIter> class JointProxy;
00019
00020 template <class KeyIter, class ValueIter> class JointIter {
00021 public:
00022 JointIter() {}
00023
00024 JointIter(const KeyIter &key_iter, const ValueIter &value_iter) : key_(key_iter), value_(value_iter) {}
00025
00026 bool operator==(const JointIter<KeyIter, ValueIter> &other) const { return key_ == other.key_; }
00027
00028 bool operator<(const JointIter<KeyIter, ValueIter> &other) const { return (key_ < other.key_); }
00029
00030 std::ptrdiff_t operator-(const JointIter<KeyIter, ValueIter> &other) const { return key_ - other.key_; }
00031
00032 JointIter<KeyIter, ValueIter> &operator+=(std::ptrdiff_t amount) {
00033 key_ += amount;
00034 value_ += amount;
00035 return *this;
00036 }
00037
00038 void swap(const JointIter &other) {
00039 std::swap(key_, other.key_);
00040 std::swap(value_, other.value_);
00041 }
00042
00043 private:
00044 friend class JointProxy<KeyIter, ValueIter>;
00045 KeyIter key_;
00046 ValueIter value_;
00047 };
00048
00049 template <class KeyIter, class ValueIter> class JointProxy {
00050 private:
00051 typedef JointIter<KeyIter, ValueIter> InnerIterator;
00052
00053 public:
00054 typedef struct {
00055 typename std::iterator_traits<KeyIter>::value_type key;
00056 typename std::iterator_traits<ValueIter>::value_type value;
00057 const typename std::iterator_traits<KeyIter>::value_type &GetKey() const { return key; }
00058 } value_type;
00059
00060 JointProxy(const KeyIter &key_iter, const ValueIter &value_iter) : inner_(key_iter, value_iter) {}
00061 JointProxy(const JointProxy<KeyIter, ValueIter> &other) : inner_(other.inner_) {}
00062
00063 operator value_type() const {
00064 value_type ret;
00065 ret.key = *inner_.key_;
00066 ret.value = *inner_.value_;
00067 return ret;
00068 }
00069
00070 JointProxy &operator=(const JointProxy &other) {
00071 *inner_.key_ = *other.inner_.key_;
00072 *inner_.value_ = *other.inner_.value_;
00073 return *this;
00074 }
00075
00076 JointProxy &operator=(const value_type &other) {
00077 *inner_.key_ = other.key;
00078 *inner_.value_ = other.value;
00079 return *this;
00080 }
00081
00082 typename std::iterator_traits<KeyIter>::reference GetKey() const {
00083 return *(inner_.key_);
00084 }
00085
00086 void swap(JointProxy<KeyIter, ValueIter> &other) {
00087 std::swap(*inner_.key_, *other.inner_.key_);
00088 std::swap(*inner_.value_, *other.inner_.value_);
00089 }
00090
00091 private:
00092 friend class ProxyIterator<JointProxy<KeyIter, ValueIter> >;
00093
00094 InnerIterator &Inner() { return inner_; }
00095 const InnerIterator &Inner() const { return inner_; }
00096 InnerIterator inner_;
00097 };
00098
00099 template <class Proxy, class Less> class LessWrapper : public std::binary_function<const typename Proxy::value_type &, const typename Proxy::value_type &, bool> {
00100 public:
00101 explicit LessWrapper(const Less &less) : less_(less) {}
00102
00103 bool operator()(const Proxy &left, const Proxy &right) const {
00104 return less_(left.GetKey(), right.GetKey());
00105 }
00106 bool operator()(const Proxy &left, const typename Proxy::value_type &right) const {
00107 return less_(left.GetKey(), right.GetKey());
00108 }
00109 bool operator()(const typename Proxy::value_type &left, const Proxy &right) const {
00110 return less_(left.GetKey(), right.GetKey());
00111 }
00112 bool operator()(const typename Proxy::value_type &left, const typename Proxy::value_type &right) const {
00113 return less_(left.GetKey(), right.GetKey());
00114 }
00115
00116 private:
00117 const Less less_;
00118 };
00119
00120 }
00121
00122 template <class KeyIter, class ValueIter> class PairedIterator : public ProxyIterator<detail::JointProxy<KeyIter, ValueIter> > {
00123 public:
00124 PairedIterator(const KeyIter &key, const ValueIter &value) :
00125 ProxyIterator<detail::JointProxy<KeyIter, ValueIter> >(detail::JointProxy<KeyIter, ValueIter>(key, value)) {}
00126 };
00127
00128 template <class KeyIter, class ValueIter, class Less> void JointSort(const KeyIter &key_begin, const KeyIter &key_end, const ValueIter &value_begin, const Less &less) {
00129 ProxyIterator<detail::JointProxy<KeyIter, ValueIter> > full_begin(detail::JointProxy<KeyIter, ValueIter>(key_begin, value_begin));
00130 detail::LessWrapper<detail::JointProxy<KeyIter, ValueIter>, Less> less_wrap(less);
00131 std::sort(full_begin, full_begin + (key_end - key_begin), less_wrap);
00132 }
00133
00134
00135 template <class KeyIter, class ValueIter> void JointSort(const KeyIter &key_begin, const KeyIter &key_end, const ValueIter &value_begin) {
00136 JointSort(key_begin, key_end, value_begin, std::less<typename std::iterator_traits<KeyIter>::value_type>());
00137 }
00138
00139 }
00140
00141 namespace std {
00142 template <class KeyIter, class ValueIter> void swap(util::detail::JointIter<KeyIter, ValueIter> &left, util::detail::JointIter<KeyIter, ValueIter> &right) {
00143 left.swap(right);
00144 }
00145
00146 template <class KeyIter, class ValueIter> void swap(util::detail::JointProxy<KeyIter, ValueIter> &left, util::detail::JointProxy<KeyIter, ValueIter> &right) {
00147 left.swap(right);
00148 }
00149 }
00150
00151 #endif // UTIL_JOINT_SORT__