gpt4 book ai didi

hashmap - 八叉树的动态 HashMap

转载 作者:行者123 更新时间:2023-12-03 19:30:54 27 4
gpt4 key购买 nike

是否有内存和速度高效的方法来在 HashMap 中动态存储唯一键:值对? key 保证是唯一的,但它们的数量经常变化。插入和删除必须很快。

我所做的是包含有符号距离场的八叉树(非线性/完整)。八叉树经常更新。我想做的是尝试让它成为无指针以节省一些空间。

最佳答案

我不确定是否使用 hashmap,因为动态结构往往会丢失 hashmap 从中受益的 O(1) 查找,或者分配额外的内存并在内存用完时需要重新分配。但是,您可以创建一个八叉树,其中每个节点只有一个指针。

例如,在 C++ 中,您可能会这样做:

struct node{
node* next;//pointer to children
unsigned byte shape;//8bit flag indicating which children actually exist

node():next(0),shape(0){}
};
void initChildren(node& parent,unsigned byte state=0){
if(!parent.next){//only allocate the array if it has not yet been allocated
parent.next=new node[8];//the children are allocated in consecutive memory
}
shape=state;
}

删除子节点只需要将 shape 字段中的一个位设置为 0。我希望这对你有帮助,即使你刚才问过。

关于hashmap - 八叉树的动态 HashMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com