当前位置: 首页 > 知识库问答 >
问题:

“继承”另一个(1:N)关系的关系

法烨烨
2023-03-14

我想要一个数据结构来支持这些特定的1: N关系:-
1#。人类提高0-N人类
2#。人类有0-N
3#。人类培育0-N
4#。是0-N寄生虫的房子。

注:
-这些关系中的状态都是暂时的,例如Human1可能会提升Human2,但一年后,Human1可能会放弃Human2
-所有对象都继承自BaseObject,并且具有唯一的int-ID。

在所有上述关系中,我希望能够支持这些功能:-
F1。添加关系,例如人狗-

这可以通过std::unordered_map或更容易定制的东西来实现。

我想将关系1#、2#、3#(即所有实线)标记为Feed。
它必须以聚合样式支持功能F3-F8。

例如:-

  • feed-

总之,我希望能够轻松地注入这种聚合
Ex.打电话给:-

void BaseObject::declareForFreedom(){
    feed->removeAllParent(this);
}

上面的示例仅显示4个关系和1个间接级别
在我的真实案例中,有8-10个关系和3-4个级别的继承/间接。

适合这种情况的数据结构/设计模式是什么?

我目前为1#-4#创建了一个自定义的1:N关系,并对每个提要的函数进行了硬编码。这很乏味<几个月来,我一直在绞尽脑汁,但没有发现任何看起来优雅的实现。

http://coliru.stacked-crooked.com/a/1f2decd7a8d96e3c

基本类型:-

#include <iostream>
#include <map>
#include <vector>
enum class Type{
    HUMAN,DOG,TREE,PARASITE,ERROR
}; //for simplicity
class BaseObject{public: Type type=Type::ERROR; };
class Human : public BaseObject{
    public: Human(){ type=Type::HUMAN; }    
};
class Dog : public BaseObject{
    public: Dog(){ type=Type::DOG; }    
};
class Tree : public BaseObject{
    public: Tree(){ type=Type::TREE; }    
};
class Parasite : public BaseObject{
    public: Parasite(){ type=Type::PARASITE; }    
};

基本1:N映射

template<class A,class B> class MapSimple{
    std::multimap<A*, B*> aToB;
    std::multimap<B*, A*> bToA;
    public: void addRelation(A* b1,B* b2){
        aToB.insert ( std::pair<A*,B*>(b1,b2) );   
        bToA.insert ( std::pair<B*,A*>(b2,b1) );   
    }
    public: std::vector<B*> queryAllChildren(A* b1){
        auto ret = aToB.equal_range(b1);
        auto result=std::vector<B*>();
        for (auto it=ret.first; it!=ret.second; ++it){
            result.push_back(it->second);
        }
        return result;
    }
    public: void removeAllParent(B* b){
        if(bToA.count(b)==0)return;
        A* a=bToA.find(b)->second;
        bToA.erase(b);
        auto iterpair = aToB.equal_range(a);
        auto it = iterpair.first;
        for (; it != iterpair.second; ++it) {
            if (it->second == b) { 
                aToB.erase(it);
                break;
            }
        }
    }
    //.. other functions 
};

下面是数据库实例和聚合:-

MapSimple<Human,Human> raise;
MapSimple<Human,Dog> has;
MapSimple<Human,Tree> cultivate;
MapSimple<Dog,Parasite> isHouseOf;
class Feed{
    public: void removeAllParent(BaseObject* b1){
        if(b1->type==Type::HUMAN){
            raise.removeAllParent(static_cast<Human*>(b1));
        } 
        if(b1->type==Type::DOG){
            has.removeAllParent(static_cast<Dog*>(b1));
        }
        //.... some other condition (I have to hard code them - tedious) ...
    }
    //other function 
};
Feed feed;

用法

int main(){
    Human h1;
    Dog d1,d2;

    has.addRelation(&h1,&d1);
    has.addRelation(&h1,&d2);
    auto result=has.queryAllChildren(&h1);
    std::cout<<result.size(); //print 2
    feed.removeAllParent(&d1);
    result=has.queryAllChildren(&h1);
    std::cout<<result.size(); //print 1
}

共有2个答案

须巴英
2023-03-14

Jarod42在本主题中提出了更好的代码。C 17风格:

#include <algorithm>
#include <tuple>
#include <vector>

class BaseObject {
public:
    virtual ~BaseObject() = default;
    virtual std::vector<BaseObject*> getAllParents() const = 0;
    virtual std::vector<BaseObject*> getAllChildren() const = 0;
    virtual void removeAllParents() = 0;
    virtual void removeAllChildren() = 0;
};

template<typename TParentTuple, typename TChilderenTuple>
class Obj;

template<typename... ParentTags,
         typename... ChildTags>
class Obj<std::tuple<ParentTags...>, std::tuple<ChildTags...>> : public BaseObject
{
    std::tuple<std::vector<typename ParentTags::obj_type*>...> parents;
    std::tuple<std::vector<typename ChildTags::obj_type*>...> children;

public:

    template <typename T>
    void addParent(T* parent) { std::get<std::vector<T*>>(parents).push_back(parent); }

    template <typename T>
    void removeParent(const T* parent) {
        auto& v = std::get<std::vector<T*>>(parents);
        auto it = std::find(std::cbegin(v), std::cend(v), parent);
        if (it != std::cend(v)) { v.erase(it); }
    }

    template <typename T>
    void addChild(T* child) { std::get<std::vector<T*>>(children).push_back(child); }

    template <typename T>
    void removeChild(const T* child) {
        auto& v = std::get<std::vector<T*>>(children);
        auto it = std::find(std::cbegin(v), std::cend(v), child);
        if (it != std::cend(v)) { v.erase(it); }
    }

    std::vector<BaseObject*> getAllParents() const override {
        std::vector<BaseObject*> res;

        std::apply([&](auto&... v){ (res.insert(res.end(), v.begin(), v.end()), ...); },
                   parents);
        return res;
    }
    std::vector<BaseObject*> getAllChildren() const override {
        std::vector<BaseObject*> res;

        std::apply([&](auto&... v){ (res.insert(res.end(), v.begin(), v.end()), ...); },
                   children);
        return res;
    }

    void removeAllParents() override {
        std::apply(
            [this](auto&... v)
            {
                [[maybe_unused]] auto clean = [this](auto& v) {
                    for (auto* parent : v) {
                        parent->removeChild(this);
                    }
                    v.clear();
                };
                (clean(v), ...);
            },
            parents);
    }

    void removeAllChildren() override {
        std::apply(
            [this](auto&... v)
            {
                [[maybe_unused]] auto clean = [this](auto& v) {
                    for (auto* child : v) {
                        child->removeParent(this);
                    }
                    v.clear();
                };
                ( clean(v), ...);
            },
            children);
    }
};

struct Human_tag;
struct Tree_tag;
struct Dog_tag;
struct Parasite_tag;

using Human = Obj<std::tuple<>, std::tuple<Tree_tag, Dog_tag>>;
using Tree = Obj<std::tuple<Human_tag>, std::tuple<>>;
using Dog = Obj<std::tuple<Human_tag>, std::tuple<Parasite_tag>>;
using Parasite = Obj<std::tuple<Dog_tag>, std::tuple<>>;

struct Human_tag { using obj_type = Human; };
struct Tree_tag { using obj_type = Tree; };
struct Dog_tag { using obj_type = Dog; };
struct Parasite_tag { using obj_type = Parasite; };

template<class A, class B>
void addRelation(A* a, B* b)
{
    a->addChild(b);
    b->addParent(a);
}

#include <iostream>
int main() {
    Human h1;
    Dog d1, d2;

    addRelation(&h1, &d1);
    addRelation(&h1, &d2);
    auto result = h1.getAllChildren();
    std::cout << result.size() << "\n"; //print 2
    d1.removeAllParents();
    result = h1.getAllChildren();
    std::cout << result.size() << "\n"; //print 1
}

好吧,既然你不想重复代码,我就把这个项目作为我第一次使用元编程/变量模板的经验。这就是我得到的:

#include <tuple>
#include <vector>
#include <algorithm>

template<class T>
using prtVector = std::vector<T*>;

// Interface, as required by assignment
class BaseObject {
public:
    virtual ~BaseObject() {}
    virtual prtVector<BaseObject> getAllParents() const = 0;
    virtual prtVector<BaseObject> getAllChildren() const = 0;
    virtual void removeAllParents() = 0;
    virtual void removeAllChildren() = 0;
};

// base prototype
template<typename TOwnTag, typename TParentTagsTuple, typename TChildTagsTuple>
class Obj;

// Parent-type deduction
template<typename TOwnTag, typename TParentTag, typename... TParentTags, typename... TChildTags>
class Obj<TOwnTag, std::tuple<TParentTag, TParentTags...>, std::tuple<TChildTags...>>
    : public Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>
{
    // local types
    using TOwn = typename TOwnTag::obj_type;
    using TParent = typename TParentTag::obj_type;
    // container
    prtVector<TParent> parentsPtrs;
    //befriend types
    friend class Obj;
    template<class A, class B>
    friend void addRelation(A* const a, B* const b);
protected:
    // prevent base function hiding with 'using'-declaration
    using Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>::addParent;
    using Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>::removeParent;
    // add and remove element functions
    void addParent(TParent* const parentPtr) { parentsPtrs.push_back(parentPtr); }
    void removeParent(TParent const* const parentPtr) {
        auto it = std::find(std::cbegin(parentsPtrs), std::cend(parentsPtrs), parentPtr);
        if (it != std::cend(parentsPtrs)) parentsPtrs.erase(it);
    }
public:
    virtual ~Obj() {}
    virtual prtVector<BaseObject> getAllParents() const override {
        auto result = Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>::getAllParents();
        result.insert(std::begin(result), std::cbegin(parentsPtrs), std::cend(parentsPtrs));
        return result;
    }
    virtual prtVector<BaseObject> getAllChildren() const override {
        return Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>::getAllChildren();
    }
    virtual void removeAllParents() override {
        Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>::removeAllParents();
        for (auto&& parent : parentsPtrs) parent->removeChild(reinterpret_cast<TOwn* const>(this));
    }
    virtual void removeAllChildren() override {
        Obj<TOwnTag, std::tuple<TParentTags...>, std::tuple<TChildTags...>>::removeAllChildren();
    }
};

// Child-type deduction
template<typename TOwnTag, typename TChildTag, typename... TChildTags>
class Obj<TOwnTag, std::tuple<>, std::tuple<TChildTag, TChildTags...>>
    : public Obj<TOwnTag, std::tuple<>, std::tuple<TChildTags...>>
{
    // local types
    using TOwn = typename TOwnTag::obj_type;
    using TChild = typename TChildTag::obj_type;
    // container
    prtVector<TChild> childrenPtrs;
    //befriend types
    friend class Obj;
    template<class A, class B>
    friend void addRelation(A* const a, B* const b);
protected:
    // empty functions required for 'using'-declaration
    void addParent() {}
    void removeParent() {}
    // prevent base function hiding with 'using'-declaration
    using Obj<TOwnTag, std::tuple<>, std::tuple<TChildTags...>>::addChild;
    using Obj<TOwnTag, std::tuple<>, std::tuple<TChildTags...>>::removeChild;
    // add and remove element functions
    void addChild(TChild* const childPtr) { childrenPtrs.push_back(childPtr); }
    void removeChild(TChild const* const childPtr) {
        auto it = std::find(std::cbegin(childrenPtrs), std::cend(childrenPtrs), childPtr);
        if (it != std::cend(childrenPtrs)) childrenPtrs.erase(it);
    }
public:
    virtual ~Obj() {}
    virtual prtVector<BaseObject> getAllParents() const override {
        return Obj<TOwnTag, std::tuple<>, std::tuple<TChildTags...>>::getAllParents();
    }
    virtual prtVector<BaseObject> getAllChildren() const override {
        auto result = Obj<TOwnTag, std::tuple<>, std::tuple<TChildTags...>>::getAllChildren();
        result.insert(std::begin(result), std::cbegin(childrenPtrs), std::cend(childrenPtrs));
        return result;
    }
    virtual void removeAllParents() override {}
    virtual void removeAllChildren() override {
        Obj<TOwnTag, std::tuple<>, std::tuple<TChildTags...>>::removeAllChildren();
        for (auto&& child : childrenPtrs) child->removeParent(reinterpret_cast<TOwn* const>(this));
    }
};

// terminator
template<typename TOwnTag>
class Obj<TOwnTag, std::tuple<>, std::tuple<>> : public BaseObject {
protected:
    // empty functions required for 'using'-declaration
    void addChild() {}
    void removeChild() {}
    void addParent() {}
    void removeParent() {}
public:
    virtual ~Obj() {}
    virtual prtVector<BaseObject> getAllParents() const override {
        return prtVector<BaseObject>();
    }
    virtual prtVector<BaseObject> getAllChildren() const override {
        return prtVector<BaseObject>();
    }
    virtual void removeAllParents() override {}
    virtual void removeAllChildren() override {}
};

//prototype class tags
struct Human_tag;
struct Tree_tag;
struct Dog_tag;
struct Parasite_tag;
//define class types
using Human = Obj<Human_tag, std::tuple<>, std::tuple<Tree_tag, Dog_tag>>;
using Tree = Obj<Tree_tag, std::tuple<Human_tag>, std::tuple<>>;
using Dog = Obj<Dog_tag, std::tuple<Human_tag>, std::tuple<Parasite_tag>>;
using Parasite = Obj<Parasite_tag, std::tuple<Dog_tag>, std::tuple<>>;
//couple tags to classes
struct Human_tag { using obj_type = Human; };
struct Tree_tag { using obj_type = Tree; };
struct Dog_tag { using obj_type = Dog; };
struct Parasite_tag { using obj_type = Parasite; };

//(befriend)helper function
// maybe could do somehting with std::enable_if
// i.e. "enable if type B is in child tuple of A and
//  type A is in parent tuple of B"
// that way the parser will already detect a relation is not possible
template<class A, class B>
void addRelation(A* const a, B* const b)
{
    a->addChild(b);
    b->addParent(a);
}

// now for some testing
#include <iostream>
int main() {
    Human h1;
    Dog d1, d2;
    Parasite p1;

    addRelation(&h1, &d1);
    addRelation(&h1, &d2);
    addRelation(&d1, &p1);
    //addRelation(&h1, &p1); // compiler error
    auto result = h1.getAllChildren();
    std::cout << result.size() << "\n"; //print 2
    d1.removeAllParents();
    result = h1.getAllChildren();
    std::cout << result.size() << "\n"; //print 1

    std::cin.ignore();
}

请问任何不清楚的问题,因为在过去的24小时里,我学到了太多新的东西,以至于我不知道从哪里开始解释。

茹正初
2023-03-14

直接实现有什么问题?

例如:BaseObject。水电站

#include <vector>

template<class T>
using prtVector = std::vector<T*>;

class BaseObject {
public:
    virtual prtVector<BaseObject> getAllParents() const = 0;
    virtual prtVector<BaseObject> getAllChilderen() const = 0;
    virtual void removeAllParents() = 0;
    virtual void removeAllChildren() = 0;
};

人类水电站

#include "BaseObject.hpp"
#include "Tree.hpp"
#include "Dog.hpp"
class Tree;
class Dog;

class Human : public BaseObject {
public:
    prtVector<BaseObject> getAllParents() const override;
    prtVector<BaseObject> getAllChildren() const override;

    void removeAllParents() override;
    void removeAllChildren() override ;

    friend class Dog;
    friend class Tree;
    template<class A, class B>
    friend void addRelation(A* a, B* b);
private:
    void addParent(Human* const);
    void removeParent(Human const* const);
    void addChild(Human* const);
    void removeChild(Human const* const);
    void addChild(Tree* const);
    void removeChild(Tree const* const);
    void addChild(Dog* const);
    void removeChild(Dog const* const);
private:
    prtVector<Human> parents;
    prtVector<Human> children;
    prtVector<Tree> plants;
    prtVector<Dog> pets;
};

Human.cpp

#include "Human.hpp"

prtVector<BaseObject> Human::getAllParents() const {
    prtVector<BaseObject> result(std::cbegin(parents), std::cend(parents));
    return result;
}

prtVector<BaseObject> Human::getAllChildren() const {
    prtVector<BaseObject> result(std::cbegin(children), std::cend(children));
    result.insert(std::end(result), std::cbegin(pets), std::cend(pets));
    result.insert(std::end(result), std::cbegin(plants), std::cend(plants));
    return result;
}

void Human::removeAllParents() {
    for (auto parent : parents) { parent->removeChild(this); }
    parents.clear();
}

void Human::removeAllChildren() {
    for (auto child : children) { child->removeParent(this); } children.clear();
    for (auto pet : pets) { pet->removeParent(this); } pets.clear();
    for (auto plant : plants) { plant->removeParent(this); } plants.clear();
}

void Human::addParent(Human* const parent) { parents.push_back(parent); }

#include <algorithm>
void Human::removeParent(Human const* const parent) {
    auto it = std::find(std::cbegin(parents), std::cend(parents), parent);
    if (it != std::cend(parents)) parents.erase(it);
}
void Human::addChild(Human* const child) { children.push_back(child); }

等等,等等...

其他类型也一样。。。。

main.cpp

#include "Human.hpp"
#include "Dog.hpp"

template<class A, class B>
void addRelation(A* a, B* b)
{
    a->addChild(b);
    b->addParent(a);
}

template<class A>
prtVector<BaseObject> queryAllChildren(A* obj)
{
    return obj->getAllChilderen();
}

template<class A>
void removeAllParents(A* obj)
{
    obj->removeAllParents();
}

#include <iostream>
int main() {
    Human h1;
    Dog d1, d2;

    addRelation(&h1, &d1);
    addRelation(&h1, &d2);
    auto result = queryAllChildren(&h1);
    std::cout << result.size() << "\n"; //print 2
    removeAllParents(&d1);
    result = queryAllChildren(&h1);
    std::cout << result.size() << "\n"; //print 1

    std::cin.ignore();
}

IMHO提供可读且可维护的代码。可能会被优化一些。但至少代码中的关系非常清楚。

 类似资料:
  • 本文向大家介绍JavaScript中的对象继承关系,包括了JavaScript中的对象继承关系的使用技巧和注意事项,需要的朋友参考一下 我们今天就来看一下继承中的类继承以及类继承和原型继承的混用,所谓类继承,就是使用call或者apply方法来进行冒充继承:   像上面这种就是我们要使用的类继承,用这种继承,我们可以访问类中的方法和属性,但是无法访问父类原型中的方法和属性,这种方法别名冒充继承,顾

  • 注意 当前章节中涉及的配置一般适用于关系数据库。这里展示的扩展方法在你安装了关系数据库提供程序之后就能获得(由Microsoft.EntityFrmeworkCore.Relational 程序包共享)。 EF 模型中的继承被用来控制实体类型继承在数据库中的表现方式。 惯例 按照惯例,使用每个层次结构一张表(table-per-hierarchy,TPH)的模式来进行映射。TPH 使用单一的表来存

  • 问题内容: 这篇文章是这个的延续职位我有DlUser类,该类的每个对象都可以具有DLFaceBook类,DlFaceBook的每个对象都可以具有被映射为myFriends的Friends。 我正在尝试使用包映射,复合主键和静态内部类将同一类的关系映射为多对多关系。我的代码如下: Facebook用户类如下所示,您可以看到我具有MyFriends类的对象集合: MyFriends类如下所示: 现在的

  • 我需要以下递归关系的帮助。 T(1)=1 T(n)=T(n-1)*n 这就是我尝试过的。我想我可能把替换部分搞砸了,但请再看一次,让我知道我得到的时间复杂度是否正确。 现在我不确定我所做的是否完全正确,但如果有任何帮助,我将不胜感激。

  • 跑步:Laravel 5.3和Laravel背包积垢3.1 我遇到的情况是,我有一个模型,我也想添加多个相关(子)记录,只使用一个CRUD表单。一些示例包括添加多个文件。。。但让我们从小事做起。我发现下面的帖子有相似的主题,但没有明确的答案。 使用字段类型的最佳方法是什么?https://laravel-backpack.readme.io/docs/crud-fields#section-但是,

  • 本文向大家介绍浅谈Python中重载isinstance继承关系的问题,包括了浅谈Python中重载isinstance继承关系的问题的使用技巧和注意事项,需要的朋友参考一下 判断继承关系 通过内建方法 isinstance(object, classinfo) 可以判断一个对象是否是某个类的实例。这个关系可以是直接,间接或抽象。 实例的检查是允许重载的,可见文档customizing-insta