【试着自己写一个STL 】Knife_STL —— algobase.h 2

// 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


转载于:https://my.oschina.net/u/565749/blog/189659

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值