1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/util/sparse/group_iterator.h"
17
18 #include <vector>
19 namespace tensorflow {
20 namespace sparse {
21
UpdateEndOfGroup()22 void GroupIterable::IteratorStep::UpdateEndOfGroup() {
23 ++next_loc_;
24 const auto& ix_t = iter_->ix_matrix_;
25 const int64 N = ix_t.dimension(0);
26 while (next_loc_ < N && iter_->GroupMatches(ix_t, loc_, next_loc_)) {
27 ++next_loc_;
28 }
29 }
30
operator !=(const IteratorStep & rhs) const31 bool GroupIterable::IteratorStep::operator!=(const IteratorStep& rhs) const {
32 CHECK_EQ(rhs.iter_, iter_) << "Can't compare steps from different iterators";
33 return (rhs.loc_ != loc_);
34 }
35
operator ==(const IteratorStep & rhs) const36 bool GroupIterable::IteratorStep::operator==(const IteratorStep& rhs) const {
37 CHECK_EQ(rhs.iter_, iter_) << "Can't compare steps from different iterators";
38 return (rhs.loc_ == loc_);
39 }
40
41 GroupIterable::IteratorStep& GroupIterable::IteratorStep::
operator ++()42 operator++() { // prefix ++
43 loc_ = next_loc_;
44 UpdateEndOfGroup();
45 return *this;
46 }
47
operator ++(int)48 GroupIterable::IteratorStep GroupIterable::IteratorStep::operator++(
49 int) { // postfix ++
50 IteratorStep lhs(*this);
51 ++(*this);
52 return lhs;
53 }
54
group() const55 std::vector<int64> Group::group() const {
56 std::vector<int64> g;
57 const auto& ix_t = iter_->ix_matrix_;
58 for (const int d : iter_->group_dims_) {
59 g.push_back(ix_t(loc_, d));
60 }
61 return g;
62 }
63
indices() const64 TTypes<int64>::UnalignedConstMatrix Group::indices() const {
65 return TTypes<int64>::UnalignedConstMatrix(&(iter_->ix_matrix_(loc_, 0)),
66 next_loc_ - loc_, iter_->dims_);
67 }
68
69 } // namespace sparse
70 } // namespace tensorflow
71