123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #pragma once
-
- #include <optional>
- #include <functional>
- #include <memory>
- #include <chrono>
- #include <type_traits>
- #include <stddef.h>
-
- namespace Swan {
-
- // Inherit from this class to make a class non-copyable
- class NonCopyable {
- protected:
- NonCopyable() = default;
- NonCopyable(NonCopyable &&) = default;
-
- NonCopyable(const NonCopyable &) = delete;
- NonCopyable &operator=(const NonCopyable &) = delete;
- };
-
- template<typename T, typename Del = void (*)(T *)>
- using RaiiPtr = std::unique_ptr<T, Del>;
-
- template<typename T, typename Del>
- RaiiPtr<T, Del> makeRaiiPtr(T *val, Del d) {
- return std::unique_ptr<T, Del>(val, d);
- }
-
- template<typename Func>
- class Deferred: NonCopyable {
- public:
- Deferred(Func func): func_(func) {}
- Deferred(Deferred &&def) noexcept: func_(def.func_) { def.active_ = false; }
- ~Deferred() { if (active_) func_(); }
-
- Deferred &operator=(Deferred &&def) noexcept {
- func_ = def.func_;
- def.active_ = false;
- return *this;
- }
-
- private:
- Func func_;
- bool active_ = true;
- };
-
- template<typename Func>
- Deferred<Func> makeDeferred(Func func) {
- return Deferred(func);
- }
-
- // Ret can't be a reference, because C++ doesn't support optional<T&>.
- template<typename Ret, typename Func = std::function<std::optional<Ret>()>>
- class Iter {
- public:
- class It {
- public:
- It(std::optional<Ret> next, Func &func): next_(std::move(next)), func_(func) {}
-
- bool operator==(const It &other) {
- return next_ == std::nullopt && other.next_ == std::nullopt;
- }
-
- bool operator!=(const It &other) {
- return !(*this == other);
- }
-
- void operator++() {
- next_ = std::move(func_());
- }
-
- Ret operator*() {
- return std::move(*next_);
- }
-
- private:
- std::optional<Ret> next_;
- Func &func_;
- };
-
- Iter(Func func): func_(func) {}
-
- operator Iter<Ret, std::function<std::optional<Ret>()>>() {
- return Iter<Ret, std::function<std::optional<Ret>()>>(func_);
- }
-
- It begin() {
- return It(func_(), func_);
- }
-
- It end() {
- return It(std::nullopt, func_);
- }
-
- private:
- Func func_;
- };
-
- template<typename InputIterator, typename Func>
- auto map(InputIterator first, InputIterator last, Func func) {
- using RetT = decltype(func(*first));
-
- auto l = [=]() mutable -> std::optional<RetT> {
- if (first == last)
- return std::nullopt;
-
- RetT r = func(*first);
- ++first;
- return r;
- };
-
- return Iter<RetT, decltype(l)>(l);
- }
-
- template<typename InputIterator, typename Func>
- auto filter(InputIterator first, InputIterator last, Func pred) {
- using RetT = std::remove_reference_t<decltype(*first)>;
-
- auto l = [=]() mutable -> std::optional<RetT> {
- if (first == last)
- return std::nullopt;
-
- while (!pred(*first)) {
- ++first;
- if (first == last)
- return std::nullopt;
- }
-
- RetT r = *first;
- ++first;
- return r;
- };
-
- return Iter<RetT, decltype(l)>(l);
- }
-
- template<typename InputIterator, typename Func>
- auto mapFilter(InputIterator first, InputIterator last, Func func) {
- using RetT = std::remove_reference_t<decltype(*func(*first))>;
-
- auto l = [=]() mutable -> std::optional<RetT> {
- if (first == last)
- return std::nullopt;
-
- std::optional<RetT> r;
- while ((r = func(*first)) == std::nullopt) {
- ++first;
- if (first == last)
- return std::nullopt;
- }
-
- ++first;
- return r;
- };
-
- return Iter<RetT, decltype(l)>(l);
- }
-
- }
|