Skip to content

Commit

Permalink
enh: add Node::path() and Schema::path() methods
Browse files Browse the repository at this point in the history
these return a string that gives you the full path to a given
node from the root of the tree it resides in

also added unit test to check proper imp
  • Loading branch information
cyrush committed Sep 16, 2016
1 parent 1f32449 commit 900e16b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/libs/conduit/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8165,7 +8165,15 @@ Node::number_of_children() const
}

//---------------------------------------------------------------------------//
bool
std::string
Node::path() const
{
return m_schema->path();
}


//---------------------------------------------------------------------------//
bool
Node::has_path(const std::string &path) const
{
return m_schema->has_path(path);
Expand Down
6 changes: 5 additions & 1 deletion src/libs/conduit/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,9 +2280,13 @@ class CONDUIT_API Node
/// access child node via index (equivalent to fetch via index)
Node &operator[](index_t idx);

/// return the number of children (list and object interfaces)
/// returns the number of children (list and object interfaces)
index_t number_of_children() const;

/// returns a string with the path of this node up
/// the tree, following the parent chain
std::string path() const;

/// checks if given path exists in the Node hierarchy
bool has_path(const std::string &path) const;
/// returns the direct child paths for this node
Expand Down
50 changes: 49 additions & 1 deletion src/libs/conduit/Schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,13 @@ Schema::child_ptr(index_t idx)
return &child(idx);
}

//---------------------------------------------------------------------------//
const Schema *
Schema::child_ptr(index_t idx) const
{
return &child(idx);
}


//---------------------------------------------------------------------------//
void
Expand Down Expand Up @@ -864,6 +871,47 @@ Schema::operator[](const std::string &path)
return fetch(path);
}

//---------------------------------------------------------------------------//
std::string
Schema::path() const
{
const Schema *p = parent();

if(p == NULL)
{
return "";
}

index_t idx = 0;
index_t nchld = p->number_of_children();
for(index_t i=0; i < nchld; i++)
{
if( p->child_ptr(i) == this )
{
idx = i;
}
}

std::ostringstream oss;
std::string parent_path = p->path();
if(parent_path.size() > 0)
oss << parent_path << "/";

// if this schema has a parent, its parent is either an object or list
if(p->dtype().is_object())
{
// use name
oss << p->child_name(idx);
}
else if(p->dtype().is_list())
{
// use order in the list
oss << "[" << idx << "]";
}

return oss.str();
}

//---------------------------------------------------------------------------//
bool
Schema::has_path(const std::string &path) const
Expand Down Expand Up @@ -945,9 +993,9 @@ Schema::remove(const std::string &path)
Schema &
Schema::append()
{
init_list();
init_list();
Schema *sch = new Schema();
sch->m_parent = this;
children().push_back(sch);
return *sch;
}
Expand Down
8 changes: 7 additions & 1 deletion src/libs/conduit/Schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class CONDUIT_API Schema



const Schema *parent() const
{ return m_parent;}

Schema *parent()
{ return m_parent;}

Expand Down Expand Up @@ -230,6 +233,7 @@ class CONDUIT_API Schema
const Schema &child(index_t idx) const;
/// access to child schema pointer by index
Schema *child_ptr(index_t idx);
const Schema *child_ptr(index_t idx) const;

/// remove a child by index
void remove(index_t idx);
Expand Down Expand Up @@ -265,7 +269,9 @@ class CONDUIT_API Schema
Schema &operator[](const std::string &path);
/// the const variant uses the "fetch_child" method
const Schema &operator[](const std::string &path) const;


std::string path() const;

bool has_path(const std::string &path) const;
void paths(std::vector<std::string> &paths) const;
const std::vector<std::string> &paths() const;
Expand Down
26 changes: 26 additions & 0 deletions src/tests/conduit/t_conduit_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,30 @@ TEST(conduit_node, check_contiguous_with)

}

//-----------------------------------------------------------------------------
TEST(conduit_node, check_path)
{

Node n;

n["a/b/c/d/e/f"] = 10;

EXPECT_EQ(n.path(),"");
EXPECT_EQ(n["a/b/c/d/e/f"].path(), "a/b/c/d/e/f");

// check roundtrip -- using path() to fetch from root node
EXPECT_EQ(n.fetch_ptr(n["a/b/c/d/e/f"].path()),&n["a/b/c/d/e/f"]);

// list cases
EXPECT_EQ(n["a/b/c/list"].append().path(), "a/b/c/list/[0]");
EXPECT_EQ(n["a/b/c/list"].append().path(), "a/b/c/list/[1]");
EXPECT_EQ(n["a/b/c/list"].append().path(), "a/b/c/list/[2]");

n.print();


}




0 comments on commit 900e16b

Please sign in to comment.