// We get the last iterator pair if these two is soooo match template <typename InputIterator_a, typename InputIterator_b> inline pair<InputIterator_a, InputIterator_b> mismatch(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b) { while (first_a != last_a && *first_a == *first_b) { ++first_a; ++first_b; } return pair<InputIterator_a, InputIterator_b>(first_a, first_b); } template <typename InputIterator_a, typename InputIterator_b, typename BinaryPredicate> // BinaryPredicate could be a functor inline pair<InputIterator_a, InputIterator_b> mismatch(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, BinaryPredicate binary_predicate) { // The binary predicate should handle the content the iterator point to // so we should pass content the iterator point to rather than the iterator // to the binary predicate to handle while (first_a != last_a && binary_predicate(*first_a, *first_b)) { ++first_a; ++first_b; } return pair<InputIterator_a, InputIterator_b>(first_a, first_b); } // 有时候咱们并不须要获得mismatch的iterator组成的pair // 这给咱们一种两层封装的感受,咱们可能只要mismatch的value组成的pair // 因此我直接封装了这样一个函数,知足需求 // 这个函数也有他的BinaryPredicate版本 template <typename InputIterator_a, typename InputIterator_b> inline pair<typename iterator_traits<InputIterator_a>::value_type, typename iterator_traits<InputIterator_b>::value_type> mismatch_content(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b) { while (first_a != last_a && *first_a == *first_b) { ++first_a; ++first_b; } return pair<typename iterator_traits<InputIterator_a>::value_type, typename iterator_traits<InputIterator_b>::value_type> (*first_a, *first_b); } template <typename InputIterator_a, typename InputIterator_b, typename BinaryPredicate> inline pair<typename iterator_traits<InputIterator_a>::value_type, typename iterator_traits<InputIterator_b>::value_type> mismatch_content(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, BinaryPredicate binary_predicate) { while (first_a != last_a && binary_predicate(*first_a, *first_b)) { ++first_a; ++first_b; } return pair<typename iterator_traits<InputIterator_a>::value_type, typename iterator_traits<InputIterator_b>::value_type> (*first_a, *first_b); } // 遍历式的实现,当咱们找到不符合的部分时,就直接退出,减小CPU占用 template <typename InputIterator_a, typename InputIterator_b> inline bool equal(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b) { while (first_a != last_a) { if (*first_a == *first_b) { ++first_a; ++first_b; } else { return false; } } return true; } template <typename InputIterator_a, typename InputIterator_b, typename BinaryPredicate> inline bool equal(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, BinaryPredicate binary_predicate) { while (first_a != last_a) { if (binary_predicate(*first_a, *first_b)) { ++first_a; ++first_b; } else { return false; } } return true; } // I added this function for the reason the same as we added the function fill_n template <typename InputIterator_a, typename _Size, typename InputIterator_b> inline bool equal_n(InputIterator_a first_a, _Size size, InputIterator_b first_b) { while (size != _Size(0)) { if (*first_a == *first_b) { --size; ++first_a; ++first_b; } else { return false; } } return true; } template <typename InputIterator_a, typename _Size, typename InputIterator_b, typename BinaryPredicate> inline bool equal(InputIterator_a first_a, _Size size, InputIterator_b first_b, BinaryPredicate binary_predicate) { while (size != _Size(0)) { if (binary_predicate(*first_a, *first_b)) { --size; ++first_a; ++first_b; } else { return false; } } return true; } // 来自有道字典:lexicographic -- 字典式的, 以字典序比较两个list // 多用于比较两个字符串,因此有专门对于字符串的优化 // 在a < b时返回true template <typename InputIterator_a, typename InputIterator_b> inline bool lexicographical_compare(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, InputIterator_b last_b) { for (; first_a != last_a || first_b != last_b; ++first_a, ++first_b) { if (*first_b < *first_a) return true; if (*first_a < *first_b) return false; } // Note:: must be && here!! return first_a == last_a && first_b != last_b; } // STL有一个对于字符串的优化版本,这个版本借用C的函数实现 // 看来C的函数确实在char*处理方面效率很高,多多学习,之后能够用上 // 可是这里CHAR_MAX == SCHAR_MAX的部分实在不懂,为什么要这样? // 因此我这里只使用了unsigned char*的实现代替了char*的实现 // 好像还挺好用 inline bool lexicographical_compare(const char* first_a, const char* last_a, const char* first_b, const char* last_b) { const size_t len_a = last_a - first_a; const size_t len_b = last_b - first_b; const int result = memcmp(first_a, first_b, min(len_a, len_b)); return result != 0 ? result < 0 : len_a < len_b; } // inline bool // lexicographical_compare(const char* first_a, // const char* last_a, // const char* first_b, // const char* last_b) // { // const size_t len_a = last_a - first_a; // const size_t len_b = last_b - first_b; // const int result = memcmp(first_a, first_b, min(len_a, len_b)); // knife::printf("Result: ", result); // return result != 0 ? result < 0 : len_a < len_b; // } // inline bool // lexicographical_compare(const char* first1, // const char* last1, // const char* first2, // const char* last2) // { // #if CHAR_MAX == SCHAR_MAX // return lexicographical_compare( (const signed char*) first1, // (const signed char*) last1, // (const signed char*) first2, // (const signed char*) last2); // #else // return lexicographical_compare( (const unsigned char*) first1, // (const unsigned char*) last1, // (const unsigned char*) first2, // (const unsigned char*) last2); // #endif // } template <typename InputIterator_a, typename InputIterator_b, typename Compare> inline bool lexicographical_compare(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, InputIterator_b last_b, Compare comp) { for (; first_a != last_a || first_b != last_b; ++first_a, ++first_b) { if (comp(*first_a, *first_b)) return true; if (comp(*first_a, *first_b)) return false; } // Note:: must be && here!! return first_a == last_a && first_b != last_b; } // 我更改了一下实现的方法,我的认为比原版的好,这里贴出原版的 // 各位能够比较一下 template <typename InputIterator_a, typename InputIterator_b> inline int lexicographical_compare_3way(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, InputIterator_b last_b) { static int more = 1; static int less = -1; static int equal = 0; for (; first_a != last_a || first_b != last_b; ++first_a, ++first_b) { if (*first_a < *first_b) return less; if (*first_b < *first_a) return more; } return (first_a == last_a) ? ((first_b == last_b) ? equal : less) : more; } // 如下都属于原版 // template <class InputIterator1, class InputIterator2> // int lexicographical_compare_3way(InputIterator1 first1, InputIterator1 last1, // InputIterator2 first2, InputIterator2 last2) // { // while (first1 != last1 && first2 != last2) { // if (*first1 < *first2) return -1; // if (*first2 < *first1) return 1; // ++first1; ++first2; // } // // if (first2 == last2) { // return !(first1 == last1); // } else { // return -1; // } // } template <typename InputIterator_a, typename InputIterator_b, typename Compare> inline int lexicographical_compare_3way(InputIterator_a first_a, InputIterator_a last_a, InputIterator_b first_b, InputIterator_b last_b, Compare comp) { static int more = 1; static int less = -1; static int equal = 0; for (; first_a != last_a || first_b != last_b; ++first_a, ++first_b) { if (comp(*first_a, *first_b)) return less; if (comp(*first_b, *first_a)) return more; } return (first_a == last_a) ? ((first_b == last_b) ? equal : less) : more; } // 这里有一个例外:如"apple"和"appleapple"比较返回的就是-5 // 这里有点没搞懂他设计的想法,难道不是a比b小的时候返回-1吗? // 他的想法貌似是对两个字符串比较时,咱们还须要得出一些额外的值 // 用以告诉user更多信息,但为何不在template的部分也这么作呢? // 恰恰要在特化部分这么作 inline int lexicographical_compare_3way(const char* first_a, const char* last_a, const char* first_b, const char* last_b) { const size_t len_a = last_a - first_a; const size_t len_b = last_b - first_b; const int result = memcmp(first_a, first_b, min(len_a, len_b)); knife::printf(len_a, len_b); return result == 0 ? len_a - len_b : result; } // 后面的问题很lexicographical_compare同样,就不举例了 // SGI的做者真是直接额,不知道的就直接叫pointer // 对于我这种命名强迫症患者真是解脱 template <typename T> inline void destroy(T* pointer) { pointer->~T(); } // placement new for initialize A_Type in // the place where the A_Type* pointer point // 这个placement new 很是的暴力,若是你传个临时变量 // 的地址过来,他也会执行的,这个函数会在你只重载了 // void* operator new(size_t)的时候报错,由于没有 // void* operator new(size_t, B_Type*)的版本 // 看来世上的事不能老是保证正确,虽然咱们已经尽力了 template <typename A_Type, typename B_Type> inline void construct(A_Type* buffer, const B_Type& value) { new (buffer) A_Type(value); } template <typename ForwardIterator> inline void __destroy_aux_aux(ForwardIterator first, ForwardIterator last, forward_iterator_tag) { for (; first != last; ++first) destroy(&*first); } template <typename RandomAccessIterator, typename Distance> inline void __destroy_aux_aux_d(RandomAccessIterator first, RandomAccessIterator last, Distance*) { for (Distance d = last - first; d != Distance(0); --d, ++first) destroy(&*first); } // 相对于原文我又作了一次封装,相信你们不介意的吧 template <typename RandomAccessIterator> inline void __destroy_aux_aux(RandomAccessIterator first, RandomAccessIterator last, random_access_iterator_tag) { __destroy_aux_aux_d(first, last, distance_type(first)); } template <typename ForwardIterator> inline void _destroy_aux(ForwardIterator first, ForwardIterator last, __false_type) { __destroy_aux_aux(first, last, iterator_category(first)); } // trivial destructor即无用的destructor,因此能够不调用 template <typename ForwardIterator> inline void _destroy_aux(ForwardIterator first, ForwardIterator last, __true_type) {} // 为啥要叫_destroy_aux?!!!!,aux是辅助的意思 // 为了获得iterator所指的类型,咱们不得不作一次这样的包装 template <typename ForwardIterator, typename Content> inline void _destroy(ForwardIterator first, ForwardIterator last, const Content&) { _destroy_aux(first, last, __type_traits<Content>::has_trivial_destructor()); } template <typename ForwardIterator> inline void destroy(ForwardIterator first, ForwardIterator last) { _destroy(first, last, value_type(first)); } // char类型固然有trivial_destructor,但你们貌似常常对char*作这样的操做 // 为了优化,就直接写了这样一个特化版本,这是C++ GP里面很是重要经常使用的方法 // 学习了! inline void destroy(char*, char*) {} inline void destroy(wchar_t*, wchar_t*) {} _STL_NAMESPACE_END #endif