迭代多级提升树

2024-04-18

我的树看起来像这样:

{
"Library":
{
    "L_ID": "1",
     "Book":
     {
         "B_ID": "1",
         "Title": "Moby Dick"
     }, 
     "Book":
     {
         "B_ID": "2",
         "Title": "Jurassic Park"
     }
},
"Library":
{
    "L_ID": "2",
     "Book":
     {
         "B_ID": "1",
         "Title": "Velocity"
     }, 
     "Book":
     {
        "B_ID": "2",
        "Title": "Creeper"
     }
}
}

我想做的是迭代库。当我找到我正在寻找的 L_ID 时,迭代书籍,直到找到我正在寻找的 B_ID。那时,我想访问该部分中的所有叶子。 IE。寻找图书馆 2,书籍 1,标题 注意:可能有比这更好的方法。

boost::property_tree::ptree libraries = config_.get_child("Library");
for (const auto &lib : libraries)
{
   if (lib.second.get<uint16_6>("L_ID") == 2)
   { 
       //at this point, i know i'm the correct library...
       boost::property_tree::ptree books = lib.get_child("Book");
       for (const auto &book : books)
       {
            if (book.second.get<uint16_t>("B_ID") == 1)
            {
                 std::string mybook = book.second.get<std::string>("Title");
            }
        }
   }

当我尝试查看我的第一个子树时,我就失败了。这里出了什么问题?


首先,“JSON”存在严重缺陷。至少修复丢失的引号和逗号:

{
    "Library": {
        "L_ID": "1",
        "Book": {
            "B_ID": "1",
            "Title": "Moby Dick"
        },
        "Book": {
            "B_ID": "2",
            "Title": "Jurassic Park"
        }
    },
    "Library": {
        "L_ID": "2",
        "Book": {
            "B_ID": "1",
            "Title": "Velocity"
        },
        "Book": {
            "B_ID": "2",
            "Title": "Creeper"
        }
    }
}

接下来,你似乎很困惑。get_child("Library")获取该名称的第一个子节点,而不是包含名为“Library”的子节点的节点(顺便说一下,这将是根节点)。

我可以建议添加一些抽象,也许还可以添加一些通过某些名称/属性进行查询的工具:

int main() {
    Config cfg;
    {
        std::ifstream ifs("input.txt");
        read_json(ifs, cfg.data_);
    }

    std::cout << "Book title: " << cfg.library(2).book(1).title() << "\n";
}

正如你所看到的,我们假设Config可以找到库的类型:

Library library(Id id) const { 
    for (const auto& lib : libraries())
        if (lib.id() == id) return lib;
    throw std::out_of_range("library");
}

What is libraries()?我们将更深入地研究它,但让我们先看一下它:

auto libraries() const {
    using namespace PtreeTools;
    return data_ | named("Library") | having("L_ID") | as<Library>();
}

这个魔法应该理解为“给我所有名为 Library 的节点,它们具有 L_ID 属性,但将它们包装在 Library 对象中”。现在先跳过细节,让我们看一下Library对象,显然知道 books():

struct Library {
    ptree const& data_;

    Id id() const { return data_.get<Id>("L_ID"); } 

    auto books() const {
        using namespace PtreeTools;
        return data_ | named("Book") | having("B_ID") | as<Book>();
    }

    Book book(Id id) const { 
        for (const auto& book : books())
            if (book.id() == id) return book;
        throw std::out_of_range("book");
    }
};

我们看到同样的模式books() and book(id)查找特定项目。

魔术

魔法使用升压范围适配器并潜伏在PtreeTools:

namespace PtreeTools {

    namespace detail {
        // ... 
    }

    auto named(std::string const& name) { 
        return detail::filtered(detail::KeyName{name});
    }

    auto having(std::string const& name) { 
        return detail::filtered(detail::HaveProperty{name});
    }

    template <typename T>
    auto as() { 
        return detail::transformed(detail::As<T>{});
    }
}

这看似简单,对吧。那是因为我们站在 Boost Range 的肩膀上:

namespace detail {
    using boost::adaptors::filtered;
    using boost::adaptors::transformed;

接下来,我们只定义知道如何过滤特定 ptree 节点的谓词:

    using Value = ptree::value_type;

    struct KeyName {
        std::string const _target;
        bool operator()(Value const& v) const {
            return v.first == _target;
        }
    };

    struct HaveProperty {
        std::string const _target;
        bool operator()(Value const& v) const {
            return v.second.get_optional<std::string>(_target).is_initialized();
        }
    };

以及投影到我们的包装对象的一种转换:

    template <typename T>
        struct As {
            T operator()(Value const& v) const {
                return T { v.second };
            }
        };
}

完整现场演示

Live On Coliru http://coliru.stacked-crooked.com/a/3d385e9d30ea6895

#include <boost/property_tree/json_parser.hpp>
#include <boost/range/adaptors.hpp>

using boost::property_tree::ptree;

namespace PtreeTools {

    namespace detail {
        using boost::adaptors::filtered;
        using boost::adaptors::transformed;

        using Value = ptree::value_type;

        struct KeyName {
            std::string const _target;
            bool operator()(Value const& v) const {
                return v.first == _target;
            }
        };

        struct HaveProperty {
            std::string const _target;
            bool operator()(Value const& v) const {
                return v.second.get_optional<std::string>(_target).is_initialized();
            }
        };

        template <typename T>
            struct As {
                T operator()(Value const& v) const {
                    return T { v.second };
                }
            };
    }

    auto named(std::string const& name) { 
        return detail::filtered(detail::KeyName{name});
    }

    auto having(std::string const& name) { 
        return detail::filtered(detail::HaveProperty{name});
    }

    template <typename T>
    auto as() { 
        return detail::transformed(detail::As<T>{});
    }
}

struct Config {
    ptree data_;

    using Id = uint16_t;

    struct Book {
        ptree const& data_;
        Id id()             const  { return data_.get<Id>("B_ID"); } 
        std::string title() const  { return data_.get<std::string>("Title"); } 
    };

    struct Library {
        ptree const& data_;

        Id id() const { return data_.get<Id>("L_ID"); } 

        auto books() const {
            using namespace PtreeTools;
            return data_ | named("Book") | having("B_ID") | as<Book>();
        }

        Book book(Id id) const { 
            for (const auto& book : books())
                if (book.id() == id) return book;
            throw std::out_of_range("book");
        }
    };

    auto libraries() const {
        using namespace PtreeTools;
        return data_ | named("Library") | having("L_ID") | as<Library>();
    }

    Library library(Id id) const { 
        for (const auto& lib : libraries())
            if (lib.id() == id) return lib;
        throw std::out_of_range("library");
    }
};

#include <iostream>
int main() {
    Config cfg;
    {
        std::ifstream ifs("input.txt");
        read_json(ifs, cfg.data_);
    }

    std::cout << "Book title: " << cfg.library(2).book(1).title() << "\n";
}

Prints:

Book title: Velocity
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

迭代多级提升树 的相关文章

  • 列表切片的迭代

    我想要一个算法来迭代列表切片 切片大小在函数外部设置并且可以不同 在我看来 它是这样的 for list of x items in fatherList foo list of x items 有没有办法正确定义list of x ite
  • 如何与android的静态boost库链接?

    我在使用 Android ndk r5b 将 boost 库移植和链接到 android 时遇到问题 我首先使用以下步骤构建 boost 库 没有 mpi python 1 注释掉boost 1 46 0 libs thread build
  • Boost python 导出单例

    我有一个单例 来自 boost serialization class LogManager public boost serialization singleton
  • 我如何使用 PyObjects 声明 Boost.Python C++ 类

    我想用 PyObjects 编写一个 C 类 以便从 Python 访问它们 并使用现有的 Python 对象实例引用它们 简而言之 我想在 C 类中存储 管理 Python 对象实例 例如 struct Var PyObject Test
  • 将向量或参数传递给 boost::process (boost::fusion)

    我正在尝试创建一个boost process来自字符串参数向量 void runProcess const std string exe const std vector
  • Spirit qi 解析为嵌套函数的抽象语法树

    我正在尝试使用 boost 的spirit qi 解析器创建一个解析器 它正在解析包含三种类型值的字符串 常量 变量或函数 这些函数可以相互嵌套 测试字符串是f a b f g z x g x h x c where a e是常数 f r是
  • 并行迭代器

    我正在设计一个 C 数据结构 用于图形 供并行代码 使用 OpenMP 使用 假设我想要一个能够迭代所有元素 节点 的方法 当然 这个迭代将是并行的 是否可以使用迭代器来实现此目的 迭代器应该是什么样子才能实现并行访问 在这种情况下 您会建
  • Visual Studio 2012 C++ 使用 Boost Signal2 编译错误

    我正在使用 Visual Studio 2012 Ultimate 和以下 Boost Signals2 代码 https github com cfobel boost signals2 blob master hello world 0
  • 如何构建 Boost::program_options

    我想使用 boost program options 安装boost后 我认为我必须单独构建program options http www boost org doc libs 1 43 0 more getting started wi
  • 嵌套列表的非递归遍历——在Python中构建类似的嵌套列表

    我需要遍历一个嵌套列表 处理每个非列表项str 并返回保持结构的类似列表 使用递归 这会相当容易 但我需要以这种迭代的方式进行 下面是我的尝试while loop def myiter e a e initial list c final
  • 如何取消 boost asio io_service 帖子

    如何取消已发布的回调 getIoService gt post boost bind MyClass myCallback this 并保持其他发布的回调不变 问题是我有一些对象从不同线程接收事件 并将它们发布到 ioservice 以便处
  • 遍历 globals() 字典

    我 尝试 使用globals 在我的程序中迭代所有全局变量 我就是这样做的 for k v in globals iteritems function k v 当然 这样做时 我只是创建了另外 2 个全局变量 k and v 所以我得到这个
  • 配置错误:无法链接到 boost_system

    我正在尝试在 Debian 上安装一个软件包 足球模拟器 2d 当我进入目录并运行时 configure 我得到以下信息 reza debian soccer rcssserver 15 0 1 configure checking for
  • Boost.Intrusive 和 unordered_map

    我希望使用侵入性的 unordered map 由于某种原因 库中只有一个 unordered set 还有一个侵入式哈希表 但我不确定它是否具有相同的功能 而且它没有相同的接口 我错了吗 我错过了 unordered map 链接吗 如果
  • n 二叉树的后序遍历

    我需要以下代码的帮助来回答 我正在尝试使用堆栈而不是递归在 n 叉树上执行后序遍历 因为 python 有 1000 次递归的限制 我找到了相同的预序遍历代码 https www geeksforgeeks org iterative pr
  • [现代] C++ 中 N 个变量的范围/循环

    遍历 N 个任意类型的变量来执行操作的简洁方法是什么 假设我有变量a b c d e并想要对他们所有人进行一些操作 使用 Boost Hana 和通用 lambda include
  • 如何修复 STL 样式容器以容纳不完整或抽象类型?

    几天前 我尝试以与 STL 容器相同的风格编写一个基本的树实现 现在我尝试在我的代码中使用它 但是有两件事似乎不起作用 但可以说std vector 即 使用不完整类型和使用抽象类型 如何修复我的树实现以获得此功能 我尝试稍微压缩一下我的代
  • 它如何清除已经在链中排队的所有已发布任务?

    如何清除已在队列中排队的所有已发布任务io service strand 我在boost文档中没有看到类似的方法 我还没有找到它的需要 因为它可以通过正确设计异步调用链来正确解决 一般来说 Boost Asio API 经过精心设计 可以防
  • 使用 nditer 进行浅层迭代

    我有这样一个数组 gt gt gt y np random randint 0 255 2 2 3 gt gt gt array 242 14 211 198 7 0 235 60 81 164 64 236 我必须迭代每个triplet元
  • 如何使用 d3.v4 中的 JSON 数据创建树布局 - 不使用 stratify()

    我正在尝试将一些 d3 代码从 v3 更新到版本 4 我有一个使用 JSON 数据的树形图 d3 v4 示例展示了如何使用 stratify 将表格数据 例如flare csv 转换为分层数据https bl ocks org mbosto

随机推荐