/*
 * Copyright 2015 - 2025 gary@drinkingtea.net
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include "def.hpp"
#include "error.hpp"

OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)

namespace ox {

template<typename It, typename T>
constexpr ox::Result<size_t> findIdx(It begin, It end, T const&value) {
	auto it = begin;
	for (; it != end; ++it) {
		if (*it == value) {
			return it.offset();
		}
	}
	return ox::Error{1, "item not found"};
}

template<typename It, typename T>
constexpr It find(It begin, It end, T const&value) {
	for (; begin != end; ++begin) {
		if (*begin == value) {
			return begin;
		}
	}
	return end;
}

template<typename It>
constexpr It find_if(It begin, It end, auto predicate) {
	for (; begin != end; ++begin) {
		if (predicate(*begin)) {
			return begin;
		}
	}
	return end;
}

template<typename It, typename Size, typename OutIt>
constexpr OutIt copy_n(It in, Size cnt, OutIt out) {
	for (Size i = 0; i < cnt; ++i) {
		*out = *in;
		++out;
		++in;
	}
	return out;
}

}

OX_CLANG_NOWARN_END
