简体   繁体   English

可扩展哈希-析构函数C ++

[英]extendible hashing - destructor C++

So... I have implemented a version of exendible hashing...my question now is for the destructor. 所以...我实现了可扩展哈希的一个版本...我的问题现在是针对析构函数的。

I used in the Hash class an array of pointers that point on buckets from class Bucket. 我在Hash类中使用了一个指针数组,这些指针指向Bucket类中的存储桶。 The problem is there can be multiple pointers on the same bucket. 问题是同一存储桶上可能有多个指针。

In the destructor of the Hash class I have to delete every bucket and then the array, but I have to be careful not to delete the same bucket twice (I think that'll result in an error). 在Hash类的析构函数中,我必须先删除每个存储桶,然后再删除数组,但是要注意不要两次删除相同的存储桶(我认为这会导致错误)。 In order to do that I used a bool vector to memorize whether or not the bucket has already been deleted. 为此,我使用了布尔向量来记住存储桶是否已被删除。

My question is now: is there a way to know if a bucket has already been deleted without using more memory (the bool vector) ? 我的问题现在是:有没有办法知道是否已在不使用更多内存的情况下删除了存储桶(布尔向量)?

LE: I solved the destructor problem using nullptr (seems to be working now), but...another question: how can I go through every bucket exactly 1 time (for finding the min and max elements, for example). LE:我使用nullptr解决了析构函数问题(似乎现在可以正常工作),但是...另一个问题:我怎样才能准确地遍历每个存储桶1次(例如,找到最小和最大元素)。 I can't use nullptr this time (the pointers need to stay where they are - on the buckets) 这次我不能使用nullptr(指针需要保持在原位-在存储桶上)

Just iteratively use your erase function 只是迭代使用擦除功能

while (size ())
{
    erase(begin());
}

I used in the Hash class an array of pointers that point on buckets from class Bucket. 我在Hash类中使用了一个指针数组,这些指针指向Bucket类中的存储桶。 The problem is there can be multiple pointers on the same bucket. 问题是同一存储桶上可能有多个指针。

My question is now: is there a way to know if a bucket has already been deleted without using more memory (the bool vector) ? 我的问题现在是:有没有办法知道是否已在不使用更多内存的情况下删除了存储桶(布尔向量)?

Sure -- use reference counting. 当然-使用参考计数。 Put an integer member variable in the Bucket class, initialized to zero. 将一个整数成员变量放入Bucket类中,初始化为零。 Whenever you create a pointer to that Bucket object, increment the integer. 每当您创建指向该Bucket对象的指针时,就增加整数。 Whenever you invalidate a pointer to the Bucket (ie whenever you would have called delete on that pointer), decrement the integer member variable. 每当使桶的指针无效时(即,只要在该指针上调用delete),就减小整数成员变量。 If that decrement causes the integer to become zero, you know the Bucket has no more pointers pointing to it, so delete the Bucket. 如果该递减导致整数变为零,则说明存储桶中没有指向它的指针,因此请删除存储桶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM