/* * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOLLY_GEN_COMBINE_H_ #error This file may only be included from folly/gen/Combine.h #endif #include #include #include #include namespace folly { namespace gen { namespace detail { /** * Interleave * * Alternate values from a sequence with values from a sequence container. * Stops once we run out of values from either source. */ template class Interleave : public Operator> { // see comment about copies in CopiedSource const std::shared_ptr container_; public: explicit Interleave(Container container) : container_(new Container(std::move(container))) {} template class Generator : public GenImpl> { Source source_; const std::shared_ptr container_; public: explicit Generator( Source source, const std::shared_ptr container) : source_(std::move(source)), container_(container) {} template bool apply(Handler&& handler) const { auto iter = container_->begin(); return source_.apply([&](Value value) -> bool { if (iter == container_->end()) { return false; } if (!handler(std::forward(value))) { return false; } if (!handler(std::move(*iter))) { return false; } iter++; return true; }); } }; template > Gen compose(GenImpl&& source) const { return Gen(std::move(source.self()), container_); } template > Gen compose(const GenImpl& source) const { return Gen(source.self(), container_); } }; /** * Zip * * Combine inputs from Source with values from a sequence container by merging * them into a tuple. * */ template class Zip : public Operator> { // see comment about copies in CopiedSource const std::shared_ptr container_; public: explicit Zip(Container container) : container_(new Container(std::move(container))) {} template < class Value, class Source, class Result = std::tuple< typename std::decay::type, typename std::decay::type>> class Generator : public GenImpl> { Source source_; const std::shared_ptr container_; public: explicit Generator( Source source, const std::shared_ptr container) : source_(std::move(source)), container_(container) {} template bool apply(Handler&& handler) const { auto iter = container_->begin(); return (source_.apply([&](Value value) -> bool { if (iter == container_->end()) { return false; } if (!handler(std::make_tuple( std::forward(value), std::move(*iter)))) { return false; } ++iter; return true; })); } }; template > Gen compose(GenImpl&& source) const { return Gen(std::move(source.self()), container_); } template > Gen compose(const GenImpl& source) const { return Gen(source.self(), container_); } }; template auto add_to_tuple(std::tuple t1, std::tuple t2) -> std::tuple { return std::tuple_cat(std::move(t1), std::move(t2)); } template auto add_to_tuple(std::tuple t1, Type2&& t2) -> decltype( std::tuple_cat(std::move(t1), std::make_tuple(std::forward(t2)))) { return std::tuple_cat( std::move(t1), std::make_tuple(std::forward(t2))); } template auto add_to_tuple(Type1&& t1, std::tuple t2) -> decltype( std::tuple_cat(std::make_tuple(std::forward(t1)), std::move(t2))) { return std::tuple_cat( std::make_tuple(std::forward(t1)), std::move(t2)); } template auto add_to_tuple(Type1&& t1, Type2&& t2) -> decltype( std::make_tuple(std::forward(t1), std::forward(t2))) { return std::make_tuple(std::forward(t1), std::forward(t2)); } // Merges a 2-tuple into a single tuple (get<0> could already be a tuple) class MergeTuples { public: template auto operator()(Tuple&& value) const -> decltype(add_to_tuple( std::get<0>(std::forward(value)), std::get<1>(std::forward(value)))) { static_assert( std::tuple_size::type>::value == 2, "Can only merge tuples of size 2"); return add_to_tuple( std::get<0>(std::forward(value)), std::get<1>(std::forward(value))); } }; } // namespace detail // TODO(mcurtiss): support zip() for N>1 operands. template < class Source, class Zip = detail::Zip::type>> Zip zip(Source&& source) { return Zip(std::forward(source)); } } // namespace gen } // namespace folly