/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#pragma once

#include <type_traits>
#include <iterator>
#include <boost/optional.hpp>

namespace LeechCraft
{
namespace Util
{
	template<typename T1, typename T2, template<typename U> class Container, typename F>
	auto ZipWith (const Container<T1>& c1, const Container<T2>& c2, F f) -> Container<typename std::result_of<F (T1, T2)>::type>
	{
		Container<typename std::result_of<F (T1, T2)>::type> result;

		auto i1 = std::begin (c1), e1 = std::end (c1);
		auto i2 = std::begin (c2), e2 = std::end (c2);
		for ( ; i1 != e1 && i2 != e2; ++i1, ++i2)
			result.push_back (f (*i1, *i2));
		return result;
	}

	template<typename T1, typename T2,
		template<typename U> class Container,
		template<typename U1, typename U2> class Pair = QPair>
	auto Zip (const Container<T1>& c1, const Container<T2>& c2) -> Container<Pair<T1, T2>>
	{
		return ZipWith (c1, c2,
				[] (const T1& t1, const T2& t2) -> Pair<T1, T2>
					{ return { t1, t2}; });
	}

	template<typename T, template<typename U> class Container, typename F>
	auto Map (const Container<T>& c, F f) -> typename std::enable_if<!std::is_same<void, typename std::result_of<F (T)>::type>::value,
			Container<typename std::decay<typename std::result_of<F (T)>::type>::type>>::type
	{
		Container<typename std::decay<typename std::result_of<F (T)>::type>::type> result;
		for (auto t : c)
			result.push_back (f (t));
		return result;
	}

	template<typename T, template<typename U> class Container, typename F>
	typename std::enable_if<std::is_same<void, typename std::result_of<F (T)>::type>::value, void>::type Map (const Container<T>& c, F f)
	{
		for (auto t : c)
			f (t);
	}

	template<typename T, template<typename U> class Container, typename F>
	auto Filter (const Container<T>& c, F f) -> Container<T>
	{
		Container<T> result;
		std::copy_if (c.begin (), c.end (), std::back_inserter (result), f);
		return result;
	}

	template<template<typename> class Container, typename T>
	Container<T> Concat (const Container<Container<T>>& containers)
	{
		Container<T> result;
		for (const auto& cont : containers)
			std::copy (cont.begin (), cont.end (), std::back_inserter (result));
		return result;
	}

	template<template<typename> class Container, typename T>
	Container<Container<T>> SplitInto (size_t numChunks, const Container<T>& container)
	{
		Container<Container<T>> result;

		const size_t chunkSize = container.size () / numChunks;
		for (size_t i = 0; i < numChunks; ++i)
		{
			Container<T> subcont;
			const auto start = container.begin () + chunkSize * i;
			const auto end = start + chunkSize;
			std::copy (start, end, std::back_inserter (subcont));
			result.push_back (subcont);
		}

		const auto lastStart = container.begin () + chunkSize * numChunks;
		const auto lastEnd = container.end ();
		std::copy (lastStart, lastEnd, std::back_inserter (result.front ()));

		return result;
	}

	template<template<typename Pair, typename... Rest> class Cont, template<typename K, typename V> class Pair, typename K, typename V, typename KV, typename... Rest>
	boost::optional<V> Lookup (const KV& key, const Cont<Pair<K, V>, Rest...>& cont)
	{
		for (const auto& pair : cont)
			if (pair.first == key)
				return pair.second;

		return {};
	}
}
}

