2022-09-18 17:07:08 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Geode/DefaultInclude.hpp>
|
|
|
|
#include <algorithm>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <string>
|
2022-09-18 17:07:08 -04:00
|
|
|
|
2022-09-26 06:53:40 -04:00
|
|
|
namespace geode::utils::container {
|
2022-09-18 17:07:08 -04:00
|
|
|
/**
|
|
|
|
* Check if a container contains an element by value.
|
|
|
|
* @param vec The vector to check.
|
|
|
|
* @param elem The element to find.
|
|
|
|
* @returns True if element is in `vec`, false if not.
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class C, class T>
|
|
|
|
[[deprecated("Use geode::utils::ranges::contains instead")]] bool contains(
|
|
|
|
C const& cont, T const& elem
|
|
|
|
) {
|
2022-09-18 17:07:08 -04:00
|
|
|
return std::find(cont.begin(), cont.end(), elem) != cont.end();
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
/**
|
2022-09-18 17:07:08 -04:00
|
|
|
* Check if a vector contains an element via a function.
|
|
|
|
* @param vec The vector to check.
|
|
|
|
* @param containFunc A function that returns bool if the
|
|
|
|
* element parameter is what is looked for.
|
|
|
|
* @returns True if an element matching `containFunc` is
|
|
|
|
* in `vec`, false if not.
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class C, class T>
|
|
|
|
[[deprecated("Use geode::utils::ranges::contains instead")]] bool contains(
|
|
|
|
C const& cont, std::function<bool(T)> containFunc
|
|
|
|
) {
|
2022-09-18 17:07:08 -04:00
|
|
|
for (auto const& item : cont) {
|
2022-10-30 14:59:20 -04:00
|
|
|
if (containFunc(item)) return true;
|
2022-09-18 17:07:08 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
/**
|
2022-09-18 17:07:08 -04:00
|
|
|
* Turn a vector into a string. T must be either a string,
|
|
|
|
* or convertible via `std::to_string`.
|
|
|
|
* @param vec The vector to add to.
|
|
|
|
* @param sep Separator string.
|
|
|
|
* @returns Joined string.
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class C>
|
|
|
|
[[deprecated("Use geode::utils::ranges::join instead")]] std::string join(
|
|
|
|
C const& cont, std::string const& sep
|
|
|
|
) {
|
2022-09-18 17:07:08 -04:00
|
|
|
std::string res = "";
|
|
|
|
bool first = true;
|
|
|
|
for (auto p : cont) {
|
|
|
|
if (!first) {
|
|
|
|
res += sep;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
else {
|
2022-09-18 17:07:08 -04:00
|
|
|
first = false;
|
|
|
|
}
|
2022-09-19 16:03:50 -04:00
|
|
|
if constexpr (std::is_same_v<decltype(p), std::string>) {
|
2022-09-18 17:07:08 -04:00
|
|
|
res += p;
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
else {
|
2022-09-18 17:07:08 -04:00
|
|
|
res += std::to_string(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
/**
|
2022-09-18 17:07:08 -04:00
|
|
|
* Map a container of items type T to a new container of items
|
|
|
|
* type T2.
|
|
|
|
* @param vec The container to map.
|
|
|
|
* @param mapFunc Function that converts an element from T to T2.
|
|
|
|
* @returns Mapped container.
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class C, class C2, class T, class T2>
|
|
|
|
[[deprecated("Use geode::utils::ranges::map instead")]] C2 map(
|
|
|
|
C const& cont, std::function<T2(T)> mapFunc
|
|
|
|
) {
|
2022-09-18 17:07:08 -04:00
|
|
|
C2 res;
|
|
|
|
std::transform(cont.begin(), cont.end(), res.end(), mapFunc);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|