2022-09-18 17:07:08 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Geode/DefaultInclude.hpp>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
template<class C, class T>
|
|
|
|
bool contains(C const& cont, T const& elem) {
|
|
|
|
return std::find(cont.begin(), cont.end(), elem) != cont.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
template<class C, class T>
|
|
|
|
bool contains(C const& cont, std::function<bool(T)> containFunc) {
|
|
|
|
for (auto const& item : cont) {
|
|
|
|
if (containFunc(item))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09-19 16:03:50 -04:00
|
|
|
template<class C>
|
2022-09-18 17:07:08 -04:00
|
|
|
std::string join(C const& cont, std::string const& sep) {
|
|
|
|
std::string res = "";
|
|
|
|
bool first = true;
|
|
|
|
for (auto p : cont) {
|
|
|
|
if (!first) {
|
|
|
|
res += sep;
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
res += std::to_string(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
template<class C, class C2, class T, class T2>
|
|
|
|
C2 map(C const& cont, std::function<T2(T)> mapFunc) {
|
|
|
|
C2 res;
|
|
|
|
std::transform(cont.begin(), cont.end(), res.end(), mapFunc);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|