如何在不使用结构修改原始对象的情况下将对象存储在另一个对象中? [英] How to store an object inside another object without modifying the original using struct?

查看:45
本文介绍了如何在不使用结构修改原始对象的情况下将对象存储在另一个对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情境化:

当数据结构执行查询时,它会在树中执行旋转转换.这样,加载的原始树就被修改了.因此,如果我立即再次预约,则咨询会有所不同.我需要找到一种方法将原始数据结构保留在内存中,以便查询数据结构是它的副本.记住所讨论的数据结构是一个对象.即如何在不改变原始对象的情况下复制对象?

When the data structure performs queries it performs rotation transformations in the tree. In this way, the original tree that was loaded is modified. So if I make another appointment instantly, the consultation is different. I need to find a way to keep the original data structure in memory so that the query data structure is a copy of it. Remembering that the data structure in question is an object. That is, how do I copy an object without changing the original?

如果我尝试下面的代码它不起作用,因为我让一个对象接收同一个类的另一个对象,所以这两个被修改:

If I try the code below it doesn't work because I'm making an object receive another object of the same class, so the two are modified:

SPSTTree Taux;
SPSTTree T = operand1->SpstManager;
Taux = T;
Filter_spst(_operand2.dbl, op, Taux);

该对象属于一个结构体

typedef struct SPSTNode *PositionSPST;
typedef struct SPSTNode *SPSTTree;
struct SPSTNode{
    ElementType Element;
    int64_t offset;
    SPSTTree lchild;
    SPSTTree rchild;

    int qtd_element = 1;

};

总而言之,我想将数据结构传递给查询..将此数据结构理解为数据索引树.这棵树被归纳为一个类的对象.但是,当我执行查询时,结构数据被修改.所以我需要保留原始对象,即加载的树,以便立即执行其他查询,而无需再次加载树.

So in summary, I want to pass a data structure to a query .. Understand this data structure as a data indexing tree. This tree is summarized to an object of a class. However, when I perform the query, the structure data is modified. So I need to preserve the original object, which is the loaded tree, to perform other queries instantly without having to load the tree again.

基本上:如何在不修改原始对象的情况下使用 struct 将对象存储在另一个对象中?

Basically: how to store an object inside another object without modifying the original using struct?

推荐答案

我不确定我是否理解您的问题,但我猜您有一个 SPSTTree 并且您希望能够制作它的副本以便您可以修改它而不改变原来的.为此,您需要为结构实现递归复制功能.在这个函数中:

I am not sure I am understanding well your question, but I guess you have an SPSTTree and you want to be able to make a copy of it so you can modify it without altering the original. To do so you need to implement a recursive copy function for the structure. In this function:

1) 复制不是指针的结构的属性,在你的例子中是 Element、offset 和 qtd_element.

1) Copy the properties of the struct which are not pointers, in your case Element, offset and qtd_element.

2) 对于所有需要递归复制指向结构的指针.在您的情况下,您有两个指针 lchild 和 rchild,它们指向相应的子树.因此,您需要做的是检查它们中的每一个是否为 NULL.如果它们不是 NULL,则在指向的树上调用您的复制函数(该函数以递归方式调用自身)并将生成的(复制的)子树与您正在制作的副本相关联.

2) For all the pointers you need to recursively copy the pointed structs. In your case you have two pointers, lchild and rchild, which point to the corresponding subtrees. So what you need to do is to check for each of them whether they are NULL or not. If they are not NULL call your copy function on the pointed tree (the function calls itself, recursively) and associate the resulting (copied) subtree to the copy you are making.

函数示例(以伪代码为例):

Example of function (take it as a pseudocode-ish example):

struct SPSTNode{
    ElementType Element;
    int64_t offset;
    SPSTTree lchild;
    SPSTTree rchild;

    int qtd_element = 1;

};
SPSTTree copyTree(SPSTTree original) {
    // Allocate here your copy using the same way you do in your code, as it is not specified in the question I will use malloc as an example
     SPSTTree copy = (SPSTTree) malloc(sizeof(SPSTNode));

     // Copy non-pointer values
     copy.Element = original.Element;
     copy.offset = original.offset;
     copy.qtd_element = original.qtd_element;

     // Recursively copy subtrees
     if (original.lchild) {
           copy.lchild = copyTree(original.lchild);
     } else {
            copy.lchild = 0;
     }
     if(original.rchild) {
           copy.rchild = copyTree(original.rchild);
     } else {
           copy.rchild = 0;
     }
}

由于我们在这个伪代码示例中使用了 malloc(我来自手机,所以我没有尝试它,但是它应该非常准确)不要忘记稍后释放您的内存!

Since we are using malloc in this pseudocode-ish example (I am from the phone so I did not try it, however it should be pretty accurate) do not forget to free your memory later!

这篇关于如何在不使用结构修改原始对象的情况下将对象存储在另一个对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆