2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @ingroup COM_AI_RUNTIME
20 * @brief This file contains nnfw::misc::tensor::Zipper class
23 #ifndef __NNFW_MISC_TENSOR_ZIPPER_H__
24 #define __NNFW_MISC_TENSOR_ZIPPER_H__
26 #include "misc/tensor/Index.h"
27 #include "misc/tensor/IndexIterator.h"
28 #include "misc/tensor/Reader.h"
38 * @brief Class to apply a function with three params: @c Index, elements of a tensor
39 * at passed index read by @c Reader objects
41 template <typename T> class Zipper
45 * @brief Construct a new @c Zipper object
46 * @param[in] shape Shape of @c lhs and @c rhs
47 * @param[in] lhs @c Reader object of a tensor
48 * @param[in] rhs @c Reader object of a tensor
50 Zipper(const Shape &shape, const Reader<T> &lhs, const Reader<T> &rhs)
51 : _shape{shape}, _lhs{lhs}, _rhs{rhs}
58 * @brief Apply @c cb to all elements of tensors. Elements of two tensors
59 * at passed @c index are read by @c lhs and @c rhs
60 * @param[in] cb Function to apply
63 template <typename Callable> void zip(Callable cb) const
66 [this, &cb](const Index &index) { cb(index, _lhs.at(index), _rhs.at(index)); };
71 const Reader<T> &_lhs;
72 const Reader<T> &_rhs;
76 * @brief Apply @c cb by using @c lhs and @c rhs passed to the constructor of @c zipper
77 * @param[in] zipper @c Zipper object
78 * @param[in] cb Function to zpply using @c zip function
79 * @return @c zipper object after applying @c cb to @c zipper
81 template <typename T, typename Callable>
82 const Zipper<T> &operator<<(const Zipper<T> &zipper, Callable cb)
89 * @brief Get @c Zipper object constructed using passed params
90 * @param shape Shape of @c lhs and @c rhs
91 * @param lhs @c Reader object of a tensor
92 * @param rhs @c Reader object of a tensor
93 * @return @c Zipper object
95 template <typename T> Zipper<T> zip(const Shape &shape, const Reader<T> &lhs, const Reader<T> &rhs)
97 return Zipper<T>{shape, lhs, rhs};
100 } // namespace tensor
104 #endif // __NNFW_MISC_TENSOR_ZIPPER_H__