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

朔-望's avatar
朔-望 已提交
34 35 36 37 38
bool Node::operator==(const Node &in) {
    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])) {
L
liuruilong 已提交
39 40 41
                    return false;
                }
            }
朔-望's avatar
朔-望 已提交
42 43
        } else {
            return false;
L
liuruilong 已提交
44
        }
朔-望's avatar
朔-望 已提交
45 46 47 48 49
    } else {
        return false;
    }
    return true;
}
L
liuruilong 已提交
50

L
liuruilong 已提交
51
std::string Node::ToString(std::string blank, const Node *node) const {
朔-望's avatar
朔-望 已提交
52
    std::stringstream ss;
L
liuruilong 已提交
53
    ss << type_ << "-> \n";
L
liuruilong 已提交
54 55 56 57 58 59 60

    if (inputs_.size() > 1 && node != inputs_.back()) {
        return ss.str();
    } else if (inputs_.size() > 1 && node == inputs_.back()) {
        ss << "\n" << blank << type_ << "\n";
    }

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

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

L
liuruilong 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
Node &Node::To(int index) {
    if (index == 0) {
        this->outputs_.clear();
    }

    for (int j = 0; j < this->outputs_.size(); ++j) {
        outputs_[j]->To(index - 1);
    }
    return *this;
}

uint Node::depth(uint begin) {
    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;
}

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

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