Is there an way to zero out an array in with time complexsity O(1)? It’s obvious that this can be done by for-loop, memset. But their time complexity are not O(1).
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes
However not any array. It takes an array that has been crafted for this to work.
The principle is simple:
generationattributeThe method has two issues:
The latter can be thwarted using a real big integer (
uint64_tat the cost of more storage).The former is a natural consequence, one possible solution is to use buckets to downplay the issue by having for example up to 64 items associated to a single counter and a bitmask identifying which are valid within this counter.
EDIT: just wanted to get back on the buckets idea.
The original solution has an overhead of 8 bytes (64 bits) per element (if already 8-bytes aligned). Depending on the elements stored it might or might not be a big deal.
If it is a big deal, the idea is to use buckets; of course like all trade-off it slows down access even more.
Note that this small array of a fixed number of elements (we could actually template this and statically check it’s inferior or equal to 64) only has 16 bytes of overhead. This means we have an overhead of 2 bits per element.
We got the space overhead down by a factor of… 32. Now the array can even be used to store
charfor example, whereas before it would have been prohibitive. The cost is that access got slower, as we get a division and modulo (when we will get a standardized operation that returns both results in one shot ?).