|
|
|
@ -13,13 +13,13 @@ See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License. */
|
|
|
|
|
|
|
|
|
|
#include "paddle/framework/scope.h"
|
|
|
|
|
#include "paddle/string/printf.h"
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
|
|
Scope::~Scope() {
|
|
|
|
|
for (Variable* v : vars_) delete v;
|
|
|
|
|
|
|
|
|
|
for (auto& kv : vars_) delete kv.second;
|
|
|
|
|
for (Scope* s : kids_) delete s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -29,28 +29,32 @@ Scope& Scope::NewScope() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::NewVar(const std::string& name) {
|
|
|
|
|
atuo iter = vars_.find(name);
|
|
|
|
|
auto iter = vars_.find(name);
|
|
|
|
|
if (iter != vars_.end()) {
|
|
|
|
|
return iter.second->get();
|
|
|
|
|
return iter->second;
|
|
|
|
|
}
|
|
|
|
|
Variable* v = new Variable();
|
|
|
|
|
v->name_ = name;
|
|
|
|
|
var_[name] = v;
|
|
|
|
|
vars_[name] = v;
|
|
|
|
|
v->name_ = &(vars_.find(name)->first);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::NewVar() {
|
|
|
|
|
return NewVar(string.Sprintf("%p.%d", this, vars_.size()));
|
|
|
|
|
return NewVar(string::Sprintf("%p.%d", this, vars_.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Variable* Scope::FindVar(const std::string& name) const {
|
|
|
|
|
auto it = vars_.find(name);
|
|
|
|
|
if (it != vars_.end()) return it->second.get();
|
|
|
|
|
if (it != vars_.end()) return it->second;
|
|
|
|
|
return (parent_ == nullptr) ? nullptr : parent_->FindVar(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scope* Scope::FindScope(const Variable* var) const {
|
|
|
|
|
if (FindVar(var->name_) != nullptr) return this;
|
|
|
|
|
Scope* Scope::FindScope(const Variable* var) {
|
|
|
|
|
for (auto& kv : vars_) {
|
|
|
|
|
if (kv.second == var) {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|