added schema node iterator
This commit is contained in:
@@ -47,6 +47,8 @@ add_library(matador-core STATIC
|
||||
object/schema.cpp
|
||||
object/schema_node.cpp
|
||||
object/basic_object_info.cpp
|
||||
../../include/matador/object/schema_node_iterator.hpp
|
||||
object/schema_node_iterator.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||
|
||||
@@ -14,11 +14,21 @@ schema::schema()
|
||||
root_->last_child_->previous_sibling_ = root_->first_child_;
|
||||
}
|
||||
|
||||
schema::const_iterator schema::begin() const {
|
||||
return const_iterator(root_->first_child_->next_sibling_);
|
||||
}
|
||||
|
||||
schema::const_iterator schema::end() const {
|
||||
return const_iterator(root_->last_child_);
|
||||
}
|
||||
|
||||
bool schema::empty() const {
|
||||
return root_->first_child_ == root_->last_child_->previous_sibling_;
|
||||
}
|
||||
|
||||
size_t schema::size() const { return 0; }
|
||||
size_t schema::size() const {
|
||||
return static_cast<size_t>(std::distance(begin(), end()));
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::string &parent) {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
#include <utility>
|
||||
|
||||
#include "matador/object/schema_node_iterator.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
const_schema_node_iterator::const_schema_node_iterator(std::shared_ptr<value_type> node)
|
||||
: node_(std::move(node))
|
||||
{}
|
||||
|
||||
bool const_schema_node_iterator::operator==(const const_schema_node_iterator &i) const
|
||||
{
|
||||
return (node_ == i.node_);
|
||||
}
|
||||
|
||||
bool const_schema_node_iterator::operator!=(const const_schema_node_iterator &i) const
|
||||
{
|
||||
return !operator==(i);
|
||||
}
|
||||
|
||||
const_schema_node_iterator& const_schema_node_iterator::operator++()
|
||||
{
|
||||
increment();
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_schema_node_iterator const_schema_node_iterator::operator++(int)
|
||||
{
|
||||
const std::shared_ptr<value_type> tmp = node_;
|
||||
increment();
|
||||
return const_schema_node_iterator(tmp);
|
||||
}
|
||||
|
||||
const_schema_node_iterator& const_schema_node_iterator::operator--()
|
||||
{
|
||||
decrement();
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_schema_node_iterator const_schema_node_iterator::operator--(int)
|
||||
{
|
||||
const std::shared_ptr<value_type> tmp = node_;
|
||||
decrement();
|
||||
return const_schema_node_iterator(tmp);
|
||||
}
|
||||
|
||||
const_schema_node_iterator::pointer const_schema_node_iterator::operator->() const
|
||||
{
|
||||
return node_.get();
|
||||
}
|
||||
|
||||
const_schema_node_iterator::reference const_schema_node_iterator::operator*() const
|
||||
{
|
||||
return *node_;
|
||||
}
|
||||
|
||||
const_schema_node_iterator::pointer const_schema_node_iterator::get() const
|
||||
{
|
||||
return node_.get();
|
||||
}
|
||||
|
||||
void const_schema_node_iterator::increment()
|
||||
{
|
||||
if (!node_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if we have a child, child is the next iterator to return
|
||||
// (if we don't do iterate over the siblings)
|
||||
if (node_->first_child_ && node_->first_child_->next_sibling_ != node_->last_child_) {
|
||||
node_ = node_->first_child_->next_sibling_;
|
||||
} else {
|
||||
// if there is no child, we check for sibling
|
||||
// if there is a sibling, this is our next iterator to return
|
||||
// if not, we go back to the parent
|
||||
std::shared_ptr<value_type> node = node_;
|
||||
while (node_->parent_ && node_->next_sibling_ == node_->parent_->last_child_) {
|
||||
node = node->parent_;
|
||||
}
|
||||
node_ = node_->next_sibling_;
|
||||
}
|
||||
|
||||
}
|
||||
void const_schema_node_iterator::decrement()
|
||||
{
|
||||
if (!node_) {
|
||||
return;
|
||||
}
|
||||
// if node has a previous sibling, we set it
|
||||
// as our next iterator. then we check if there
|
||||
// are last children. if so, we set the last-last
|
||||
// child as our iterator
|
||||
if (node_->previous_sibling_ && node_->previous_sibling_->previous_sibling_) {
|
||||
std::shared_ptr<value_type> node = node_->previous_sibling_;
|
||||
while (node_->last_child_ && node_->first_child_->next_sibling_ != node_->last_child_) {
|
||||
node = node->last_child_->previous_sibling_;
|
||||
}
|
||||
node_ = node;
|
||||
// if there is no previous sibling, our next iterator
|
||||
// is the parent of the node
|
||||
} else {
|
||||
node_ = node_->parent_;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user