scope.cpp 2.8 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */

#include "framework/scope.h"
朔-望's avatar
朔-望 已提交
16 17

#include <set>
朔-望's avatar
朔-望 已提交
18
#include <string>
朔-望's avatar
朔-望 已提交
19 20 21
#include <vector>

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
22
namespace framework {
朔-望's avatar
朔-望 已提交
23

朔-望's avatar
朔-望 已提交
24
Scope &Scope::NewScope() const {
25 26
  kids_.push_back(new Scope(this));
  return *kids_.back();
朔-望's avatar
朔-望 已提交
27
}
朔-望's avatar
朔-望 已提交
28

朔-望's avatar
朔-望 已提交
29
Variable *Scope::Var(const std::string &name) {
30 31
  auto *pvar = FindVarLocally(name);
  if (pvar != nullptr) {
朔-望's avatar
朔-望 已提交
32
    return pvar;
朔-望's avatar
朔-望 已提交
33
  }
34 35
  pvar = new Variable;
  vars_[name] = pvar;
W
wangliu 已提交
36
  pvar->name_ = vars_.find(name)->first;
37
  return pvar;
朔-望's avatar
朔-望 已提交
38
}
朔-望's avatar
朔-望 已提交
39

朔-望's avatar
朔-望 已提交
40
Variable *Scope::FindVar(const std::string &name) const {
41 42 43 44 45
  auto *pvar = FindVarLocally(name);
  if (pvar != nullptr) {
    return pvar;
  }
  return (parent_ == nullptr) ? nullptr : parent_->FindVar(name);
朔-望's avatar
朔-望 已提交
46
}
朔-望's avatar
朔-望 已提交
47

朔-望's avatar
朔-望 已提交
48
const Scope *Scope::FindScope(const Variable *var) const {
49 50 51
  for (auto &name_var : vars_) {
    if (name_var.second == var) {
      return this;
朔-望's avatar
朔-望 已提交
52
    }
53 54
  }
  return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
朔-望's avatar
朔-望 已提交
55
}
朔-望's avatar
朔-望 已提交
56

朔-望's avatar
朔-望 已提交
57
void Scope::DropKids() {
58 59 60 61
  for (Scope *s : kids_) {
    delete s;
  }
  kids_.clear();
朔-望's avatar
朔-望 已提交
62
}
朔-望's avatar
朔-望 已提交
63

朔-望's avatar
朔-望 已提交
64
std::vector<std::string> Scope::LocalVarNames() const {
65 66 67 68 69 70
  std::vector<std::string> known_vars;
  known_vars.reserve(vars_.size());
  for (auto &name_var : vars_) {
    known_vars.emplace_back(name_var.first);
  }
  return known_vars;
朔-望's avatar
朔-望 已提交
71
}
朔-望's avatar
朔-望 已提交
72

朔-望's avatar
朔-望 已提交
73
void Scope::DeleteScope(Scope *scope) const {
74 75 76
  auto it = std::find(kids_.begin(), kids_.end(), scope);
  kids_.erase(it);
  delete scope;
朔-望's avatar
朔-望 已提交
77
}
朔-望's avatar
朔-望 已提交
78

朔-望's avatar
朔-望 已提交
79
void Scope::EraseVars(const std::vector<std::string> &var_names) {
80 81 82 83 84 85 86
  std::set<std::string> var_set(var_names.begin(), var_names.end());
  for (auto it = vars_.begin(); it != vars_.end();) {
    if (var_set.find(it->first) != var_set.end()) {
      delete it->second;
      it = vars_.erase(it);
    } else {
      ++it;
朔-望's avatar
朔-望 已提交
87
    }
88
  }
朔-望's avatar
朔-望 已提交
89
}
朔-望's avatar
朔-望 已提交
90

朔-望's avatar
朔-望 已提交
91 92
void Scope::Rename(const std::string &origin_name,
                   const std::string &new_name) const {
93 94 95 96 97 98 99 100 101 102
  auto origin_it = vars_.find(origin_name);
  if (origin_it == vars_.end()) {
    return;
  }
  auto new_it = vars_.find(new_name);
  if (new_it != vars_.end()) {
    return;
  }
  vars_[new_name] = origin_it->second;
  vars_.erase(origin_it);
朔-望's avatar
朔-望 已提交
103
}
朔-望's avatar
朔-望 已提交
104

朔-望's avatar
朔-望 已提交
105
Variable *Scope::FindVarLocally(const std::string &name) const {
106 107 108 109 110
  auto it = vars_.find(name);
  if (it != vars_.end()) {
    return it->second;
  }
  return nullptr;
朔-望's avatar
朔-望 已提交
111
}
朔-望's avatar
朔-望 已提交
112

朔-望's avatar
朔-望 已提交
113 114
}  // namespace framework
}  // namespace paddle_mobile