#pragma once #include #include #include #include #include #include #include #include #include #include #include // namespace boolinq { struct LinqEndException {}; enum BytesDirection { BytesFirstToLast, BytesLastToFirst, }; enum BitsDirection { BitsHighToLow, BitsLowToHigh, }; template class Linq { std::function nextFunc; S storage; public: typedef T value_type; Linq() : nextFunc(), storage() {} Linq(S storage, std::function nextFunc) : nextFunc(nextFunc), storage(storage) {} T next() { return nextFunc(storage); } void for_each_i(std::function apply) const { Linq linq = *this; try { for (int i = 0; ; i++) { apply(linq.next(), i); } } catch (LinqEndException&) {} } void for_each(std::function apply) const { return for_each_i([apply](T value, int) { return apply(value); }); } Linq, int>, T> where_i(std::function filter) const { return Linq, int>, T>( std::make_tuple(*this, 0), [filter](std::tuple, int>& tuple) { Linq& linq = std::get<0>(tuple); int& index = std::get<1>(tuple); while (true) { T ret = linq.next(); if (filter(ret, index++)) { return ret; } } } ); } Linq, int>, T> where(std::function filter) const { return where_i([filter](T value, int) { return filter(value); }); } Linq, int>, T> take(int count) const { return where_i([count](T /*value*/, int i) { if (i == count) { throw LinqEndException(); } return true; }); } Linq, int>, T> takeWhile_i(std::function predicate) const { return where_i([predicate](T value, int i) { if (!predicate(value, i)) { throw LinqEndException(); } return true; }); } Linq, int>, T> takeWhile(std::function predicate) const { return takeWhile_i([predicate](T value, int /*i*/) { return predicate(value); }); } Linq, int>, T> skip(int count) const { return where_i([count](T, int i) { return i >= count; }); } Linq, int, bool>, T> skipWhile_i(std::function predicate) const { return Linq, int, bool>, T>( std::make_tuple(*this, 0, false), [predicate](std::tuple, int, bool>& tuple) { Linq& linq = std::get<0>(tuple); int& index = std::get<1>(tuple); bool& flag = std::get<2>(tuple); if (flag) { return linq.next(); } while (true) { T ret = linq.next(); if (!predicate(ret, index++)) { flag = true; return ret; } } } ); } Linq, int, bool>, T> skipWhile(std::function predicate) const { return skipWhile_i([predicate](T value, int /*i*/) { return predicate(value); }); } template Linq, std::vector, int>, T> append(Types ... newValues) const { return Linq, std::vector, int>, T>( std::make_tuple(*this, std::vector{ newValues ... }, -1), [](std::tuple, std::vector, int>& tuple) { Linq& linq = std::get<0>(tuple); std::vector& values = std::get<1>(tuple); int& index = std::get<2>(tuple); if (index == -1) { try { return linq.next(); } catch (LinqEndException&) { index = 0; } } if (index < values.size()) { return values[index++]; } throw LinqEndException(); } ); } template Linq, std::vector, int>, T> prepend(Types ... newValues) const { return Linq, std::vector, int>, T>( std::make_tuple(*this, std::vector{ newValues ... }, 0), [](std::tuple, std::vector, int>& tuple) { Linq& linq = std::get<0>(tuple); std::vector& values = std::get<1>(tuple); int& index = std::get<2>(tuple); if (index < values.size()) { return values[index++]; } return linq.next(); } ); } template::type> Linq, int>, _TRet> select_i(F apply) const { return Linq, int>, _TRet>( std::make_tuple(*this, 0), [apply](std::tuple, int>& tuple) { Linq& linq = std::get<0>(tuple); int& index = std::get<1>(tuple); return apply(linq.next(), index++); } ); } template::type> Linq, int>, _TRet> select(F apply) const { return select_i([apply](T value, int /*index*/) { return apply(value); }); } template Linq, int>, TRet> cast() const { return select_i([](T value, int /*i*/) { return TRet(value); }); } template Linq, Linq, bool>, T> concat(const Linq& rhs) const { return Linq, Linq, bool>, T>( std::make_tuple(*this, rhs, false), [](std::tuple, Linq, bool>& tuple) { Linq& first = std::get<0>(tuple); Linq& second = std::get<1>(tuple); bool& flag = std::get<2>(tuple); if (!flag) { try { return first.next(); } catch (LinqEndException&) {} } return second.next(); } ); } template< typename F, typename _TRet = typename std::result_of::type, typename _TRetVal = typename _TRet::value_type > Linq, _TRet, int, bool>, _TRetVal> selectMany_i(F apply) const { return Linq, _TRet, int, bool>, _TRetVal>( std::make_tuple(*this, _TRet(), 0, true), [apply](std::tuple, _TRet, int, bool>& tuple) { Linq& linq = std::get<0>(tuple); _TRet& current = std::get<1>(tuple); int& index = std::get<2>(tuple); bool& finished = std::get<3>(tuple); while (true) { if (finished) { current = apply(linq.next(), index++); finished = false; } try { return current.next(); } catch (LinqEndException&) { finished = true; } } } ); } template< typename F, typename _TRet = typename std::result_of::type, typename _TRetVal = typename _TRet::value_type > Linq, _TRet, int, bool>, _TRetVal> selectMany(F apply) const { return selectMany_i([apply](T value, int) { return apply(value); }); } template< typename F, typename _TKey = typename std::result_of::type, typename _TValue = Linq, int>, T> // where(predicate) > Linq, Linq, std::unordered_set<_TKey>>, std::pair<_TKey, _TValue>> groupBy(F apply) const { return Linq, Linq, std::unordered_set<_TKey>>, std::pair<_TKey, _TValue>>( std::make_tuple(*this, *this, std::unordered_set<_TKey>()), [apply](std::tuple, Linq, std::unordered_set<_TKey>>& tuple) { Linq& linq = std::get<0>(tuple); Linq& linqCopy = std::get<1>(tuple); std::unordered_set<_TKey>& set = std::get<2>(tuple); while (true) { _TKey key = apply(linq.next()); if (set.insert(key).second) { return std::make_pair(key, linqCopy.where([apply, key](T v) { return apply(v) == key; })); } } } ); } template::type> Linq, std::unordered_set<_TRet>>, T> distinct(F transform) const { return Linq, std::unordered_set<_TRet>>, T>( std::make_tuple(*this, std::unordered_set<_TRet>()), [transform](std::tuple, std::unordered_set<_TRet>>& tuple) { Linq& linq = std::get<0>(tuple); std::unordered_set<_TRet>& set = std::get<1>(tuple); while (true) { T value = linq.next(); if (set.insert(transform(value)).second) { return value; } } } ); } Linq, std::unordered_set>, T> distinct() const { return distinct([](T value) { return value; }); } template::const_iterator> Linq, _TIter, bool>, T> orderBy(F transform) const { std::vector items = toStdVector(); std::sort(items.begin(), items.end(), [transform](const T& a, const T& b) { return transform(a) < transform(b); }); return Linq, _TIter, bool>, T>( std::make_tuple(items, _TIter(), false), [](std::tuple, _TIter, bool>& tuple) { std::vector& vec = std::get<0>(tuple); _TIter& it = std::get<1>(tuple); bool& flag = std::get<2>(tuple); if (!flag) { flag = true; it = vec.cbegin(); } if (it == vec.cend()) { throw LinqEndException(); } return *(it++); } ); } Linq, typename std::vector::const_iterator, bool>, T> orderBy() const { return orderBy([](T value) { return value; }); } template::const_reverse_iterator> Linq, _TIter, bool>, T> reverse() const { return Linq, _TIter, bool>, T>( std::make_tuple(toStdList(), _TIter(), false), [](std::tuple, _TIter, bool>& tuple) { std::list& list = std::get<0>(tuple); _TIter& it = std::get<1>(tuple); bool& flag = std::get<2>(tuple); if (!flag) { flag = true; it = list.crbegin(); } if (it == list.crend()) { throw LinqEndException(); } return *(it++); } ); } // Aggregators template TRet aggregate(TRet start, std::function accumulate) const { Linq linq = *this; try { while (true) { start = accumulate(start, linq.next()); } } catch (LinqEndException&) {} return start; } template::type> _TRet sum(F transform) const { return aggregate<_TRet>(_TRet(), [transform](_TRet accumulator, T value) { return accumulator + transform(value); }); } template TRet sum() const { return sum([](T value) { return TRet(value); }); } template::type> _TRet avg(F transform) const { int count = 0; _TRet res = sum([transform, &count](T value) { count++; return transform(value); }); return res / count; } template TRet avg() const { return avg([](T value) { return TRet(value); }); } int count() const { int index = 0; for_each([&index](T /*a*/) { index++; }); return index; } int count(std::function predicate) const { return where(predicate).count(); } int count(const T& item) const { return count([item](T value) { return item == value; }); } // Bool aggregators bool any(std::function predicate) const { Linq linq = *this; try { while (true) { if (predicate(linq.next())) return true; } } catch (LinqEndException&) {} return false; } bool any() const { return any([](T value) { return static_cast(value); }); } bool all(std::function predicate) const { return !any([predicate](T value) { return !predicate(value); }); } bool all() const { return all([](T value) { return static_cast(value); }); } bool contains(const T& item) const { return any([&item](T value) { return value == item; }); } // Election aggregators T elect(std::function accumulate) const { T result; for_each_i([accumulate, &result](T value, int i) { if (i == 0) { result = value; } else { result = accumulate(result, value); } }); return result; } template T max(F transform) const { return elect([transform](const T& a, const T& b) { return (transform(a) < transform(b)) ? b : a; }); } T max() const { return max([](T value) { return value; }); } template T min(F transform) const { return elect([transform](const T& a, const T& b) { return (transform(a) < transform(b)) ? a : b; }); } T min() const { return min([](T value) { return value; }); } // Single object returners T elementAt(int index) const { return skip(index).next(); } T first(std::function predicate) const { return where(predicate).next(); } T first() const { return Linq(*this).next(); } T firstOrDefault(std::function predicate, T const& defaultValue = T()) const { try { return where(predicate).next(); } catch (LinqEndException&) {} return defaultValue; } T firstOrDefault(T const& defaultValue = T()) const { try { return Linq(*this).next(); } catch (LinqEndException&) {} return defaultValue; } T last(std::function predicate) const { T res; int index = -1; where(predicate).for_each_i([&res, &index](T value, int i) { res = value; index = i; }); if (index == -1) { throw LinqEndException(); } return res; } T last() const { return last([](T /*value*/) { return true; }); } T lastOrDefault(std::function predicate, T const& defaultValue = T()) const { T res = defaultValue; where(predicate).for_each([&res](T value) { res = value; }); return res; } T lastOrDefault(T const& defaultValue = T()) const { return lastOrDefault([](T /*value*/) { return true; }, defaultValue); } // Export to containers std::vector toStdVector() const { std::vector items; for_each([&items](T value) { items.push_back(value); }); return items; } std::list toStdList() const { std::list items; for_each([&items](T value) { items.push_back(value); }); return items; } std::deque toStdDeque() const { std::deque items; for_each([&items](T value) { items.push_back(value); }); return items; } std::set toStdSet() const { std::set items; for_each([&items](T value) { items.insert(value); }); return items; } std::unordered_set toStdUnorderedSet() const { std::unordered_set items; for_each([&items](T value) { items.insert(value); }); return items; } // Bits and bytes Linq, BytesDirection, T, int>, int> bytes(BytesDirection direction = BytesFirstToLast) const { return Linq, BytesDirection, T, int>, int>( std::make_tuple(*this, direction, T(), sizeof(T)), [](std::tuple, BytesDirection, T, int>& tuple) { Linq& linq = std::get<0>(tuple); BytesDirection& bytesDirection = std::get<1>(tuple); T& value = std::get<2>(tuple); int& index = std::get<3>(tuple); if (index == sizeof(T)) { value = linq.next(); index = 0; } unsigned char* ptr = reinterpret_cast(&value); int byteIndex = index; if (bytesDirection == BytesLastToFirst) { byteIndex = sizeof(T) - 1 - byteIndex; } index++; return ptr[byteIndex]; } ); } template Linq, BytesDirection, int>, TRet> unbytes(BytesDirection direction = BytesFirstToLast) const { return Linq, BytesDirection, int>, TRet>( std::make_tuple(*this, direction, 0), [](std::tuple, BytesDirection, int>& tuple) { Linq& linq = std::get<0>(tuple); BytesDirection& bytesDirection = std::get<1>(tuple); /*int& index =*/ std::get<2>(tuple); TRet value; unsigned char* ptr = reinterpret_cast(&value); for (int i = 0; i < sizeof(TRet); i++) { int byteIndex = i; if (bytesDirection == BytesLastToFirst) { byteIndex = sizeof(TRet) - 1 - byteIndex; } ptr[byteIndex] = linq.next(); } return value; } ); } Linq, BytesDirection, BitsDirection, T, int>, int> bits(BitsDirection bitsDir = BitsHighToLow, BytesDirection bytesDir = BytesFirstToLast) const { return Linq, BytesDirection, BitsDirection, T, int>, int>( std::make_tuple(*this, bytesDir, bitsDir, T(), sizeof(T) * CHAR_BIT), [](std::tuple, BytesDirection, BitsDirection, T, int>& tuple) { Linq& linq = std::get<0>(tuple); BytesDirection& bytesDirection = std::get<1>(tuple); BitsDirection& bitsDirection = std::get<2>(tuple); T& value = std::get<3>(tuple); int& index = std::get<4>(tuple); if (index == sizeof(T) * CHAR_BIT) { value = linq.next(); index = 0; } unsigned char* ptr = reinterpret_cast(&value); int byteIndex = index / CHAR_BIT; if (bytesDirection == BytesLastToFirst) { byteIndex = sizeof(T) - 1 - byteIndex; } int bitIndex = index % CHAR_BIT; if (bitsDirection == BitsHighToLow) { bitIndex = CHAR_BIT - 1 - bitIndex; } index++; return (ptr[byteIndex] >> bitIndex) & 1; } ); } template Linq, BytesDirection, BitsDirection, int>, TRet> unbits(BitsDirection bitsDir = BitsHighToLow, BytesDirection bytesDir = BytesFirstToLast) const { return Linq, BytesDirection, BitsDirection, int>, TRet>( std::make_tuple(*this, bytesDir, bitsDir, 0), [](std::tuple, BytesDirection, BitsDirection, int>& tuple) { Linq& linq = std::get<0>(tuple); BytesDirection& bytesDirection = std::get<1>(tuple); BitsDirection& bitsDirection = std::get<2>(tuple); /*int& index =*/ std::get<3>(tuple); TRet value = TRet(); unsigned char* ptr = reinterpret_cast(&value); for (int i = 0; i < sizeof(TRet) * CHAR_BIT; i++) { int byteIndex = i / CHAR_BIT; if (bytesDirection == BytesLastToFirst) { byteIndex = sizeof(TRet) - 1 - byteIndex; } int bitIndex = i % CHAR_BIT; if (bitsDirection == BitsHighToLow) { bitIndex = CHAR_BIT - 1 - bitIndex; } ptr[byteIndex] &= ~(1 << bitIndex); ptr[byteIndex] |= bool(linq.next()) << bitIndex; } return value; } ); } }; template std::ostream& operator<<(std::ostream& stream, Linq linq) { try { while (true) { stream << linq.next() << ' '; } } catch (LinqEndException&) {} return stream; } //////////////////////////////////////////////////////////////// // Linq Creators //////////////////////////////////////////////////////////////// template Linq, typename std::iterator_traits::value_type> from(const T& begin, const T& end) { return Linq, typename std::iterator_traits::value_type>( std::make_pair(begin, end), [](std::pair& pair) { if (pair.first == pair.second) { throw LinqEndException(); } return *(pair.first++); } ); } template Linq, typename std::iterator_traits::value_type> from(const T& it, int n) { return from(it, it + n); } template Linq, T> from(T (& array)[N]) { return from((const T*)(&array), (const T*)(&array) + N); } template class TV, typename TT> auto from(const TV& container) -> decltype(from(container.cbegin(), container.cend())) { return from(container.cbegin(), container.cend()); } // std::list, std::vector, std::dequeue template class TV, typename TT, typename TU> auto from(const TV& container) -> decltype(from(container.cbegin(), container.cend())) { return from(container.cbegin(), container.cend()); } // std::set template class TV, typename TT, typename TS, typename TU> auto from(const TV& container) -> decltype(from(container.cbegin(), container.cend())) { return from(container.cbegin(), container.cend()); } // std::map template class TV, typename TK, typename TT, typename TS, typename TU> auto from(const TV& container) -> decltype(from(container.cbegin(), container.cend())) { return from(container.cbegin(), container.cend()); } // std::array template class TV, typename TT, size_t TL> auto from(const TV& container) -> decltype(from(container.cbegin(), container.cend())) { return from(container.cbegin(), container.cend()); } template Linq, T> repeat(const T& value, int count) { return Linq, T>( std::make_pair(value, count), [](std::pair& pair) { if (pair.second > 0) { pair.second--; return pair.first; } throw LinqEndException(); } ); } template Linq, T> range(const T& start, const T& end, const T& step) { return Linq, T>( std::make_tuple(start, end, step), [](std::tuple& tuple) { T& start = std::get<0>(tuple); T& end = std::get<1>(tuple); T& step = std::get<2>(tuple); T value = start; if (value < end) { start += step; return value; } throw LinqEndException(); } ); } }