/*** Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: auContainerUtils.hpp Date: 2022-2-1 File: AuroraUtils.hpp File: auROXTLUtils.hpp Date: 2021-6-9 Author: Reece ***/ #pragma once template inline bool _AuRemoveIfBase(T &in, const AuPredicate &> &predicate); template inline bool AuRemoveAllIf(T &in, const AuPredicate &> &predicate) { return _AuRemoveIfBase(in, predicate); } template inline bool AuRemoveIf(T &in, const AuPredicate &> &predicate) { return _AuRemoveIfBase(in, predicate); } template inline bool AuTryRemove(T &in, const Z &type) { try { auto itr = in.find(type); if (itr == in.end()) { return false; } in.erase(itr); return true; } catch (...) { return false; } } template inline void AuRemove(T &in, const Z &type) { auto itr = in.find(type); if (itr == in.end()) { return; } in.erase(itr); } template inline void AuRemoveRange(T &in, AuUInt index, AuUInt length) { if (index + length > in.size()) { return; } auto begin = in.begin(); std::advance(begin, index); auto end = begin; std::advance(end, length); while (begin != end) { in.erase(begin++); } } template inline bool AuTryRemoveRange(T &in, AuUInt index, AuUInt length) { try { if (index + length > in.size()) { return false; } auto begin = in.begin(); auto end = begin; std::advance(end, index + length); while (begin != end) { in.erase(begin++); } } catch (...) { return false; } return true; } template inline bool AuTryFind(Map &map, const Key &key, Value *&ptr) { auto itr = map.find(key); if (itr != map.end()) { ptr = &itr->second; return true; } else { ptr = nullptr; return false; } } template inline bool AuTryFind(Map *map, const Key &key, Value *&ptr) { auto itr = map->find(key); if (itr != map->end()) { ptr = &itr->second; return true; } else { ptr = nullptr; return false; } } template inline bool AuTryFind(Map &map, const Key &key) { auto itr = map.find(key); if (itr != map.end()) { return true; } else { return false; } } template inline auto AuTryFindByTupleN(List &list, const Key &key) { for (auto itr = list.begin(); itr != list.end(); ) { if (AuGet(*itr) == key) { return itr; } else { itr++; } } return list.end(); } template inline bool AuExists(Range &a, const Key &item) { return std::find(a.begin(), a.end(), item) != a.end(); } template inline bool AuTryFind(Map *map, const Key &key) { auto itr = map->find(key); if (itr != map->end()) { return true; } else { return false; } } template inline bool AuTryFindGeneric(Map &map, const Key &key, Value *&ptr) { auto itr = map.find(key); if (itr != map.end()) { ptr = &*itr; return true; } else { ptr = nullptr; return false; } } template inline bool AuTryFindGeneric(Map &map, const Key &key) { if (map.find(key) != map.end()) { return true; } else { return false; } } template inline bool AuTryClear(Container &container) { container.clear(); // clears are generally noexcept. return true; } template inline bool AuTryDelete(Map &map, const Key &key) { auto itr = map.find(key); if (itr != map.end()) { map.erase(itr); return true; } else { return false; } } /// @deprecated -> Should've been named AuTryRemoveList template inline bool AuTryDeleteList(List &list, const Key &key) { auto itr = std::find(list.begin(), list.end(), key); if (itr != list.end()) { list.erase(itr); return true; } else { return false; } } template inline bool AuTryRemoveList(List &list, const Key &key) { auto itr = std::find(list.begin(), list.end(), key); if (itr != list.end()) { list.erase(itr); return true; } else { return false; } } template inline bool AuTryRemoveByTupleN(List &list, const Key &key) { for (auto itr = list.begin(); itr != list.end(); ) { if (AuGet(*itr) == key) { itr = list.erase(itr); return true; } else { itr++; } } return false; } #include template inline bool AuTryInsert(Container &container, const Key_t &key, Type_t &&value) { try { auto itr = container.find(key); if (itr == container.end()) { container.insert(AuMakePair(key, value)); } else { itr->second = value; } return true; } catch (...) { return false; } } template inline bool AuTryInsert(Container &container, const Type &value) { try { container.insert(container.end(), value); return true; } catch (...) { return false; } } template inline bool AuTryInsert(Container &container, Type &&value) { try { container.insert(container.end(), value); return true; } catch (...) { return false; } } template inline bool AuTryInsert(Container *container, const Type &value) { try { container->insert(container->end(), value); return true; } catch (...) { return false; } } template inline bool AuTryInsert(Container *container, Type &&value) { try { container->insert(container->end(), value); return true; } catch (...) { return false; } } template inline bool AuTryInsertNoEnd(Container &container, Type &&value) // move { try { container.insert(value); return true; } catch (...) { return false; } } template inline bool AuTryInsertNoEnd(Container &container, const Type &value) // copy { try { container.insert(value); return true; } catch (...) { return false; } } template inline bool AuTryInsertNoEnd(Container *container, Type &&value) // move { try { container->insert(value); return true; } catch (...) { return false; } } template inline bool AuTryInsertNoEnd(Container *container, const Type &value) // copy { try { container->insert(value); return true; } catch (...) { return false; } } namespace Aurora::Memory { struct ByteBuffer; } template inline bool AuTryResize(T &list, AuUInt length) { try { if constexpr (AuIsSame_v) { return list.Resize(length); } else { list.resize(length); return true; } } catch (...) { return false; } } template inline bool AuTryDownsize(T &list, AuUInt length) { try { if constexpr (AuIsSame_v) { if (!list.Resize(length)) { return false; } list.GC(); return true; } else { list.resize(length); list.shrink_to_fit(); return true; } } catch (...) { return false; } } template inline bool _AuRemoveIfBase(T &in, const AuPredicate &> &predicate) { bool retOne {}; for (auto itr = in.begin(); itr != in.end(); ) { if (predicate(*itr)) { itr = in.erase(itr); if constexpr (!all) { return true; } retOne = true; } else { itr++; } } // optimization hint if constexpr (all) return retOne; return {}; }