LoginSignup
0
0

More than 5 years have passed since last update.

Node::getChildByName()で"/"区切りで再帰的に検索し取得できるようにする

Last updated at Posted at 2016-06-02

Node::getChildByName()で"/"区切りで再帰的に検索し取得する

CCNode.cpp
Node* Node::getChildByName(const std::string& name) const
{
  std::string str = name;
  auto pos = str.find("/");
  if( pos == 0 ){
    str = str.substr(1,std::string::npos);
    pos = str.find("/");
  }
  std::string a = str.substr(0,pos);
  std::string b;
  if( pos != std::string::npos ){
    b = str.substr(pos,std::string::npos);
  }

  std::hash<std::string> h;
  size_t hash = h(a);

  for (const auto& child : _children)
  {
    // Different strings may have the same hash code, but can use it to compare first for speed
    if(child->_hashOfName == hash && child->_name.compare(a) == 0){
      if( b.empty() || b == "/" ){
        return child;
      }
      else{
        return child->getChildByName(b);
      }
    }
  }
  return nullptr;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0