node.cpp 2.8 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/

#include <sstream>

#include "node.h"

namespace paddle_mobile {
L
liuruilong 已提交
24

朔-望's avatar
朔-望 已提交
25
namespace framework {
L
liuruilong 已提交
26 27

Node &Node::operator>(std::shared_ptr<Node> node) {
28 29 30 31
  outputs_.push_back(node);
  std::shared_ptr<Node> this_node;
  node->inputs_.push_back(this);
  return *node;
朔-望's avatar
朔-望 已提交
32
}
L
liuruilong 已提交
33

朔-望's avatar
朔-望 已提交
34
bool Node::operator==(const Node &in) {
35 36 37 38 39
  if (in.type_ == this->type_) {
    if (this->outputs_.size() == in.outputs_.size()) {
      for (int i = 0; i < outputs_.size(); ++i) {
        if (!(*outputs_[i] == *in.outputs_[i])) {
          return false;
L
liuruilong 已提交
40
        }
41
      }
朔-望's avatar
朔-望 已提交
42
    } else {
43
      return false;
朔-望's avatar
朔-望 已提交
44
    }
45 46 47 48
  } else {
    return false;
  }
  return true;
朔-望's avatar
朔-望 已提交
49
}
L
liuruilong 已提交
50

L
liuruilong 已提交
51
std::string Node::ToString(std::string blank, const Node *node) const {
52 53
  std::stringstream ss;
  ss << type_ << "-> \n";
L
liuruilong 已提交
54

55
  if (inputs_.size() > 1 && node != inputs_.back()) {
朔-望's avatar
朔-望 已提交
56
    return ss.str();
57 58 59 60 61 62 63 64
  } else if (inputs_.size() > 1 && node == inputs_.back()) {
    ss << "\n" << blank << type_ << "\n";
  }

  for (int i = 0; i < outputs_.size(); ++i) {
    ss << blank << outputs_[i]->ToString(blank + " ", this) << "";
  }
  return ss.str();
朔-望's avatar
朔-望 已提交
65
}
L
liuruilong 已提交
66

L
liuruilong 已提交
67
std::string Node::ToString() const { return this->ToString(" ", this); }
L
liuruilong 已提交
68

L
liuruilong 已提交
69
Node &Node::To(int index) {
70 71 72
  if (index == 0) {
    this->outputs_.clear();
  }
L
liuruilong 已提交
73

74 75 76 77
  for (int j = 0; j < this->outputs_.size(); ++j) {
    outputs_[j]->To(index - 1);
  }
  return *this;
L
liuruilong 已提交
78 79 80
}

uint Node::depth(uint begin) {
81 82 83 84 85 86 87
  uint depth = 0;
  begin++;
  for (int i = 0; i < outputs_.size(); ++i) {
    uint output_depth = outputs_[i]->depth(begin);
    depth = output_depth > depth ? output_depth : depth;
  }
  return begin > depth ? begin : depth;
L
liuruilong 已提交
88 89
}

朔-望's avatar
朔-望 已提交
90
Print &operator<<(Print &printer, const Node &node) {
91 92
  printer << node.ToString();
  return printer;
L
liuruilong 已提交
93
}
L
liuruilong 已提交
94

朔-望's avatar
朔-望 已提交
95 96
}  // namespace framework
}  // namespace paddle_mobile