//
// Copyright (c) 2017-2026, Manticore Software LTD (https://manticoresearch.com)
// Copyright (c) 2001-2016, Andrew Aksyonoff
// Copyright (c) 2008-2016, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org
//

#include "thread_annotations.h"

template<typename CONTAINER, typename FILTER>
FORCE_INLINE bool all_of ( const CONTAINER& dData, FILTER cond ) NO_THREAD_SAFETY_ANALYSIS
{
	for ( const auto& dItem : dData )
		if ( !cond ( dItem ) )
			return false;
	return true;
}

template<typename CONTAINER, typename FILTER>
FORCE_INLINE bool any_of ( const CONTAINER& dData, FILTER cond ) NO_THREAD_SAFETY_ANALYSIS
{
	for ( const auto& dItem : dData )
		if ( cond ( dItem ) )
			return true;

	return false;
}

template<typename CONTAINER, typename FILTER>
FORCE_INLINE bool none_of ( const CONTAINER& dData, FILTER cond ) NO_THREAD_SAFETY_ANALYSIS
{
	return !any_of ( dData, std::forward<FILTER> ( cond ) );
}

template<typename CONTAINER, typename FILTER>
FORCE_INLINE int64_t count_of ( const CONTAINER& dData, FILTER cond ) NO_THREAD_SAFETY_ANALYSIS
{
	int64_t iRes = 0;
	for ( const auto& dItem : dData )
		if ( cond ( dItem ) )
			++iRes;

	return iRes;
}

template<typename INT, typename CONTAINER, typename SIZER>
FORCE_INLINE INT sum_of ( const CONTAINER& dData, SIZER amount ) NO_THREAD_SAFETY_ANALYSIS
{
	INT iRes = 0;
	for ( const auto& dItem : dData )
		iRes += amount (dItem);

	return iRes;
}

template<typename CONTAINER, typename ACTION>
FORCE_INLINE void for_each ( CONTAINER&& dData, ACTION fnAction ) NO_THREAD_SAFETY_ANALYSIS
{
	for ( auto& dItem : dData )
		fnAction ( dItem );
}